storebound/apps/desktop/src/ui/ReadButtons.tsx

40 lines
2.4 KiB
TypeScript

// Read buttons used on several screens. Their busy state comes from the read registry (useReadState), so every copy
// of a button, and every other button whose read overlaps it, spins and is disabled together.
import { t } from '../i18n';
import { accountCheckKey, importKey } from '../lib/read-registry';
import { useData, useReadState } from '../store';
import { Icon } from './icons';
/** Refresh all: busy while any read it covers runs (its own pass, a card refresh, Check again, the background reader). */
export function RefreshAllButton({ className = 'btn btn-sm' }: { className?: string }) {
const d = useData();
const s = useReadState({ tags: ['all'] });
return (
<button className={className} onClick={() => { d.readAll(true).catch(() => {}); }} disabled={s.busy} aria-busy={s.busy} title={s.busy ? t('read.busyShared') : t('action.refreshAllHint')}>
<Icon name="refresh" className={s.busy ? 'spin' : undefined} />{t('action.refreshAll')}
</button>
);
}
/** Import apps (every account, or one): busy while that account's (or any account's) app list is being read. */
export function ImportAppsButton({ account, className = 'btn btn-sm', label }: { account?: string; className?: string; label?: string }) {
const d = useData();
const s = useReadState(account ? { keys: [importKey(account)] } : { tags: ['import'] });
return (
<button className={className} onClick={() => { d.importApps(account).catch(() => {}); }} disabled={s.busy || !d.accounts.length} aria-busy={s.busy} title={s.busy ? t('read.busyShared') : t('action.importAppsHint')}>
<Icon name="download" className={s.busy ? 'spin' : undefined} />{label ?? t('action.importApps')}
</button>
);
}
/** Test one account (or all): single-flight per account; Test all and a row's Test are busy together. */
export function TestAccountButton({ account, className = 'btn btn-sm', label }: { account?: string; className?: string; label: string }) {
const d = useData();
const s = useReadState(account ? { keys: [accountCheckKey(account)] } : { tags: ['check'] });
const run = () => { (account ? d.checkAccount(account) : Promise.all(d.accounts.map(a => d.checkAccount(a.id)))).catch(() => {}); };
return (
<button className={className} onClick={run} disabled={s.busy || !d.accounts.length} aria-busy={s.busy}>
<Icon name="shield" className={s.busy ? 'spin' : undefined} />{label}
</button>
);
}