mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-09 19:46:37 +08:00
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:
parent
fc4953f9a7
commit
49be21bef9
@ -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,32 +154,38 @@ 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(
|
||||
([title, content]) =>
|
||||
({
|
||||
id: nanoid(),
|
||||
title,
|
||||
content,
|
||||
createdAt: Date.now(),
|
||||
}) as Prompt,
|
||||
);
|
||||
});
|
||||
|
||||
const builtinPrompts = fetchPrompts.map(
|
||||
([title, content]) =>
|
||||
({
|
||||
id: nanoid(),
|
||||
title,
|
||||
content,
|
||||
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);
|
||||
});
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user