@koinzhang/paseo-plugin-activity 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -2
- package/README.md +3 -3
- package/client/activity-heatmap.tsx +17 -25
- package/client/agent-creations.tsx +207 -0
- package/client/agent-directory.test.ts +191 -0
- package/client/agent-directory.ts +150 -0
- package/client/attention-pill.tsx +17 -105
- package/client/color-mix.test.ts +27 -0
- package/client/color-mix.ts +38 -0
- package/client/fit-text.test.ts +41 -0
- package/client/fit-text.ts +33 -0
- package/client/global-surface.tsx +104 -31
- package/client/hourly-activity-timeline.tsx +392 -0
- package/client/measured-width.ts +72 -0
- package/client/pill-directory.ts +20 -68
- package/client/range.test.ts +44 -0
- package/client/range.ts +14 -0
- package/client/rank-color.test.ts +108 -14
- package/client/rank-color.ts +261 -48
- package/client/usage-stats.tsx +116 -5
- package/client/use-agent-turn-end.ts +19 -2
- package/client/use-workspace-agent-statuses.ts +18 -42
- package/client/workspace/constants.ts +2 -0
- package/client/workspace/host-archive-sync.test.ts +46 -0
- package/client/workspace/host-archive-sync.ts +15 -0
- package/client/workspace/list-host-agents.test.ts +11 -63
- package/client/workspace/list-host-agents.ts +4 -58
- package/client/workspace/panel.tsx +99 -12
- package/client/workspace/rank-section.tsx +150 -11
- package/index.server.ts +30 -0
- package/package.json +3 -3
- package/paseo-plugin.json +1 -1
- package/server/background-sync.test.ts +43 -0
- package/server/background-sync.ts +9 -0
- package/server/handlers-query.test.ts +225 -0
- package/server/handlers.ts +117 -0
- package/shared/activity.test.ts +123 -6
- package/shared/activity.ts +103 -5
- package/shared/classify.test.ts +10 -0
- package/shared/classify.ts +10 -0
- package/shared/explorer-agent-display.test.ts +61 -0
- package/shared/explorer-agent-display.ts +21 -2
- package/shared/format.test.ts +28 -3
- package/shared/format.ts +22 -2
- package/shared/insights.test.ts +113 -70
- package/shared/insights.ts +53 -39
- package/shared/usage.test.ts +209 -0
- package/shared/usage.ts +283 -1
- package/client/pill-directory.test.ts +0 -92
- package/client/workspace/agent-live-status-sync.tsx +0 -103
- package/client/workspace/status-cache.test.ts +0 -62
- package/client/workspace/status-cache.ts +0 -23
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
|
+
|
|
3
|
+
export type AgentsApi = PluginClientContext["paseo"]["agents"];
|
|
4
|
+
export type DirectoryAgent = Awaited<ReturnType<AgentsApi["list"]>>["entries"][number]["agent"];
|
|
5
|
+
type ListResult = Awaited<ReturnType<AgentsApi["list"]>>;
|
|
6
|
+
export type DirectoryUpdate = Parameters<Parameters<AgentsApi["subscribe"]>[0]>[0];
|
|
7
|
+
export type DirectoryListener = (
|
|
8
|
+
agents: ReadonlyMap<string, DirectoryAgent>,
|
|
9
|
+
update?: DirectoryUpdate,
|
|
10
|
+
) => void;
|
|
11
|
+
|
|
12
|
+
const directories = new WeakMap<AgentsApi, {
|
|
13
|
+
listen(listener: DirectoryListener): () => void;
|
|
14
|
+
}>();
|
|
15
|
+
|
|
16
|
+
/** One owned beta.2 observation per API, shared only by that API's consumers. */
|
|
17
|
+
export function watchAgentDirectory(
|
|
18
|
+
agents: AgentsApi,
|
|
19
|
+
listener: DirectoryListener,
|
|
20
|
+
intervalMs = 15_000,
|
|
21
|
+
): () => void {
|
|
22
|
+
let directory = directories.get(agents);
|
|
23
|
+
if (!directory) {
|
|
24
|
+
const listeners = new Set<DirectoryListener>();
|
|
25
|
+
let snapshot: Map<string, DirectoryAgent> | undefined;
|
|
26
|
+
let disposed = false;
|
|
27
|
+
let connecting = false;
|
|
28
|
+
let reading = false;
|
|
29
|
+
let generation = 0;
|
|
30
|
+
let pending: DirectoryUpdate[] = [];
|
|
31
|
+
const lifetime = new AbortController();
|
|
32
|
+
let controller: AbortController | undefined;
|
|
33
|
+
let release: (() => Promise<void>) | undefined;
|
|
34
|
+
const report = (error: unknown) => console.error("[activity] agent directory failed", error);
|
|
35
|
+
const safeRelease = () => {
|
|
36
|
+
const stop = release;
|
|
37
|
+
release = undefined;
|
|
38
|
+
if (stop) void stop().catch(report);
|
|
39
|
+
};
|
|
40
|
+
const publish = (update?: DirectoryUpdate) => {
|
|
41
|
+
if (disposed || !snapshot) return;
|
|
42
|
+
for (const callback of listeners) {
|
|
43
|
+
try { callback(snapshot, update); } catch (error) { report(error); }
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const apply = (map: Map<string, DirectoryAgent>, update: DirectoryUpdate) => {
|
|
47
|
+
if (update.kind === "remove") map.delete(update.agentId);
|
|
48
|
+
else map.set(update.agent.id, update.agent);
|
|
49
|
+
};
|
|
50
|
+
async function read(first?: ListResult) {
|
|
51
|
+
const run = ++generation;
|
|
52
|
+
const signal = lifetime.signal;
|
|
53
|
+
reading = true;
|
|
54
|
+
pending = [];
|
|
55
|
+
try {
|
|
56
|
+
let page = first ?? await agents.list({
|
|
57
|
+
filter: { includeArchived: true }, page: { limit: 200 }, signal,
|
|
58
|
+
});
|
|
59
|
+
const next = new Map<string, DirectoryAgent>();
|
|
60
|
+
for (let pages = 0; ; pages++) {
|
|
61
|
+
if (disposed || run !== generation) return;
|
|
62
|
+
for (const { agent } of page.entries) next.set(agent.id, agent);
|
|
63
|
+
if (!page.pageInfo.hasMore) break;
|
|
64
|
+
if (!page.pageInfo.nextCursor || pages >= 99) throw new Error("Incomplete agent directory pagination");
|
|
65
|
+
page = await agents.list({
|
|
66
|
+
filter: { includeArchived: true },
|
|
67
|
+
page: { limit: 200, cursor: page.pageInfo.nextCursor }, signal,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
for (const update of pending) apply(next, update);
|
|
71
|
+
snapshot = next;
|
|
72
|
+
publish();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (!disposed && run === generation) report(error);
|
|
75
|
+
} finally {
|
|
76
|
+
if (run === generation) { reading = false; pending = []; }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function connect() {
|
|
80
|
+
if (disposed || connecting || release) return;
|
|
81
|
+
connecting = true;
|
|
82
|
+
controller = new AbortController();
|
|
83
|
+
try {
|
|
84
|
+
const result = await agents.list({
|
|
85
|
+
filter: { includeArchived: true }, page: { limit: 200 },
|
|
86
|
+
subscribe: {}, signal: controller.signal,
|
|
87
|
+
});
|
|
88
|
+
if (disposed) {
|
|
89
|
+
await result.subscription.release();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
release = () => result.subscription.release();
|
|
93
|
+
result.subscription.subscribe({
|
|
94
|
+
// Called immediately and again after reconnect. Rebuild all pages each time.
|
|
95
|
+
snapshot: (page) => { void read(page); },
|
|
96
|
+
update: (message) => {
|
|
97
|
+
if (disposed || message.type !== "agent_update") return;
|
|
98
|
+
const update = message.payload;
|
|
99
|
+
if (reading) pending.push(update);
|
|
100
|
+
if (snapshot) {
|
|
101
|
+
snapshot = new Map(snapshot);
|
|
102
|
+
apply(snapshot, update);
|
|
103
|
+
publish(update);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
error: (error) => {
|
|
107
|
+
if (disposed) return;
|
|
108
|
+
report(error);
|
|
109
|
+
++generation;
|
|
110
|
+
reading = false;
|
|
111
|
+
controller?.abort();
|
|
112
|
+
safeRelease();
|
|
113
|
+
void read();
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (!disposed) {
|
|
118
|
+
report(error);
|
|
119
|
+
await read();
|
|
120
|
+
}
|
|
121
|
+
} finally {
|
|
122
|
+
connecting = false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const timer = setInterval(() => {
|
|
126
|
+
if (!release) void connect();
|
|
127
|
+
else if (!reading) void read();
|
|
128
|
+
}, intervalMs);
|
|
129
|
+
directory = {
|
|
130
|
+
listen(callback) {
|
|
131
|
+
listeners.add(callback);
|
|
132
|
+
if (snapshot) callback(snapshot);
|
|
133
|
+
return () => {
|
|
134
|
+
listeners.delete(callback);
|
|
135
|
+
if (listeners.size !== 0 || disposed) return;
|
|
136
|
+
disposed = true;
|
|
137
|
+
++generation;
|
|
138
|
+
clearInterval(timer);
|
|
139
|
+
lifetime.abort();
|
|
140
|
+
controller?.abort();
|
|
141
|
+
safeRelease();
|
|
142
|
+
directories.delete(agents);
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
directories.set(agents, directory);
|
|
147
|
+
void connect();
|
|
148
|
+
}
|
|
149
|
+
return directory.listen(listener);
|
|
150
|
+
}
|
|
@@ -18,18 +18,12 @@ import {
|
|
|
18
18
|
publishAttentionStatuses,
|
|
19
19
|
useAttentionStatuses,
|
|
20
20
|
} from "./attention-status-store.ts";
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
applyAgentStatusUpdate,
|
|
24
|
-
loadWorkspaceAgentStatuses,
|
|
25
|
-
type HostAgentUpdate,
|
|
26
|
-
} from "./workspace/list-host-agents.ts";
|
|
21
|
+
import { watchAgentDirectory } from "./agent-directory.ts";
|
|
22
|
+
import { agentStatusInfo } from "./workspace/list-host-agents.ts";
|
|
27
23
|
import type { AgentStatusInfo } from "./workspace/constants.ts";
|
|
28
24
|
|
|
29
25
|
const TITLE = "Needs attention";
|
|
30
26
|
const PILL_ID = "attention";
|
|
31
|
-
/** Completeness backup only — live path is agents.subscribe → store → icon. */
|
|
32
|
-
const SYNC_MS = 15_000;
|
|
33
27
|
|
|
34
28
|
type PillEntry = {
|
|
35
29
|
registration?: PluginButtonRegistration;
|
|
@@ -45,25 +39,9 @@ function attentionSignature(
|
|
|
45
39
|
return items.map((i) => `${i.agentId}:${i.kind}:${i.permissionCount}`).join("|");
|
|
46
40
|
}
|
|
47
41
|
|
|
48
|
-
/**
|
|
49
|
-
* Composer pill for other same-workspace agents needing attention.
|
|
50
|
-
*
|
|
51
|
-
* Live updates: `agents.subscribe` → in-memory store → pill visibility + icon tint.
|
|
52
|
-
* 15s poll is completeness only. Icon reads the same store (not a separate 15s query).
|
|
53
|
-
*/
|
|
42
|
+
/** Entry-owned directory drives pill registrations and attention, with 15s reconciliation. */
|
|
54
43
|
export function contributeAttentionPills(client: PluginClientContext): () => void {
|
|
55
44
|
const pills = new Map<string, PillEntry>();
|
|
56
|
-
const inflight = new Map<string, Promise<void>>();
|
|
57
|
-
let disposed = false;
|
|
58
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
59
|
-
|
|
60
|
-
function setWorkspacePillsDisabled(workspaceId: string, disabled: boolean) {
|
|
61
|
-
for (const entry of pills.values()) {
|
|
62
|
-
if (entry.workspaceId !== workspaceId) continue;
|
|
63
|
-
entry.registration?.update({ disabled });
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
45
|
function applyPill(
|
|
68
46
|
agentId: string,
|
|
69
47
|
entry: PillEntry,
|
|
@@ -103,43 +81,6 @@ export function contributeAttentionPills(client: PluginClientContext): () => voi
|
|
|
103
81
|
}
|
|
104
82
|
}
|
|
105
83
|
|
|
106
|
-
function syncWorkspace(workspaceId: string): Promise<void> {
|
|
107
|
-
const existing = inflight.get(workspaceId);
|
|
108
|
-
if (existing) return existing;
|
|
109
|
-
const firstLoad = getAttentionStatuses(workspaceId) == null;
|
|
110
|
-
// Only block clicks on the cold first fetch — background polls stay interactive.
|
|
111
|
-
if (firstLoad) setWorkspacePillsDisabled(workspaceId, true);
|
|
112
|
-
const run = (async () => {
|
|
113
|
-
try {
|
|
114
|
-
const statuses = await loadWorkspaceAgentStatuses(client.paseo, workspaceId, null);
|
|
115
|
-
if (disposed) return;
|
|
116
|
-
applyWorkspace(workspaceId, statuses);
|
|
117
|
-
} catch (error) {
|
|
118
|
-
if (!disposed) console.error("[activity] attention pill sync failed", workspaceId, error);
|
|
119
|
-
} finally {
|
|
120
|
-
inflight.delete(workspaceId);
|
|
121
|
-
if (!disposed && firstLoad) setWorkspacePillsDisabled(workspaceId, false);
|
|
122
|
-
}
|
|
123
|
-
})();
|
|
124
|
-
inflight.set(workspaceId, run);
|
|
125
|
-
return run;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function syncAllWorkspaces() {
|
|
129
|
-
const ids = new Set<string>();
|
|
130
|
-
for (const entry of pills.values()) ids.add(entry.workspaceId);
|
|
131
|
-
for (const workspaceId of ids) void syncWorkspace(workspaceId);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function scheduleSync() {
|
|
135
|
-
clearTimeout(timer);
|
|
136
|
-
if (disposed) return;
|
|
137
|
-
timer = setTimeout(() => {
|
|
138
|
-
syncAllWorkspaces();
|
|
139
|
-
scheduleSync();
|
|
140
|
-
}, SYNC_MS);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
84
|
function createPillIcon(agentId: string) {
|
|
144
85
|
return function AttentionPillIcon({
|
|
145
86
|
size,
|
|
@@ -189,7 +130,6 @@ export function contributeAttentionPills(client: PluginClientContext): () => voi
|
|
|
189
130
|
pills.set(agentId, entry);
|
|
190
131
|
const cached = getAttentionStatuses(workspaceId);
|
|
191
132
|
if (cached) applyPill(agentId, entry, cached);
|
|
192
|
-
else void syncWorkspace(workspaceId);
|
|
193
133
|
}
|
|
194
134
|
|
|
195
135
|
function removePill(agentId: string) {
|
|
@@ -197,53 +137,25 @@ export function contributeAttentionPills(client: PluginClientContext): () => voi
|
|
|
197
137
|
pills.delete(agentId);
|
|
198
138
|
}
|
|
199
139
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const map = getAttentionStatuses(workspaceId);
|
|
207
|
-
if (!map || !(update.agentId in map)) continue;
|
|
208
|
-
const next = { ...map };
|
|
209
|
-
delete next[update.agentId];
|
|
210
|
-
applyWorkspace(workspaceId, next);
|
|
211
|
-
}
|
|
212
|
-
return;
|
|
140
|
+
const stopDirectory = watchAgentDirectory(client.paseo.agents, (snapshot) => {
|
|
141
|
+
const workspaces = new Map<string, Record<string, AgentStatusInfo>>();
|
|
142
|
+
for (const [id, entry] of pills) {
|
|
143
|
+
const agent = snapshot.get(id);
|
|
144
|
+
if (!agent || agent.archivedAt || agent.workspaceId !== entry.workspaceId) removePill(id);
|
|
145
|
+
workspaces.set(entry.workspaceId, {});
|
|
213
146
|
}
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
applyAgentStatusUpdate(next, workspaceId, update);
|
|
221
|
-
applyWorkspace(workspaceId, next);
|
|
222
|
-
void syncWorkspace(workspaceId);
|
|
223
|
-
return;
|
|
147
|
+
for (const agent of snapshot.values()) {
|
|
148
|
+
if (agent.archivedAt || !agent.workspaceId) continue;
|
|
149
|
+
const statuses = workspaces.get(agent.workspaceId) ?? {};
|
|
150
|
+
statuses[agent.id] = agentStatusInfo(agent);
|
|
151
|
+
workspaces.set(agent.workspaceId, statuses);
|
|
152
|
+
addPill(agent.id, agent.workspaceId);
|
|
224
153
|
}
|
|
225
|
-
const
|
|
226
|
-
applyAgentStatusUpdate(next, workspaceId, update);
|
|
227
|
-
applyWorkspace(workspaceId, next);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const unsubscribeDir = watchPillDirectory(client.paseo.agents, (agent) => {
|
|
231
|
-
const { id, workspaceId } = agent;
|
|
232
|
-
if (!workspaceId) return;
|
|
233
|
-
addPill(id, workspaceId);
|
|
234
|
-
}, removePill);
|
|
235
|
-
|
|
236
|
-
const unsubscribePush = client.paseo.agents.subscribe((update) => {
|
|
237
|
-
onHostUpdate(update as HostAgentUpdate);
|
|
154
|
+
for (const [workspaceId, statuses] of workspaces) applyWorkspace(workspaceId, statuses);
|
|
238
155
|
});
|
|
239
156
|
|
|
240
|
-
scheduleSync();
|
|
241
|
-
|
|
242
157
|
return () => {
|
|
243
|
-
|
|
244
|
-
clearTimeout(timer);
|
|
245
|
-
unsubscribeDir();
|
|
246
|
-
unsubscribePush();
|
|
158
|
+
stopDirectory();
|
|
247
159
|
pills.forEach((entry) => entry.registration?.remove());
|
|
248
160
|
pills.clear();
|
|
249
161
|
clearAttentionStatuses();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { creationBarColor, mixColor } from "./color-mix.ts";
|
|
4
|
+
|
|
5
|
+
describe("creationBarColor (057)", () => {
|
|
6
|
+
const surface = "#1a1a1c";
|
|
7
|
+
const accent = "#6d49b5";
|
|
8
|
+
|
|
9
|
+
it("returns surface for empty or zero max", () => {
|
|
10
|
+
assert.equal(creationBarColor(0, 10, surface, accent), surface);
|
|
11
|
+
assert.equal(creationBarColor(5, 0, surface, accent), surface);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("uses a deeper accent mix for the window peak than a small day", () => {
|
|
15
|
+
const peak = creationBarColor(20, 20, surface, accent);
|
|
16
|
+
const low = creationBarColor(1, 20, surface, accent);
|
|
17
|
+
assert.equal(peak, mixColor(surface, accent, 1));
|
|
18
|
+
assert.notEqual(peak, low);
|
|
19
|
+
assert.notEqual(low, surface);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("stays in the accent family (no provider brand)", () => {
|
|
23
|
+
const mid = creationBarColor(5, 20, surface, accent);
|
|
24
|
+
assert.match(mid, /^rgb\(/);
|
|
25
|
+
assert.ok(!mid.toLowerCase().includes("d94816"));
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { activityLevel } from "../shared/activity.ts";
|
|
2
|
+
|
|
3
|
+
/** Derive a palette colour by mixing `base` toward `accent` (hex themes only). */
|
|
4
|
+
export function mixColor(base: string, accent: string, amount: number): string {
|
|
5
|
+
const parse = (value: string): number[] | null => {
|
|
6
|
+
const hex = value.replace("#", "").trim();
|
|
7
|
+
const expanded = hex.length === 3 || hex.length === 4
|
|
8
|
+
? hex.split("").map((c) => c + c).join("")
|
|
9
|
+
: hex;
|
|
10
|
+
// Use first 6 digits only — ignore trailing alpha on 8-digit hex.
|
|
11
|
+
const rgb = expanded.slice(0, 6);
|
|
12
|
+
if (!/^[0-9a-fA-F]{6}$/.test(rgb)) return null;
|
|
13
|
+
return [0, 2, 4].map((i) => parseInt(rgb.slice(i, i + 2), 16));
|
|
14
|
+
};
|
|
15
|
+
const a = parse(base);
|
|
16
|
+
const b = parse(accent);
|
|
17
|
+
if (!a || !b) return base;
|
|
18
|
+
return `rgb(${a.map((v, i) => Math.round(v + (b[i]! - v) * amount)).join(",")})`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Intensity steps for activity visuals, indexed by `activityLevel` - 1. */
|
|
22
|
+
export const ACTIVITY_MIX_STEPS = [0.22, 0.43, 0.68, 1] as const;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Accent intensity fill for a creations day bar (057) — same steps as the
|
|
26
|
+
* Activity heatmap so Global charts share one hue family.
|
|
27
|
+
*/
|
|
28
|
+
export function creationBarColor(
|
|
29
|
+
count: number,
|
|
30
|
+
max: number,
|
|
31
|
+
surface2: string,
|
|
32
|
+
accent: string,
|
|
33
|
+
): string {
|
|
34
|
+
const level = activityLevel(count, max);
|
|
35
|
+
if (level <= 0) return surface2;
|
|
36
|
+
const amount = ACTIVITY_MIX_STEPS[level - 1] ?? 1;
|
|
37
|
+
return mixColor(surface2, accent, amount);
|
|
38
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { fitFontSize } from "./fit-text.ts";
|
|
4
|
+
|
|
5
|
+
const options = { base: 18, min: 12 };
|
|
6
|
+
const measured = (size: number, width: number) => ({ size, width });
|
|
7
|
+
|
|
8
|
+
describe("fitFontSize (051)", () => {
|
|
9
|
+
it("keeps the base size when the text fits", () => {
|
|
10
|
+
assert.equal(fitFontSize(measured(18, 80), 100, options), 18);
|
|
11
|
+
assert.equal(fitFontSize(measured(18, 100), 100, options), 18);
|
|
12
|
+
assert.equal(fitFontSize(measured(12, 60), 120, options), 18);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("scales the size down by the measured overflow", () => {
|
|
16
|
+
// 138px at 18px in a 120px tile → 15.5px (1% headroom).
|
|
17
|
+
assert.equal(fitFontSize(measured(18, 138), 120, options), 15.5);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("is independent of the size the text is currently rendered at", () => {
|
|
21
|
+
// The same physical width measured at 15.5px resolves to the same size.
|
|
22
|
+
const shrunk = fitFontSize(measured(15.5, 138 * (15.5 / 18)), 120, options);
|
|
23
|
+
assert.equal(shrunk, 15.5);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("grows back to base once the tile widens", () => {
|
|
27
|
+
assert.equal(fitFontSize(measured(15.5, 118.8), 160, options), 18);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("clamps to min and ignores unusable measurements", () => {
|
|
31
|
+
assert.equal(fitFontSize(measured(18, 400), 60, options), 12);
|
|
32
|
+
assert.equal(fitFontSize(measured(18, 0), 120, options), 18);
|
|
33
|
+
assert.equal(fitFontSize(measured(0, 120), 120, options), 18);
|
|
34
|
+
assert.equal(fitFontSize(measured(18, 138), 0, options), 18);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("derives a min floor from the base size when none is given", () => {
|
|
38
|
+
assert.equal(fitFontSize(measured(18, 400), 40, { base: 18 }), 12);
|
|
39
|
+
assert.equal(fitFontSize(measured(12, 400), 40, { base: 12 }), 8);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single-line text sizing (051).
|
|
3
|
+
*
|
|
4
|
+
* KPI tiles are ~130px wide at 18px values ("Cursor · 61%"), so values used to
|
|
5
|
+
* wrap onto two lines and push the labels out of line. The text is measured
|
|
6
|
+
* once at a known size, and the size that fits `available` is derived from that
|
|
7
|
+
* measurement alone: width scales linearly with font size, so the target does
|
|
8
|
+
* not depend on the size currently rendered — measure, fit, converge, and grow
|
|
9
|
+
* back to `base` when the tile widens.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type TextMeasurement = {
|
|
13
|
+
/** Font size the text was measured at. */
|
|
14
|
+
size: number;
|
|
15
|
+
/** Rendered width of the text at `size`. */
|
|
16
|
+
width: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Font size that fits `available`, clamped to `[min, base]`. */
|
|
20
|
+
export function fitFontSize(
|
|
21
|
+
measured: TextMeasurement,
|
|
22
|
+
available: number,
|
|
23
|
+
options: { base: number; min?: number },
|
|
24
|
+
): number {
|
|
25
|
+
const { base } = options;
|
|
26
|
+
const min = options.min ?? Math.max(8, base - 6);
|
|
27
|
+
if (!(measured.width > 0) || !(measured.size > 0) || !(available > 0)) return base;
|
|
28
|
+
const exact = (measured.size * available) / measured.width;
|
|
29
|
+
if (exact >= base) return base;
|
|
30
|
+
// 1% headroom on the shrink so sub-pixel rounding cannot trip the ellipsis.
|
|
31
|
+
const target = Math.min(base, Math.max(min, Math.round(exact * 0.99 * 10) / 10));
|
|
32
|
+
return target;
|
|
33
|
+
}
|