|
1 | 1 | import { $JAWN_API } from "@/lib/clients/jawn"; |
2 | 2 | import { TagType } from "../../../packages/common/sessions/tags"; |
3 | 3 | import { create } from "zustand"; |
| 4 | +import { produce } from "immer"; |
4 | 5 |
|
5 | | -interface TagStore { |
| 6 | +type TagStoreState = { |
6 | 7 | tagStore: { |
7 | | - [key in TagType]: { |
8 | | - [id: string]: string; |
| 8 | + [ordId: string]: { |
| 9 | + [key in TagType]: { |
| 10 | + [id: string]: string; |
| 11 | + }; |
9 | 12 | }; |
10 | 13 | }; |
11 | | - getTag: (id: string, type: TagType) => string | undefined; |
12 | | - setTag: (id: string, tag: string, type: TagType) => void; |
13 | | -} |
| 14 | +}; |
| 15 | +type TagStoreAction = { |
| 16 | + getTag: (orgId: string, session: string, type: TagType) => string | undefined; |
| 17 | + setTag: ( |
| 18 | + orgId: string, |
| 19 | + sessionId: string, |
| 20 | + tag: string, |
| 21 | + type: TagType |
| 22 | + ) => void; |
| 23 | +}; |
14 | 24 |
|
15 | | -export const useTagStore = create<TagStore>((set, get) => ({ |
16 | | - tagStore: { |
17 | | - [TagType.REQUEST]: {}, |
18 | | - [TagType.SESSION]: {}, |
19 | | - }, |
20 | | - getTag: (id: string, type: TagType) => get().tagStore[type][id], |
21 | | - setTag: (id: string, tag: string, type: TagType) => |
22 | | - set((state) => ({ |
23 | | - tagStore: { |
24 | | - ...state.tagStore, |
25 | | - [type]: { |
26 | | - ...state.tagStore[type], |
27 | | - [id]: tag, |
28 | | - }, |
29 | | - }, |
30 | | - })), |
31 | | -})); |
| 25 | +export const useTagStore = create<TagStoreState & TagStoreAction>( |
| 26 | + (set, get) => ({ |
| 27 | + tagStore: {}, |
| 28 | + |
| 29 | + getTag: (orgId: string, sessionId: string, type: TagType) => |
| 30 | + get().tagStore[orgId]?.[type]?.[sessionId], |
| 31 | + |
| 32 | + setTag: (orgId: string, sessionId: string, tag: string, type: TagType) => |
| 33 | + set( |
| 34 | + produce((state) => { |
| 35 | + // Initialize with empty objects using nullish coalescing |
| 36 | + state.tagStore[orgId] = state.tagStore[orgId] ?? {}; |
| 37 | + state.tagStore[orgId][type] = state.tagStore[orgId][type] ?? {}; |
| 38 | + state.tagStore[orgId][type][sessionId] = tag; |
| 39 | + }) |
| 40 | + ), |
| 41 | + }) |
| 42 | +); |
32 | 43 |
|
33 | | -export async function fetchTag(id: string, type: TagType) { |
| 44 | +export async function fetchTag( |
| 45 | + orgId: string, |
| 46 | + sessionId: string, |
| 47 | + type: TagType, |
| 48 | + setTag: TagStoreAction["setTag"] |
| 49 | +) { |
34 | 50 | if (type === TagType.SESSION) { |
35 | 51 | const response = await $JAWN_API.GET("/v1/session/{sessionId}/tag", { |
36 | 52 | params: { |
37 | | - path: { sessionId: id }, |
| 53 | + path: { sessionId: sessionId }, |
38 | 54 | }, |
39 | 55 | }); |
40 | 56 |
|
| 57 | + if (!response.error && response.data?.data) { |
| 58 | + setTag(orgId, sessionId, response.data?.data, TagType.SESSION); |
| 59 | + } |
| 60 | + |
41 | 61 | return response; |
42 | 62 | } |
| 63 | + |
| 64 | + // TODO: Implement fetching tags for other types |
| 65 | + throw new Error(`Fetching tags for ${type} is not implemented`); |
43 | 66 | } |
44 | 67 |
|
45 | | -export async function updateTag(id: string, tag: string, type: TagType) { |
| 68 | +export async function updateTag( |
| 69 | + orgId: string, |
| 70 | + sessionId: string, |
| 71 | + tag: string, |
| 72 | + type: TagType, |
| 73 | + setTag: TagStoreAction["setTag"] |
| 74 | +) { |
46 | 75 | if (type === TagType.SESSION) { |
47 | 76 | const response = await $JAWN_API.POST("/v1/session/{sessionId}/tag", { |
48 | 77 | params: { |
49 | | - path: { sessionId: id }, |
| 78 | + path: { sessionId: sessionId }, |
50 | 79 | }, |
51 | 80 | body: { |
52 | 81 | tag: tag, |
53 | 82 | }, |
54 | 83 | }); |
55 | 84 |
|
| 85 | + if (!response.error) { |
| 86 | + setTag(orgId, sessionId, tag, TagType.SESSION); |
| 87 | + } |
| 88 | + |
56 | 89 | return response; |
57 | 90 | } |
| 91 | + |
| 92 | + // TODO: Implement updating tags for other types |
| 93 | + throw new Error(`Updating tags for ${type} is not implemented`); |
58 | 94 | } |
0 commit comments