summaryrefslogtreecommitdiffhomepage
path: root/src/features/workspaces/logic/view-model.ts
blob: cdf99c86193d605c3d29fca3aec80d4b9b911bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Pure view-model helpers for the workspaces feature — zero DOM, zero effects.
 */

import type { WorkspaceEntry } from "@dispatch/wire";
import type { Route } from "./route";

/**
 * A home tile's color tone. Windows 8's Start screen gave every tile its own
 * saturated color; here the tile's workspace id DETERMINISTICALLY picks one of
 * the theme's six semantic tones, so the same workspace keeps its color across
 * sessions and themes (the tone name maps to a DaisyUI color, which adapts to
 * the active theme). Starred stays a separate visual concern (a badge), NOT a
 * tone — any tile may be starred regardless of its color.
 */
export type TileTone = "primary" | "secondary" | "accent" | "info" | "success" | "warning";

/** The tones a tile may take, in hash order. */
const TILE_TONES: readonly TileTone[] = [
  "primary",
  "secondary",
  "accent",
  "info",
  "success",
  "warning",
];

/**
 * Map a workspace id to its tile tone. Deterministic (FNV-1a hash of the id)
 * and uniformly distributed across the six tones — pure: id in, tone out.
 */
export function tileTone(id: string): TileTone {
  let hash = 0x811c9dc5;
  for (let index = 0; index < id.length; index++) {
    hash ^= id.charCodeAt(index);
    // FNV prime (16777619), kept in 32-bit unsigned range.
    hash = Math.imul(hash, 0x01000193) >>> 0;
  }
  return TILE_TONES[hash % TILE_TONES.length] ?? "primary";
}

/**
 * The browser tab / page (`document.title`) text for a route. The home route
 * (`/`) is "Dispatch"; a workspace route (`/<id>`) is "Dispatch: {title}",
 * using the workspace's display title and falling back to the URL slug (`id`)
 * until the workspace list has loaded it — the backend defaults a workspace's
 * title to its id, so the slug is the correct transient value. Pure: route +
 * workspaces in, string out.
 */
export function pageTitle(route: Route, workspaces: readonly WorkspaceEntry[]): string {
  if (route.kind === "home") return "Dispatch";
  const ws = workspaces.find((w) => w.id === route.id);
  return `Dispatch: ${ws?.title ?? route.id}`;
}

/**
 * Sort workspaces for display: starred first, then most-recently-active. Pure:
 * the list in, a NEW sorted array out (the input is not mutated). Starred
 * workspaces jump to the top (the FE-side echo of their concurrency-priority);
 * within each group (starred / not) `lastActivityAt` desc breaks ties, matching
 * the backend's list ordering. Stable for equal `lastActivityAt`.
 */
export function sortWorkspaces<T extends WorkspaceEntry>(workspaces: readonly T[]): T[] {
  return [...workspaces].sort((a, b) => {
    if (a.starred !== b.starred) return a.starred ? -1 : 1;
    return b.lastActivityAt - a.lastActivityAt;
  });
}

/**
 * Return a NEW list with the one workspace's `starred` flag set (immutably —
 * the entry is replaced, the rest keep their identity). Pure: the optimistic
 * star/unstar transformation shared by the apply + the error revert. A missing
 * `id` (not yet in the list — e.g. starring a workspace the home view hasn't
 * loaded) leaves the list unchanged; the backend's create-on-miss still applies
 * server-side and a subsequent refresh reconciles.
 */
export function applyStarred<T extends WorkspaceEntry>(
  workspaces: readonly T[],
  id: string,
  starred: boolean,
): T[] {
  return workspaces.map((w) => (w.id === id ? { ...w, starred } : w));
}

/**
 * Return a NEW list with the one workspace's `archived` flag set (immutably —
 * the entry is replaced, the rest keep their identity). Pure: the optimistic
 * archive/unarchive transformation shared by the apply + the error revert. A
 * missing `id` leaves the list unchanged; a subsequent refresh reconciles.
 */
export function applyArchived<T extends WorkspaceEntry>(
  workspaces: readonly T[],
  id: string,
  archived: boolean,
): T[] {
  return workspaces.map((w) => (w.id === id ? { ...w, archived } : w));
}

/**
 * Format an epoch-ms timestamp as a short relative string ("now", "3m", "2h",
 * "5d", or a date). Pure: `now` + `then` in, string out. Future timestamps
 * (a workspace just created) read as "now".
 */
export function relativeTime(then: number, now: number): string {
  const diff = now - then;
  if (diff < 60_000) return "now";
  const mins = Math.floor(diff / 60_000);
  if (mins < 60) return `${mins}m`;
  const hours = Math.floor(mins / 60);
  if (hours < 24) return `${hours}h`;
  const days = Math.floor(hours / 24);
  if (days < 30) return `${days}d`;
  // Past a month: months (a month ≈ 30 days). No short date — the label
  // stays relative forever.
  return `${Math.floor(days / 30)}mo`;
}