sync-outcome.ts

3.9 KB · raw

1// Why a sync run ended, in a form both the service worker and the popup can2// name. Before this existed, "no eligible host", "runner not wired" and3// "synced fine, nothing to do" were all a bare `return` — indistinguishable4// to the user and to us. A failed sync and a successful no-op must never5// look the same again.67export type SyncOutcome =8  | "completed_changes"9  | "completed_no_changes"10  | "no_paired_devices"11  | "no_eligible_host"12  | "runner_unavailable"13  | "sync_disabled"14  | "private_relay_unset"15  | "vault_locked"16  | "relay_unreachable"17  | "handshake_failed"18  | "vault_id_mismatch"19  | "sync_timeout"20  | "run_failed";2122/** Outcomes where sync did not do what the user asked, so the UI shows red. */23const FAILURES: ReadonlySet<SyncOutcome> = new Set<SyncOutcome>([24  "no_eligible_host",25  "runner_unavailable",26  "private_relay_unset",27  "vault_locked",28  "relay_unreachable",29  "handshake_failed",30  "vault_id_mismatch",31  "sync_timeout",32  "run_failed",33]);3435export function isSyncFailure(outcome: SyncOutcome): boolean {36  return FAILURES.has(outcome);37}3839export interface SyncOutcomeSummary {40  status?: "idle" | "syncing" | "error";41  outcome?: SyncOutcome;42  entriesSent?: number;43  entriesReceived?: number;44  detail?: string;45}4647/**48 * The one indicator light. `busy` pulses green, `ok` is solid green, `error`49 * is red, `idle` is unlit. Nothing that is actually broken may resolve to50 * `ok` — a paired-but-unsyncable device is red, not green.51 */52export type SyncIndicator = "busy" | "ok" | "error" | "idle";5354export function syncIndicator(s: SyncOutcomeSummary, pairedCount: number): SyncIndicator {55  if (s.status === "syncing") return "busy";56  if (s.outcome && isSyncFailure(s.outcome)) return "error";57  if (s.outcome === "completed_changes" || s.outcome === "completed_no_changes") {58    return "ok";59  }60  // Paired with a healthy history but no attempt since this SW woke: still a61  // successful pair, which is what the light is claiming.62  if (pairedCount > 0 && s.outcome === undefined) return "ok";63  return "idle";64}6566const INDICATOR_LABEL: Record<SyncIndicator, string> = {67  busy: "Syncing",68  ok: "Sync healthy",69  error: "Sync problem",70  idle: "Not syncing",71};7273/** Colour alone is never the signal — this is the same claim in words. */74export function syncIndicatorLabel(75  indicator: SyncIndicator,76  s: SyncOutcomeSummary,77): string {78  return `${INDICATOR_LABEL[indicator]} — ${describeSyncOutcome(s)}`;79}8081const COPY: Record<SyncOutcome, string> = {82  completed_changes: "Synced",83  completed_no_changes: "Up to date — nothing to send or receive",84  no_paired_devices: "No devices paired yet",85  no_eligible_host: "Paired, but no device here can accept a sync",86  runner_unavailable: "Sync isn't running in the background — reload Peach",87  sync_disabled: "Sync is switched off",88  private_relay_unset: "Private relay chosen but no address set",89  vault_locked: "Vault was locked — unlock and sync again",90  relay_unreachable: "Couldn't reach the relay",91  handshake_failed: "Reached the relay, but the device didn't answer",92  vault_id_mismatch: "That device holds a different vault",93  sync_timeout: "The device stopped responding mid-sync",94  run_failed: "Sync failed",95};9697/**98 * One plain-English line for a sync outcome. `detail` — the raw internal code99 * or the device inventory behind `no_eligible_host` — is appended rather than100 * hidden: it is the whole diagnostic on a real device, where devtools is not101 * an option.102 */103export function describeSyncOutcome(s: SyncOutcomeSummary): string {104  if (!s.outcome) return "Not synced yet";105  const base = COPY[s.outcome] ?? "Sync failed";106  if (s.outcome === "completed_changes") {107    const moved = [108      (s.entriesSent ?? 0) > 0 ? `${s.entriesSent} sent` : null,109      (s.entriesReceived ?? 0) > 0 ? `${s.entriesReceived} received` : null,110    ].filter(Boolean);111    return moved.length > 0 ? `${base} — ${moved.join(", ")}` : base;112  }113  return s.detail ? `${base} (${s.detail})` : base;114}