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) { search(text: string) {
if (text.length === 0) { if (text.length === 0) {
// return all rompts // return all prompts
return this.getUserPrompts().concat(SearchService.builtinPrompts); return this.getUserPrompts().concat(SearchService.builtinPrompts);
} }
return SearchService.search(text) as Prompt[]; return SearchService.search(text) as Prompt[];
@ -154,32 +154,38 @@ export const usePromptStore = createPersistStore(
fetch(PROMPT_URL) fetch(PROMPT_URL)
.then((res) => res.json()) .then((res) => res.json())
.then((res) => { .then((res) => {
let fetchPrompts = [res.en, res.id, res.cn]; const lang = getLang();
if (getLang() === "cn") { let fetchPrompts: PromptList;
fetchPrompts = fetchPrompts.reverse();
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(
} ([title, content]) =>
const builtinPrompts = fetchPrompts.map((promptList: PromptList) => { ({
return promptList.map( id: nanoid(),
([title, content]) => title,
({ content,
id: nanoid(), createdAt: Date.now(),
title, }) as Prompt,
content, );
createdAt: Date.now(),
}) as Prompt,
);
});
const userPrompts = usePromptStore.getState().getUserPrompts() ?? []; const userPrompts = usePromptStore.getState().getUserPrompts() ?? [];
const allPromptsForSearch = builtinPrompts const allPromptsForSearch = builtinPrompts
.reduce((pre, cur) => pre.concat(cur), []) .concat(userPrompts)
.filter((v) => !!v.title && !!v.content); .filter((v) => !!v.title && !!v.content);
SearchService.count.builtin = SearchService.count.builtin = fetchPrompts.length;
res.en.length + res.id.length + res.cn.length;
SearchService.init(allPromptsForSearch, userPrompts); SearchService.init(allPromptsForSearch, userPrompts);
}); });
}, },