160 lines
8.6 KiB
TypeScript
160 lines
8.6 KiB
TypeScript
// Shared UI pieces of the design system (styles.css): chips, store badges, app icons, KPI tiles, bars, sparklines,
|
|
// stars, notices, skeletons, empty states, segmented controls, cards.
|
|
import { type ReactNode, useId } from 'react';
|
|
import type { ReleasePhase, StoreRelease } from '../api';
|
|
import { t } from '../i18n';
|
|
import { share } from '../lib/format';
|
|
import { hueOf, monogram, type Tone } from '../lib/model';
|
|
import { Icon, type IconName, StoreGlyph } from './icons';
|
|
import { Illo, type IlloName } from './illustrations';
|
|
|
|
export function Chip({ tone = 'neutral', children, dot = true, title, icon }: { tone?: Tone | 'outline'; children: ReactNode; dot?: boolean; title?: string; icon?: IconName }) {
|
|
return <span className={`chip ${tone}`} title={title}>{icon ? <Icon name={icon} /> : dot && tone !== 'outline' && <i />}{children}</span>;
|
|
}
|
|
|
|
const PHASE_TONE: Record<ReleasePhase, Tone> = { live: 'ok', rollout: 'accent', halted: 'bad', approved: 'ok', review: 'warn', rejected: 'bad', testing: 'info', draft: 'neutral', other: 'neutral' };
|
|
|
|
/** Chip of a store release: "Live", "Rollout 20%", "In review" ... (iOS phased release: "Phased 50%", "Paused"). */
|
|
export function phaseLabel(r: StoreRelease | null | undefined): { text: string; tone: Tone } {
|
|
if (!r) return { text: t('phase.none'), tone: 'neutral' };
|
|
const pct = r.fraction !== null && r.fraction !== undefined && r.fraction < 1 ? ` ${share(r.fraction)}` : '';
|
|
if (r.phase === 'rollout') return { text: (r.store === 'ios' ? t('phase.phased') : t('phase.rollout')) + pct, tone: 'accent' };
|
|
if (r.phase === 'halted') return { text: (r.store === 'ios' ? t('phase.paused') : t('phase.halted')) + pct, tone: 'bad' };
|
|
if (r.phase === 'testing' && r.track) return { text: `${t('phase.testing')} · ${r.track}`, tone: 'info' };
|
|
return { text: t(`phase.${r.phase}`), tone: PHASE_TONE[r.phase] };
|
|
}
|
|
|
|
export function PhaseChip({ r }: { r: StoreRelease | null | undefined }) {
|
|
const p = phaseLabel(r);
|
|
return <Chip tone={p.tone} title={r?.state}>{p.text}</Chip>;
|
|
}
|
|
|
|
export const storeName = (s: 'ios' | 'android'): string => (s === 'ios' ? 'App Store' : 'Google Play');
|
|
|
|
/** "App Store · com.example.app" badge, dashed when the app is not on that store. */
|
|
export function StoreBadge({ store, id, off, children }: { store: 'ios' | 'android'; id?: string | null; off?: boolean; children?: ReactNode }) {
|
|
return (
|
|
<span className={`store-badge${off ? ' off' : ''}`} title={off ? t('app.notSetUp') : id ?? undefined}>
|
|
<StoreGlyph store={store} className={store === 'ios' ? 'glyph-ios' : 'glyph-play'} />
|
|
{storeName(store)}
|
|
{id && <><span className="sb-sep" /><span className="mono-id">{id}</span></>}
|
|
{off && <span className="muted">· {t('app.notSetUp')}</span>}
|
|
{children && <><span className="sb-sep" />{children}</>}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** The app's own icon when the project has one, else a monogram on a gradient picked from the name. */
|
|
export function AppIcon({ name, src, className = '' }: { name: string; src?: string | null; className?: string }) {
|
|
if (src) return <span className={`app-icon ${className}`}><img src={src} alt="" draggable={false} /></span>;
|
|
const h = hueOf(name);
|
|
return (
|
|
<span className={`app-icon mono ${className}`} style={{ background: `linear-gradient(135deg, hsl(${h} 72% 58%), hsl(${(h + 40) % 360} 70% 44%))` }} aria-hidden="true">
|
|
{monogram(name)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export function Kpi({ label, value, sub, icon, tone }: { label: string; value: ReactNode; sub?: ReactNode; icon?: IconName; tone?: Tone }) {
|
|
return (
|
|
<div className={`kpi${tone ? ` tone-${tone}` : ''}`}>
|
|
<div className="k-label">{icon && <Icon name={icon} />}{label}</div>
|
|
<div className="k-value">{value}</div>
|
|
{sub !== undefined && <div className="k-sub">{sub}</div>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Bar({ value, tone, thin, label }: { value: number; tone?: Tone; thin?: boolean; label?: string }) {
|
|
const v = Math.max(0, Math.min(1, value));
|
|
return (
|
|
<div className={`bar${tone && tone !== 'accent' ? ` ${tone}` : ''}${thin ? ' thin' : ''}`} role="meter" aria-valuemin={0} aria-valuemax={100} aria-valuenow={Math.round(v * 100)} aria-label={label}>
|
|
<span style={{ width: `${v * 100}%` }} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Line + area sparkline with a dashed threshold line. Values are fractions; the y range always shows the threshold. */
|
|
export function Sparkline({ values, threshold, tone = 'neutral', label }: { values: number[]; threshold?: number; tone?: Tone; label: string }) {
|
|
const W = 300, H = 72, pad = 4;
|
|
const gid = useId().replace(/:/g, '');
|
|
if (!values.length) return <div className="spark" />;
|
|
const max = Math.max(...values, (threshold ?? 0) * 1.25) * 1.08 || 1;
|
|
const x = (i: number) => (values.length === 1 ? W / 2 : (i / (values.length - 1)) * W);
|
|
const y = (v: number) => H - pad - (v / max) * (H - pad * 2);
|
|
const line = values.map((v, i) => `${i ? 'L' : 'M'}${x(i).toFixed(1)} ${y(v).toFixed(1)}`).join(' ');
|
|
const area = `${line} L${W} ${H} L0 ${H} Z`;
|
|
return (
|
|
<svg className={`spark ${tone}`} viewBox={`0 0 ${W} ${H}`} preserveAspectRatio="none" role="img" aria-label={label}>
|
|
<defs><linearGradient id={`sg${gid}`} x1="0" y1="0" x2="0" y2="1"><stop offset="0" className="stop-a" /><stop offset="1" className="stop-b" /></linearGradient></defs>
|
|
<path className="area" d={area} fill={`url(#sg${gid})`} />
|
|
{threshold !== undefined && <line className="thr" x1="0" x2={W} y1={y(threshold)} y2={y(threshold)} />}
|
|
<path className="line" d={line} />
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
export function Stars({ n, label }: { n: number; label?: string }) {
|
|
return (
|
|
<span className="stars" role="img" aria-label={label ?? t('reviews.starsAria', { count: n })}>
|
|
{[1, 2, 3, 4, 5].map(i => (
|
|
<svg key={i} viewBox="0 0 24 24" className={i <= n ? '' : 'off'} aria-hidden="true"><path fill="currentColor" d="M12 3.8l2.5 5.1 5.6.8-4 4 1 5.5-5.1-2.7-5 2.7 1-5.5-4.1-4 5.7-.8z" /></svg>
|
|
))}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export function Notice({ tone, children, icon, action }: { tone: 'bad' | 'warn' | 'ok' | 'info'; children: ReactNode; icon?: IconName; action?: ReactNode }) {
|
|
const ic: IconName = icon ?? (tone === 'bad' ? 'alertCircle' : tone === 'warn' ? 'alert' : tone === 'ok' ? 'check' : 'info');
|
|
return <div className={`notice ${tone}`} role={tone === 'bad' ? 'alert' : 'status'}><Icon name={ic} /><div className="grow">{children}</div>{action}</div>;
|
|
}
|
|
|
|
export function Skeleton({ lines = 3, block }: { lines?: number; block?: boolean }) {
|
|
return (
|
|
<div className="stack" aria-busy="true" aria-label={t('common.loading')}>
|
|
{block && <span className="sk sk-block" />}
|
|
{Array.from({ length: lines }, (_, i) => <span key={i} className="sk sk-line" style={{ width: `${[92, 76, 84, 58, 70][i % 5]}%` }} />)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Empty({ illo, title, children, actions, compact }: { illo: IlloName; title: string; children?: ReactNode; actions?: ReactNode; compact?: boolean }) {
|
|
return (
|
|
<div className={`empty${compact ? ' compact' : ''}`}>
|
|
<Illo name={illo} />
|
|
<h3>{title}</h3>
|
|
{children && <p>{children}</p>}
|
|
{actions && <div className="actions">{actions}</div>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Seg<T extends string>({ value, options, onChange, label }: { value: T; options: [T, string, IconName?][]; onChange: (v: T) => void; label: string }) {
|
|
return (
|
|
<div className="seg" role="group" aria-label={label}>
|
|
{options.map(([v, text, icon]) => <button key={v} type="button" aria-pressed={value === v} onClick={() => onChange(v)}>{icon && <Icon name={icon} />}{text}</button>)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Card({ title, icon, sub, actions, children, foot, className = '', pad = true }: { title?: ReactNode; icon?: IconName; sub?: ReactNode; actions?: ReactNode; children: ReactNode; foot?: ReactNode; className?: string; pad?: boolean }) {
|
|
return (
|
|
<section className={`card ${className}`}>
|
|
{title !== undefined && (
|
|
<div className="card-head">
|
|
<div className="stack" style={{ gap: 2 }}><h2>{icon && <Icon name={icon} />}{title}</h2>{sub && <div className="sub">{sub}</div>}</div>
|
|
{actions && <div className="actions">{actions}</div>}
|
|
</div>
|
|
)}
|
|
{pad ? <div className="card-body">{children}</div> : children}
|
|
{foot && <div className="card-foot">{foot}</div>}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
export function Kbd({ keys }: { keys: string[] }) {
|
|
return <>{keys.map(k => <span key={k} className="kbd">{k}</span>)}</>;
|
|
}
|
|
|
|
/** Platform modifier label (⌘ on macOS, Ctrl elsewhere). */
|
|
export const MOD = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform) ? '⌘' : 'Ctrl';
|