220 lines
14 KiB
TypeScript
220 lines
14 KiB
TypeScript
// Portfolio: every app on both stores. KPIs, one row per app (live version and state per store with its place on
|
|
// the release track, next version, catalog parity, config check), what needs attention, and the workspace state.
|
|
import { useEffect, useState } from 'react';
|
|
import { api, type AppStatus, type Project, type StoreRelease, type ToolResult } from '../api';
|
|
import { type Key, t } from '../i18n';
|
|
import { ago, pct, share } from '../lib/format';
|
|
import { type Attention, attention, healthSeries, type HealthSnapshot, isError, laneOf, PLAY_THRESHOLDS, STAGES, storeNow, type Tone } from '../lib/model';
|
|
import { useNav } from '../nav';
|
|
import { useData } from '../store';
|
|
import { Icon, StoreGlyph } from '../ui/icons';
|
|
import { AppIcon, Card, Chip, Empty, Kpi, Notice, PhaseChip, Skeleton, Sparkline } from '../ui/kit';
|
|
|
|
/** Tag of one app's offline config check. */
|
|
export function checkState(e: Pick<AppStatus, 'errors' | 'check'> | undefined): { text: string; tone: Tone } {
|
|
if (!e) return { text: t('check.notChecked'), tone: 'neutral' };
|
|
if (e.errors.length || !e.check) return { text: t('check.notLoaded'), tone: 'bad' };
|
|
if (e.check.problems.length) return { text: t('check.problems', { count: e.check.problems.length }), tone: 'bad' };
|
|
if (e.check.warnings.length) return { text: t('check.warnings', { count: e.check.warnings.length }), tone: 'warn' };
|
|
return { text: t('check.ok'), tone: 'ok' };
|
|
}
|
|
|
|
export function Pips({ r, store }: { r: StoreRelease | null; store: 'ios' | 'android' }) {
|
|
const lane = laneOf(store, r);
|
|
return (
|
|
<span className="pips" aria-hidden="true">
|
|
{STAGES.map((s, i) => <b key={s} className={i < lane.at ? 'on' : i === lane.at ? (lane.tone === 'neutral' ? 'on' : lane.tone === 'accent' ? 'on' : lane.tone) : ''} />)}
|
|
{lane.share !== null && lane.share < 1 && <span className="pct">{share(lane.share)}</span>}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function StoreCell({ e, store, project }: { e: AppStatus | undefined; store: 'ios' | 'android'; project: AppStatus | undefined }) {
|
|
const on = store === 'ios' ? project?.app?.platforms.ios : project?.app?.platforms.android;
|
|
if (project && !on) return <span className="muted small">{t('app.notOnStore')}</span>;
|
|
if (!e) return <span className="muted small">{t('common.unknown')}</span>;
|
|
const err = e.storeErrors[store];
|
|
if (err) return <span title={err}><Chip tone="warn">{t('portfolio.unreadable')}</Chip></span>;
|
|
const all = e.release.all ?? [e.release.current?.ios, e.release.current?.android, e.release.next?.ios, e.release.next?.android].filter((x): x is StoreRelease => !!x);
|
|
const { now, next } = storeNow(all, store);
|
|
const r = now ?? next;
|
|
if (!r) return <span className="muted small">{t('portfolio.noVersion')}</span>;
|
|
return (
|
|
<div className="mini">
|
|
<div className="row" style={{ gap: 8 }}><span className="strong num">{r.version}</span><PhaseChip r={r} /></div>
|
|
<Pips r={r} store={store} />
|
|
{now && next && <span className="small muted">{t('portfolio.thenNext', { version: next.version ?? '', phase: t(`phase.${next.phase}` as Key) })}</span>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function NextCell({ e }: { e: AppStatus | undefined }) {
|
|
const n = e?.release.next;
|
|
if (!e) return <span className="muted small">{t('common.unknown')}</span>;
|
|
if (!n) return <span className="muted small">{t('portfolio.noNext')}</span>;
|
|
return (
|
|
<div className="mini">
|
|
<span className="strong num">{n.version}</span>
|
|
<div className="row small muted" style={{ gap: 10 }}>
|
|
{n.ios && <span className="row" style={{ gap: 4 }}><StoreGlyph store="ios" className="ic13" />{t(`phase.${n.ios.phase}`)}</span>}
|
|
{n.android && <span className="row" style={{ gap: 4 }}><StoreGlyph store="android" className="ic13" />{t(`phase.${n.android.phase}`)}</span>}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Last cached health read of every app (the Health tab reads it; the portfolio only shows what is saved). */
|
|
function useCachedHealth(projects: Project[]): Record<string, { h: HealthSnapshot; at: string } | null> {
|
|
const [m, set] = useState<Record<string, { h: HealthSnapshot; at: string } | null>>({});
|
|
const key = projects.map(p => p.id).join();
|
|
useEffect(() => {
|
|
for (const p of projects) {
|
|
api.cached<ToolResult>('health', p.id).then(c => set(x => ({ ...x, [p.id]: c?.value.data ? { h: c.value.data as HealthSnapshot, at: c.at } : null })), () => {});
|
|
}
|
|
}, [key]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
return m;
|
|
}
|
|
|
|
function HealthCell({ c }: { c: { h: HealthSnapshot; at: string } | null | undefined }) {
|
|
if (!c) return <span className="muted small">{t('portfolio.healthNotRead')}</span>;
|
|
const play = c.h.play;
|
|
if (!play) return <span className="muted small">{t('portfolio.healthNoPlay')}</span>;
|
|
if (isError(play)) return <Chip tone="warn">{t('portfolio.unreadable')}</Chip>;
|
|
const s = healthSeries(play.crash, 'userPerceivedCrashRate', PLAY_THRESHOLDS.crash);
|
|
return (
|
|
<div className="mini-health" title={t('portfolio.healthTitle', { when: ago(c.at) })}>
|
|
<Sparkline values={s.values.slice(-14)} threshold={s.threshold} tone={s.tone} label={t('health.chartAria', { metric: t('health.crash'), value: pct(s.current) })} />
|
|
<span className={`num small strong ${s.tone === 'bad' ? 'tbad' : s.tone === 'warn' ? 'twarn' : ''}`}>{pct(s.current)}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function AttentionList({ items }: { items: Attention[] }) {
|
|
const nav = useNav();
|
|
if (!items.length) return <Empty compact illo="shield" title={t('portfolio.allClear')}>{t('portfolio.allClearText')}</Empty>;
|
|
return (
|
|
<div className="list">
|
|
{items.slice(0, 8).map((a, i) => (
|
|
<button key={i} className="li" onClick={() => nav.go({ screen: 'app', appId: a.projectId, tab: a.tab ?? 'checks' })}>
|
|
<span className={`li-icon tone-bg-${a.tone}`}><Icon name={a.tone === 'bad' ? 'alertCircle' : a.tone === 'warn' ? 'alert' : 'info'} /></span>
|
|
<span className="li-main"><span className="li-title">{a.text}</span><span className="li-sub">{a.app}{a.tab ? ` · ${t(`tab.${a.tab}`)}` : ''}</span></span>
|
|
<Icon name="chevronRight" className="ic14 muted" />
|
|
</button>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Portfolio() {
|
|
const d = useData();
|
|
const nav = useNav();
|
|
const open = (p: Project) => nav.go({ screen: 'app', appId: p.id, tab: 'releases' });
|
|
|
|
if (d.ready && !d.projects.length) {
|
|
return (
|
|
<div className="page">
|
|
<div className="ph"><div><h1>{t('nav.portfolio')}</h1><p>{t('portfolio.lead')}</p></div></div>
|
|
<Card pad={false}>
|
|
<Empty illo="apps" title={t('portfolio.emptyTitle')} actions={<>
|
|
<button className="btn btn-primary" onClick={() => nav.go({ screen: 'apps' })}><Icon name="folderPlus" />{t('action.addFolder')}</button>
|
|
<button className="btn" onClick={nav.openWelcome}><Icon name="sparkle" />{t('action.welcome')}</button>
|
|
</>}>{t('portfolio.emptyText')}</Empty>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const health = useCachedHealth(d.projects);
|
|
const rows = d.projects.map(p => ({ p, e: d.statusOf(p), l: d.liveOf(p) }));
|
|
const lives = rows.map(r => r.l).filter((x): x is AppStatus => !!x);
|
|
const all = lives.flatMap(l => l.release.all ?? [l.release.current?.ios, l.release.current?.android, l.release.next?.ios, l.release.next?.android].filter((x): x is StoreRelease => !!x));
|
|
const stores = rows.reduce((n, r) => n + (r.e?.app?.platforms.ios ? 1 : 0) + (r.e?.app?.platforms.android ? 1 : 0), 0);
|
|
const liveCount = lives.reduce((n, l) => n + (l.release.current?.ios ? 1 : 0) + (l.release.current?.android ? 1 : 0), 0);
|
|
const rolling = all.filter(r => r.phase === 'rollout');
|
|
const review = all.filter(r => r.phase === 'review');
|
|
const att = rows.flatMap(r => attention(r.p.id, r.e?.app?.name ?? r.p.name, r.e, r.l, health[r.p.id]?.h));
|
|
const blockers = att.filter(a => a.tone === 'bad').length;
|
|
const known = lives.length > 0;
|
|
const iosApps = rows.filter(r => r.e?.app?.platforms.ios).length, androidApps = rows.filter(r => r.e?.app?.platforms.android).length;
|
|
|
|
return (
|
|
<div className="page">
|
|
<div className="ph">
|
|
<div><h1>{t('nav.portfolio')}</h1><p>{t('portfolio.sub', { count: d.projects.length, stores })}</p></div>
|
|
<div className="actions">
|
|
<button className="btn" onClick={() => d.refreshPortfolio(false)} disabled={!!d.portfolioBusy}><Icon name="shield" />{t('action.checkConfigs')}</button>
|
|
<button className="btn btn-primary" onClick={() => d.refreshPortfolio(true)} disabled={!!d.portfolioBusy} title={t('action.readStoresHint')}>
|
|
<Icon name="refresh" className={d.portfolioBusy === 'live' ? 'spin' : undefined} />{d.portfolioBusy === 'live' ? t('action.reading') : t('action.readStores')}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{d.portfolioError && <Notice tone="bad">{d.portfolioError}</Notice>}
|
|
{!known && d.ready && <Notice tone="info" action={<button className="btn btn-sm" onClick={() => d.refreshPortfolio(true)}>{t('action.readStores')}</button>}>{t('portfolio.notReadNotice')}</Notice>}
|
|
|
|
<div className="kpis">
|
|
<Kpi icon="layers" label={t('kpi.apps')} value={d.projects.length} sub={t('kpi.appsSub', { ios: iosApps, android: androidApps })} />
|
|
<Kpi icon="check" label={t('kpi.live')} value={known ? liveCount : '–'} sub={known ? t('kpi.liveSub', { count: stores }) : t('kpi.notRead')} />
|
|
<Kpi icon="rocket" label={t('kpi.rolling')} value={known ? rolling.length : '–'} sub={!known ? t('kpi.notRead') : rolling.length ? [...new Set(rolling.map(r => r.version))].map(v => `${v}: ${rolling.filter(r => r.version === v).map(r => `${r.store === 'ios' ? 'iOS' : 'Android'}${r.fraction !== null ? ` ${share(r.fraction)}` : ''}`).join(', ')}`).join(' · ') : t('kpi.none')} />
|
|
<Kpi icon="clock" label={t('kpi.review')} value={known ? review.length : '–'} sub={!known ? t('kpi.notRead') : review.length ? review.map(r => `${r.store === 'ios' ? 'iOS' : 'Android'} ${r.version}`).join(' · ') : t('kpi.none')} tone={review.length ? 'warn' : undefined} />
|
|
<Kpi icon="alert" label={t('kpi.attention')} value={att.length} sub={blockers ? t('kpi.blockers', { count: blockers }) : t('kpi.noBlockers')} tone={blockers ? 'bad' : att.length ? 'warn' : undefined} />
|
|
</div>
|
|
|
|
<Card title={t('portfolio.apps')} icon="portfolio" sub={known ? t('portfolio.appsSub', { when: ago(d.live?.at) }) : t('portfolio.appsSubOffline')} pad={false}
|
|
actions={<div className="legend-row"><span><span className="pips"><b className="on" /></span>{t('legend.done')}</span><span>{t('legend.stages')}</span></div>}>
|
|
<div className="table-wrap">
|
|
<table className="table">
|
|
<thead><tr>
|
|
<th>{t('col.app')}</th><th>App Store</th><th>Google Play</th><th>{t('col.next')}</th><th>{t('col.crash')}</th><th>{t('col.catalog')}</th><th className="right">{t('col.config')}</th>
|
|
</tr></thead>
|
|
<tbody>
|
|
{rows.map(({ p, e, l }) => {
|
|
const st = checkState(e);
|
|
const cat = l?.catalog ?? e?.catalog;
|
|
return (
|
|
<tr key={p.id} className="clickable" onClick={() => open(p)}>
|
|
<td>
|
|
<div className="cell-app">
|
|
<AppIcon name={e?.app?.name ?? p.name} src={d.icons[p.id]} />
|
|
<div className="t">
|
|
<button className="link n" onClick={ev => { ev.stopPropagation(); open(p); }}>{e?.app?.name ?? p.name}</button>
|
|
<div className="s mono-id">{e?.app?.platforms.ios?.bundleId ?? e?.app?.platforms.android?.packageName ?? ''}</div>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
<td>{e ? <StoreCell e={l} store="ios" project={e} /> : <Skeleton lines={2} />}</td>
|
|
<td>{e ? <StoreCell e={l} store="android" project={e} /> : <Skeleton lines={2} />}</td>
|
|
<td><NextCell e={l} /></td>
|
|
<td><HealthCell c={health[p.id]} /></td>
|
|
<td>
|
|
{cat ? (
|
|
<div className="mini">
|
|
<span className="num small strong">{t('portfolio.products', { count: cat.products })} · {t('portfolio.subs', { count: cat.subscriptions })}</span>
|
|
{cat.checked === 'live' ? (cat.ok ? <Chip tone="ok">{t('portfolio.inParity')}</Chip> : <Chip tone="warn">{t('portfolio.differs', { count: cat.missingIos.length + cat.missingAndroid.length + cat.onlyIos.length + cat.onlyAndroid.length })}</Chip>)
|
|
: <span className="small muted">{t('portfolio.configOnly')}</span>}
|
|
</div>
|
|
) : <span className="muted small">–</span>}
|
|
</td>
|
|
<td className="right"><Chip tone={st.tone}>{st.text}</Chip></td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</Card>
|
|
|
|
<div className="grid g-main-side">
|
|
<Card title={t('portfolio.attention')} icon="alert" sub={t('portfolio.attentionSub')} pad={false}><AttentionList items={att} /></Card>
|
|
<Card title={t('portfolio.workspace')} icon="settings">
|
|
<dl className="kv">
|
|
<dt>{t('nav.accounts')}</dt><dd>{d.accounts.length ? t('portfolio.accounts', { play: d.accounts.filter(a => a.kind === 'play').length, apple: d.accounts.filter(a => a.kind === 'apple').length }) : <button className="link" onClick={() => nav.go({ screen: 'accounts' })}>{t('action.addAccount')}</button>}</dd>
|
|
<dt>{t('portfolio.storesRead')}</dt><dd>{d.live ? ago(d.live.at) : t('time.never')}</dd>
|
|
<dt>{t('portfolio.tools')}</dt><dd>{d.sidecar?.tools ? t('portfolio.toolsFound', { found: d.sidecar.tools.tools.filter(x => x.found).length, total: d.sidecar.tools.tools.length }) : '–'}</dd>
|
|
<dt>CLI</dt><dd>{d.sidecar ? `storebound ${d.sidecar.version}` : d.sidecarError ?? '–'}</dd>
|
|
</dl>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|