TypeScript · 1702 bytes Raw Blame History
1 /** Short human relative-time ("2m ago") with a fallback for nulls. */
2 export function relativeTime(iso: string | null | undefined): string {
3 if (!iso) return "";
4 const ms = Date.now() - new Date(iso).getTime();
5 if (Number.isNaN(ms)) return "";
6 if (ms < 0) return "just now";
7 const s = Math.round(ms / 1000);
8 if (s < 45) return "just now";
9 const m = Math.round(s / 60);
10 if (m < 60) return `${m}m ago`;
11 const h = Math.round(m / 60);
12 if (h < 24) return `${h}h ago`;
13 const d = Math.round(h / 24);
14 if (d < 30) return `${d}d ago`;
15 const mo = Math.round(d / 30);
16 if (mo < 12) return `${mo}mo ago`;
17 const y = Math.round(mo / 12);
18 return `${y}y ago`;
19 }
20
21 /** Short model name — drops the "claude-" prefix. */
22 export function shortModel(model: string | null | undefined): string {
23 if (!model) return "";
24 return model.replace(/^claude-/, "");
25 }
26
27 /** Short entrypoint label for the sidebar badge. Returns null when
28 * the entrypoint is `cli` (the default — we don't bother rendering
29 * a badge for it to keep the sidebar quiet). */
30 export function shortEntrypoint(
31 entrypoint: string | null | undefined,
32 ): string | null {
33 if (!entrypoint) return null;
34 switch (entrypoint) {
35 case "cli":
36 return null;
37 case "claude-vscode":
38 return "vscode";
39 case "sdk-ts":
40 return "sdk";
41 default:
42 return entrypoint;
43 }
44 }
45
46 /** Shorten a user's home-directory absolute path to `~/rest`. */
47 export function tildeify(path: string): string {
48 // We can't read $HOME in the webview, but we can heuristically strip
49 // `/Users/<name>/` or `/home/<name>/`.
50 const m = path.match(/^\/(?:Users|home)\/[^/]+\/(.*)$/);
51 return m ? `~/${m[1]}` : path;
52 }