mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
chore: optim
This commit is contained in:
15
src/view/LanguageModel/config.tsx
vendored
15
src/view/LanguageModel/config.tsx
vendored
@@ -1,4 +1,4 @@
|
||||
import { Tag, Switch, Tooltip, Space } from 'antd';
|
||||
import { Tag, Switch, Tooltip, Space, Popconfirm } from 'antd';
|
||||
|
||||
export const modelColumns = () => [
|
||||
{
|
||||
@@ -29,7 +29,9 @@ export const modelColumns = () => [
|
||||
dataIndex: 'enable',
|
||||
key: 'enable',
|
||||
width: 80,
|
||||
render: (v: boolean = false) => <Switch checked={v} disabled />,
|
||||
render: (v: boolean = false, row: Record<string, any>, action: Record<string, any>) => (
|
||||
<Switch checked={v} onChange={(v) => action.setRecord({ ...row, enable: v }, 'enable')} />
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Prompt',
|
||||
@@ -48,7 +50,14 @@ export const modelColumns = () => [
|
||||
render: (_: any, row: any, actions: any) => (
|
||||
<Space size="middle">
|
||||
<a onClick={() => actions.setRecord(row, 'edit')}>Edit</a>
|
||||
<a onClick={() => actions.setRecord(row, 'delete')}>Delete</a>
|
||||
<Popconfirm
|
||||
title="Are you sure to delete this model?"
|
||||
onConfirm={() => actions.setRecord(row, 'delete')}
|
||||
okText="Yes"
|
||||
cancelText="No"
|
||||
>
|
||||
<a>Delete</a>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
}
|
||||
|
||||
16
src/view/LanguageModel/index.tsx
vendored
16
src/view/LanguageModel/index.tsx
vendored
@@ -3,9 +3,9 @@ import { Table, Button, Modal, message } from 'antd';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
|
||||
import useInit from '@/hooks/useInit';
|
||||
import useData from '@/hooks/useData';
|
||||
import useChatModel from '@/hooks/useChatModel';
|
||||
import useColumns from '@/hooks/useColumns';
|
||||
import useData from '@/hooks/useData';
|
||||
import { chatModelPath } from '@/utils';
|
||||
import { modelColumns } from './config';
|
||||
import LanguageModelForm from './Form';
|
||||
@@ -15,10 +15,15 @@ export default function LanguageModel() {
|
||||
const [isVisible, setVisible] = useState(false);
|
||||
const [modelPath, setChatModelPath] = useState('');
|
||||
const { modelData, modelSet } = useChatModel('user_custom');
|
||||
const { opData, opAdd, opRemove, opReplace, opSafeKey } = useData(modelData);
|
||||
const { opData, opInit, opAdd, opRemove, opReplace, opSafeKey } = useData([]);
|
||||
const { columns, ...opInfo } = useColumns(modelColumns());
|
||||
const formRef = useRef<any>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (modelData.length <= 0) return;
|
||||
opInit(modelData);
|
||||
}, [modelData])
|
||||
|
||||
useEffect(() => {
|
||||
if (!opInfo.opType) return;
|
||||
if (['edit', 'new'].includes(opInfo.opType)) {
|
||||
@@ -31,6 +36,13 @@ export default function LanguageModel() {
|
||||
}
|
||||
}, [opInfo.opType, formRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (opInfo.opType === 'enable') {
|
||||
const data = opReplace(opInfo?.opRecord?.[opSafeKey], opInfo?.opRecord);
|
||||
modelSet(data);
|
||||
}
|
||||
}, [opInfo.opTime])
|
||||
|
||||
useInit(async () => {
|
||||
const path = await chatModelPath();
|
||||
setChatModelPath(path);
|
||||
|
||||
24
src/view/SyncPrompts/config.tsx
vendored
24
src/view/SyncPrompts/config.tsx
vendored
@@ -1,4 +1,4 @@
|
||||
import { Tag } from 'antd';
|
||||
import { Switch, Tag, Tooltip } from 'antd';
|
||||
|
||||
export const genCmd = (act: string) => act.replace(/\s+|\/+/g, '_').replace(/[^\d\w]/g, '').toLocaleLowerCase();
|
||||
|
||||
@@ -26,20 +26,22 @@ export const modelColumns = () => [
|
||||
// width: 150,
|
||||
render: () => <Tag>chatgpt-prompts</Tag>,
|
||||
},
|
||||
// {
|
||||
// title: 'Enable',
|
||||
// dataIndex: 'enable',
|
||||
// key: 'enable',
|
||||
// width: 80,
|
||||
// render: (v: boolean = false) => <Switch checked={v} disabled />,
|
||||
// },
|
||||
{
|
||||
title: 'Enable',
|
||||
dataIndex: 'enable',
|
||||
key: 'enable',
|
||||
// width: 80,
|
||||
render: (v: boolean = false, row: Record<string, any>, action: Record<string, any>) => (
|
||||
<Switch checked={v} onChange={(v) => action.setRecord({ ...row, enable: v }, 'enable')} />
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Prompt',
|
||||
dataIndex: 'prompt',
|
||||
key: 'prompt',
|
||||
// width: 300,
|
||||
// render: (v: string) => (
|
||||
// <Tooltip overlayInnerStyle={{ width: 350 }} title={v}><span className="chat-prompts-val">{v}</span></Tooltip>
|
||||
// ),
|
||||
render: (v: string) => (
|
||||
<Tooltip overlayInnerStyle={{ width: 350 }} title={v}><span className="chat-prompts-val">{v}</span></Tooltip>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
60
src/view/SyncPrompts/index.tsx
vendored
60
src/view/SyncPrompts/index.tsx
vendored
@@ -1,33 +1,42 @@
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Table, Button, message } from 'antd';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import { fetch, ResponseType } from '@tauri-apps/api/http';
|
||||
import { writeTextFile, readTextFile } from '@tauri-apps/api/fs';
|
||||
|
||||
import useInit from '@/hooks/useInit';
|
||||
import useColumns from '@/hooks/useColumns';
|
||||
import useData from '@/hooks/useData';
|
||||
import useChatModel from '@/hooks/useChatModel';
|
||||
import { fmtDate, chatPromptsPath, GITHUB_PROMPTS_CSV_URL } from '@/utils';
|
||||
import { modelColumns, genCmd } from './config';
|
||||
import './index.scss';
|
||||
import useInit from '@/hooks/useInit';
|
||||
|
||||
const promptsURL = 'https://github.com/f/awesome-chatgpt-prompts/blob/main/prompts.csv';
|
||||
|
||||
export default function LanguageModel() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [lastUpdated, setLastUpdated] = useState();
|
||||
const { modelSet } = useChatModel('sys_sync_prompts');
|
||||
const [tableData, setTableData] = useState<Record<string, string>[]>([]);
|
||||
const { modelJson, modelSet } = useChatModel('sys_sync_prompts');
|
||||
const { opData, opInit, opReplace, opSafeKey } = useData([]);
|
||||
const { columns, ...opInfo } = useColumns(modelColumns());
|
||||
|
||||
useInit(async () => {
|
||||
const filename = await chatPromptsPath();
|
||||
const data = await readTextFile(filename);
|
||||
const list: Record<string, string>[] = await invoke('parse_prompt', { data });
|
||||
const fileData: Record<string, any> = await invoke('metadata', { path: filename });
|
||||
setLastUpdated(fileData.accessedAtMs);
|
||||
setTableData(list);
|
||||
})
|
||||
// useInit(async () => {
|
||||
// // const filename = await chatPromptsPath();
|
||||
// // const data = await readTextFile(filename);
|
||||
// // const list: Record<string, string>[] = await invoke('parse_prompt', { data });
|
||||
// // const fileData: Record<string, any> = await invoke('metadata', { path: filename });
|
||||
// // setLastUpdated(fileData.accessedAtMs);
|
||||
// // opInit(list);
|
||||
// console.log('«31» /view/SyncPrompts/index.tsx ~> ', modelJson);
|
||||
|
||||
// opInit([]);
|
||||
// })
|
||||
|
||||
useEffect(() => {
|
||||
if (!modelJson?.sys_sync_prompts) return;
|
||||
opInit(modelJson?.sys_sync_prompts)
|
||||
}, [modelJson?.sys_sync_prompts])
|
||||
|
||||
const handleSync = async () => {
|
||||
setLoading(true);
|
||||
@@ -36,16 +45,27 @@ export default function LanguageModel() {
|
||||
responseType: ResponseType.Text,
|
||||
});
|
||||
const data = (res.data || '') as string;
|
||||
// const content = data.replace(/"(\s+)?,(\s+)?"/g, '","');
|
||||
await writeTextFile(await chatPromptsPath(), data);
|
||||
const list: Record<string, string>[] = await invoke('parse_prompt', { data });
|
||||
setTableData(list);
|
||||
modelSet(list.map(i => ({ cmd: genCmd(i.act), enable: true, tags: ['chatgpt-prompts'], ...i })));
|
||||
if (res.ok) {
|
||||
// const content = data.replace(/"(\s+)?,(\s+)?"/g, '","');
|
||||
await writeTextFile(await chatPromptsPath(), data);
|
||||
const list: Record<string, string>[] = await invoke('parse_prompt', { data });
|
||||
opInit(list);
|
||||
modelSet(list.map(i => ({ cmd: genCmd(i.act), enable: true, tags: ['chatgpt-prompts'], ...i })));
|
||||
setLastUpdated(fmtDate(Date.now()) as any);
|
||||
message.success('ChatGPT Prompts data has been synchronized!');
|
||||
} else {
|
||||
message.error('ChatGPT Prompts data sync failed, please try again!');
|
||||
}
|
||||
setLoading(false);
|
||||
setLastUpdated(fmtDate(Date.now()) as any);
|
||||
message.success('ChatGPT Prompts data synchronization completed!');
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (opInfo.opType === 'enable') {
|
||||
const data = opReplace(opInfo?.opRecord?.[opSafeKey], opInfo?.opRecord);
|
||||
modelSet(data);
|
||||
}
|
||||
}, [opInfo.opTime]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Button type="primary" loading={loading} onClick={handleSync}>Sync</Button>
|
||||
@@ -56,7 +76,7 @@ export default function LanguageModel() {
|
||||
rowKey="act"
|
||||
columns={columns}
|
||||
scroll={{ x: 'auto' }}
|
||||
dataSource={tableData}
|
||||
dataSource={opData}
|
||||
pagination={{
|
||||
hideOnSinglePage: true,
|
||||
showSizeChanger: true,
|
||||
|
||||
Reference in New Issue
Block a user