perf: improve prompt list performance

This commit is contained in:
Yidadaa
2023-05-02 02:26:43 +08:00
parent a69cec89fb
commit e509749421
12 changed files with 240 additions and 80 deletions

View File

@@ -17,11 +17,12 @@ export interface PromptStore {
prompts: Record<number, Prompt>;
add: (prompt: Prompt) => number;
get: (id: number) => Prompt | undefined;
remove: (id: number) => void;
search: (text: string) => Prompt[];
update: (id: number, updater: (prompt: Prompt) => void) => void;
getUserPrompts: () => Prompt[];
updateUserPrompts: (id: number, updater: (prompt: Prompt) => void) => void;
}
export const SearchService = {
@@ -81,6 +82,16 @@ export const usePromptStore = create<PromptStore>()(
return prompt.id!;
},
get(id) {
const targetPrompt = get().prompts[id];
if (!targetPrompt) {
return SearchService.builtinPrompts.find((v) => v.id === id);
}
return targetPrompt;
},
remove(id) {
const prompts = get().prompts;
delete prompts[id];
@@ -98,7 +109,7 @@ export const usePromptStore = create<PromptStore>()(
return userPrompts;
},
updateUserPrompts(id: number, updater) {
update(id: number, updater) {
const prompt = get().prompts[id] ?? {
title: "",
content: "",