Skip to content

Commit c3632c7

Browse files
fix(mobile): reset the presented flag when the previewed item changes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1d29043 commit c3632c7

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎apps/mobile/src/components/ui/FilePreviewSession.test.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,35 @@ it.each([
208208
expect(harness.refreshAssetUrl).toHaveBeenCalledTimes(starts);
209209
},
210210
);
211+
212+
it("does not carry a presented viewer over to a different item that is still resolving", async () => {
213+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
214+
harness.refreshAssetUrl.mockReset();
215+
harness.refreshAssetUrl.mockImplementation((_environmentId, resource) =>
216+
resource.url.endsWith("a.png")
217+
? Promise.resolve("https://signed.test/a.png")
218+
: new Promise(() => {}),
219+
);
220+
const shown = image("https://github.com/a.png");
221+
const resolving: FilePreviewItem = { ...image("https://github.com/b.pdf"), kind: "pdf" };
222+
const root = createRoot(document.createElement("div"));
223+
await using _root = { [Symbol.asyncDispose]: () => act(async () => root.unmount()) };
224+
const render = (item: FilePreviewItem) =>
225+
act(async () =>
226+
root.render(
227+
<FilePreviewSession
228+
source={{ ...item, gallery: [item] }}
229+
onRequestClose={() => {}}
230+
progressiveGallery
231+
Viewer={Viewer}
232+
isViewerActive={() => false}
233+
/>,
234+
),
235+
);
236+
await render(shown);
237+
await render(resolving);
238+
await render(resolving);
239+
expect(
240+
harness.refreshAssetUrl.mock.calls.filter(([, r]) => r.url.endsWith("b.pdf")),
241+
).toHaveLength(1);
242+
});

‎apps/mobile/src/components/ui/FilePreviewSession.tsx‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,15 @@ export function FilePreviewSession(props: FilePreviewModalProps & FilePreviewSes
230230
// A viewer still resolving its file has not presented yet, so a re-render then never reopens.
231231
const [opening, setOpening] = useState({ source: props.source, count: 0, presented: false });
232232
if (opening.source !== props.source) {
233-
const reopened =
234-
opening.presented &&
233+
const sameItem =
235234
opening.source !== null &&
236235
props.source !== null &&
237-
filePreviewKey(opening.source) === filePreviewKey(props.source) &&
238-
props.isViewerActive?.() === false;
236+
filePreviewKey(opening.source) === filePreviewKey(props.source);
237+
const reopened = opening.presented && sameItem && props.isViewerActive?.() === false;
239238
setOpening({
240239
source: props.source,
241240
count: opening.count + (reopened ? 1 : 0),
242-
presented: opening.presented && !reopened && props.source !== null,
241+
presented: opening.presented && sameItem && !reopened,
243242
});
244243
}
245244
const { count } = opening;

0 commit comments

Comments
 (0)