// The wayBetter Store view, shared by every wayBetter app: themes and plugins from the signed // catalog (native code verifies its Ed25519 signature, keeps this app's items, then checks each // download's sha256). Search, kinds, categories, cards with previews and permission badges, and // Install / Update / Remove. Installing always goes through the install screen (InstallHost), never // straight from here. Styles: `store.css` (framework `--wb-*` tokens). The app may pass its own // controls (buttons, search field, segmented control) so the view matches its settings. import { useEffect, useMemo, useState, useSyncExternalStore, type ReactNode } from 'react'; import type { InstallController } from './installController.ts'; import type { StoreText } from './storeMessages.ts'; import { categoryLabel, errorText, filterItems, itemState, permissionBadges, type ItemState, type StoreCatalogView, type StoreItem } from './storeModel.ts'; type ButtonProps = { children: ReactNode; onClick?(): void; tone?: 'plain' | 'primary' | 'ghost' | 'danger'; size?: 'sm' | 'md'; disabled?: boolean; title?: string }; type Option = { value: T; label: string }; export type StoreControls = { Button(props: ButtonProps): ReactNode; SearchField(props: { value: string; onChange(next: string): void; placeholder: string; label: string }): ReactNode; Segmented(props: { value: T; options: Array>; onChange(next: T): void; label: string; size?: 'sm' | 'md' }): ReactNode; EmptyState(props: { icon?: ReactNode; title: string; children?: ReactNode; action?: ReactNode }): ReactNode; Badge(props: { children: ReactNode }): ReactNode; }; export const DEFAULT_CONTROLS: StoreControls = { Button: ({ children, onClick, tone = 'plain', size = 'md', disabled, title }) => , SearchField: ({ value, onChange, placeholder, label }) => onChange(event.target.value)} onKeyDown={event => { if (event.key === 'Escape' && value) { event.stopPropagation(); onChange(''); } }}/>, Segmented: ({ value, options, onChange, label }) =>
{options.map(option => )}
, EmptyState: ({ icon, title, children, action }) =>
{icon && }{title}{children &&

{children}

}{action}
, Badge: ({ children }) => {children}, }; export type StoreViewProps = { t: StoreText; /** The site's name in messages ("X"). */ site: string; /** `store_catalog` (the app passes `refresh => invoke('store_catalog', { refresh })`). */ load(refresh: boolean): Promise; /** The install screen's controller: Install opens it; the list refreshes when it closes. */ controller: InstallController; /** Theme ids that ship with the app, and the owner's own theme ids now. */ builtinThemes: readonly string[]; userThemes: readonly string[]; /** Removes an installed item (plugin file, or the owner's copy of a store theme); false: nothing to remove. */ onRemove(item: StoreItem, view: StoreCatalogView): Promise; /** Makes a built-in theme the current one. */ onUse(item: StoreItem): Promise; notify(text: string, error?: boolean): void; /** "3 minutes ago" for a timestamp (the app's relative-time formatter). */ relativeTime(at: number): string; /** Window events after which the list reloads (e.g. the app's "plugins changed"). */ reloadOn?: readonly string[]; controls?: Partial; }; const STORE_ICON = ; function Preview({ item }: { item: StoreItem }) { const [broken, setBroken] = useState(false); const c = item.colors; if (item.preview && !broken) return setBroken(true)}/>; // Offline or not published yet: swatches from the signed catalog (themes) or a plain tile. return ; } function Action({ item, state, busy, t, ui, onInstall, onRemove, onUse }: { item: StoreItem; state: ItemState; busy: boolean; t: StoreText; ui: StoreControls; onInstall(): void; onRemove(): void; onUse(): void }) { const { Button } = ui; const [asking, setAsking] = useState(false); if (state.state === 'builtin') return
{t('wb.store.builtIn')}
; if (state.state === 'install') return
; const remove = asking ? <> : ; if (state.state === 'update') return
{remove}
; if (state.state === 'modified') return
{t('wb.store.modified')}{remove}
; return
{state.version ? t('wb.store.installedVersion', { version: state.version }) : t('wb.store.installed')}{remove}
; } export function StoreView(props: StoreViewProps) { const { t, site, controller, notify } = props; const ui: StoreControls = { ...DEFAULT_CONTROLS, ...props.controls }; const { Button, SearchField, Segmented, EmptyState, Badge } = ui; const [view, setView] = useState(null); const [loading, setLoading] = useState(false); const [query, setQuery] = useState(''); const [kind, setKind] = useState<'all' | 'theme' | 'plugin'>('all'); const [category, setCategory] = useState('all'); const request = useSyncExternalStore(controller.subscribe, controller.current, controller.current); const load = (refresh: boolean) => { setLoading(true); return props.load(refresh).then(setView).catch(error => notify(errorText(error), true)).finally(() => setLoading(false)); }; useEffect(() => { void load(false); }, []); // Installs finish in the install screen; refresh what is installed afterwards. useEffect(() => { const again = () => void load(false); const events = props.reloadOn ?? []; for (const name of events) window.addEventListener(name, again); return () => { for (const name of events) window.removeEventListener(name, again); }; }, [(props.reloadOn ?? []).join(' ')]); useEffect(() => { if (request === null) void load(false); }, [request === null]); const items = view?.catalog?.items ?? []; const context = { plugins: view?.plugins ?? {}, records: view?.records.items ?? {}, builtinThemes: props.builtinThemes, userThemes: props.userThemes }; const categories = useMemo(() => [...new Set(items.filter(item => kind === 'all' || item.kind === kind).map(item => item.category))].sort(), [items, kind]); const shown = filterItems(items, query, kind, category); const updates = items.filter(item => itemState(item, context).state === 'update'); const ago = (iso: string | null) => { if (!iso) return ''; const at = Date.parse(iso); return Number.isFinite(at) ? props.relativeTime(Math.min(at, Date.now())) : ''; }; async function removeItem(item: StoreItem) { if (!view) return; try { if (!(await props.onRemove(item, view))) return; notify(t('wb.store.removed', { name: item.name })); await load(false); } catch (error) { notify(errorText(error), true); } } const status = view?.error ? {view.catalog ? t('wb.store.offline', { when: ago(view.fetchedAt), error: errorText(view.error) }) : t('wb.store.unreachableShort')} : view?.catalog ? {t('wb.store.verified', { when: ago(view.fetchedAt) })} : null; return

{t('wb.store.intro')}

{ setKind(next); setCategory('all'); }} options={[{ value: 'all', label: t('wb.store.all') }, { value: 'theme', label: t('wb.store.themes') }, { value: 'plugin', label: t('wb.store.pluginsTab') }]}/>
{['all', ...categories].map(id => )} {status}
{updates.length > 0 &&
{t('wb.store.updates', { count: updates.length })}{updates.map(item => item.name).join(', ')}
} {!view && loading &&
{Array.from({ length: 6 }, (_, i) =>
)}
} {view && !view.catalog && void load(true)}>{t('wb.store.tryAgain')}}> {view.error ? errorText(view.error).replace(/\.?$/, '.') : t('wb.store.noCatalog')} {t('wb.store.nothingChanged')} } {view?.catalog && shown.length === 0 && {t('wb.store.noMatchHint')}}
{shown.map(item => { const state = itemState(item, context); return

{item.name}

{item.kind === 'theme' ? t('wb.store.theme') : t('wb.store.plugin')}

{item.author && `${t('wb.store.by', { author: item.author })} · `}v{item.version} · {categoryLabel(item.category, t)}

{item.description}

{item.kind === 'plugin' &&
    {permissionBadges(item.permissions, t, site).map(badge =>
  • {badge.label}
  • )}
} void controller.requestStoreItem(item)} onRemove={() => void removeItem(item)} onUse={() => void props.onUse(item).then(() => notify(t('wb.store.themeNow', { name: item.name }))).catch(error => notify(errorText(error), true))}/>
; })}

{t('wb.store.foot')}

; }