Commit 6a01b97b authored by 熊洋洋's avatar 熊洋洋

improve: improve ts code

parent 52ea0215
...@@ -24,37 +24,31 @@ import React, { ...@@ -24,37 +24,31 @@ import React, {
useState, useState,
} from 'react'; } from 'react';
import { IconRecord, IconRobot, IconSend, IconSwap, IconVoice } from '@arco-design/web-react/icon'; import { IconRecord, IconRobot, IconSend, IconSwap, IconVoice } from '@arco-design/web-react/icon';
import { getModel } from '@/utils/gpt';
import OpenAI from '@/client/api/openAI'; import OpenAI from '@/client/api/openAI';
// import useSpeechToText from 'react-hook-speech-to-text';
let useSpeechToText: any; let useSpeechToText: any;
// (async () => {
// if (process.browser) useSpeechToText = (await import('react-hook-speech-to-text')).default;
// })();
export interface ChatMessage { export interface ChatMessage {
role: 'system' | 'user' | 'assistant'; role: 'system' | 'user' | 'assistant';
content: string; content: string;
} }
const getQuestion = (question: string) => `${question}`; const getQuestion = (question: string) => `${question}`;
const model = getModel('gpt35');
type AIProps = { type AIProps = {
context: any; context?: any;
messageList: any[]; messageList: any[];
setMessageList: (value: any[]) => any; setMessageList: (value: any[]) => any;
startView: number; startView?: number;
renderMessageItem: any; renderMessageItem?: any;
doneFx: (message: string) => any; doneFx?: (message: string) => any;
simpleMode?: string | true; simpleMode?: string | true;
simpleModeVisible?: boolean; simpleModeVisible?: boolean;
setSimpleModeVisible?: () => any; setSimpleModeVisible?: (flag?: boolean) => any;
welcome: ReactElement | string; welcome?: ReactElement | string;
quickTip?: string[] | { value: string; name: string; [key: string]: any }[]; quickTip?: string[] | { value: string; name: string; [key: string]: any }[];
searchFlag: RegExp; searchFlag?: RegExp;
inputProps?: Record<string, any>; inputProps?: Record<string, any>;
SendButton: ({ inputRef }: { inputRef: any }) => JSX.Element; SendButton?: ({ inputRef }: { inputRef: any }) => JSX.Element;
}; };
export function AIWrapper({ export function AIWrapper({
...@@ -73,18 +67,13 @@ export function AIWrapper({ ...@@ -73,18 +67,13 @@ export function AIWrapper({
inputProps = {}, inputProps = {},
SendButton, SendButton,
}: AIProps) { }: AIProps) {
const input = useRef<HTMLInputElement>(); const input = useRef<any>();
const panelRef = useRef<HTMLInputElement>(); const scrollContainer = useRef<any>();
const [currentAssistantMessage, setCurrentAssistantMessage] = useState('');
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [_, refresh] = useReducer(state => state++, 0);
const scrollContainer = useRef();
const message = useRef([]);
const working = useRef(false);
const [Niche, setNiche] = useState('you');
const [tinking, setTinking] = useState(false);
// 发送信息事件和成功的事件
const [currentAssistantMessage, setCurrentAssistantMessage] = useState('');
const [tinking, setTinking] = useState(false); //假的加载
const handleButtonClickSuccess = useEffect(() => { const handleButtonClickSuccess = useEffect(() => {
if (!currentAssistantMessage || loading) { if (!currentAssistantMessage || loading) {
return; return;
...@@ -100,7 +89,6 @@ export function AIWrapper({ ...@@ -100,7 +89,6 @@ export function AIWrapper({
setCurrentAssistantMessage(''); setCurrentAssistantMessage('');
// return clearContext; // return clearContext;
}, [currentAssistantMessage, loading]); }, [currentAssistantMessage, loading]);
const toView = useCallback( const toView = useCallback(
debounce(() => { debounce(() => {
scrollContainer && scrollContainer &&
...@@ -111,7 +99,6 @@ export function AIWrapper({ ...@@ -111,7 +99,6 @@ export function AIWrapper({
}, 50), }, 50),
[] []
); );
const handleButtonClick = useCallback( const handleButtonClick = useCallback(
(message?: string, callBack?: (m: string) => void) => { (message?: string, callBack?: (m: string) => void) => {
const inputRef = input.current?.dom; const inputRef = input.current?.dom;
...@@ -139,7 +126,7 @@ export function AIWrapper({ ...@@ -139,7 +126,7 @@ export function AIWrapper({
setLoading(false); setLoading(false);
setTinking(false); setTinking(false);
setTimeout(toView, 100); setTimeout(toView, 100);
}, 2000); }, 1000);
setCurrentAssistantMessage(currentAssistantMessageStr); setCurrentAssistantMessage(currentAssistantMessageStr);
callBack && callBack(currentAssistantMessageStr); callBack && callBack(currentAssistantMessageStr);
doneFx && doneFx(currentAssistantMessageStr); doneFx && doneFx(currentAssistantMessageStr);
...@@ -164,18 +151,21 @@ export function AIWrapper({ ...@@ -164,18 +151,21 @@ export function AIWrapper({
] ]
); );
// 清除功能
const clear = () => { const clear = () => {
// clearContext().then(() => { const inputRef = get(input, 'current.dom');
const inputRef = input.current.dom; if (inputRef) {
inputRef.value = ''; inputRef.value = '';
setMessageList(messageList.slice(0, startView)); setMessageList(messageList.slice(0, startView));
setCurrentAssistantMessage(''); setCurrentAssistantMessage('');
// }); }
}; };
const [visible, setVisible] = React.useState(false);
const user = {};
// 用户信息
const [Niche, setNiche] = useState('you');
const user: {
photoId?: string;
} = {};
const userIcon = user?.photoId ? ( const userIcon = user?.photoId ? (
`/api/v1/assets/${user?.photoId}` `/api/v1/assets/${user?.photoId}`
) : ( ) : (
...@@ -187,30 +177,16 @@ export function AIWrapper({ ...@@ -187,30 +177,16 @@ export function AIWrapper({
</Avatar> </Avatar>
); );
// 上下文传递
useEffect(() => { useEffect(() => {
context && context({ clear }); context && context({ clear });
}, [context]); }, [context]);
const RenderMessageItem = renderMessageItem || MessageItem;
const { t } = useTranslation('actions'); // 快捷功能
const animaeString = t(
!quickTip.length
? 'Please input your question'
: 'Starting with "/", retrieve frequently asked questions'
);
const [placeholder, setPlaceholder] = useState('');
const [isSearching, setSearching] = useState<string | false>(false); const [isSearching, setSearching] = useState<string | false>(false);
const quickTipData = useMemo(() => { const quickTipData = useMemo(() => {
const el = input.current?.dom; const el = input.current?.dom;
if (isSearching && el) { if (isSearching && el) {
console.log(
filter(quickTip, ({ value }: { value: string }) => {
if (el) {
return value.indexOf(isSearching.replace(searchFlag, '').trim()) !== -1;
}
})
);
return filter(quickTip, ({ value }: { value: string }) => { return filter(quickTip, ({ value }: { value: string }) => {
if (el) { if (el) {
return value.indexOf(isSearching.replace(searchFlag, '').trim()) !== -1; return value.indexOf(isSearching.replace(searchFlag, '').trim()) !== -1;
...@@ -221,6 +197,14 @@ export function AIWrapper({ ...@@ -221,6 +197,14 @@ export function AIWrapper({
return []; return [];
}, [isSearching, quickTip, input]); }, [isSearching, quickTip, input]);
//动画
const { t } = useTranslation('actions');
const animaeString = t(
!quickTip.length
? 'Please input your question'
: 'Starting with "/", retrieve frequently asked questions'
);
const [placeholder, setPlaceholder] = useState('');
let timer = useRef<any>(); let timer = useRef<any>();
useEffect(() => { useEffect(() => {
timer.current && clearTimeout(timer.current); timer.current && clearTimeout(timer.current);
...@@ -243,6 +227,8 @@ export function AIWrapper({ ...@@ -243,6 +227,8 @@ export function AIWrapper({
} }
return () => clearTimeout(timer.current); return () => clearTimeout(timer.current);
}, [timer, simpleModeVisible, animaeString]); }, [timer, simpleModeVisible, animaeString]);
//语音功能
const { error, interimResult, isRecording, results, startSpeechToText, stopSpeechToText } = const { error, interimResult, isRecording, results, startSpeechToText, stopSpeechToText } =
useSpeechToText({ useSpeechToText({
continuous: true, continuous: true,
...@@ -271,6 +257,7 @@ export function AIWrapper({ ...@@ -271,6 +257,7 @@ export function AIWrapper({
}, [interimResult, beforeRecordValue]); }, [interimResult, beforeRecordValue]);
const inputValue = input.current?.dom.value || ''; const inputValue = input.current?.dom.value || '';
const RenderMessageItem = renderMessageItem || MessageItem;
const inputNode = ( const inputNode = (
<Input <Input
...@@ -441,14 +428,6 @@ export function AIWrapper({ ...@@ -441,14 +428,6 @@ export function AIWrapper({
/> />
</div> </div>
) : ( ) : (
// <div>
// {
// <RenderMessageItem
// message={currentAssistantMessage}
// role="assistant"
// />
// }
// </div>
<div className="my-[20px]"> <div className="my-[20px]">
<div <div
className={`flex gap-3 p-4 box-border shadow mx-[5px] rounded transition-colors mt-[20px] font-hm ${'bg-[var(--white-bg)] text-[#333]'}`} className={`flex gap-3 p-4 box-border shadow mx-[5px] rounded transition-colors mt-[20px] font-hm ${'bg-[var(--white-bg)] text-[#333]'}`}
......
...@@ -7,7 +7,6 @@ import ReactFlow, { ...@@ -7,7 +7,6 @@ import ReactFlow, {
useEdgesState, useEdgesState,
addEdge, addEdge,
} from 'reactflow'; } from 'reactflow';
// 👇 you need to import the reactflow styles
import 'reactflow/dist/style.css'; import 'reactflow/dist/style.css';
const initialNodes = []; const initialNodes = [];
......
...@@ -7,22 +7,21 @@ import '../styles/markdown/index.scss'; ...@@ -7,22 +7,21 @@ import '../styles/markdown/index.scss';
import '../next-i18next.config.js'; import '../next-i18next.config.js';
import GraphContainer from '../hooks/use-graph-state'; import GraphContainer from '../hooks/use-graph-state';
import { ConfigProvider, ConfigContext } from '@arco-design/web-react'; import { ConfigProvider } from '@arco-design/web-react';
import { useState } from 'react'; import { useState } from 'react';
import { Spin } from '@arco-design/web-react'; import { Spin } from '@arco-design/web-react';
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useContext } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
function MyApp({ Component, pageProps }) { type MyAppProps = {
const [local, setLocal] = useState(); Component: React.FunctionComponent;
pageProps: Record<string, any>;
};
function MyApp({ Component, pageProps }: MyAppProps) {
const [local, setLocal] = useState<any>();
const { i18n } = useTranslation(); const { i18n } = useTranslation();
useEffect(() => { useEffect(() => {
// import('@arco-design/web-react/es/locale/en-US').then(lang => {
// console.log(lang.default);
// setLocal(lang.default);
// });
// console.log(i18n.language);
if (i18n.language === 'en') { if (i18n.language === 'en') {
import('@arco-design/web-react/es/locale/en-US').then(lang => { import('@arco-design/web-react/es/locale/en-US').then(lang => {
setLocal(lang.default); setLocal(lang.default);
......
import React from 'react'; import React, { Ref } from 'react';
import { Form, Input, Checkbox, Button, Radio } from '@arco-design/web-react'; import { Form, Input, Checkbox, Button, Radio } from '@arco-design/web-react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
const FormItem = Form.Item; const FormItem = Form.Item;
const RadioGroup = Radio.Group; const RadioGroup = Radio.Group;
function ActionForm({ form, initialValues = {} }) { type ActionFormProp = {
form: Ref<any>;
initialValues: Record<string, any>;
};
function ActionForm({ form, initialValues }: ActionFormProp) {
const { t } = useTranslation('actionForm'); const { t } = useTranslation('actionForm');
return ( return (
<Form autoComplete="off" form={form} initialValues={initialValues}> <Form autoComplete="off" form={form} initialValues={initialValues}>
......
...@@ -479,7 +479,7 @@ const MessageItemHOC = ({ setShowQueriesList, activeDb, currentModels, activeMod ...@@ -479,7 +479,7 @@ const MessageItemHOC = ({ setShowQueriesList, activeDb, currentModels, activeMod
return component; return component;
}; };
export default function Actions(props) { export default function Actions() {
const [graphs, setGraphs] = useState([]); const [graphs, setGraphs] = useState([]);
const params = useSearchParams(); const params = useSearchParams();
const defaultId = params.get('id'); const defaultId = params.get('id');
......
...@@ -321,7 +321,7 @@ export function QueriesList({ ...@@ -321,7 +321,7 @@ export function QueriesList({
function QueriesRender({ modelId, refresh }: { modelId: string; refresh?: boolean }) { function QueriesRender({ modelId, refresh }: { modelId: string; refresh?: boolean }) {
const { data, isLoading, mutate } = useSWR('all_query_for_Schema', () => { const { data, isLoading, mutate } = useSWR('all_query_for_Schema', () => {
return modelId && ConnectDb.getQueries(modelId); return modelId ? ConnectDb.getQueries(modelId) : undefined;
}); });
useEffect(() => { useEffect(() => {
...@@ -377,14 +377,19 @@ export default function QueriesPage() { ...@@ -377,14 +377,19 @@ export default function QueriesPage() {
const params = useSearchParams(); const params = useSearchParams();
const { t } = useTranslation('queriesList'); const { t } = useTranslation('queriesList');
const { t: ct } = useTranslation(); const { t: ct } = useTranslation();
const id = params.get('id');
return ( return (
<> <>
<Head> <Head>
<title>CHAT QUERY</title> <title>CHAT QUERY</title>
<meta name="description" content={ct('Data Query Based on Data Model and AI')} /> <meta
name="description"
content={ct('Data Query Based on Data Model and AI') as string}
/>
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
</Head> </Head>
<ListNav <ListNav
customNode={[ customNode={[
<Link <Link
...@@ -409,6 +414,7 @@ export default function QueriesPage() { ...@@ -409,6 +414,7 @@ export default function QueriesPage() {
importDBML={undefined} importDBML={undefined}
handlerImportGraph={undefined} handlerImportGraph={undefined}
/> />
<Layout className="overflow-auto mt-[85px]"> <Layout className="overflow-auto mt-[85px]">
<div <div
className="w-[85%] m-auto" className="w-[85%] m-auto"
...@@ -416,7 +422,7 @@ export default function QueriesPage() { ...@@ -416,7 +422,7 @@ export default function QueriesPage() {
height: 'calc(100vh - 120px)', height: 'calc(100vh - 120px)',
}} }}
> >
<QueriesRender modelId={params.get('id')} /> {id && <QueriesRender modelId={id} />}
</div> </div>
</Layout> </Layout>
</> </>
......
...@@ -16,7 +16,10 @@ function Home() { ...@@ -16,7 +16,10 @@ function Home() {
<> <>
<Head> <Head>
<title>CHAT QUERY</title> <title>CHAT QUERY</title>
<meta name="description" content={t('Data Query Based on Data Model and AI')} /> <meta
name="description"
content={t('Data Query Based on Data Model and AI') as string}
/>
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
</Head> </Head>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment