1234567export 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 | "connection_lost"21 | "run_failed";2223242526272829303132const FAILURES: ReadonlySet<SyncOutcome> = new Set<SyncOutcome>([33 "no_eligible_host",34 "runner_unavailable",35 "private_relay_unset",36 "vault_id_mismatch",37 "run_failed",38]);394041424344const WARNINGS: ReadonlySet<SyncOutcome> = new Set<SyncOutcome>([45 "relay_unreachable",46 "handshake_failed",47 "sync_timeout",48 "connection_lost",49 50 51 52 53 "vault_locked",54]);55565758596061626364656667export function isConnectionClosedError(err: unknown): boolean {68 const message = err instanceof Error ? err.message : String(err);69 return /websocket closed|connection closed/i.test(message);70}7172export function isSyncFailure(outcome: SyncOutcome): boolean {73 return FAILURES.has(outcome);74}7576export function isSyncWarning(outcome: SyncOutcome): boolean {77 return WARNINGS.has(outcome);78}7980export interface SyncOutcomeSummary {81 status?: "idle" | "syncing" | "error";82 outcome?: SyncOutcome;83 entriesSent?: number;84 entriesReceived?: number;85 detail?: string;86 87 finishedAt?: number;88}89909192939495969798export type SyncIndicator = "busy" | "ok" | "warn" | "error" | "idle";99100export function syncIndicator(s: SyncOutcomeSummary, pairedCount: number): SyncIndicator {101 if (s.status === "syncing") return "busy";102 if (s.outcome && isSyncFailure(s.outcome)) return "error";103 if (s.outcome && isSyncWarning(s.outcome)) return "warn";104 if (s.outcome === "completed_changes" || s.outcome === "completed_no_changes") {105 return "ok";106 }107 108 109 if (pairedCount > 0 && s.outcome === undefined) return "ok";110 return "idle";111}112113const INDICATOR_LABEL: Record<SyncIndicator, string> = {114 busy: "Syncing",115 ok: "Sync healthy",116 warn: "Last sync attempt missed — will retry",117 error: "Sync problem",118 idle: "Not syncing",119};120121122export function syncIndicatorLabel(123 indicator: SyncIndicator,124 s: SyncOutcomeSummary,125): string {126 return `${INDICATOR_LABEL[indicator]} — ${describeSyncOutcome(s)}`;127}128129const COPY: Record<SyncOutcome, string> = {130 completed_changes: "Synced",131 completed_no_changes: "Up to date — nothing to send or receive",132 no_paired_devices: "No devices paired yet",133 no_eligible_host: "Paired, but no device here can accept a sync",134 runner_unavailable: "Sync isn't running in the background — reload Peach",135 sync_disabled: "Sync is switched off",136 private_relay_unset: "Private relay chosen but no address set",137 vault_locked: "Vault was locked — unlock and sync again",138 relay_unreachable: "Couldn't reach the relay",139 handshake_failed: "Reached the relay, but the device didn't answer",140 vault_id_mismatch: "That device holds a different vault",141 sync_timeout: "The device stopped responding mid-sync",142 connection_lost: "The connection dropped mid-sync — will retry",143 run_failed: "Sync failed",144};145146147148149150151152export function describeSyncOutcome(s: SyncOutcomeSummary): string {153 if (!s.outcome) return "Not synced yet";154 const base = COPY[s.outcome] ?? "Sync failed";155 if (s.outcome === "completed_changes") {156 const moved = [157 (s.entriesSent ?? 0) > 0 ? `${s.entriesSent} sent` : null,158 (s.entriesReceived ?? 0) > 0 ? `${s.entriesReceived} received` : null,159 ].filter(Boolean);160 return moved.length > 0 ? `${base} — ${moved.join(", ")}` : base;161 }162 return s.detail ? `${base} (${s.detail})` : base;163}