Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadHandoverDraft.ts

47 lines1.6 KB

1import type { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { threadHandoverSourceKey } from "@t3tools/shared/threadReference";
3​
4/**
5 * Stable receipt for importing one source thread's handover into its project
6 * draft. The receipt is persisted with the draft, so retries remain idempotent
7 * after navigation, module reload, or an app restart.
8 */
9export function threadHandoverDraftImportId(
10 environmentId: EnvironmentId,
11 threadId: ThreadId,
12): string {
13 return threadHandoverSourceKey(environmentId, threadId);
15​
16import { appAtomRegistry } from "./atom-registry";
17import {
18 composerDraftsAtom,
19 createNewTaskDraft,
20 mergeComposerDraftContent,
21 waitForComposerDraftsLoaded,
22 type ComposerDraftWorkspaceSelection,
23} from "./use-composer-drafts";
24​
25/** Reopens the receipt-bearing draft after restart, or creates a new project-stamped draft. */
26export async function prepareThreadHandoverDraft(input: {
27 environmentId: EnvironmentId;
28 projectId: ProjectId;
29 importId: string;
30 handover: string;
31 workspaceSelection: ComposerDraftWorkspaceSelection;
32}): Promise<string> {
33 await waitForComposerDraftsLoaded();
34 const existing = Object.entries(appAtomRegistry.get(composerDraftsAtom)).find(
35 ([, draft]) =>
36 draft.project?.environmentId === input.environmentId &&
37 draft.project.projectId === input.projectId &&
38 draft.importedShareIds?.includes(input.importId),
39 );
40 const key = existing?.[0] ?? createNewTaskDraft(input);
41 await mergeComposerDraftContent(
42 key,
43 { text: input.handover, attachments: [], sourceShareId: input.importId },
44 { workspaceSelection: input.workspaceSelection },
45 );
46 return key;