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

102 lines
5.8 KiB
TypeScript

// A store that could not be read, in one plain line: Google's typed errors (an API turned off in the Cloud project, no
// access to the app, a revoked key, a used-up quota) with the button that fixes them, and the raw API text only in a
// collapsed "Details" and the log (never a JSON dump in the window).
import { useEffect } from 'react';
import { api, errorText } from '../api';
import { type GoogleFinding, parseGoogleError, plainError } from '../../../../cli/lib/google-errors';
import { t, type Key } from '../i18n';
import { useData } from '../store';
import { Icon } from './icons';
import { Notice } from './kit';
const WHAT: Record<string, Key> = {
'playdeveloperreporting.googleapis.com': 'gerr.what.reporting',
'androidpublisher.googleapis.com': 'gerr.what.publisher',
'storage.googleapis.com': 'gerr.what.storage',
'storage-api.googleapis.com': 'gerr.what.storage',
};
/** The typed finding of a store read error (from the snapshot, else parsed from its text). */
export function findingOf(e: { error: string; finding?: GoogleFinding; detail?: string }, app?: string | null): GoogleFinding | null {
return e.finding ?? parseGoogleError(e.detail ?? e.error, { app });
}
/** The one line for a finding, in the window's language. */
export function findingLine(f: GoogleFinding): string {
switch (f.kind) {
case 'api-disabled': return t('gerr.apiDisabled', { what: t(WHAT[f.service] ?? 'gerr.what.other'), service: f.serviceTitle, project: f.projectId ?? t('gerr.projectUnknown') });
case 'no-app-access': return t('gerr.noAccess', { app: f.app ?? t('gerr.thisApp') });
case 'auth-revoked': return t('gerr.revoked');
case 'quota': return t('gerr.quota');
}
}
export function TurnOnButton({ url, primary = true }: { url: string; primary?: boolean }) {
const d = useData();
return (
<button className={`btn btn-sm${primary ? ' btn-primary' : ''}`} onClick={() => api.openExternal(url).catch(e => d.toast({ kind: 'bad', title: t('gerr.openFailed'), detail: errorText(e) }))}>
<Icon name="external" />{t('gerr.turnOn')}
</button>
);
}
/** One store's read error. `unreadable`: the sentence for an unknown error ({store} / {error}). */
export function StoreReadNotice({ store, e, app, onRetry, busy, unreadable }: {
store: string; e: { error: string; finding?: GoogleFinding; detail?: string }; app?: string | null; onRetry?: () => void; busy?: boolean; unreadable: Key;
}) {
const d = useData();
const f = findingOf(e, app);
const raw = e.detail ?? e.error;
useEffect(() => { api.logWebview('warn', `${store} could not be read${f ? ` (${f.kind})` : ''}`, raw).catch(() => {}); }, [store, raw]); // eslint-disable-line react-hooks/exhaustive-deps
const line = f ? findingLine(f) : t(unreadable, { store, error: plainError(raw) });
const retry = onRetry && <button className="btn btn-sm" onClick={onRetry} disabled={busy}><Icon name="refresh" className={busy ? 'spin' : undefined} />{t('gerr.checkAgain')}</button>;
const open = (url: string) => api.openExternal(url).catch(err => d.toast({ kind: 'bad', title: t('gerr.openFailed'), detail: errorText(err) }));
return (
<Notice tone="warn" action={<div className="row" style={{ gap: 8, flex: 'none' }}>
{f?.kind === 'api-disabled' && <TurnOnButton url={f.activationUrl} />}
{f?.kind === 'no-app-access' && <button className="btn btn-sm btn-primary" onClick={() => open(f.usersUrl)}><Icon name="external" />{t('gerr.openUsers')}</button>}
{retry}
</div>}>
<div className="stack" style={{ gap: 4 }}>
<span className="store-error-line">{line}</span>
{raw !== line && <details className="toast-log store-error-details"><summary className="small">{t('gerr.details')}</summary><pre className="log selectable">{raw}</pre></details>}
</div>
</Notice>
);
}
export interface BlockedProject { projectId: string | null; service: string; serviceTitle: string; activationUrl: string; apps: string[] }
/** api-disabled findings across apps, one entry per Cloud project and API (several apps share one service account). Pure. */
export function blockedProjects(items: { app: string; errors: ({ error: string; finding?: GoogleFinding; detail?: string } | string | undefined | null)[] }[]): BlockedProject[] {
const out = new Map<string, BlockedProject>();
for (const it of items) for (const e of it.errors) {
if (!e) continue;
const f = typeof e === 'string' ? parseGoogleError(e) : findingOf(e);
if (f?.kind !== 'api-disabled') continue;
const k = `${f.projectId ?? '?'}|${f.service}`;
const b = out.get(k) ?? { projectId: f.projectId, service: f.service, serviceTitle: f.serviceTitle, activationUrl: f.activationUrl, apps: [] };
if (!b.apps.includes(it.app)) b.apps.push(it.app);
out.set(k, b);
}
return [...out.values()];
}
/** The portfolio's one notice for Cloud projects that block reads of several apps. */
export function BlockedProjectsNotice({ blocked, onRetry, busy }: { blocked: BlockedProject[]; onRetry: () => void; busy: boolean }) {
if (!blocked.length) return null;
const apps = [...new Set(blocked.flatMap(b => b.apps))];
return (
<Notice tone="warn" icon="cloud" action={<button className="btn btn-sm" onClick={onRetry} disabled={busy}><Icon name="refresh" className={busy ? 'spin' : undefined} />{t('gerr.checkAgain')}</button>}>
<div className="stack blocked-projects" style={{ gap: 6 }}>
<span className="strong">{t('gerr.portfolio', { count: blocked.length, apps: apps.join(', ') })}</span>
{blocked.map(b => (
<div key={`${b.projectId}|${b.service}`} className="row" style={{ gap: 8, flexWrap: 'wrap' }}>
<span className="grow small">{findingLine({ kind: 'api-disabled', ...b })}</span>
<TurnOnButton url={b.activationUrl} />
</div>
))}
</div>
</Notice>
);
}