diff options
| author | Adam Malczewski <[email protected]> | 2026-06-28 15:29:55 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-28 15:29:55 +0900 |
| commit | 12cf599378bab3df6cb688fac1288fa7414b389a (patch) | |
| tree | ff4a6260cd7c2720abed097fe3c434193b2c421c /src/features/workspaces/logic/view-model.ts | |
| parent | 4373761a75b7b8b56ba6f3c8c942a13acd18e7c5 (diff) | |
| parent | 9d18dcfb84d5b5ae03e6be16b5e5af92bf10d530 (diff) | |
| download | dispatch-web-12cf599378bab3df6cb688fac1288fa7414b389a.tar.gz dispatch-web-12cf599378bab3df6cb688fac1288fa7414b389a.zip | |
Merge branch 'feature/workspace-star' into predev
# Conflicts:
# backend-handoff.md
# src/features/workspaces/ui/WorkspaceCard.test.ts
Diffstat (limited to 'src/features/workspaces/logic/view-model.ts')
| -rw-r--r-- | src/features/workspaces/logic/view-model.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/features/workspaces/logic/view-model.ts b/src/features/workspaces/logic/view-model.ts index 6dd64c6..b994b7c 100644 --- a/src/features/workspaces/logic/view-model.ts +++ b/src/features/workspaces/logic/view-model.ts @@ -20,6 +20,36 @@ export function pageTitle(route: Route, workspaces: readonly WorkspaceEntry[]): } /** + * 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)); +} + +/** * 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". |
