Skip to content
TypeScript

fix(mobile): a file shared into a new task shows in the composer before you send (#21659)

shivam5307c233b1f0
diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts
index 8d48fa129c7..43e14f90ec7 100644
--- a/apps/mobile/src/state/use-composer-drafts.test.ts
+++ b/apps/mobile/src/state/use-composer-drafts.test.ts
@@ -148,7 +148,10 @@ vi.mock("../features/sharing/incoming-share-storage", () => ({
}));
import type { DraftComposerAttachment } from "../lib/composerImages";
-import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
+import {
+ collectComposerContextReferences,
+ formatComposerContextReference,
+} from "@t3tools/shared/composerContextReferences";
import { appAtomRegistry } from "./atom-registry";
import { threadOutboxManager } from "./thread-outbox";
import {
@@ -2304,6 +2307,44 @@ describe("mobile composer drafts", () => {
expect(mergeComposerDraftContentState(edited, draftKey, content)).toBe(edited);
});
+ it("shows a shared non-image file as a composer chip", () => {
+ const draftKey = "new-task:environment-1:project-1";
+ const sharedFile = {
+ id: "share-1:file:0",
+ type: "file" as const,
+ name: "backup.json",
+ mimeType: "application/json",
+ sizeBytes: 41,
+ fileUri: "file:///documents/backup.json",
+ };
+
+ const existingFile = { ...sharedFile, id: "existing-file", name: "notes.txt" };
+
+ const merged = mergeComposerDraftContentState(
+ { [draftKey]: { text: "", attachments: [existingFile] } },
+ draftKey,
+ { text: "", attachments: [sharedFile], sourceShareId: "share-1" },
+ )[draftKey]!;
+
+ expect(merged.attachments).toEqual([existingFile, sharedFile]);
+ const references = collectComposerContextReferences(merged.text);
+ expect(references).toHaveLength(1);
+ expect(merged.context?.records).toEqual([
+ expect.objectContaining({
+ kind: "file",
+ contextId: references[0]!.contextId,
+ attachmentId: sharedFile.id,
+ }),
+ ]);
+
+ // A recovery merge carries its own text; a chip appended to it would
+ // break the duplicate-text check when the merge is retried.
+ const recovered = { text: "Retry this", attachments: [sharedFile] };
+ const once = mergeComposerDraftContentState({}, draftKey, recovered);
+ expect(once[draftKey]?.text).toBe("Retry this");
+ expect(mergeComposerDraftContentState(once, draftKey, recovered)).toBe(once);
+ });
+
it("preserves existing images when shared content exceeds the draft attachment limit", () => {
const draftKey = "new-task:environment-1:project-1";
const image = (id: string) => ({
diff --git a/apps/mobile/src/state/use-composer-drafts.ts b/apps/mobile/src/state/use-composer-drafts.ts
index c60e9e2d111..b804bb1c926 100644
--- a/apps/mobile/src/state/use-composer-drafts.ts
+++ b/apps/mobile/src/state/use-composer-drafts.ts
@@ -489,7 +489,10 @@ function attachmentContextRecord(
: { ...common, kind: "file" as const };
}
-function restoreMissingComposerFileReferences(draft: ComposerDraft): ComposerDraft {
+function restoreMissingComposerFileReferences(
+ draft: ComposerDraft,
+ attachments = draft.attachments,
+): ComposerDraft {
const records = [...(draft.context?.records ?? [])];
const usedIds = new Set<string>(records.map((record) => record.contextId));
const referenced = new Set(
@@ -499,7 +502,7 @@ function restoreMissingComposerFileReferences(draft: ComposerDraft): ComposerDra
);
let text = draft.text;
let changed = false;
- for (const attachment of draft.attachments) {
+ for (const attachment of attachments) {
if (
attachment.type === "image" ||
imageMimeType(attachment) !== null ||
@@ -1578,13 +1581,20 @@ export function mergeComposerDraftContentState(
}
return {
...current,
- [draftKey]: {
- ...existing,
- text,
- attachments,
- context,
- ...(importedShareIds ? { importedShareIds } : {}),
- },
+ // The composer shows non-image files only as inline chips, so a shared file
+ // merged without one would be attached but invisible until sent. Other
+ // merges carry their own text, and appending chips to it would defeat
+ // mergeComposerDraftText's duplicate check on a retried merge.
+ [draftKey]: restoreMissingComposerFileReferences(
+ {
+ ...existing,
+ text,
+ attachments,
+ context,
+ ...(importedShareIds ? { importedShareIds } : {}),
+ },
+ content.sourceShareId ? attachments.slice(existing.attachments.length) : [],
+ ),
};
}