mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
feat: chatgpt-prompts sync
This commit is contained in:
45
src/view/SyncPrompts/config.tsx
vendored
Normal file
45
src/view/SyncPrompts/config.tsx
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Tag } from 'antd';
|
||||
|
||||
export const genCmd = (act: string) => act.replace(/\s+|\/+/g, '_').replace(/[^\d\w]/g, '').toLocaleLowerCase();
|
||||
|
||||
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) => <Switch checked={v} disabled />,
|
||||
// },
|
||||
{
|
||||
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>
|
||||
// ),
|
||||
},
|
||||
];
|
||||
28
src/view/SyncPrompts/index.scss
vendored
Normal file
28
src/view/SyncPrompts/index.scss
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
.chat-prompts-tags {
|
||||
.ant-tag {
|
||||
margin: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
}
|
||||
71
src/view/SyncPrompts/index.tsx
vendored
Normal file
71
src/view/SyncPrompts/index.tsx
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
import { 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 useColumns from '@/hooks/useColumns';
|
||||
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 { 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);
|
||||
})
|
||||
|
||||
const handleSync = async () => {
|
||||
setLoading(true);
|
||||
const res = await fetch(GITHUB_PROMPTS_CSV_URL, {
|
||||
method: 'GET',
|
||||
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 })));
|
||||
setLoading(false);
|
||||
setLastUpdated(fmtDate(Date.now()) as any);
|
||||
message.success('ChatGPT Prompts data synchronization completed!');
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Button type="primary" loading={loading} onClick={handleSync}>Sync</Button>
|
||||
{lastUpdated && <span style={{ marginLeft: 10, color: '#999' }}>Last updated on {fmtDate(lastUpdated)}</span>}
|
||||
<div className="chat-model-path">URL: <a href={promptsURL} target="_blank">{promptsURL}</a></div>
|
||||
<Table
|
||||
key={lastUpdated}
|
||||
rowKey="act"
|
||||
columns={columns}
|
||||
scroll={{ x: 'auto' }}
|
||||
dataSource={tableData}
|
||||
pagination={{
|
||||
hideOnSinglePage: true,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
defaultPageSize: 5,
|
||||
pageSizeOptions: [5, 10, 15, 20],
|
||||
showTotal: (total) => <span>Total {total} items</span>,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user