Skip to content

Commit

Permalink
Feat: If the token is wrong, a prompt message will pop up
Browse files Browse the repository at this point in the history
  • Loading branch information
cike8899 committed Dec 10, 2024
1 parent d6777fa commit b445e43
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
16 changes: 15 additions & 1 deletion web/src/components/api-service/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,23 @@ export const useShowTokenEmptyError = () => {
return { showTokenEmptyError };
};

export const useShowBetaEmptyError = () => {
const { t } = useTranslate('chat');

const showBetaEmptyError = useCallback(() => {
message.error(t('betaError'));
}, [t]);
return { showBetaEmptyError };
};

const getUrlWithToken = (token: string, from: string = 'chat') => {
const { protocol, host } = window.location;
return `${protocol}//${host}/chat/share?shared_id=${token}&from=${from}`;
};

const useFetchTokenListBeforeOtherStep = () => {
const { showTokenEmptyError } = useShowTokenEmptyError();
const { showBetaEmptyError } = useShowBetaEmptyError();

const { data: tokenList, fetchSystemTokenList } =
useFetchManualSystemTokenList();
Expand All @@ -97,12 +107,16 @@ const useFetchTokenListBeforeOtherStep = () => {
const ret = await fetchSystemTokenList();
const list = ret;
if (Array.isArray(list) && list.length > 0) {
if (!list[0].beta) {
showBetaEmptyError();
return false;
}
return list[0]?.token;
} else {
showTokenEmptyError();
return false;
}
}, [fetchSystemTokenList, showTokenEmptyError]);
}, [fetchSystemTokenList, showBetaEmptyError, showTokenEmptyError]);

return {
token,
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ The above is the content you need to summarize.`,
partialTitle: 'Partial Embed',
extensionTitle: 'Chrome Extension',
tokenError: 'Please create API Token first!',
betaError: 'The beta field of the API Token cannot be empty!',
searching: 'searching...',
parsing: 'Parsing',
uploading: 'Uploading',
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ export default {
partialTitle: '部分嵌入',
extensionTitle: 'Chrome 插件',
tokenError: '請先創建 API Token!',
betaError: 'API Token的beta欄位不可以為空!',
searching: '搜索中',
parsing: '解析中',
uploading: '上傳中',
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ export default {
partialTitle: '部分嵌入',
extensionTitle: 'Chrome 插件',
tokenError: '请先创建 API Token!',
betaError: 'API Token的beta字段不可以为空!',
searching: '搜索中',
parsing: '解析中',
uploading: '上传中',
Expand Down
3 changes: 2 additions & 1 deletion web/src/pages/chat/share/large.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ChatContainer = () => {
loading,
ref,
derivedMessages,
hasError,
} = useSendSharedMessage();
const sendDisabled = useSendButtonDisabled(value);

Expand Down Expand Up @@ -71,7 +72,7 @@ const ChatContainer = () => {
<MessageInput
isShared
value={value}
disabled={false}
disabled={hasError}
sendDisabled={sendDisabled}
conversationId={conversationId}
onInputChange={handleInputChange}
Expand Down
42 changes: 12 additions & 30 deletions web/src/pages/chat/shared-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,16 @@ import {
useSendMessageWithSse,
} from '@/hooks/logic-hooks';
import { Message } from '@/interfaces/database/chat';
import { message } from 'antd';
import { get } from 'lodash';
import trim from 'lodash/trim';
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useSearchParams } from 'umi';
import { v4 as uuid } from 'uuid';
import { useHandleMessageInputChange } from './hooks';

export const useSelectNextSharedMessages = () => {
// const { data, loading } = useFetchNextSharedConversation(conversationId);

const {
derivedMessages,
ref,
setDerivedMessages,
addNewestAnswer,
addNewestQuestion,
removeLatestMessage,
} = useSelectDerivedMessages();

// useEffect(() => {
// setDerivedMessages(data?.data?.message);
// }, [setDerivedMessages, data]);

return {
derivedMessages,
addNewestAnswer,
addNewestQuestion,
removeLatestMessage,
// loading,
ref,
setDerivedMessages,
};
};
const isCompletionError = (res: any) =>
res && (res?.response.status !== 200 || res?.data?.code !== 0);

export const useSendButtonDisabled = (value: string) => {
return trim(value) === '';
Expand Down Expand Up @@ -66,7 +43,8 @@ export const useSendSharedMessage = () => {
removeLatestMessage,
addNewestAnswer,
addNewestQuestion,
} = useSelectNextSharedMessages();
} = useSelectDerivedMessages();
const [hasError, setHasError] = useState(false);

const sendMessage = useCallback(
async (message: Message, id?: string) => {
Expand All @@ -77,7 +55,7 @@ export const useSendSharedMessage = () => {
session_id: get(derivedMessages, '0.session_id'),
});

if (res && (res?.response.status !== 200 || res?.data?.code !== 0)) {
if (isCompletionError(res)) {
// cancel loading
setValue(message.content);
removeLatestMessage();
Expand All @@ -103,7 +81,10 @@ export const useSendSharedMessage = () => {

const fetchSessionId = useCallback(async () => {
const ret = await send({ question: '' });
console.log('🚀 ~ fetchSessionId ~ ret:', ret);
if (isCompletionError(ret)) {
message.error(ret?.data.message);
setHasError(true);
}
}, [send]);

useEffect(() => {
Expand Down Expand Up @@ -146,5 +127,6 @@ export const useSendSharedMessage = () => {
ref,
loading: false,
derivedMessages,
hasError,
};
};

0 comments on commit b445e43

Please sign in to comment.