Open History to see the last commit
1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3import { create } from "zustand";
4import { DraftId, useComposerDraftStore } from "./composerDraftStore";
5import { releaseDraftAttachments } from "./lib/attachmentUploadQueue";
6
7export function questionAttachmentDraftPrefix(
8 environmentId: EnvironmentId,
9 threadId: ThreadId,
10): string {
11 return `${encodeURIComponent(JSON.stringify(environmentId))}:question-${encodeURIComponent(JSON.stringify(threadId))}-`;
12}
13
14export function questionAttachmentDraftId(
15 environmentId: EnvironmentId,
16 threadId: ThreadId,
17 requestId: string,
18 questionId: string,
19): DraftId {
20 return DraftId.make(
21 `${questionAttachmentDraftPrefix(environmentId, threadId)}${encodeURIComponent(JSON.stringify([requestId, questionId]))}`,
22 );
23}
24
25type OpenQuestionAttachmentDraft = {
26 readonly draftId: DraftId;
27 readonly requestKeys: ReadonlyArray<DraftId>;
28};
29
30const openQuestionDrafts = new Map<string, OpenQuestionAttachmentDraft>();
31
32export function openQuestionAttachmentDraft(
33 threadRef: ScopedThreadRef,
34): OpenQuestionAttachmentDraft | null {
35 return openQuestionDrafts.get(scopedThreadKey(threadRef)) ?? null;
36}
37
38export function countQuestionRequestAttachments(draftId: DraftId): number | null {
39 for (const draft of openQuestionDrafts.values()) {
40 if (draft.draftId === draftId) return countQuestionAttachments(draft.requestKeys);
41 }
42 return null;
43}
44
45export function trackOpenQuestionAttachmentDraft(
46 threadRef: ScopedThreadRef,
47 draft: OpenQuestionAttachmentDraft,
48): () => void {
49 const key = scopedThreadKey(threadRef);
50 openQuestionDrafts.set(key, draft);
51 return () => {
52 if (openQuestionDrafts.get(key) === draft) openQuestionDrafts.delete(key);
53 };
54}
55
56export const useQuestionAttachmentPreparation = create<{ counts: Record<string, number> }>(() => ({
57 counts: {},
58}));
59
60export function countQuestionAttachments(keys: ReadonlyArray<DraftId>): number {
61 const store = useComposerDraftStore.getState();
62 const { counts } = useQuestionAttachmentPreparation.getState();
63 return keys.reduce((total, key) => {
64 const draft = store.getComposerDraft(key);
65 return total + (draft?.images.length ?? 0) + (draft?.files.length ?? 0) + (counts[key] ?? 0);
66 }, 0);
67}
68
69export function changeQuestionAttachmentPreparation(key: DraftId, delta: number): void {
70 useQuestionAttachmentPreparation.setState((state) =>
71 delta < 0 && !(key in state.counts)
72 ? state
73 : {
74 counts: { ...state.counts, [key]: Math.max(0, (state.counts[key] ?? 0) + delta) },
75 },
76 );
77}
78
79export function clearQuestionAttachmentDraft(key: DraftId): void {
80 const store = useComposerDraftStore.getState();
81 const draft = store.getComposerDraft(key);
82 if (draft) {
83 releaseDraftAttachments([...draft.images, ...draft.files]);
84 for (const image of draft.images) {
85 if (image.previewUrl.startsWith("blob:")) URL.revokeObjectURL(image.previewUrl);
86 }
87 }
88 store.clearComposerContent(key);
89 useQuestionAttachmentPreparation.setState(({ counts }) => {
90 const next = { ...counts };
91 delete next[key];
92 return { counts: next };
93 });
94}