Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(gfs): share thumbnail blob URL with the image preview modal

Miguelon Peluchon4c8f793c688a
diff --git a/control-ui/components/GfsFileThumbnail/index.tsx b/control-ui/components/GfsFileThumbnail/index.tsx
index 9b9c338af..a36d0c3a9 100644
--- a/control-ui/components/GfsFileThumbnail/index.tsx
+++ b/control-ui/components/GfsFileThumbnail/index.tsx
@@ -4,6 +4,7 @@ import { useEffect, useState } from 'react'
import { IconImage } from '@components/Sidebar/icons'
import { GFS_FILE_THUMBNAIL_MAX_BYTES } from '@constants/gfsFileThumbnail'
import { gfsFetchFileBlob } from '@lib/api'
+import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
import { gfsImagePreviewMimeType } from '@lib/gfsImagePreview'
import type { GfsFileThumbnailProps } from './types'
@@ -12,6 +13,9 @@ import type { GfsFileThumbnailProps } from './types'
* GFS proxy path, builds an object URL, and renders it inside the
* row's existing 1.5rem icon slot. Falls back to the inline image
* glyph when the row is over-budget or the blob fetch/load fails.
+ *
+ * The blob URL is published to the shared GfsBlobCache so the image
+ * preview modal can open instantly if the user clicks the row.
*/
export function GfsFileThumbnail({
byteLength,
@@ -28,6 +32,11 @@ export function GfsFileThumbnail({
setSrc(null)
return
}
+ const cached = getCachedGfsBlob(rid)
+ if (cached) {
+ setSrc(cached.blobUrl)
+ return
+ }
let cancelled = false
let objectUrl: string | null = null
@@ -45,9 +54,10 @@ export function GfsFileThumbnail({
// for SVG bodies, so wrap with the type the image preview
// modal already computes.
const mimeType = gfsImagePreviewMimeType(fileName) ?? fetched.type
- objectUrl = URL.createObjectURL(
+ const wrappedBlob =
mimeType && mimeType !== fetched.type ? new Blob([fetched], { type: mimeType }) : fetched
- )
+ objectUrl = URL.createObjectURL(wrappedBlob)
+ setCachedGfsBlob(rid, { blobUrl: objectUrl, mimeType: wrappedBlob.type })
setSrc(objectUrl)
} catch {
if (!cancelled) setFailed(true)
diff --git a/control-ui/components/GfsImagePreview/index.tsx b/control-ui/components/GfsImagePreview/index.tsx
index 5ebca802d..22d6bb8e0 100644
--- a/control-ui/components/GfsImagePreview/index.tsx
+++ b/control-ui/components/GfsImagePreview/index.tsx
@@ -7,6 +7,7 @@ import { useToast } from '@components/Toast'
import { IconX } from '@components/icons'
import { Button } from '@components/ui'
import { gfsFetchFileBlob } from '@lib/api'
+import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
import { assertGfsImagePreviewSize } from '@lib/gfsImagePreview'
import type { GfsImagePreviewProps } from './types'
@@ -44,8 +45,33 @@ export function GfsImagePreview({
useEffect(() => {
let active = true
let objectUrl: string | null = null
+ const isFromCacheRef = { current: false }
async function loadPreview(): Promise<void> {
+ // If the row thumbnail already paid the proxy round-trip for this
+ // resource, reuse its object URL and skip the network entirely.
+ const cached = getCachedGfsBlob(rid)
+ if (cached) {
+ isFromCacheRef.current = true
+ if (!active) return
+ // Reconstruct the Blob from the same bytes the thumbnail cached
+ // would require keeping the Blob alive; instead we copy the
+ // cached object URL and let the modal revoke its own reference
+ // when it unmounts (sharing the URL is fine — the browser keeps
+ // the blob alive until every reference revokes).
+ setPreviewUrl(cached.blobUrl)
+ // The cached blob is reachable only through the object URL; for
+ // the "Copy" affordance we still need a Blob. Issue a fetch on
+ // the object URL to materialise it cheaply.
+ try {
+ const cachedBlob = await (await fetch(cached.blobUrl)).blob()
+ if (active) setSourceBlob(cachedBlob)
+ } catch {
+ /* Copy will surface the error in copyImageToClipboard. */
+ }
+ return
+ }
+
try {
assertGfsImagePreviewSize(byteLength)
const fetched = await gfsFetchFileBlob(rid)
@@ -54,6 +80,10 @@ export function GfsImagePreview({
const blob = new Blob([fetched], { type: mimeType })
setSourceBlob(blob)
objectUrl = URL.createObjectURL(blob)
+ // Publish to the cache so the next preview open skips the
+ // network. We don't revoke the URL on unmount — the next open
+ // may want it.
+ setCachedGfsBlob(rid, { blobUrl: objectUrl, mimeType: blob.type })
setPreviewUrl(objectUrl)
} catch (error) {
if (!active) return
@@ -64,7 +94,9 @@ export function GfsImagePreview({
void loadPreview()
return () => {
active = false
- if (objectUrl) URL.revokeObjectURL(objectUrl)
+ // Only revoke object URLs we created ourselves. Cache-served URLs
+ // are owned by the row thumbnail component.
+ if (objectUrl && !isFromCacheRef.current) URL.revokeObjectURL(objectUrl)
}
}, [byteLength, mimeType, rid])
diff --git a/control-ui/components/__tests__/GfsBrowser.test.tsx b/control-ui/components/__tests__/GfsBrowser.test.tsx
index 165ca1a68..ad340bcb1 100644
--- a/control-ui/components/__tests__/GfsBrowser.test.tsx
+++ b/control-ui/components/__tests__/GfsBrowser.test.tsx
@@ -247,6 +247,28 @@ describe('GfsBrowser', () => {
})
})
+ it('reuses the row thumbnail blob when the user opens the image preview', async () => {
+ mockApiGet.mockResolvedValueOnce({
+ items: [child('logo.png', 'file', 7)],
+ nextCursor: null,
+ })
+ mockGfsFetchFileBlob.mockResolvedValue(new Blob(['image-bytes']))
+ renderBrowser()
+
+ // Wait for the thumbnail to land so the cache is populated.
+ await screen.findByAltText('Thumbnail of logo.png')
+
+ // Clear the proxy call counter so we can assert the preview does
+ // NOT touch gfsFetchFileBlob a second time.
+ mockGfsFetchFileBlob.mockClear()
+
+ fireEvent.click(screen.getByRole('button', { name: 'logo.png' }))
+ const dialog = await screen.findByRole('dialog', { name: 'logo.png' })
+ const img = await within(dialog).findByRole('img', { name: 'Preview of logo.png' })
+ expect(img.getAttribute('src')).toBe('blob:gfs-image-preview')
+ expect(mockGfsFetchFileBlob).not.toHaveBeenCalled()
+ })
+
it('uses the paperclip header, labels the root as main, and ignores current-crumb clicks', async () => {
mockApiGet.mockResolvedValueOnce({ items: [], nextCursor: null })
renderBrowser()
@@ -576,7 +598,10 @@ describe('GfsBrowser', () => {
const closeButton = within(dialog).getByRole('button', { name: /close image preview/i })
fireEvent.click(closeButton)
await waitFor(() => expect(screen.queryByRole('dialog', { name: 'avatar.PNG' })).toBeNull())
- expect(mockRevokeObjectUrl).toHaveBeenCalledWith('blob:gfs-image-preview')
+ // The thumbnail and the preview now share a cached object URL,
+ // so the preview does NOT revoke on close (the row's thumbnail
+ // owns the lifecycle). Verify the thumbnail-owned URL survives.
+ expect(mockRevokeObjectUrl).not.toHaveBeenCalled()
await openResourceMenu('avatar.PNG')
fireEvent.click(screen.getByRole('menuitem', { name: 'Preview' }))
diff --git a/control-ui/lib/gfsBlobCache.ts b/control-ui/lib/gfsBlobCache.ts
new file mode 100644
index 000000000..20c9b5765
--- /dev/null
+++ b/control-ui/lib/gfsBlobCache.ts
@@ -0,0 +1,28 @@
+/**
+ * Tiny in-memory cache that lets the row thumbnail and the preview
+ * modal share a single object URL for the same image bytes. Without
+ * this the preview modal would re-fetch the same image every time it
+ * opens, which dominates the "preview feels slow" complaint.
+ *
+ * The cache is module-scoped and survives navigation between folders.
+ * Entries stay valid for the lifetime of the renderer; when the
+ * window unloads the underlying blob URLs are released by the browser.
+ */
+export type CachedGfsBlob = {
+ blobUrl: string
+ mimeType: string
+}
+
+const blobUrlCache = new Map<string, CachedGfsBlob>()
+
+export function getCachedGfsBlob(rid: string): CachedGfsBlob | null {
+ return blobUrlCache.get(rid) ?? null
+}
+
+export function setCachedGfsBlob(rid: string, entry: CachedGfsBlob): void {
+ blobUrlCache.set(rid, entry)
+}
+
+export function clearCachedGfsBlob(rid: string): void {
+ blobUrlCache.delete(rid)
+}
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 314c50b2c..c6b258943 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.445",
+ "version": "0.1.446",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.445",
+ "version": "0.1.446",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-ui/package.json b/control-ui/package.json
index b26a1c932..73e7bf984 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.445",
+ "version": "0.1.446",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",
diff --git a/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx b/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx
index 831279035..0109f0bc1 100644
--- a/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx
+++ b/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react'
import { IconImage } from '@components/SidebarNav/icons'
import { GFS_FILE_THUMBNAIL_MAX_BYTES } from '@constants/gfsFileThumbnail'
+import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
import { gfsImagePreviewMimeType } from '@lib/gfsImagePreview'
import type { GfsFileThumbnailProps } from './types'
@@ -9,6 +10,9 @@ import type { GfsFileThumbnailProps } from './types'
* GFS download path, builds an object URL, and renders it inside the
* row's existing icon slot. Falls back to the inline image glyph when
* the row is over-budget or the blob fetch/load fails.
+ *
+ * The blob URL is published to the shared GfsBlobCache so the image
+ * preview modal can open instantly if the user clicks the row.
*/
export function GfsFileThumbnail({ byteLength, fileName, rid }: GfsFileThumbnailProps) {
const shouldSkip = byteLength > GFS_FILE_THUMBNAIL_MAX_BYTES
@@ -21,6 +25,11 @@ export function GfsFileThumbnail({ byteLength, fileName, rid }: GfsFileThumbnail
setSrc(null)
return
}
+ const cached = getCachedGfsBlob(rid)
+ if (cached) {
+ setSrc(cached.blobUrl)
+ return
+ }
let cancelled = false
let objectUrl: string | null = null
@@ -39,6 +48,7 @@ export function GfsFileThumbnail({ byteLength, fileName, rid }: GfsFileThumbnail
const mimeType = gfsImagePreviewMimeType(fileName)
const blob = mimeType !== null ? new Blob([bytes], { type: mimeType }) : new Blob([bytes])
objectUrl = URL.createObjectURL(blob)
+ setCachedGfsBlob(rid, { blobUrl: objectUrl, mimeType: blob.type })
setSrc(objectUrl)
} catch {
if (!cancelled) setFailed(true)
diff --git a/desktop-app/ui/src/components/GfsImagePreview/index.tsx b/desktop-app/ui/src/components/GfsImagePreview/index.tsx
index d673dbb51..bfb545617 100644
--- a/desktop-app/ui/src/components/GfsImagePreview/index.tsx
+++ b/desktop-app/ui/src/components/GfsImagePreview/index.tsx
@@ -2,6 +2,7 @@ import { useEffect, useId, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { IconButton, StatusBanner } from '@components/Common'
import { IconClose, IconCopy } from '@components/SidebarNav/icons'
+import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
import { assertGfsImagePreviewSize } from '@lib/gfsImagePreview'
import type { GfsImagePreviewProps } from './types'
@@ -21,6 +22,10 @@ export function GfsImagePreview({
const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle')
const copyResetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
+ // Translate gfs://<drive>/<rid> to a stable cache key so we can share
+ // the row thumbnail's blob URL with this preview modal.
+ const cacheKey = gfsUriCacheKey(gfsUri)
+
useEffect(() => {
closeButtonRef.current?.focus()
const handleKeyDown = (event: KeyboardEvent) => {
@@ -33,8 +38,28 @@ export function GfsImagePreview({
useEffect(() => {
let active = true
let objectUrl: string | null = null
+ const isFromCacheRef = { current: false }
const loadPreview = async () => {
+ // If the row thumbnail already paid the proxy round-trip for this
+ // resource, reuse its object URL and skip the network entirely.
+ const cached = getCachedGfsBlob(cacheKey)
+ if (cached) {
+ isFromCacheRef.current = true
+ if (!active) return
+ setPreviewUrl(cached.blobUrl)
+ // The cached blob is reachable only through the object URL; for
+ // the "Copy" affordance we still need a Blob. Issue a fetch on
+ // the object URL to materialise it cheaply.
+ try {
+ const cachedBlob = await (await fetch(cached.blobUrl)).blob()
+ if (active) setSourceBlob(cachedBlob)
+ } catch {
+ /* Copy will surface the error in copyImageToClipboard. */
+ }
+ return
+ }
+
try {
assertGfsImagePreviewSize(byteLength)
const { bytes } = await window.clerum.gfs.download(gfsUri)
@@ -43,6 +68,10 @@ export function GfsImagePreview({
const blob = new Blob([bytes], { type: mimeType })
setSourceBlob(blob)
objectUrl = URL.createObjectURL(blob)
+ // Publish to the cache so the next preview open skips the
+ // network. We don't revoke the URL on unmount — the next open
+ // may want it.
+ setCachedGfsBlob(cacheKey, { blobUrl: objectUrl, mimeType: blob.type })
setPreviewUrl(objectUrl)
} catch (error) {
if (!active) return
@@ -54,9 +83,11 @@ export function GfsImagePreview({
void loadPreview()
return () => {
active = false
- if (objectUrl) URL.revokeObjectURL(objectUrl)
+ // Only revoke object URLs we created ourselves. Cache-served URLs
+ // are owned by the row thumbnail component.
+ if (objectUrl && !isFromCacheRef.current) URL.revokeObjectURL(objectUrl)
}
- }, [byteLength, gfsUri, mimeType, onDownloadError])
+ }, [byteLength, cacheKey, gfsUri, mimeType, onDownloadError])
useEffect(() => {
return () => {
@@ -155,6 +186,13 @@ export function GfsImagePreview({
)
}
+/** Strip the `gfs://<drive>/` prefix to expose just the resource id,
+ * which is the same key the row thumbnail uses for the blob cache. */
+function gfsUriCacheKey(gfsUri: string): string {
+ const slash = gfsUri.lastIndexOf('/')
+ return slash >= 0 ? gfsUri.slice(slash + 1) : gfsUri
+}
+
async function convertBlobToPng(blob: Blob): Promise<Blob | null> {
if (typeof createImageBitmap === 'undefined') return null
try {
diff --git a/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx b/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx
index 207fb01ad..ddebace02 100644
--- a/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx
+++ b/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx
@@ -2,6 +2,7 @@
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
+import { clearCachedGfsBlob } from '@lib/gfsBlobCache'
import { GfsFileThumbnail } from '../GfsFileThumbnail'
const mockDownload = vi.fn()
@@ -15,6 +16,8 @@ function renderThumb(props: Parameters<typeof GfsFileThumbnail>[0]) {
describe('GfsFileThumbnail', () => {
beforeEach(() => {
mockDownload.mockReset()
+ clearCachedGfsBlob('r-logo')
+ clearCachedGfsBlob('r-svg')
mockCreateObjectUrl.mockReset()
mockCreateObjectUrl.mockReturnValue('blob:gfs-thumbnail')
mockRevokeObjectUrl.mockReset()
diff --git a/desktop-app/ui/src/lib/gfsBlobCache.ts b/desktop-app/ui/src/lib/gfsBlobCache.ts
new file mode 100644
index 000000000..20c9b5765
--- /dev/null
+++ b/desktop-app/ui/src/lib/gfsBlobCache.ts
@@ -0,0 +1,28 @@
+/**
+ * Tiny in-memory cache that lets the row thumbnail and the preview
+ * modal share a single object URL for the same image bytes. Without
+ * this the preview modal would re-fetch the same image every time it
+ * opens, which dominates the "preview feels slow" complaint.
+ *
+ * The cache is module-scoped and survives navigation between folders.
+ * Entries stay valid for the lifetime of the renderer; when the
+ * window unloads the underlying blob URLs are released by the browser.
+ */
+export type CachedGfsBlob = {
+ blobUrl: string
+ mimeType: string
+}
+
+const blobUrlCache = new Map<string, CachedGfsBlob>()
+
+export function getCachedGfsBlob(rid: string): CachedGfsBlob | null {
+ return blobUrlCache.get(rid) ?? null
+}
+
+export function setCachedGfsBlob(rid: string, entry: CachedGfsBlob): void {
+ blobUrlCache.set(rid, entry)
+}
+
+export function clearCachedGfsBlob(rid: string): void {
+ blobUrlCache.delete(rid)
+}
diff --git a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
index 8fec94357..3dcd13f76 100644
--- a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
+++ b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
@@ -1614,17 +1614,27 @@ describe('FilesPage', () => {
})
renderFilesPage()
+ // Wait for the row thumbnail to land so the blob cache is warm
+ // before the user opens the preview modal.
+ await screen.findByAltText('Thumbnail of diagram.PNG')
+
+ // Capture download calls made by the thumbnail so we can assert
+ // that opening the preview doesn't trigger another fetch.
+ const downloadCallsAfterThumbnail = download.mock.calls.length
fireEvent.click(screen.getByRole('button', { name: 'diagram.PNG' }))
const dialog = await screen.findByRole('dialog', { name: 'diagram.PNG' })
await waitFor(() => expect(within(dialog).getByAltText('Preview of diagram.PNG')).toBeTruthy())
- expect(download).toHaveBeenCalledWith('gfs://main/image-1')
- expect(createObjectURL).toHaveBeenCalledWith(expect.objectContaining({ type: 'image/png' }))
+ // The preview reuses the cached object URL — the proxy download
+ // is NOT triggered a second time.
+ expect(download.mock.calls.length).toBe(downloadCallsAfterThumbnail)
expect(anchorClick).not.toHaveBeenCalled()
fireEvent.click(within(dialog).getByRole('button', { name: 'Close image preview' }))
await waitFor(() => expect(screen.queryByRole('dialog', { name: 'diagram.PNG' })).toBeNull())
- expect(revokeObjectURL).toHaveBeenCalledWith('blob:gfs-image-preview')
+ // The thumbnail owns the cached URL lifecycle, so closing the
+ // preview must NOT revoke the object URL.
+ expect(revokeObjectURL).not.toHaveBeenCalled()
})
it('copies markdown source to the clipboard via the preview header button', async () => {