Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): preserve tapped images during initial preview

xd1480fe6cd209
diff --git a/apps/mobile/src/__tests__/botGroupAttachmentLightbox.test.tsx b/apps/mobile/src/__tests__/botGroupAttachmentLightbox.test.tsx
index 7ebe0c8788..46a4e2aa6f 100644
--- a/apps/mobile/src/__tests__/botGroupAttachmentLightbox.test.tsx
+++ b/apps/mobile/src/__tests__/botGroupAttachmentLightbox.test.tsx
@@ -267,6 +267,59 @@ describe('Chat Server group image attachments (#5875)', () => {
expect(container.querySelector('img')).toBeNull();
});
+ // PR #7145 r4371557412: the first automatic request has no cached preview.
+ // A successful tap must populate the group list even if that request fails or hangs.
+ it.each(['late-success', 'late-failure', 'already-failed'])('keeps the tapped preview before initial authorization completes (%s)', async outcome => {
+ let finishInitial!: (value: BotGroupAttachment) => void;
+ let failInitial!: (error: Error) => void;
+ const tappedUrl = `${signedImageUrl}&tap=initial`;
+ const resolveServerMedia = vi.fn<() => Promise<BotGroupAttachment>>()
+ .mockImplementationOnce(() => new Promise((resolve, reject) => { finishInitial = resolve; failInitial = reject; }))
+ .mockResolvedValueOnce({ ...serverAttachment, category: 'image', url: tappedUrl });
+ const container = await render({ resolveServerMedia });
+ try {
+ if (outcome === 'already-failed') await act(async () => failInitial(new Error('initial failed')));
+ await click(container.querySelector('[data-testid="message.filePreviewButton"]'));
+ await click(container.querySelector('[data-testid="message.imageLightboxCloseButton"]'));
+ expect(container.querySelector('img')?.getAttribute('src')).toBe(tappedUrl);
+ } finally {
+ await act(async () => {
+ if (outcome === 'late-success') finishInitial({ ...serverAttachment, category: 'image', url: `${signedImageUrl}&older=1` });
+ else failInitial(new Error('initial failed'));
+ });
+ }
+ expect(container.querySelector('img')?.getAttribute('src')).toBe(tappedUrl);
+ expect(resolveServerMedia).toHaveBeenCalledTimes(2);
+ });
+
+ it('keeps a newer tapped image retry queued when its initial authorization fails late', async () => {
+ let failInitial!: (error: Error) => void;
+ const finishNeighbours: Array<(value: BotGroupAttachment) => void> = [];
+ const tappedUrl = `${signedImageUrl}&tap=initial`;
+ const refreshedUrl = `${signedImageUrl}&retry=initial`;
+ let targetCalls = 0;
+ const resolveServerMedia = vi.fn((id: string): Promise<BotGroupAttachment> => {
+ if (id !== serverAttachment.id) return new Promise(resolve => finishNeighbours.push(resolve));
+ targetCalls++;
+ if (targetCalls === 1) return new Promise((_resolve, reject) => { failInitial = reject; });
+ return Promise.resolve({ ...serverAttachment, category: 'image', url: targetCalls === 2 ? tappedUrl : refreshedUrl });
+ });
+ const container = await render({ resolveServerMedia,
+ attachments: [serverAttachment, ...['neighbour-1', 'neighbour-2'].map(id => ({ ...serverAttachment, id }))] });
+ try {
+ await click(container.querySelector('[data-testid="message.filePreviewButton"]'));
+ await click(container.querySelector('[data-testid="message.imageLightboxCloseButton"]'));
+ await act(async () => runtime.imageEvents.get(tappedUrl)!.onError?.());
+ expect(targetCalls).toBe(2); // retry waits behind the three original authorizations
+ await act(async () => failInitial(new Error('older request failed')));
+ expect(targetCalls).toBe(3);
+ expect(container.querySelector('img')?.getAttribute('src')).toBe(refreshedUrl);
+ } finally {
+ failInitial(new Error('cleanup'));
+ await act(async () => finishNeighbours.forEach((finish, index) => finish({ ...serverAttachment, id: `neighbour-${index + 1}` })));
+ }
+ });
+
it('does not let a late automatic refresh replace a newer explicit authorization', async () => {
let finishRefresh!: (value: BotGroupAttachment) => void;
const tappedUrl = `${signedImageUrl}&tap=1`;
diff --git a/apps/mobile/src/session/BotGroupMessageAttachments.tsx b/apps/mobile/src/session/BotGroupMessageAttachments.tsx
index c8a276d7b1..e8fc93d8db 100644
--- a/apps/mobile/src/session/BotGroupMessageAttachments.tsx
+++ b/apps/mobile/src/session/BotGroupMessageAttachments.tsx
@@ -64,7 +64,8 @@ export function BotGroupMessageAttachments({ messageId, attachments, align, onRe
const gallery = useMemo(() => botGroupAttachmentGallery(messageId, items), [items, messageId]);
const runs = useMemo(() => botGroupAttachmentRuns(attachments), [attachments]);
const [openUrl, setOpenUrl] = useState<string | null>(null);
- if (resolveServerMedia) return <ServerAttachments key={`${accountGeneration}:${messageId}`} runs={runs} visible={previewVisible} align={align} layout={layout} resolve={resolveServerMedia} />;
+ if (resolveServerMedia) return <ServerAttachments key={`${accountGeneration}:${messageId}`} runs={runs} visible={previewVisible}
+ previewServerImages={previewServerImages} align={align} layout={layout} resolve={resolveServerMedia} />;
const open = (payload: MessagePayload) => {
if (payload.kind === 'media' && payload.media.kind === 'image') {
setOpenUrl(payload.media.url);
@@ -164,8 +165,9 @@ function drainPreviews() {
}
/** Image preview URLs live only in this mounted message; opening always re-authorizes. */
-function ServerAttachments({ runs, visible, align, layout, resolve }: {
+function ServerAttachments({ runs, visible, previewServerImages, align, layout, resolve }: {
visible: boolean;
+ previewServerImages: boolean;
align: 'left' | 'right';
runs: ReturnType<typeof botGroupAttachmentRuns>;
layout: ReturnType<typeof buildMessageContentLayout>;
@@ -202,7 +204,7 @@ function ServerAttachments({ runs, visible, align, layout, resolve }: {
if (refreshing) setPreviewVersions(versions => ({ ...versions, [id]: (versions[id] ?? 0) + 1 }));
}
} catch {
- if (!cancelled) failedPreviewIds.current.delete(id);
+ if (!cancelled && previewsRef.current[id] === previousPreview) failedPreviewIds.current.delete(id);
// Keep the chip / fallback available for an explicit retry on tap.
}
}));
@@ -232,7 +234,7 @@ function ServerAttachments({ runs, visible, align, layout, resolve }: {
if (value.category === 'image') {
setImage(value);
// Cloud transcripts never populate this cache: their media remains tap-only.
- if (previewsRef.current[id]) {
+ if (previewServerImages) {
previewsRef.current = { ...previewsRef.current, [id]: value };
setPreviews(previewsRef.current);
failedPreviewIds.current.delete(id);