Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

feat(console): download and lightbox for chat images (CHOO-3740)

raullopez-sandboxdf12fee7722d
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-attachments.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-attachments.tsx
index 8da03418b..473f9527f 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-attachments.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-attachments.tsx
@@ -1,9 +1,11 @@
-import { FileIcon, Loader2 } from 'lucide-react';
+import { Download, FileIcon, Loader2, X } from 'lucide-react';
import { useEffect, useState } from 'react';
import { failureText } from '@renderer/lib/errors/describe-failure';
import { toast } from '@renderer/lib/hooks/use-toast';
import { rpc } from '@renderer/lib/ipc';
import { events } from '@renderer/lib/ipc';
+import { Button } from '@renderer/lib/ui/button';
+import { Dialog, DialogClose, DialogContent, DialogTitle } from '@renderer/lib/ui/dialog';
import { chatRemovedChannel, chatResetChannel } from '@shared/core/chats/chatEvents';
import type { ChatAttachment } from '@shared/core/chats/chats';
@@ -44,6 +46,37 @@ function mediaUrl(serverId: string, roomId: string, uri: string): Promise<string
return url;
}
+async function saveAttachment(
+ serverId: string,
+ roomId: string,
+ attachment: ChatAttachment
+): Promise<void> {
+ try {
+ const url = await mediaUrl(serverId, roomId, attachment.uri);
+ const link = document.createElement('a');
+ link.href = url;
+ link.download = attachment.filename;
+ link.click();
+ } catch (caught) {
+ toast({ title: failureText(caught, 'Could not download the file.'), variant: 'destructive' });
+ }
+}
+
+function DownloadButton({ filename, onClick }: { filename: string; onClick: () => void }) {
+ return (
+ <Button
+ type="button"
+ variant="secondary"
+ size="icon-xs"
+ aria-label={`Download ${filename}`}
+ title={`Download ${filename}`}
+ onClick={onClick}
+ >
+ <Download className="size-3" />
+ </Button>
+ );
+}
+
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
@@ -61,6 +94,7 @@ function ChatImage({
}) {
const [url, setUrl] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
+ const [open, setOpen] = useState(false);
useEffect(() => {
let cancelled = false;
mediaUrl(serverId, roomId, attachment.uri)
@@ -80,12 +114,55 @@ function ChatImage({
<Loader2 className="size-4 animate-spin text-foreground-muted" />
</span>
);
+ const save = () => void saveAttachment(serverId, roomId, attachment);
return (
- <img
- src={url}
- alt={attachment.filename}
- className="max-h-72 max-w-full rounded-lg border border-border object-contain"
- />
+ <span className="group/image relative inline-block max-w-full">
+ <button
+ type="button"
+ aria-label={`Open ${attachment.filename}`}
+ onClick={() => setOpen(true)}
+ className="block max-w-full cursor-zoom-in rounded-lg"
+ >
+ <img
+ src={url}
+ alt={attachment.filename}
+ className="max-h-72 max-w-full rounded-lg border border-border object-contain"
+ />
+ </button>
+ <span className="absolute top-1 right-1 opacity-0 shadow-sm transition-opacity group-focus-within/image:opacity-100 group-hover/image:opacity-100">
+ <DownloadButton filename={attachment.filename} onClick={save} />
+ </span>
+ <Dialog open={open} onOpenChange={setOpen}>
+ <DialogContent
+ className="inset-0 h-full max-h-none w-full max-w-none translate-x-0 translate-y-0 gap-2 rounded-none bg-black/85 p-4 ring-0 sm:max-w-none"
+ onClick={(event) => {
+ // The popup fills the window, so its empty area stands in for the backdrop.
+ if (!(event.target as Element).closest('img, button')) setOpen(false);
+ }}
+ >
+ <div className="flex shrink-0 items-center gap-2">
+ <DialogTitle className="min-w-0 flex-1 truncate text-sm font-medium text-white">
+ {attachment.filename}
+ </DialogTitle>
+ <DownloadButton filename={attachment.filename} onClick={save} />
+ <DialogClose
+ render={
+ <Button type="button" variant="secondary" size="icon-xs" aria-label="Close" />
+ }
+ >
+ <X className="size-3" />
+ </DialogClose>
+ </div>
+ <div className="flex min-h-0 flex-1 items-center justify-center">
+ <img
+ src={url}
+ alt={attachment.filename}
+ className="max-h-full max-w-full object-contain"
+ />
+ </div>
+ </DialogContent>
+ </Dialog>
+ </span>
);
}
@@ -101,17 +178,8 @@ function ChatFileChip({
const [busy, setBusy] = useState(false);
const download = async () => {
setBusy(true);
- try {
- const url = await mediaUrl(serverId, roomId, attachment.uri);
- const link = document.createElement('a');
- link.href = url;
- link.download = attachment.filename;
- link.click();
- } catch (caught) {
- toast({ title: failureText(caught, 'Could not download the file.'), variant: 'destructive' });
- } finally {
- setBusy(false);
- }
+ await saveAttachment(serverId, roomId, attachment);
+ setBusy(false);
};
return (
<button
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-image-lightbox.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-image-lightbox.test.tsx
new file mode 100644
index 000000000..8c4441e19
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-image-lightbox.test.tsx
@@ -0,0 +1,102 @@
+/**
+ * An image attachment shows a download button on hover; the button saves the
+ * file without opening the lightbox, and clicking the image opens it.
+ */
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, expect, it, vi } from 'vitest';
+import { userEvent } from 'vitest/browser';
+
+const media = vi.hoisted(() => vi.fn());
+vi.mock('@renderer/lib/ipc', () => ({
+ events: { on: () => () => {} },
+ rpc: { chats: { media } },
+}));
+
+import { ChatAttachments } from '@renderer/features/chats/components/chat-attachments';
+import '@renderer/index.css';
+
+let container: HTMLDivElement | null = null;
+let root: Root | null = null;
+
+afterEach(async () => {
+ if (root) await act(async () => root!.unmount());
+ container?.remove();
+ container = null;
+ root = null;
+ vi.restoreAllMocks();
+});
+
+function samplePng(): string {
+ const canvas = document.createElement('canvas');
+ canvas.width = 640;
+ canvas.height = 400;
+ const context = canvas.getContext('2d')!;
+ const gradient = context.createLinearGradient(0, 0, 640, 400);
+ gradient.addColorStop(0, '#4f46e5');
+ gradient.addColorStop(1, '#06b6d4');
+ context.fillStyle = gradient;
+ context.fillRect(0, 0, 640, 400);
+ return canvas.toDataURL('image/png').split(',')[1]!;
+}
+
+async function render(): Promise<HTMLElement> {
+ media.mockResolvedValue({ mimeType: 'image/png', data: samplePng() });
+ container = document.createElement('div');
+ container.style.padding = '24px';
+ document.body.appendChild(container);
+ root = createRoot(container);
+ await act(async () =>
+ root!.render(
+ <ChatAttachments
+ serverId="s"
+ roomId="r"
+ attachments={[
+ {
+ uri: 'mxc://example.org/shot',
+ filename: 'shot.png',
+ mimetype: 'image/png',
+ size: 70,
+ msgtype: 'm.image',
+ },
+ ]}
+ />
+ )
+ );
+ await expect.poll(() => container!.querySelector('img')).toBeTruthy();
+ return container!.querySelector<HTMLElement>('[aria-label="Open shot.png"]')!;
+}
+
+const downloadButton = () =>
+ container!.querySelector<HTMLButtonElement>('[aria-label="Download shot.png"]')!;
+const lightbox = () => document.querySelector('[role="dialog"]');
+
+it('shows the download button on hover', async () => {
+ const image = await render();
+ const overlay = downloadButton().parentElement!;
+ expect(getComputedStyle(overlay).opacity).toBe('0');
+ await userEvent.hover(image);
+ await expect.poll(() => getComputedStyle(overlay).opacity).toBe('1');
+});
+
+it('downloads without opening the lightbox', async () => {
+ const image = await render();
+ const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});
+ await userEvent.hover(image);
+ await userEvent.click(downloadButton());
+ await expect.poll(() => click.mock.calls.length).toBe(1);
+ const link = click.mock.contexts[0] as HTMLAnchorElement;
+ expect(link.download).toBe('shot.png');
+ expect(link.href).toMatch(/^blob:/);
+ expect(lightbox()).toBeNull();
+});
+
+it('opens the lightbox on click and closes it on Escape', async () => {
+ const image = await render();
+ await userEvent.click(image);
+ await expect.poll(lightbox).toBeTruthy();
+ expect(lightbox()!.textContent).toContain('shot.png');
+ expect(lightbox()!.querySelector('[aria-label="Download shot.png"]')).toBeTruthy();
+ await userEvent.keyboard('{Escape}');
+ await expect.poll(lightbox).toBeNull();
+});