forked from ChatGPTNextWeb/NextChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.ts
81 lines (73 loc) · 1.9 KB
/
mask.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { getLang, Lang } from "../locales";
import { Message } from "./chat";
import { ModelConfig, useAppConfig } from "./config";
export const MASK_KEY = "mask-store";
export type Mask = {
id: number;
avatar: string;
name: string;
context: Message[];
config: ModelConfig;
lang: Lang;
};
export const DEFAULT_MASK_STATE = {
masks: {} as Record<number, Mask>,
globalMaskId: 0,
};
export type MaskState = typeof DEFAULT_MASK_STATE;
type MaskStore = MaskState & {
create: (mask: Partial<Mask>) => Mask;
update: (id: number, updater: (mask: Mask) => void) => void;
delete: (id: number) => void;
search: (text: string) => Mask[];
getAll: () => Mask[];
};
export const useMaskStore = create<MaskStore>()(
persist(
(set, get) => ({
...DEFAULT_MASK_STATE,
create(mask) {
set(() => ({ globalMaskId: get().globalMaskId + 1 }));
const id = get().globalMaskId;
const masks = get().masks;
masks[id] = {
id,
avatar: "1f916",
name: "",
config: useAppConfig.getState().modelConfig,
context: [],
lang: getLang(),
...mask,
};
set(() => ({ masks }));
return masks[id];
},
update(id, updater) {
const masks = get().masks;
const mask = masks[id];
if (!mask) return;
const updateMask = { ...mask };
updater(updateMask);
masks[id] = updateMask;
set(() => ({ masks }));
},
delete(id) {
const masks = get().masks;
delete masks[id];
set(() => ({ masks }));
},
getAll() {
return Object.values(get().masks).sort((a, b) => a.id - b.id);
},
search(text) {
return Object.values(get().masks);
},
}),
{
name: MASK_KEY,
version: 2,
},
),
);