Skip to content

Commit e35435d

Browse files
author
Morgarita
committed
Gate attachment selection and retry on upload availability
Co-authored-by: Morgarita <6e967af659416160ab4f2fd56d74c8c6f273791ee7bfc9468a1be37c387947be@buzz.block.builderlab.xyz> Signed-off-by: Morgarita <6e967af659416160ab4f2fd56d74c8c6f273791ee7bfc9468a1be37c387947be@buzz.block.builderlab.xyz>
1 parent c765b89 commit e35435d

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎src/features/messages/ComposerAttachments.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ export function useLocalAttachments(upload?: AttachmentUpload) {
3636
}, []);
3737
return {
3838
files,
39+
available: !!upload,
3940
error,
4041
input,
4142
blocked: files.some((file) => file.state !== "ready"),
4243
content: (text: string) => queue.current?.content(text) ?? text,
4344
add(selected: FileList | null) {
44-
if (selected?.length) setError(queue.current?.add(Array.from(selected)));
45+
if (currentUpload.current && selected?.length)
46+
setError(queue.current?.add(Array.from(selected)));
4547
},
4648
remove(id: number) {
4749
queue.current?.remove(id);
4850
setError(undefined);
4951
},
5052
retry(id: number) {
51-
queue.current?.retry(id);
53+
if (currentUpload.current) queue.current?.retry(id);
5254
},
5355
clear() {
5456
queue.current?.clear();
@@ -150,7 +152,7 @@ function AttachmentItem({
150152
<div>
151153
<Button
152154
size="compact"
153-
disabled={disabled}
155+
disabled={disabled || !selection.available}
154156
onClick={() => selection.retry(id)}
155157
>
156158
Retry {file.name}
@@ -176,7 +178,7 @@ export function ComposerAttachments({
176178
multiple
177179
hidden
178180
aria-label="Choose attachments"
179-
disabled={disabled}
181+
disabled={disabled || !selection.available}
180182
onChange={(event) => {
181183
if (!disabled) selection.add(event.currentTarget.files);
182184
event.currentTarget.value = "";

‎src/features/messages/MessageComposer.test.tsx‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,3 +1271,58 @@ it("keeps an oversized link insertion out of the draft and leaves the dialog ope
12711271
within(screen.getByRole("dialog")).getByRole("alert"),
12721272
).toHaveTextContent("limit");
12731273
});
1274+
1275+
it("disables attachment entry without upload capability and keeps text sending available", () => {
1276+
const h = mount();
1277+
expect(screen.getByRole("button", { name: "Attach file" })).toBeDisabled();
1278+
const picker = screen.getByLabelText("Choose attachments");
1279+
expect(picker).toBeDisabled();
1280+
fireEvent.change(picker, {
1281+
target: { files: [new File(["note"], "note.txt")] },
1282+
});
1283+
expect(
1284+
screen.queryByRole("region", { name: "Selected attachments" }),
1285+
).not.toBeInTheDocument();
1286+
h.fill("text still works");
1287+
h.submit();
1288+
expect(h.messages.send).toHaveBeenCalledExactlyOnceWith(
1289+
"channel",
1290+
"text still works",
1291+
[],
1292+
);
1293+
});
1294+
1295+
it("disables futile attachment retry when upload capability disappears and recovers when restored", async () => {
1296+
const h = mount();
1297+
const upload = vi.fn<NonNullable<RelaySession["attachments"]>["upload"]>();
1298+
let capability: RelaySession["attachments"];
1299+
Object.defineProperty(h.session, "attachments", { get: () => capability });
1300+
upload.mockRejectedValueOnce(new Error("upload failed"));
1301+
capability = { upload };
1302+
h.retarget({});
1303+
expect(screen.getByRole("button", { name: "Attach file" })).toBeEnabled();
1304+
fireEvent.change(screen.getByLabelText("Choose attachments"), {
1305+
target: { files: [new File(["note"], "note.txt", { type: "text/plain" })] },
1306+
});
1307+
const retry = await screen.findByRole("button", { name: "Retry note.txt" });
1308+
expect(upload).toHaveBeenCalledOnce();
1309+
capability = undefined;
1310+
h.retarget({});
1311+
expect(retry).toBeDisabled();
1312+
fireEvent.click(retry);
1313+
expect(upload).toHaveBeenCalledOnce();
1314+
upload.mockResolvedValueOnce({
1315+
url: "https://example.com/media/file.txt",
1316+
type: "text/plain",
1317+
size: 4,
1318+
sha256: "a".repeat(64),
1319+
});
1320+
capability = { upload };
1321+
h.retarget({});
1322+
await h.user.click(screen.getByRole("button", { name: "Retry note.txt" }));
1323+
await waitFor(() =>
1324+
expect(screen.getByText("Ready to send")).toBeInTheDocument(),
1325+
);
1326+
expect(upload).toHaveBeenCalledTimes(2);
1327+
expect(screen.getByRole("button", { name: "Send message" })).toBeEnabled();
1328+
});

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ function Composer({
616616
<>
617617
<IconButton
618618
aria-label="Attach file"
619-
disabled={editingDisabled}
619+
disabled={editingDisabled || !uploadCapability}
620620
onClick={() => attachments.input.current?.click()}
621621
icon={<PaperclipIcon size={16} aria-hidden="true" />}
622622
size="toolbar"

0 commit comments

Comments
 (0)