返回 DeepSeek-Reasonix
modelFavorites.ts
根目录 / desktop / frontend / src / lib / modelFavorites.ts
1 export const MODEL_FAVORITES_STORAGE_KEY = "reasonix-model-favorites-v1";
2
3 type ReadableStorage = Pick<Storage, "getItem">;
4 type WritableStorage = Pick<Storage, "setItem">;
5
6 function browserStorage(): Storage | undefined {
7 return typeof localStorage === "undefined" ? undefined : localStorage;
8 }
9
10 export function normalizeModelFavorites(value: unknown): string[] {
11 if (!Array.isArray(value)) return [];
12 return [...new Set(value.filter((item): item is string => typeof item === "string" && item.trim().length > 0))];
13 }
14
15 export function readModelFavorites(storage: ReadableStorage | undefined = browserStorage()): Set<string> {
16 if (!storage) return new Set();
17 try {
18 const raw = storage.getItem(MODEL_FAVORITES_STORAGE_KEY);
19 return new Set(raw ? normalizeModelFavorites(JSON.parse(raw)) : []);
20 } catch {
21 return new Set();
22 }
23 }
24
25 export function writeModelFavorites(favorites: ReadonlySet<string>, storage: WritableStorage | undefined = browserStorage()): boolean {
26 if (!storage) return false;
27 try {
28 storage.setItem(MODEL_FAVORITES_STORAGE_KEY, JSON.stringify([...favorites].sort()));
29 return true;
30 } catch {
31 return false;
32 }
33 }
34
34 lines TYPESCRIPT