From 371e0e36c65cfc1d145cb0b466abe89d106a378a Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Mon, 15 May 2023 22:27:54 +0800 Subject: [PATCH] perf: error show --- src/components/Layout/index.tsx | 2 +- src/pages/_error.tsx | 4 ++- src/pages/api/model/data/pushModelDataCsv.ts | 8 ++++++ src/pages/chat/components/Empty.tsx | 21 +++++++++----- src/pages/chat/index.tsx | 30 +++++++++----------- src/pages/chat/share.tsx | 18 ++++++------ src/pages/login/index.tsx | 2 +- src/pages/model/components/detail/index.tsx | 11 ++++++- src/pages/model/index.tsx | 2 +- src/utils/tools.ts | 1 + 10 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index fea9ae15c..c5b4afe01 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -78,6 +78,6 @@ export default Layout; Layout.getInitialProps = ({ req }: any) => { return { - isPcDevice: !/Mobile/.test(req ? req.headers['user-agent'] : navigator.userAgent) + isPcDevice: !/Mobile/.test(req?.headers?.['user-agent']) }; }; diff --git a/src/pages/_error.tsx b/src/pages/_error.tsx index 1196209d7..745497af7 100644 --- a/src/pages/_error.tsx +++ b/src/pages/_error.tsx @@ -4,7 +4,9 @@ function Error({ errStr }: { errStr: string }) { Error.getInitialProps = ({ res, err }: { res: any; err: any }) => { console.log(err); - return { errStr: JSON.stringify(err) }; + return { + errStr: `部分系统不兼容,导致页面崩溃。如果可以,请联系作者,反馈下具体操作和页面。大部分是 苹果 的 safari 浏览器导致,可以尝试更换 chrome 浏览器。` + }; }; export default Error; diff --git a/src/pages/api/model/data/pushModelDataCsv.ts b/src/pages/api/model/data/pushModelDataCsv.ts index b628ef1bc..9a736280b 100644 --- a/src/pages/api/model/data/pushModelDataCsv.ts +++ b/src/pages/api/model/data/pushModelDataCsv.ts @@ -89,3 +89,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< }); } } + +export const config = { + api: { + bodyParser: { + sizeLimit: '100mb' + } + } +}; diff --git a/src/pages/chat/components/Empty.tsx b/src/pages/chat/components/Empty.tsx index 9a686d3dc..b04fce788 100644 --- a/src/pages/chat/components/Empty.tsx +++ b/src/pages/chat/components/Empty.tsx @@ -5,8 +5,10 @@ import Markdown from '@/components/Markdown'; import { LOGO_ICON } from '@/constants/chat'; const Empty = ({ + showChatProblem, model: { name, intro, avatar } }: { + showChatProblem: boolean; model: { name: string; intro: string; @@ -43,13 +45,18 @@ const Empty = ({ {intro} )} - {/* version intro */} - - - - - - + + {showChatProblem && ( + <> + {/* version intro */} + + + + + + + + )} ); }; diff --git a/src/pages/chat/index.tsx b/src/pages/chat/index.tsx index c119b0b6d..9cc240516 100644 --- a/src/pages/chat/index.tsx +++ b/src/pages/chat/index.tsx @@ -36,12 +36,11 @@ import { useToast } from '@/hooks/useToast'; import { useScreen } from '@/hooks/useScreen'; import { useQuery } from '@tanstack/react-query'; import dynamic from 'next/dynamic'; -import { useCopyData, voiceBroadcast, formatLinkTextToHtml } from '@/utils/tools'; +import { useCopyData, voiceBroadcast, hasVoiceApi } from '@/utils/tools'; import { streamFetch } from '@/api/fetch'; import MyIcon from '@/components/Icon'; import { throttle } from 'lodash'; import { Types } from 'mongoose'; -import Markdown from '@/components/Markdown'; import { LOGO_ICON } from '@/constants/chat'; import { ChatModelMap } from '@/constants/model'; import { useChatStore } from '@/store/chat'; @@ -50,19 +49,16 @@ import { fileDownload } from '@/utils/file'; import { htmlTemplate } from '@/constants/common'; import { useUserStore } from '@/store/user'; import Loading from '@/components/Loading'; +import Markdown from '@/components/Markdown'; +import Empty from './components/Empty'; const PhoneSliderBar = dynamic(() => import('./components/PhoneSliderBar'), { - loading: () => , ssr: false }); const History = dynamic(() => import('./components/History'), { loading: () => , ssr: false }); -const Empty = dynamic(() => import('./components/Empty'), { - loading: () => , - ssr: false -}); import styles from './index.module.scss'; @@ -77,7 +73,6 @@ const Chat = ({ chatId: string; isPcDevice: boolean; }) => { - const hasVoiceApi = typeof window === 'undefined' ? false : !!window.speechSynthesis; const router = useRouter(); const theme = useTheme(); @@ -627,7 +622,6 @@ const Chat = ({ chatData.model.canUse, chatData.modelId, delChatRecord, - hasVoiceApi, onclickCopy, router, theme.borders.base @@ -714,7 +708,7 @@ const Chat = ({ justifyContent={'space-between'} py={[3, 5]} px={5} - borderBottom={'1px solid '} + borderBottom={'1px solid'} borderBottomColor={useColorModeValue('gray.200', 'gray.700')} color={useColorModeValue('myGray.900', 'white')} > @@ -790,7 +784,10 @@ const Chat = ({ order: 1, mr: ['6px', 2], cursor: 'pointer', - onClick: () => isPc && router.push(`/model?modelId=${chatData.modelId}`) + onClick: () => + isPc && + chatData.model.canUse && + router.push(`/model?modelId=${chatData.modelId}`) } : { order: 3, @@ -856,17 +853,16 @@ const Chat = ({ bg={'myBlue.300'} onContextMenu={(e) => onclickContextMenu(e, item)} > - + {item.value} )} ))} - {chatData.history.length === 0 && } + {chatData.history.length === 0 && ( + + )} {/* 发送区 */} @@ -1012,7 +1008,7 @@ Chat.getInitialProps = ({ query, req }: any) => { return { modelId: query?.modelId || '', chatId: query?.chatId || '', - isPcDevice: !/Mobile/.test(req ? req.headers['user-agent'] : navigator.userAgent) + isPcDevice: !/Mobile/.test(req?.headers?.['user-agent']) }; }; diff --git a/src/pages/chat/share.tsx b/src/pages/chat/share.tsx index 539cec10b..7539d3163 100644 --- a/src/pages/chat/share.tsx +++ b/src/pages/chat/share.tsx @@ -34,12 +34,11 @@ import { useToast } from '@/hooks/useToast'; import { useScreen } from '@/hooks/useScreen'; import { useQuery } from '@tanstack/react-query'; import dynamic from 'next/dynamic'; -import { useCopyData, voiceBroadcast } from '@/utils/tools'; +import { useCopyData, voiceBroadcast, hasVoiceApi } from '@/utils/tools'; import { streamFetch } from '@/api/fetch'; import MyIcon from '@/components/Icon'; import { throttle } from 'lodash'; import { Types } from 'mongoose'; -import Markdown from '@/components/Markdown'; import { LOGO_ICON } from '@/constants/chat'; import { useChatStore } from '@/store/chat'; import { useLoading } from '@/hooks/useLoading'; @@ -47,15 +46,13 @@ import { fileDownload } from '@/utils/file'; import { htmlTemplate } from '@/constants/common'; import { useUserStore } from '@/store/user'; import Loading from '@/components/Loading'; +import Markdown from '@/components/Markdown'; +import Empty from './components/Empty'; const ShareHistory = dynamic(() => import('./components/ShareHistory'), { loading: () => , ssr: false }); -const Empty = dynamic(() => import('./components/Empty'), { - loading: () => , - ssr: false -}); import styles from './index.module.scss'; @@ -70,7 +67,6 @@ const Chat = ({ historyId: string; isPcDevice: boolean; }) => { - const hasVoiceApi = typeof window === 'undefined' ? false : !!window.speechSynthesis; const router = useRouter(); const theme = useTheme(); @@ -536,7 +532,7 @@ const Chat = ({ delShareChatHistoryItemById(historyId, index)}>删除 ), - [delShareChatHistoryItemById, hasVoiceApi, historyId, onclickCopy, theme.borders.base] + [delShareChatHistoryItemById, historyId, onclickCopy, theme.borders.base] ); return ( @@ -754,7 +750,9 @@ const Chat = ({ ))} - {shareChatData.history.length === 0 && } + {shareChatData.history.length === 0 && ( + + )} {/* 发送区 */} @@ -926,7 +924,7 @@ Chat.getInitialProps = ({ query, req }: any) => { return { shareId: query?.shareId || '', historyId: query?.historyId || '', - isPcDevice: !/Mobile/.test(req ? req.headers['user-agent'] : navigator.userAgent) + isPcDevice: !/Mobile/.test(req?.headers?.['user-agent']) }; }; diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 732c207bd..3de87e2d6 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -117,6 +117,6 @@ export default Login; Login.getInitialProps = ({ query, req }: any) => { return { - isPcDevice: !/Mobile/.test(req ? req.headers['user-agent'] : navigator.userAgent) + isPcDevice: !/Mobile/.test(req?.headers?.['user-agent']) }; }; diff --git a/src/pages/model/components/detail/index.tsx b/src/pages/model/components/detail/index.tsx index ddc6e7088..548a0668f 100644 --- a/src/pages/model/components/detail/index.tsx +++ b/src/pages/model/components/detail/index.tsx @@ -45,6 +45,11 @@ const ModelDetail = ({ modelId, isPc }: { modelId: string; isPc: boolean }) => { [modelDetail.userId, userInfo?._id] ); + const canRead = useMemo( + () => isOwner || isLoading || modelDetail.share.isShareDetail, + [isLoading, isOwner, modelDetail.share.isShareDetail] + ); + /* 点击删除 */ const handleDelModel = useCallback(async () => { if (!modelDetail) return; @@ -129,7 +134,7 @@ const ModelDetail = ({ modelId, isPc }: { modelId: string; isPc: boolean }) => { }; }, [router]); - return ( + return canRead ? ( {/* 头部 */} @@ -196,6 +201,10 @@ const ModelDetail = ({ modelId, isPc }: { modelId: string; isPc: boolean }) => { + ) : ( + + 无权查看模型配置 + ); }; diff --git a/src/pages/model/index.tsx b/src/pages/model/index.tsx index 41f4d91e4..290b63e70 100644 --- a/src/pages/model/index.tsx +++ b/src/pages/model/index.tsx @@ -46,6 +46,6 @@ export default Model; Model.getInitialProps = ({ query, req }: any) => { return { modelId: query?.modelId || '', - isPcDevice: !/Mobile/.test(req ? req.headers['user-agent'] : navigator.userAgent) + isPcDevice: !/Mobile/.test(req?.headers?.['user-agent']) }; }; diff --git a/src/utils/tools.ts b/src/utils/tools.ts index 780369597..64c4bd8a7 100644 --- a/src/utils/tools.ts +++ b/src/utils/tools.ts @@ -89,6 +89,7 @@ export const formatTimeToChatTime = (time: Date) => { return target.format('YYYY/M/D'); }; +export const hasVoiceApi = typeof window !== 'undefined' && 'speechSynthesis' in window; /** * voice broadcast */