The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cause from "effect/Cause"; |
| 2 | import { AsyncResult } from "effect/reactivity"; |
| 3 | import { msg } from "gt-react"; |
| 4 | |
| 5 | import { cancelPreparingComposerImages } from "../components/chat/preparingComposerImages"; |
| 6 | import { |
| 7 | type ComposerThreadTarget, |
| 8 | composerDraftHasUserContent, |
| 9 | composerTargetKey, |
| 10 | resolveComposerDraftKey, |
| 11 | useComposerDraftStore, |
| 12 | } from "../composerDraftStore"; |
| 13 | import { showThreadUndoNotice } from "../hooks/showThreadUndoNotice"; |
| 14 | import { m } from "../i18n/messages"; |
| 15 | import * as ThreadUndo from "@t3tools/client-runtime/state/thread-undo"; |
| 16 | import { releaseDraftAttachments } from "./attachmentUploadQueue"; |
| 17 | |
| 18 | export function discardComposerDraft(target: ComposerThreadTarget): void { |
| 19 | const store = useComposerDraftStore.getState(); |
| 20 | const key = resolveComposerDraftKey(store, target); |
| 21 | const draft = key === null ? undefined : store.draftsByThreadKey[key]; |
| 22 | if (key === null || !draft) return; |
| 23 | const session = store.draftThreadsByThreadKey[key]; |
| 24 | const logicalProjectKeys = Object.entries(store.logicalProjectDraftThreadKeyByLogicalProjectKey) |
| 25 | .filter(([, draftKey]) => draftKey === key) |
| 26 | .map(([logicalProjectKey]) => logicalProjectKey); |
| 27 | const discardsSession = typeof target === "string"; |
| 28 | |
| 29 | const claim = ThreadUndo.begin("discard", key); |
| 30 | // The composer may key attachments by the target or by its resolved draft id; a late |
| 31 | // compression under either would recreate the draft. |
| 32 | cancelPreparingComposerImages(composerTargetKey(target)); |
| 33 | cancelPreparingComposerImages(key); |
| 34 | if (discardsSession) { |
| 35 | store.clearDraftThread(target); |
| 36 | } else { |
| 37 | store.clearComposerContent(target); |
| 38 | } |
| 39 | |
| 40 | const releaseDiscardedDraft = () => { |
| 41 | releaseDraftAttachments([...draft.images, ...draft.files]); |
| 42 | // Draft-session removal already revoked these previews. |
| 43 | if (!discardsSession) { |
| 44 | for (const image of draft.images) { |
| 45 | if (image.previewUrl.startsWith("blob:")) URL.revokeObjectURL(image.previewUrl); |
| 46 | } |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | showThreadUndoNotice({ |
| 51 | action: "Discarded", |
| 52 | claim, |
| 53 | failureTitle: m(msg("Failed to restore draft")), |
| 54 | undo: async () => { |
| 55 | const current = useComposerDraftStore.getState().draftsByThreadKey[key]; |
| 56 | if (current && composerDraftHasUserContent(current)) { |
| 57 | releaseDiscardedDraft(); |
| 58 | return AsyncResult.failure(Cause.fail(new Error(m(msg("The draft has new content."))))); |
| 59 | } |
| 60 | useComposerDraftStore.setState((state) => { |
| 61 | const logicalProjectDraftThreadKeyByLogicalProjectKey = { |
| 62 | ...state.logicalProjectDraftThreadKeyByLogicalProjectKey, |
| 63 | }; |
| 64 | for (const logicalProjectKey of logicalProjectKeys) { |
| 65 | logicalProjectDraftThreadKeyByLogicalProjectKey[logicalProjectKey] ??= key; |
| 66 | } |
| 67 | return { |
| 68 | draftsByThreadKey: { |
| 69 | ...state.draftsByThreadKey, |
| 70 | [key]: discardsSession |
| 71 | ? { |
| 72 | ...draft, |
| 73 | images: draft.images.map((image) => |
| 74 | image.previewUrl.startsWith("blob:") |
| 75 | ? { ...image, previewUrl: URL.createObjectURL(image.file) } |
| 76 | : image, |
| 77 | ), |
| 78 | } |
| 79 | : draft, |
| 80 | }, |
| 81 | draftThreadsByThreadKey: session |
| 82 | ? { ...state.draftThreadsByThreadKey, [key]: session } |
| 83 | : state.draftThreadsByThreadKey, |
| 84 | logicalProjectDraftThreadKeyByLogicalProjectKey, |
| 85 | }; |
| 86 | }); |
| 87 | return AsyncResult.success(undefined); |
| 88 | }, |
| 89 | commit: releaseDiscardedDraft, |
| 90 | }); |
| 91 | } |