feat: chatgpt-prompts sync

This commit is contained in:
lencx
2022-12-19 02:56:53 +08:00
parent 02fb4dd3b7
commit c54aec88c0
21 changed files with 367 additions and 23 deletions

View File

@@ -4,20 +4,20 @@ import { clone } from 'lodash';
import { CHAT_MODEL_JSON, readJSON, writeJSON } from '@/utils';
import useInit from '@/hooks/useInit';
export default function useChatModel() {
export default function useChatModel(key: string) {
const [modelJson, setModelJson] = useState<Record<string, any>>({});
useInit(async () => {
const data = await readJSON(CHAT_MODEL_JSON, { name: 'ChatGPT Model', data: [] });
const data = await readJSON(CHAT_MODEL_JSON, { name: 'ChatGPT Model', [key]: [] });
setModelJson(data);
});
const modelSet = async (data: Record<string, any>[]) => {
const oData = clone(modelJson);
oData.data = data;
oData[key] = data;
await writeJSON(CHAT_MODEL_JSON, oData);
setModelJson(oData);
}
return { modelJson, modelSet, modelData: modelJson?.data || [] }
return { modelJson, modelSet, modelData: modelJson?.[key] || [] }
}

View File

@@ -8,5 +8,5 @@ export default function useInit(callback: () => void) {
callback();
isInit.current = false;
}
}, [])
})
}