@kevisual/api 0.0.48 → 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+ import { create } from 'zustand';
3
+ import { toast } from 'sonner';
4
+ import { useContextKey } from '@kevisual/context';
5
+ import { type QueryLoginBrowser } from '@/query/query-login/index.ts'
6
+ const queryLogin = useContextKey<QueryLoginBrowser>('queryLogin');
7
+
8
+ type Me = {
9
+ id?: string;
10
+ username?: string;
11
+ nickname?: string | null;
12
+ needChangePassword?: boolean;
13
+ role?: string;
14
+ description?: string | null;
15
+ type?: 'user' | 'org';
16
+ orgs?: string[];
17
+ avatar?: string;
18
+ };
19
+ export type LayoutStore = {
20
+ open: boolean;
21
+ setOpen: (open: boolean) => void;
22
+ me: Me;
23
+ setMe: (me: Me) => void;
24
+ getMe: () => Promise<void>;
25
+ openUser: boolean;
26
+ setOpenUser: (openUser: boolean) => void;
27
+ switchOrg: (username?: string, type?: 'user' | 'org') => Promise<void>;
28
+ isAdmin: boolean;
29
+ setIsAdmin: (isAdmin: boolean) => void;
30
+ checkHasOrg: () => boolean;
31
+ };
32
+ export const useLayoutStore = create<LayoutStore>((set, get) => ({
33
+ open: false,
34
+ setOpen: (open) => set({ open }),
35
+ me: {},
36
+ setMe: (me) => set({ me }),
37
+ getMe: async () => {
38
+ const res = await queryLogin.getMe();
39
+ if (res.code === 200) {
40
+ set({ me: res.data });
41
+ set({ isAdmin: res.data.orgs?.includes('admin') });
42
+ }
43
+ },
44
+ openUser: false,
45
+ setOpenUser: (openUser) => set({ openUser }),
46
+ switchOrg: async (username?: string, type?: string) => {
47
+ const res = await queryLogin.switchUser(username || '');
48
+ if (res.code === 200) {
49
+ toast.success('Switch success');
50
+ setTimeout(() => {
51
+ window.location.reload();
52
+ }, 1000);
53
+ } else {
54
+ toast.error(res.message || 'Request failed');
55
+ }
56
+ },
57
+ isAdmin: false,
58
+ setIsAdmin: (isAdmin) => set({ isAdmin }),
59
+ checkHasOrg: () => {
60
+ const user = get().me || {};
61
+ if (!user.orgs) {
62
+ return false;
63
+ }
64
+ return user?.orgs?.length > 0;
65
+ },
66
+ }));
@@ -0,0 +1,132 @@
1
+ import { create } from 'zustand';
2
+ import { type Result } from '@kevisual/query/query';
3
+ import { MarkType, Mark, QueryMark } from '@kevisual/api/query-mark';
4
+ import { uniqBy } from 'es-toolkit';
5
+ import { useContextKey } from '@kevisual/context';
6
+ import { type QueryLoginBrowser } from '@/query/query-login/index.ts'
7
+ const queryClient = useContextKey<QueryLoginBrowser>('queryLogin');
8
+
9
+ type ManagerStore = {
10
+ /** 当前选中的Mark */
11
+ currentMarkId: string;
12
+ setCurrentMarkId: (markId: string) => void;
13
+ markData?: Mark | undefined;
14
+ setMarkData: (mark?: Partial<Mark>) => void;
15
+ /** 获取Mark列表 */
16
+ getList: () => Promise<any>;
17
+ getMarkFromList: (markId: string) => Mark | undefined;
18
+ updateMark: (mark: Mark) => Promise<any>;
19
+ getMark: (markId: string) => Promise<Result<Mark>>;
20
+ deleteMark: (markId: string) => Promise<any>;
21
+ /** Mark列表 */
22
+ list: Mark[];
23
+ setList: (list: Mark[]) => void;
24
+ pagination: {
25
+ current: number;
26
+ pageSize: number;
27
+ total: number;
28
+ };
29
+ setPagination: (pagination: { current: number; pageSize: number; total: number }) => void;
30
+ /** 搜索 */
31
+ search: string;
32
+ setSearch: (search: string) => void;
33
+ /** 初始化 */
34
+ init: (markType: MarkType) => Promise<void>;
35
+ queryMark?: QueryMark;
36
+ markType?: MarkType;
37
+ open: boolean;
38
+ setOpen: (open: boolean) => void;
39
+ };
40
+ export const useMarkStore = create<ManagerStore>((set, get) => {
41
+ return {
42
+ currentMarkId: '',
43
+ setCurrentMarkId: (markId: string) => set(() => ({ currentMarkId: markId })),
44
+ open: false,
45
+ setOpen: (open: boolean) => set(() => ({ open })),
46
+ getList: async () => {
47
+ const queryMark = get().queryMark!;
48
+ const { search, pagination } = get();
49
+ const res = await queryMark.getMarkList({ page: pagination.current, pageSize: pagination.pageSize, search });
50
+ const oldList = get().list;
51
+ if (res.code === 200) {
52
+ const { pagination, list } = res.data || {};
53
+ const newList = [...oldList, ...list!];
54
+ const uniqueList = uniqBy(newList, (item) => item.id);
55
+ set(() => ({ list: uniqueList }));
56
+ set(() => ({ pagination: { current: pagination!.current, pageSize: pagination!.pageSize, total: pagination!.total } }));
57
+ }
58
+ },
59
+ getMarkFromList: (markId: string) => {
60
+ return get().list.find((item) => item.id === markId);
61
+ },
62
+ updateMark: async (mark: Mark) => {
63
+ const queryMark = get().queryMark!;
64
+ const res = await queryMark.updateMark(mark);
65
+ if (res.code === 200) {
66
+ set((state) => {
67
+ const oldList = state.list;
68
+ const resMark = res.data!;
69
+ const newList = oldList.map((item) => (item.id === mark.id ? mark : item));
70
+ if (!mark.id) {
71
+ newList.unshift(resMark);
72
+ }
73
+ return {
74
+ list: newList,
75
+ };
76
+ });
77
+ }
78
+ return res;
79
+ },
80
+ getMark: async (markId: string) => {
81
+ const queryMark = get().queryMark!;
82
+ const res = await queryMark.getMark(markId);
83
+ return res;
84
+ },
85
+ list: [],
86
+ setList: (list: any[]) => set(() => ({ list })),
87
+ init: async (markType: MarkType = 'wallnote') => {
88
+ // await get().getList();
89
+ console.log('init', set, get);
90
+ const queryMark = new QueryMark({
91
+ query: queryClient as any,
92
+ markType,
93
+ });
94
+ const url = new URL(window.location.href);
95
+ const pageSize = url.searchParams.get('pageSize') || '10';
96
+ set({ queryMark, markType, list: [], pagination: { current: 1, pageSize: parseInt(pageSize), total: 0 }, currentMarkId: '', markData: undefined });
97
+ setTimeout(async () => {
98
+ console.log('get', get);
99
+ get().getList();
100
+ }, 1000);
101
+ },
102
+ deleteMark: async (markId: string) => {
103
+ const queryMark = get().queryMark!;
104
+ const res = await queryMark.deleteMark(markId);
105
+ const currentMarkId = get().currentMarkId;
106
+ if (res.code === 200) {
107
+ // get().getList();
108
+ set((state) => ({
109
+ list: state.list.filter((item) => item.id !== markId),
110
+ }));
111
+ if (currentMarkId === markId) {
112
+ set(() => ({ currentMarkId: '', markData: undefined }));
113
+ }
114
+ }
115
+ return res;
116
+ },
117
+ queryMark: undefined,
118
+ markType: 'simple',
119
+ markData: undefined,
120
+ setMarkData: (mark?: Partial<Mark>) => set(() => ({ markData: mark as Mark })),
121
+ pagination: {
122
+ current: 1,
123
+ pageSize: 10,
124
+ total: 0,
125
+ },
126
+ setPagination: (pagination: { current: number; pageSize: number; total: number }) => set(() => ({ pagination })),
127
+ /** 搜索 */
128
+ search: '',
129
+ setSearch: (search: string) => set(() => ({ search, list: [], pagination: { current: 1, pageSize: 10, total: 0 } })),
130
+ };
131
+ });
132
+