mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
chore: sync
This commit is contained in:
47
src/view/model/SyncPrompts/config.tsx
vendored
Normal file
47
src/view/model/SyncPrompts/config.tsx
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Switch, Tag, Tooltip } from 'antd';
|
||||
|
||||
import { genCmd } from '@/utils';
|
||||
|
||||
export const modelColumns = () => [
|
||||
{
|
||||
title: '/{cmd}',
|
||||
dataIndex: 'cmd',
|
||||
fixed: 'left',
|
||||
// width: 120,
|
||||
key: 'cmd',
|
||||
render: (_: string, row: Record<string, string>) => (
|
||||
<Tag color="#2a2a2a">/{genCmd(row.act)}</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Act',
|
||||
dataIndex: 'act',
|
||||
key: 'act',
|
||||
// width: 200,
|
||||
},
|
||||
{
|
||||
title: 'Tags',
|
||||
dataIndex: 'tags',
|
||||
key: 'tags',
|
||||
// width: 150,
|
||||
render: () => <Tag>chatgpt-prompts</Tag>,
|
||||
},
|
||||
{
|
||||
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>
|
||||
),
|
||||
},
|
||||
];
|
||||
41
src/view/model/SyncPrompts/index.scss
vendored
Normal file
41
src/view/model/SyncPrompts/index.scss
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
.chat-prompts-tags {
|
||||
.ant-tag {
|
||||
margin: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.chat-table-tip, .chat-table-btns {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.chat-table-btns {
|
||||
margin-bottom: 5px;
|
||||
|
||||
.num {
|
||||
margin-left: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
.chat-model-path {
|
||||
font-size: 12px;
|
||||
font-weight: bold;
|
||||
color: #888;
|
||||
margin-bottom: 5px;
|
||||
|
||||
span {
|
||||
display: inline-block;
|
||||
// background-color: #d8d8d8;
|
||||
color: #4096ff;
|
||||
padding: 0 8px;
|
||||
height: 20px;
|
||||
line-height: 20px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
104
src/view/model/SyncPrompts/index.tsx
vendored
Normal file
104
src/view/model/SyncPrompts/index.tsx
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Table, Button, message, Popconfirm } from 'antd';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import { fetch, ResponseType } from '@tauri-apps/api/http';
|
||||
import { writeTextFile } from '@tauri-apps/api/fs';
|
||||
|
||||
import useColumns from '@/hooks/useColumns';
|
||||
import useData from '@/hooks/useData';
|
||||
import useChatModel from '@/hooks/useChatModel';
|
||||
import useTable, { TABLE_PAGINATION } from '@/hooks/useTable';
|
||||
import { fmtDate, chatPromptsPath, GITHUB_PROMPTS_CSV_URL, genCmd } from '@/utils';
|
||||
import { modelColumns } from './config';
|
||||
import './index.scss';
|
||||
|
||||
const promptsURL = 'https://github.com/f/awesome-chatgpt-prompts/blob/main/prompts.csv';
|
||||
|
||||
export default function LanguageModel() {
|
||||
const { rowSelection, selectedRowIDs } = useTable();
|
||||
const [lastUpdated, setLastUpdated] = useState();
|
||||
const { modelJson, modelSet } = useChatModel('sys_sync_prompts');
|
||||
const { opData, opInit, opReplace, opReplaceItems, opSafeKey } = useData([]);
|
||||
const { columns, ...opInfo } = useColumns(modelColumns());
|
||||
|
||||
const selectedItems = rowSelection.selectedRowKeys || [];
|
||||
|
||||
useEffect(() => {
|
||||
if (!modelJson?.sys_sync_prompts) return;
|
||||
opInit(modelJson?.sys_sync_prompts);
|
||||
if (lastUpdated) return;
|
||||
(async () => {
|
||||
const fileData: Record<string, any> = await invoke('metadata', { path: await chatPromptsPath() });
|
||||
setLastUpdated(fileData.accessedAtMs);
|
||||
})();
|
||||
}, [modelJson?.sys_sync_prompts])
|
||||
|
||||
const handleSync = async () => {
|
||||
const res = await fetch(GITHUB_PROMPTS_CSV_URL, {
|
||||
method: 'GET',
|
||||
responseType: ResponseType.Text,
|
||||
});
|
||||
const data = (res.data || '') as string;
|
||||
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!');
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (opInfo.opType === 'enable') {
|
||||
const data = opReplace(opInfo?.opRecord?.[opSafeKey], opInfo?.opRecord);
|
||||
modelSet(data);
|
||||
}
|
||||
}, [opInfo.opTime]);
|
||||
|
||||
const handleEnable = (isEnable: boolean) => {
|
||||
const data = opReplaceItems(selectedRowIDs, { enable: isEnable })
|
||||
modelSet(data);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="chat-table-btns">
|
||||
<div>
|
||||
{selectedItems.length > 0 && (
|
||||
<>
|
||||
<Button type="primary" onClick={() => handleEnable(true)}>Enable</Button>
|
||||
<Button onClick={() => handleEnable(false)}>Disable</Button>
|
||||
<span className="num">Selected {selectedItems.length} items</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<Popconfirm
|
||||
title={<span>Data sync will enable all prompts,<br/>are you sure you want to sync?</span>}
|
||||
placement="topLeft"
|
||||
onConfirm={handleSync}
|
||||
okText="Yes"
|
||||
cancelText="No"
|
||||
>
|
||||
<Button type="primary">Sync</Button>
|
||||
</Popconfirm>
|
||||
</div>
|
||||
<div className="chat-table-tip">
|
||||
<span className="chat-model-path">URL: <a href={promptsURL} target="_blank" title={promptsURL}>f/awesome-chatgpt-prompts/prompts.csv</a></span>
|
||||
{lastUpdated && <span style={{ marginLeft: 10, color: '#999' }}>Last updated on {fmtDate(lastUpdated)}</span>}
|
||||
</div>
|
||||
<Table
|
||||
key={lastUpdated}
|
||||
rowKey="act"
|
||||
columns={columns}
|
||||
scroll={{ x: 'auto' }}
|
||||
dataSource={opData}
|
||||
rowSelection={rowSelection}
|
||||
pagination={TABLE_PAGINATION}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user