fix: save chat and hook

This commit is contained in:
archer
2023-07-28 15:56:50 +08:00
parent a77e3880f4
commit 0a689b0ab8
5 changed files with 71 additions and 55 deletions

View File

@@ -385,8 +385,8 @@ async function init(limit: number, skip: number) {
// 遍历 app // 遍历 app
const apps = await App.find( const apps = await App.find(
{ {
chat: { $ne: null } chat: { $ne: null },
// modules: { $exists: false }, modules: { $exists: false }
// userId: '63f9a14228d2a688d8dc9e1b' // userId: '63f9a14228d2a688d8dc9e1b'
}, },
'_id chat' '_id chat'

View File

@@ -1,4 +1,4 @@
import React, { useRef } from 'react'; import React, { useMemo } from 'react';
import { useChatBox } from '@/components/ChatBox'; import { useChatBox } from '@/components/ChatBox';
import { ChatItemType } from '@/types/chat'; import { ChatItemType } from '@/types/chat';
import { Menu, MenuButton, MenuList, MenuItem, Box } from '@chakra-ui/react'; import { Menu, MenuButton, MenuList, MenuItem, Box } from '@chakra-ui/react';
@@ -8,14 +8,17 @@ import { useRouter } from 'next/router';
const ToolMenu = ({ history }: { history: ChatItemType[] }) => { const ToolMenu = ({ history }: { history: ChatItemType[] }) => {
const { onExportChat } = useChatBox(); const { onExportChat } = useChatBox();
const router = useRouter(); const router = useRouter();
const menuList = useRef([ const { appId } = router.query;
const menuList = useMemo(
() => [
{ {
icon: 'chat', icon: 'chat',
label: '新对话', label: '新对话',
onClick: () => { onClick: () => {
router.push({ router.push({
query: { query: {
appId: router.query?.appId appId
} }
}); });
} }
@@ -31,7 +34,10 @@ const ToolMenu = ({ history }: { history: ChatItemType[] }) => {
onClick: () => onExportChat({ type: 'md', history }) onClick: () => onExportChat({ type: 'md', history })
}, },
{ icon: 'pdf', label: 'PDF导出', onClick: () => onExportChat({ type: 'pdf', history }) } { icon: 'pdf', label: 'PDF导出', onClick: () => onExportChat({ type: 'pdf', history }) }
]); ],
[appId, history, onExportChat, router]
);
return history.length > 0 ? ( return history.length > 0 ? (
<Menu autoSelect={false} isLazy> <Menu autoSelect={false} isLazy>
<MenuButton <MenuButton
@@ -45,7 +51,7 @@ const ToolMenu = ({ history }: { history: ChatItemType[] }) => {
<MyIcon name={'more'} w={'14px'} p={2} /> <MyIcon name={'more'} w={'14px'} p={2} />
</MenuButton> </MenuButton>
<MenuList color={'myGray.700'} minW={`120px !important`} zIndex={10}> <MenuList color={'myGray.700'} minW={`120px !important`} zIndex={10}>
{menuList.current.map((item) => ( {menuList.map((item) => (
<MenuItem key={item.label} onClick={item.onClick} py={[2, 3]}> <MenuItem key={item.label} onClick={item.onClick} py={[2, 3]}>
<MyIcon name={item.icon as any} w={['14px', '16px']} /> <MyIcon name={item.icon as any} w={['14px', '16px']} />
<Box ml={[1, 2]}>{item.label}</Box> <Box ml={[1, 2]}>{item.label}</Box>

View File

@@ -19,7 +19,7 @@ export const pushTaskBill = async ({
shareId?: string; shareId?: string;
response: ChatHistoryItemResType[]; response: ChatHistoryItemResType[];
}) => { }) => {
const total = response.reduce((sum, item) => sum + item.price, 0) || 1; const total = response.reduce((sum, item) => sum + item.price, 0);
await Promise.allSettled([ await Promise.allSettled([
Bill.create({ Bill.create({

View File

@@ -179,10 +179,10 @@ export const authApp = async ({
authOwner?: boolean; authOwner?: boolean;
reserveDetail?: boolean; // focus reserve detail reserveDetail?: boolean; // focus reserve detail
}) => { }) => {
// 获取 model 数据 // 获取 app 数据
const app = await App.findById<AppSchema>(appId); const app = await App.findById<AppSchema>(appId);
if (!app) { if (!app) {
return Promise.reject('模型不存在'); return Promise.reject('App is not exists');
} }
/* /*

View File

@@ -31,8 +31,11 @@ export async function saveChat({
'_id' '_id'
); );
const promise = [];
if (chatHistory) { if (chatHistory) {
await Chat.findOneAndUpdate( promise.push(
Chat.findOneAndUpdate(
{ chatId }, { chatId },
{ {
$push: { $push: {
@@ -43,9 +46,11 @@ export async function saveChat({
title: content[0].value.slice(0, 20), title: content[0].value.slice(0, 20),
updateTime: new Date() updateTime: new Date()
} }
)
); );
} else { } else {
await Chat.create({ promise.push(
Chat.create({
chatId, chatId,
userId, userId,
appId, appId,
@@ -54,12 +59,17 @@ export async function saveChat({
source, source,
shareId, shareId,
content: content content: content
}); })
);
} }
if (isOwner && source === ChatSourceEnum.online) { if (isOwner && source === ChatSourceEnum.online) {
promise.push(
App.findByIdAndUpdate(appId, { App.findByIdAndUpdate(appId, {
updateTime: new Date() updateTime: new Date()
}); })
);
} }
await Promise.all(promise);
} }