Refactor "UI Page [Chats Prompt] (#20))"

[+] refactor(prompt.ts): simplify sorting logic in getUserPrompts method
[+] feat(prompt.ts): add support for different languages in fetching prompts
This commit is contained in:
H0llyW00dzZ 2023-09-28 02:38:16 +07:00
parent fc4953f9a7
commit 49be21bef9
No known key found for this signature in database
GPG Key ID: 05C7FFFC0845C930

View File

@ -124,7 +124,7 @@ export const usePromptStore = createPersistStore(
search(text: string) {
if (text.length === 0) {
// return all rompts
// return all prompts
return this.getUserPrompts().concat(SearchService.builtinPrompts);
}
return SearchService.search(text) as Prompt[];
@ -154,15 +154,23 @@ export const usePromptStore = createPersistStore(
fetch(PROMPT_URL)
.then((res) => res.json())
.then((res) => {
let fetchPrompts = [res.en, res.id, res.cn];
if (getLang() === "cn") {
fetchPrompts = fetchPrompts.reverse();
const lang = getLang();
let fetchPrompts: PromptList;
switch (lang) {
case "cn":
fetchPrompts = res.cn;
break;
case "id":
fetchPrompts = res.id;
break;
// Add cases for other languages here
default:
fetchPrompts = res.en;
break;
}
if (getLang() === "id") {
fetchPrompts = fetchPrompts.reverse();
}
const builtinPrompts = fetchPrompts.map((promptList: PromptList) => {
return promptList.map(
const builtinPrompts = fetchPrompts.map(
([title, content]) =>
({
id: nanoid(),
@ -171,15 +179,13 @@ export const usePromptStore = createPersistStore(
createdAt: Date.now(),
}) as Prompt,
);
});
const userPrompts = usePromptStore.getState().getUserPrompts() ?? [];
const allPromptsForSearch = builtinPrompts
.reduce((pre, cur) => pre.concat(cur), [])
.concat(userPrompts)
.filter((v) => !!v.title && !!v.content);
SearchService.count.builtin =
res.en.length + res.id.length + res.cn.length;
SearchService.count.builtin = fetchPrompts.length;
SearchService.init(allPromptsForSearch, userPrompts);
});
},