Files
Reims-Go/frontend/src/components/ImagesPage.tsx
T
NwflowerandClaude Opus 5.5 50c03a0835 perf(frontend): load the settings and plugin texts on demand again
App.tsx imported a one-line helper from SchemaForm, which pulled both text
domains into the startup bundle. The i18n test now follows the real import
graph instead of a hand-kept file list, so it catches this. npm test runs
every test file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:38:17 -07:00

142 lines
4.7 KiB
TypeScript

import { Disc3, Puzzle, RefreshCw } from 'lucide-react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import '../i18n/domains/settings.gen'
import { I18nKey, locText, t } from '../i18n'
import { ImageSourceContribution, Locale, PluginUIContributions } from '../types'
import { Button, IconButton } from '../ui/controls'
import { Segmented } from '../ui/settings'
import { PluginPanelFrame } from './PluginPanelFrame'
import { WordmarkLoader } from './WordmarkLoader'
interface NativeImageSourceRenderOpts {
refreshSignal: number
onBusyChange: (busy: boolean) => void
}
interface Props {
locale: Locale
appearance: 'light' | 'dark'
pluginUI: PluginUIContributions | null
renderNativeSource?: (nativeId: string, opts: NativeImageSourceRenderOpts) => React.ReactNode
onOpenPlugins: () => void
}
function sourceKey(src: ImageSourceContribution): string {
return `${src.pluginId ?? ''}:${src.id}`
}
/**
* Top-level Images page: hosts plugin-registered image source tabs via a top
* Segmented control. Built-in plugins (such as macOS Images) mount native host
* views; external plugins render in a sandboxed L4 iframe.
*/
export const ImagesPage: React.FC<Props> = ({
locale,
appearance,
pluginUI,
renderNativeSource,
onOpenPlugins,
}) => {
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
const sources = useMemo(() => pluginUI?.image_sources ?? [], [pluginUI?.image_sources])
const [selectedKey, setSelectedKey] = useState('')
const [refreshSignals, setRefreshSignals] = useState<Record<string, number>>({})
const [busyByKey, setBusyByKey] = useState<Record<string, boolean>>({})
const activeSource = useMemo(() => {
if (sources.length === 0) return null
return sources.find((s) => sourceKey(s) === selectedKey) ?? sources[0]
}, [sources, selectedKey])
const activeKey = activeSource ? sourceKey(activeSource) : ''
useEffect(() => {
if (activeKey && activeKey !== selectedKey) {
setSelectedKey(activeKey)
}
}, [activeKey, selectedKey])
const activeBusy = activeKey ? !!busyByKey[activeKey] : false
const handleActiveBusyChange = useCallback((busy: boolean) => {
if (!activeKey) return
setBusyByKey((prev) => (prev[activeKey] === busy ? prev : { ...prev, [activeKey]: busy }))
}, [activeKey])
const triggerRefresh = () => {
if (!activeKey) return
setRefreshSignals((prev) => ({ ...prev, [activeKey]: (prev[activeKey] ?? 0) + 1 }))
}
return (
<div className="page installers-page">
<header className="page-head">
<div className="page-title installers-page-title">
<h1>{tr('installersTitle')}</h1>
<span>{tr('installersHint')}</span>
</div>
{activeSource && (
<div className="actions">
<IconButton
label={tr('refresh')}
icon={<RefreshCw size={16} className={activeBusy ? 'spin' : undefined} />}
disabled={activeBusy}
onClick={triggerRefresh}
/>
</div>
)}
</header>
{sources.length > 0 && (
<div className="images-switch">
<Segmented
label={tr('imagesSourcesLabel')}
value={activeKey}
onChange={setSelectedKey}
options={sources.map((src) => ({
value: sourceKey(src),
label: locText(src.title, locale, src.id),
}))}
/>
</div>
)}
{pluginUI === null ? (
<WordmarkLoader className="installers-loader" />
) : !activeSource ? (
<div className="empty-panel images-empty">
<Disc3 size={28} aria-hidden="true" />
<div className="images-empty-title">{tr('imagesNoSourcesTitle')}</div>
<div className="images-empty-desc">{tr('imagesNoSourcesDesc')}</div>
<Button variant="primary" icon={<Puzzle size={14} aria-hidden="true" />} onClick={onOpenPlugins}>
{tr('imagesOpenPlugins')}
</Button>
</div>
) : activeSource.native ? (
renderNativeSource?.(activeSource.native, {
refreshSignal: refreshSignals[activeKey] ?? 0,
onBusyChange: handleActiveBusyChange,
})
) : (
<PluginPanelFrame
key={activeKey}
locale={locale}
panel={{
id: activeSource.id,
slot: 'images.source',
title: activeSource.title,
entry: activeSource.entry || 'index.html',
height: activeSource.height || 560,
pluginId: activeSource.pluginId,
}}
assetToken={pluginUI.asset_token ?? ''}
appearance={appearance}
headless
maxHeight={1200}
refreshSignal={refreshSignals[activeKey] ?? 0}
/>
)}
</div>
)
}