storebound/apps/desktop/src/lib/tray.ts

48 lines
2.8 KiB
TypeScript

// What the tray shows: one short state per app and store, the tooltip counts and the attention flag. Pure; the host
// (src-tauri/src/tray.rs) only draws it.
import type { AppStatus } from '../api';
import type { ReleasePhase } from '../../../../cli/lib/desktop-protocol';
export interface TrayApp { key: string; name: string; state: string }
export interface TrayLabels { open: string; readNow: string; apps: string; noApps: string; mute: string; unmute: string; update: string; settings: string; quit: string }
export interface TraySummary { tooltip: string; attention: number; muted: boolean; apps: TrayApp[]; labels: TrayLabels }
type Phase = ReleasePhase;
/** What matters most first: a rejection, a halt, a review, an approval, a rollout, live. */
const RANK: Record<string, number> = { rejected: 0, halted: 1, review: 2, sent: 2, approved: 3, rollout: 4, live: 5, released: 6 };
/** The release of each store that the tray reports: the one with the most pressing phase (testing / draft only when nothing else). */
export function trayPhases(s: AppStatus | undefined): { ios?: Phase; android?: Phase } {
const out: { ios?: Phase; android?: Phase } = {};
for (const r of s?.release.all ?? []) {
const cur = out[r.store];
if (!cur || (RANK[r.phase] ?? 9) < (RANK[cur] ?? 9)) out[r.store] = r.phase;
}
return out;
}
/** True for a phase that needs the owner (rejected, halted). */
export const needsYou = (p: string | undefined): boolean => p === 'rejected' || p === 'halted';
export const inReview = (p: string | undefined): boolean => p === 'review' || p === 'sent';
/**
* Builds the summary the host shows. `phaseText` gives the words of a phase, `text` the tray strings (all i18n by the
* caller). The attention count is the number of apps with a rejected or halted release; the tooltip counts apps.
*/
export function traySummary(
apps: { key: string; name: string; status: AppStatus | undefined }[], muted: boolean,
tr: { phaseText: (p: string) => string; store: (s: 'ios' | 'android') => string; tipReview: (n: number) => string; tipAttention: (n: number) => string; muted: string; none: string; labels: TrayLabels },
): TraySummary {
let review = 0, attention = 0;
const list: TrayApp[] = [];
for (const a of apps) {
const ph = trayPhases(a.status);
if (inReview(ph.ios) || inReview(ph.android)) review++;
if (needsYou(ph.ios) || needsYou(ph.android)) attention++;
const parts = (['ios', 'android'] as const).filter(s => ph[s]).map(s => `${tr.store(s)}: ${tr.phaseText(ph[s]!)}`);
list.push({ key: a.key, name: a.name, state: parts.length ? parts.join(' · ') : tr.none });
}
const bits = [review ? tr.tipReview(review) : '', attention ? tr.tipAttention(attention) : '', muted ? tr.muted : ''].filter(Boolean);
return { tooltip: ['storebound', ...bits].join(' · '), attention, muted, apps: list, labels: tr.labels };
}