One ContextMenu in the design system (keyboard, type-ahead, submenus, check items, disabled reasons, Shift+F10 / Menu key, viewport placement, light and dark). The native menu is prevented globally; text fields keep Cut / Copy / Paste / Select all. Menus on sidebar and portfolio apps, app hero, releases, store art, live art, reviews, checks, editor fields, catalog, prices, accounts, app folders, ignored.
272 lines
17 KiB
TypeScript
272 lines
17 KiB
TypeScript
// Checks: the offline rejection-risk scan of the project files (`scan`, every time), the offline config check and
|
|
// the read-only store readiness report (`validate`, on demand) with its ordered blockers and fix hints.
|
|
import { type MenuInput, type MenuItem, useContextMenu } from '../../ui/ContextMenu';
|
|
import { copyToast, openUrl } from '../../ui/app-menu';
|
|
import type { Project } from '../../api';
|
|
import { t } from '../../i18n';
|
|
import { ago } from '../../lib/format';
|
|
import type { Finding, FixAction, ScanFinding, ScanResult, ScanSeverity, ValidateResult } from '../../lib/model';
|
|
import { useData, useRead, useWrite } from '../../store';
|
|
import { WriteButton } from '../../ui/WriteButton';
|
|
import { AiFix, aiFinding } from '../../ui/AiFix';
|
|
import { api, errorText, newJob } from '../../api';
|
|
import type { ToolResult } from '../../api';
|
|
import type { ReportData } from '../../../../../cli/lib/report';
|
|
import { type ReactNode, useEffect, useState } from 'react';
|
|
import { Icon, type IconName, StoreGlyph } from '../../ui/icons';
|
|
import { OpenLink } from '../../ui/OpenLink';
|
|
import { Bar, ErrorNotice, ReadControl, Skeleton } from '../../ui/kit';
|
|
import { EmptyState, Section, StatusPill } from '../../ui/ds';
|
|
import '../../styles/checks.css';
|
|
import { exportName, validateMd } from '../../lib/exports';
|
|
import { useExports } from '../../ui/Exports';
|
|
|
|
type ReportResult = ReportData & { file: string; path: string };
|
|
|
|
const LEVEL: Record<Finding['level'], ['bad' | 'warn' | 'ok', 'alertCircle' | 'alert' | 'check']> = { blocker: ['bad', 'alertCircle'], warn: ['warn', 'alert'], ok: ['ok', 'check'] };
|
|
|
|
/** Fix methods the window runs from a finding (cli/lib/fix-actions.ts FIX_METHODS): local config writes and store
|
|
* text pushes, each a dry run, the native dialog showing it, then the real run. Anything else gets no button. */
|
|
export const FIX_METHODS = new Set(['scan.write', 'play.listing', 'apple.listing', 'apple.review_details']);
|
|
/** The context-menu twin of FixButton (same write: dry run, then confirm). */
|
|
const fixItem = (w: ReturnType<typeof useWrite>, action: FixAction, what: string): MenuItem => ({ label: t('checks.fix'), icon: 'wand', onSelect: () => { void w.run({ method: action.method, args: action.args, title: t('checks.fixing', { what }), done: t('checks.fixed') }); } });
|
|
export const fixable = (a: FixAction | undefined): a is FixAction => !!a && FIX_METHODS.has(a.method) && !!a.args && typeof a.args === 'object';
|
|
|
|
/** The Fix button of one finding: its action as a confirm-gated write. */
|
|
function FixButton({ action, what, w }: { action: FixAction; what: string; w: ReturnType<typeof useWrite> }) {
|
|
return (
|
|
<WriteButton run={w} icon="wand" hint={t(`checks.fixHint.${action.method}` as Parameters<typeof t>[0])}
|
|
w={{ method: action.method, args: action.args, title: t('checks.fixing', { what }), done: t('checks.fixed') }}>{t('checks.fix')}</WriteButton>
|
|
);
|
|
}
|
|
|
|
const SEV: Record<ScanSeverity, ['bad' | 'warn' | 'info', 'alertCircle' | 'alert' | 'info']> = { blocker: ['bad', 'alertCircle'], high: ['bad', 'alert'], medium: ['warn', 'alert'], low: ['info', 'info'] };
|
|
const SEV_ORDER: ScanSeverity[] = ['blocker', 'high', 'medium', 'low'];
|
|
|
|
/** One check as a card: tone icon tile, title, detail lines, the fix, and its actions on the right. */
|
|
function CheckCard({ tone, icon, title, children, actions, tags, menu }: { tone: 'bad' | 'warn' | 'ok' | 'info'; icon: IconName; title: ReactNode; children?: ReactNode; actions?: ReactNode; tags?: ReactNode; menu?: () => MenuInput }) {
|
|
const cm = useContextMenu();
|
|
return (
|
|
<div className={`ck-card tone-${tone}`} {...(menu ? cm(menu) : {})}>
|
|
<span className={`ck-ic tone-bg-${tone}`}><Icon name={icon} /></span>
|
|
<div className="ck-main">
|
|
<div className="ck-title">{title}</div>
|
|
{children}
|
|
</div>
|
|
<div className="ck-side">
|
|
{tags && <div className="ck-tags">{tags}</div>}
|
|
{actions && <div className="ck-acts">{actions}</div>}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** The `storebound scan` section: rejection risks with guideline, evidence file:line, fix and skill; open risks first,
|
|
* accepted ones collapsed. */
|
|
function ScanCard({ project }: { project: Project }) {
|
|
const s = useRead<ScanResult>(project.id, 'scan', { network: false, title: t('checks.scan') });
|
|
const w = useWrite(project.id);
|
|
const d = useData();
|
|
const findings = Array.isArray(s.data?.findings) ? s.data!.findings : [];
|
|
// Fix with AI: is claude installed (else Copy for AI); one run at a time
|
|
const [claude, setClaude] = useState<boolean | null>(null);
|
|
const [aiBusy, setAiBusy] = useState(false);
|
|
// the finding an AI fix works on stays in the list (its diff, Keep / Undo) even after the scan stops reporting it
|
|
const [aiActive, setAiActive] = useState<ScanFinding | null>(null);
|
|
const same = (a: ScanFinding, b: ScanFinding) => a.store === b.store && a.rule === b.rule && a.title === b.title;
|
|
const rows = aiActive && !findings.some(x => same(x, aiActive)) ? [...findings, aiActive] : findings;
|
|
const seen = new Map<string, number>();
|
|
const keyOf = (f: ScanFinding) => { const k = `${f.store}|${f.rule}|${f.title}`; const n = (seen.get(k) ?? 0) + 1; seen.set(k, n); return `${k}|${n}`; };
|
|
const firstAi = findings.find(f => f.aiFix && !f.accepted && !fixable(f.action));
|
|
useEffect(() => {
|
|
if (!firstAi || claude !== null) return;
|
|
api.aiFixInfo(project.id, aiFinding(firstAi)).then(i => setClaude(i.claude), () => setClaude(false));
|
|
}, [project.id, firstAi, claude]);
|
|
const sdks = Array.isArray(s.data?.sdks) ? s.data!.sdks : [];
|
|
const asks = (Array.isArray(s.data?.declarations?.items) ? s.data!.declarations.items : []).filter(x => x.status === 'ask').length;
|
|
const keyed = rows.map(f => ({ f, k: keyOf(f) }));
|
|
const open = keyed.filter(x => !x.f.accepted).sort((a, b) => SEV_ORDER.indexOf(a.f.severity) - SEV_ORDER.indexOf(b.f.severity));
|
|
const accepted = keyed.filter(x => !!x.f.accepted);
|
|
const card = ({ f, k }: { f: ScanFinding; k: string }) => {
|
|
const [tone, icon] = f.accepted ? ['ok', 'check'] as const : SEV[f.severity] ?? SEV.low;
|
|
const ev = Array.isArray(f.evidence) ? f.evidence : [];
|
|
return (
|
|
<CheckCard key={k} tone={tone} icon={icon} title={<><span className="ck-gl">{f.guideline}</span>{f.title}</>}
|
|
menu={() => [
|
|
!f.accepted && fixable(f.action) && fixItem(w, f.action, f.title),
|
|
!f.accepted && f.url && /^https:\/\//.test(f.url) && { label: t('link.open'), icon: 'external', onSelect: () => openUrl(d, f.url!) },
|
|
ev[0] && { label: t('cm.revealEvidence'), icon: 'folder', shortcut: `${ev[0].file.split('/').pop()}:${ev[0].line}`, onSelect: () => { api.reveal(project.id, ev[0]!.file).catch(() => {}); } },
|
|
'sep',
|
|
{ label: t('cm.copy'), icon: 'copy', submenu: [
|
|
{ label: t('cm.copyTitle'), icon: 'text', onSelect: () => copyToast(d, `${f.guideline} ${f.title}`) },
|
|
f.fix && { label: t('cm.copyFix'), icon: 'wand', onSelect: () => copyToast(d, f.fix) },
|
|
f.skill && { label: t('cm.copySkill'), icon: 'sparkle', shortcut: f.skill, onSelect: () => copyToast(d, f.skill) },
|
|
ev.length > 0 && { label: t('cm.copyEvidence'), icon: 'clipboard', onSelect: () => copyToast(d, ev.map(e => `${e.file}:${e.line}${e.text ? ` ${e.text}` : ''}`).join('\n')) },
|
|
] },
|
|
]}
|
|
tags={<><StatusPill tone={f.accepted ? 'ok' : tone}>{t(f.accepted ? 'checks.sev.accepted' : `checks.sev.${f.severity}` as Parameters<typeof t>[0])}</StatusPill><span className="ck-store"><StoreGlyph store={f.store === 'apple' ? 'ios' : 'android'} className="ic13" />{f.store === 'apple' ? 'App Store' : 'Google Play'}</span></>}
|
|
actions={<>{!f.accepted && <OpenLink url={f.url} />}{!f.accepted && fixable(f.action) && <FixButton action={f.action} what={f.title} w={w} />}</>}>
|
|
{ev.slice(0, 2).map((e, j) => <span className="ck-sub selectable" key={j}><code>{e.file}:{e.line}</code>{e.text ? ` ${e.text}` : ''}</span>)}
|
|
{f.accepted ? <span className="ck-sub">{t('checks.accepted', { reason: f.accepted })}</span>
|
|
: <span className="ck-fix"><Icon name="wand" /><span className="selectable">{f.fix} <code>{f.skill}</code></span></span>}
|
|
{!f.accepted && f.aiFix && !fixable(f.action) && <div className="ck-ai"><AiFix projectId={project.id} f={f} claude={claude} busy={aiBusy} setBusy={setAiBusy} rescan={() => { s.refresh(); }}
|
|
stillThere={findings.some(x => same(x, f))} onActive={on => setAiActive(on ? f : null)} /></div>}
|
|
</CheckCard>
|
|
);
|
|
};
|
|
return (
|
|
<Section title={t('checks.scan')} icon="search" sub={s.data && sdks.length ? t('checks.scanSdks', { list: sdks.map(x => x.name).join(', ') }) : t('checks.scanSub')}
|
|
actions={<ReadControl r={s} label={t('checks.scanAgain')} />}>
|
|
<div className="ck-list">
|
|
{s.error && <ErrorNotice error={s.error} keep={!!s.data} />}
|
|
{!s.data ? (s.loading && !s.error ? <Skeleton lines={4} /> : null)
|
|
: !rows.length ? <EmptyState icon="shield" tone="ok"><b>{t('checks.scanClean')}</b><br />{t('checks.scanCleanText')}</EmptyState> : (
|
|
<>
|
|
{open.map(card)}
|
|
{accepted.length > 0 && (
|
|
<details className="ck-passed">
|
|
<summary><Icon name="check" />{t('v2ins.cAccepted', { count: accepted.length })}</summary>
|
|
<div className="ck-list">{accepted.map(card)}</div>
|
|
</details>
|
|
)}
|
|
</>
|
|
)}
|
|
{s.data && (asks > 0 || s.data.declarations?.changed) && (
|
|
<div className="ck-notes">
|
|
{asks > 0 && <div className="ck-sub">{t('checks.scanAsk', { count: asks })}</div>}
|
|
{s.data.declarations?.changed && <div className="ck-sub"><code className="selectable">{t('checks.scanWrite')}</code></div>}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</Section>
|
|
);
|
|
}
|
|
|
|
/** The shareable readiness report (`storebound report`, the same code path as the CLI): one HTML page in
|
|
* .storebound/reports/, opened in the default browser through project_reveal (the host only opens that page). */
|
|
function ReportBar({ project }: { project: Project }) {
|
|
const d = useData();
|
|
const [busy, setBusy] = useState(false);
|
|
const [last, setLast] = useState<ReportResult | null>(null);
|
|
const run = async () => {
|
|
setBusy(true);
|
|
try {
|
|
const r = await api.call<ToolResult & { data?: ReportResult }>('report', project.id, {}, newJob('report'));
|
|
if (!r.ok || !r.data?.path) throw new Error(r.output || t('checks.reportFailed'));
|
|
setLast(r.data);
|
|
await api.reveal(project.id, r.data.path, true);
|
|
} catch (e) { d.toast({ kind: 'bad', title: t('checks.reportFailed'), detail: errorText(e) }); }
|
|
finally { setBusy(false); }
|
|
};
|
|
return (
|
|
<div className="ck-report">
|
|
<span className="ck-report-t"><b>{t('checks.report')}</b><span>{last ? reportSummary(last) : t('checks.reportSub')}</span></span>
|
|
<button className="btn btn-sm" onClick={run} disabled={busy}><Icon name={busy ? 'refresh' : 'external'} className={busy ? 'spin' : undefined} />{t('checks.reportOpen')}</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** One line, both stores: "App Store: 2 blockers · Google Play: not checked". */
|
|
export function reportSummary(r: ReportResult): string {
|
|
const one = (s: 'apple' | 'play') => {
|
|
const x = r.summary[s], read = r.read[s];
|
|
const v = x.blockers ? t('checks.reportBlockers', { count: x.blockers }) : !r.stores.includes(s) ? t('checks.reportNotSetUp') : !read.checked ? t('checks.reportNotChecked') : t('checks.reportReady');
|
|
return `${s === 'apple' ? 'App Store' : 'Google Play'}: ${v}`;
|
|
};
|
|
return `${one('apple')} · ${one('play')}`;
|
|
}
|
|
|
|
export function ChecksTab({ project }: { project: Project }) {
|
|
const d = useData();
|
|
const e = d.statusOf(project);
|
|
const v = useRead<ValidateResult>(project.id, 'validate', { network: true, title: t('checks.validate') });
|
|
const w = useWrite(project.id);
|
|
const name = e?.summary?.name ?? project.name;
|
|
const stores = (['apple', 'play'] as const).filter(x => !!e?.summary?.[x]);
|
|
useExports('checks', [{ id: 'validate-md', label: t('export.validate'), icon: 'shield', kind: 'md', file: exportName(name, 'store readiness'), disabled: v.data ? false : t('export.readFirst'),
|
|
build: () => validateMd(name, v.data!, v.at, { check: e?.check ?? null, version: d.info?.version, ...(stores.length ? { stores } : {}) }) }]);
|
|
const problems = [...(e?.errors ?? []), ...(e?.check?.problems ?? [])];
|
|
const warnings = e?.check?.warnings ?? [];
|
|
const findings = [...(v.data?.findings ?? [])].sort((a, b) => ['blocker', 'warn', 'ok'].indexOf(a.level) - ['blocker', 'warn', 'ok'].indexOf(b.level));
|
|
const vBlock = findings.filter(f => f.level === 'blocker'), vWarn = findings.filter(f => f.level === 'warn'), vOk = findings.filter(f => f.level === 'ok');
|
|
const blockers = problems.length + vBlock.length;
|
|
const warns = warnings.length + vWarn.length;
|
|
const total = findings.length + problems.length + warnings.length;
|
|
const passed = vOk.length;
|
|
const checked = !!v.data && !!e;
|
|
const state: 'bad' | 'warn' | 'ok' | 'neutral' = blockers ? 'bad' : !checked ? 'neutral' : warns ? 'warn' : 'ok';
|
|
const headline = blockers ? t('v2ins.cBlocked', { count: blockers }) : !checked ? t('v2ins.cNotRun') : t('v2ins.cReady');
|
|
const finding = (f: Finding, i: number) => {
|
|
const [tone, icon] = LEVEL[f.level];
|
|
return (
|
|
<CheckCard key={`${f.level}${i}`} tone={tone} icon={icon} title={f.msg}
|
|
menu={() => [
|
|
f.level !== 'ok' && fixable(f.action) && fixItem(w, f.action, f.msg),
|
|
f.url && /^https:\/\//.test(f.url) && { label: t('link.open'), icon: 'external', onSelect: () => openUrl(d, f.url!) },
|
|
'sep',
|
|
{ label: t('cm.copyTitle'), icon: 'copy', onSelect: () => copyToast(d, f.msg) },
|
|
f.fix && { label: t('cm.copyFix'), icon: 'wand', onSelect: () => copyToast(d, f.fix!) },
|
|
{ label: t('cm.copyCode'), icon: 'tag', shortcut: f.code, onSelect: () => copyToast(d, f.code) },
|
|
]}
|
|
tags={<span className="ck-store"><StoreGlyph store={f.store === 'apple' ? 'ios' : 'android'} className="ic13" />{f.store === 'apple' ? 'App Store' : 'Google Play'}</span>}
|
|
actions={<><OpenLink url={f.url} />{f.level !== 'ok' && fixable(f.action) && <FixButton action={f.action} what={f.msg} w={w} />}</>}>
|
|
{f.fix && <span className="ck-fix"><Icon name="wand" /><code className="selectable">{f.fix}</code></span>}
|
|
</CheckCard>
|
|
);
|
|
};
|
|
return (
|
|
<div className="scr-checks">
|
|
<section className={`ck-hero tone-${state}`} aria-label={t('v2ins.cScore')}>
|
|
<div className="ck-score">
|
|
<span className={`ck-score-ic tone-bg-${state === 'neutral' ? 'info' : state}`}><Icon name={blockers ? 'alertCircle' : !checked ? 'shield' : warns ? 'alert' : 'check'} weight="fill" /></span>
|
|
<div className="ck-score-t">
|
|
<span className="ds-eyebrow">{t('v2ins.cScore')}</span>
|
|
<h2>{headline}</h2>
|
|
<p>{v.data ? t('v2ins.cScoreSub', { ok: passed, total }) : t('checks.validateSub')}{v.data ? ` · ${v.fromCache ? t('common.cachedFrom', { when: ago(v.at) }) : t('common.readWhen', { when: ago(v.at) })}` : ''}</p>
|
|
{total > 0 && <Bar value={passed / total} tone={state === 'neutral' ? 'info' : state} label={t('v2ins.cScore')} />}
|
|
</div>
|
|
</div>
|
|
<div className="ck-counts">
|
|
<span className="ck-count bad"><b className="num">{blockers}</b>{t('v2ins.cBlockers')}</span>
|
|
<span className="ck-count warn"><b className="num">{warns}</b>{t('v2ins.cWarnings')}</span>
|
|
<span className="ck-count ok"><b className="num">{passed}</b>{t('v2ins.cPassedN')}</span>
|
|
</div>
|
|
<div className="ck-hero-acts">
|
|
<ReadControl r={v} label={t('checks.run')} />
|
|
<button className="btn btn-sm" onClick={() => d.refreshPortfolio(false)} disabled={!!d.portfolioBusy}><Icon name="refresh" className={d.portfolioBusy === 'offline' ? 'spin' : undefined} />{t('checks.again')}</button>
|
|
</div>
|
|
</section>
|
|
{v.error && <ErrorNotice error={v.error} keep={!!v.data} />}
|
|
|
|
<Section title={t('v2ins.cFixFirst')} icon="alertCircle" sub={`${t('checks.offline')} · ${t('checks.validate')}`}>
|
|
<div className="ck-list">
|
|
{!e ? <Skeleton lines={3} /> : null}
|
|
{problems.map((p, i) => <CheckCard key={`p${i}`} tone="bad" icon="alertCircle" title={p}><span className="ck-sub">{t('checks.problem')}</span></CheckCard>)}
|
|
{vBlock.map(finding)}
|
|
{!v.data && !v.error && (v.loading ? <Skeleton lines={3} /> : <EmptyState icon="cloud"><b>{t('checks.validateEmpty')}</b><br />{t('checks.validateEmptyText')}</EmptyState>)}
|
|
{e && v.data && !blockers && <EmptyState icon="shield" tone="ok"><b>{t('checks.ready')}</b><br />{t('checks.readyText')}</EmptyState>}
|
|
</div>
|
|
</Section>
|
|
|
|
<ScanCard project={project} />
|
|
|
|
{(warnings.length > 0 || vWarn.length > 0) && (
|
|
<Section title={t('v2ins.cWarnings')} icon="alert" sub={t('checks.offlineSub')}>
|
|
<div className="ck-list">
|
|
{warnings.map((x, i) => <CheckCard key={`w${i}`} tone="warn" icon="alert" title={x}><span className="ck-sub">{t('checks.warning')}</span></CheckCard>)}
|
|
{vWarn.map(finding)}
|
|
</div>
|
|
</Section>
|
|
)}
|
|
|
|
{vOk.length > 0 && (
|
|
<details className="ck-passed ck-passed-top">
|
|
<summary><Icon name="check" />{t('v2ins.cPassed', { count: vOk.length })}</summary>
|
|
<div className="ck-list">{vOk.map(finding)}</div>
|
|
</details>
|
|
)}
|
|
|
|
<ReportBar project={project} />
|
|
</div>
|
|
);
|
|
}
|