waybetter/packages/shell/src/panel/StoreSection.tsx

62 lines
2.9 KiB
TypeScript

// The wayBetter Store section: the Store view (StoreView.tsx) with the panel's own controls, the
// app's built-in and own themes, and removal of installed items. Native code verifies the catalog's
// Ed25519 signature, keeps the app's items, then checks each download's sha256. Installing always
// goes through the shell's install screen (InstallDialog), never straight from here.
import { invoke } from '../frame/bridge.ts';
import { useT } from '../frame/hooks.ts';
import { relativeTime } from '../frame/i18n.ts';
import { site } from '../frame/site.ts';
import type { StoreCatalogView, StoreItem } from '../storeModel.ts';
import { StoreView, type StoreControls } from '../StoreView.tsx';
import '../store.css';
import { Badge, Button, EmptyState, SearchField, Segmented, useSettingsQuery } from './controls.tsx';
import { panelSite, panelThemes, type Notify } from './site.ts';
import { useThemeState } from './themes.ts';
import './plugins.css';
const CONTROLS: Partial<StoreControls> = { Button, SearchField, Segmented, EmptyState, Badge };
const RELOAD_ON = ['bt-plugins-changed', 'bt-store-changed', 'bt-strings-changed'];
export function StoreSection({ notify }: { notify: Notify }) {
const t = useT();
const { store, catalog } = panelThemes();
const themes = useThemeState();
const settingsQuery = useSettingsQuery();
if (settingsQuery) return null;
const userThemes = themes.choice.themes.map(theme => theme.id);
async function remove(item: StoreItem, view: StoreCatalogView): Promise<boolean> {
if (item.kind === 'strings') {
await invoke('strings_remove', { id: item.id });
window.dispatchEvent(new CustomEvent('bt-strings-changed'));
return true;
}
if (item.kind === 'plugin' && item.file) {
await invoke('plugin_remove', { file: item.file });
window.dispatchEvent(new CustomEvent('bt-plugins-changed'));
return true;
}
const record = Object.entries(view.records.items).find(([key, value]) => key.startsWith('theme:') && value.storeId === item.id && userThemes.includes(key.slice(6)));
const id = record?.[0].slice(6);
if (!id) return false;
const choice = themes.choice;
await store.setChoice({ ...choice, themes: choice.themes.filter(theme => theme.id !== id), ...(choice.preset === id ? { preset: catalog.defaultChoice.preset, overrides: {}, accent: null } : {}) });
await invoke('install_forget', { kind: 'theme', name: id });
return true;
}
return <StoreView
t={t}
site={site().name}
load={refresh => invoke<StoreCatalogView>('store_catalog', { refresh })}
controller={panelSite().store!.controller}
builtinThemes={catalog.builtins.map(theme => theme.id)}
userThemes={userThemes}
onRemove={remove}
onUse={item => store.setChoice({ ...themes.choice, preset: item.id, overrides: {}, accent: null })}
notify={notify}
relativeTime={relativeTime}
reloadOn={RELOAD_ON}
controls={CONTROLS}
/>;
}