@koinzhang/paseo-plugin-activity 0.6.0 → 0.8.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.
Files changed (68) hide show
  1. package/CHANGELOG.md +50 -1
  2. package/README.md +18 -11
  3. package/client/activity-heatmap.tsx +115 -74
  4. package/client/agent-creations.tsx +100 -90
  5. package/client/attention-pill.tsx +17 -6
  6. package/client/attention-popover.tsx +19 -23
  7. package/client/design-tokens.test.ts +39 -0
  8. package/client/design-tokens.ts +180 -0
  9. package/client/formatted-time.tsx +4 -4
  10. package/client/global-surface.tsx +243 -268
  11. package/client/header-button.ts +136 -0
  12. package/client/hourly-activity-timeline.tsx +153 -103
  13. package/client/panel-visibility.test.ts +25 -0
  14. package/client/panel-visibility.ts +45 -0
  15. package/client/panel.tsx +94 -111
  16. package/client/pill.tsx +1 -1
  17. package/client/provider-dropdown.tsx +134 -0
  18. package/client/provider-filter.test.ts +17 -13
  19. package/client/provider-filter.ts +5 -7
  20. package/client/rank-color.ts +3 -3
  21. package/client/ranking-bars.tsx +294 -0
  22. package/client/sidebar-item.tsx +15 -0
  23. package/client/ui.tsx +351 -0
  24. package/client/usage-popover.tsx +72 -137
  25. package/client/usage-stats.tsx +29 -11
  26. package/client/use-agent-turn-end.ts +2 -1
  27. package/client/use-app-language.ts +17 -0
  28. package/client/web.ts +73 -18
  29. package/client/workspace/agent-row.tsx +28 -28
  30. package/client/workspace/agents-section.tsx +42 -57
  31. package/client/workspace/constants.ts +8 -6
  32. package/client/workspace/display-menu.tsx +6 -5
  33. package/client/workspace/filters.test.ts +1 -1
  34. package/client/workspace/filters.ts +4 -2
  35. package/client/workspace/panel.tsx +96 -104
  36. package/client/workspace/rank-section.tsx +40 -65
  37. package/client/workspace/running-indicator.tsx +1 -1
  38. package/client/workspace/terminals-section.tsx +24 -12
  39. package/index.client.tsx +53 -31
  40. package/index.server.ts +55 -73
  41. package/package.json +3 -3
  42. package/paseo-plugin.json +2 -1
  43. package/server/agents.ts +20 -2
  44. package/server/background-sync.ts +34 -4
  45. package/server/handlers-query.test.ts +27 -1
  46. package/server/handlers.ts +75 -31
  47. package/server/host-types.ts +6 -6
  48. package/server/ingest.test.ts +8 -0
  49. package/server/messages.test.ts +96 -1
  50. package/server/prompt-dedupe.ts +52 -0
  51. package/server/rpc-cache.test.ts +74 -0
  52. package/server/rpc-cache.ts +48 -0
  53. package/server/store.test.ts +122 -0
  54. package/server/store.ts +313 -19
  55. package/server/ttl-cache.ts +3 -0
  56. package/server/turn-ingest.test.ts +180 -0
  57. package/server/turn-ingest.ts +71 -0
  58. package/shared/activity.test.ts +36 -0
  59. package/shared/activity.ts +37 -0
  60. package/shared/classify.test.ts +1 -10
  61. package/shared/classify.ts +3 -10
  62. package/shared/format.ts +10 -7
  63. package/shared/i18n.test.ts +34 -0
  64. package/shared/i18n.ts +363 -0
  65. package/shared/insights.test.ts +120 -81
  66. package/shared/insights.ts +125 -64
  67. package/shared/usage.test.ts +121 -0
  68. package/shared/usage.ts +191 -2
@@ -2,26 +2,36 @@ import {
2
2
  type PluginButtonContentProps,
3
3
  useRpc,
4
4
  } from "@getpaseo/plugin/client";
5
- import { Icon } from "@getpaseo/plugin/client/react-native";
5
+ import { Icon } from "@getpaseo/plugin/client/ui";
6
6
  import { useQuery } from "@tanstack/react-query";
7
- import { useEffect, useMemo, useState, type ReactNode } from "react";
8
- import {
9
- ActivityIndicator,
10
- Platform,
11
- Pressable,
12
- Text,
13
- View,
14
- type TextStyle,
15
- type ViewStyle,
16
- } from "react-native";
7
+ import { useEffect, useMemo, useState } from "react";
8
+ import { Pressable, Text, View } from "react-native";
17
9
  import {
18
10
  usageReadSkillRpc,
19
11
  } from "../shared/usage.ts";
20
12
  import { formatDisplayName } from "../shared/format.ts";
21
13
  import { FormattedTime } from "./formatted-time.tsx";
22
14
  import { useUsagePillData } from "./usage-query.tsx";
15
+ import { useMessages } from "./use-app-language.ts";
16
+ import {
17
+ CountText,
18
+ ErrorState,
19
+ IconButton,
20
+ InlineEmpty,
21
+ LoadingState,
22
+ SectionHeader,
23
+ } from "./ui.tsx";
24
+ import {
25
+ ICON_SIZE,
26
+ PRESS,
27
+ RADIUS,
28
+ ROW_PADDING,
29
+ TEXT,
30
+ popoverFrame,
31
+ sectionTitle,
32
+ titleGap,
33
+ } from "./design-tokens.ts";
23
34
 
24
- const MONO = Platform.select({ ios: "Menlo", default: "monospace" });
25
35
 
26
36
  export type SkillDetail = { skillName: string; path: string };
27
37
 
@@ -30,31 +40,14 @@ export type UsagePopoverProps = PluginButtonContentProps & {
30
40
  openSkillInPanel?: (skill: SkillDetail) => void;
31
41
  };
32
42
 
33
- function CountBadge({
34
- value,
35
- styles,
36
- }: {
37
- value: number | string;
38
- styles: { countBadge: ViewStyle; countText: TextStyle };
39
- }): ReactNode {
40
- return (
41
- <View style={styles.countBadge}>
42
- <Text style={styles.countText}>{value}</Text>
43
- </View>
44
- );
45
- }
46
-
47
43
  /**
48
44
  * Composer-pill popover body. Host owns anchoring / sheet chrome / outer scroll;
49
45
  * render content only.
50
46
  */
51
47
  export function UsagePopover(props: UsagePopoverProps) {
48
+ const m = useMessages();
52
49
  if (props.context !== "agent") {
53
- return (
54
- <Text style={{ color: props.theme.colors.foregroundMuted }}>
55
- Activity is only available for an agent
56
- </Text>
57
- );
50
+ return <InlineEmpty text={m.common.agentOnly} color={props.theme.colors.foregroundMuted} />;
58
51
  }
59
52
  return <UsagePopoverAgent {...props} context="agent" />;
60
53
  }
@@ -66,6 +59,7 @@ function UsagePopoverAgent(
66
59
  const [tab, setTab] = useState<"skills" | "mcp" | null>(null);
67
60
  const [skillDetail, setSkillDetail] = useState<SkillDetail | null>(null);
68
61
  const compact = layout.compact;
62
+ const m = useMessages();
69
63
 
70
64
  const usage = useUsagePillData(agentId);
71
65
  const readSkillRpc = useRpc(usageReadSkillRpc);
@@ -108,38 +102,17 @@ function UsagePopoverAgent(
108
102
  const styles = useMemo(
109
103
  () => ({
110
104
  root: {
111
- gap: 16,
112
- minWidth: compact ? undefined : 300,
113
- maxWidth: compact ? undefined : 380,
114
- },
115
- sectionHeaderRow: {
116
- flexDirection: "row" as const,
117
- alignItems: "center" as const,
118
- justifyContent: "space-between" as const,
119
- gap: 8,
120
- },
121
- sectionTitle: {
122
- color: theme.colors.foreground,
123
- fontSize: 14,
124
- fontWeight: "600" as const,
125
- flexShrink: 1,
126
- },
127
- titleAction: {
128
- width: 24,
129
- height: 24,
130
- alignItems: "center" as const,
131
- justifyContent: "center" as const,
132
- borderRadius: 6,
133
- flexShrink: 0,
105
+ gap: titleGap(true),
106
+ ...popoverFrame(compact),
134
107
  },
135
108
  list: {
136
- gap: 0,
109
+ gap: 2,
137
110
  },
138
111
  row: {
139
112
  flexDirection: "row" as const,
140
113
  alignItems: "center" as const,
141
114
  gap: 10,
142
- paddingVertical: 10,
115
+ paddingVertical: ROW_PADDING.dense,
143
116
  borderTopWidth: 0,
144
117
  borderTopColor: theme.colors.border,
145
118
  },
@@ -150,30 +123,15 @@ function UsagePopoverAgent(
150
123
  rowMain: {
151
124
  flex: 1,
152
125
  minWidth: 0,
153
- gap: 2,
126
+ gap: 3,
154
127
  },
155
128
  rowTitle: {
129
+ ...TEXT.rowTitle,
156
130
  color: theme.colors.foreground,
157
- fontSize: 13,
158
- fontWeight: "500" as const,
159
131
  },
160
132
  rowMeta: {
133
+ ...TEXT.meta,
161
134
  color: theme.colors.foregroundMuted,
162
- fontSize: 11,
163
- },
164
- countBadge: {
165
- minWidth: 22,
166
- alignItems: "flex-end" as const,
167
- },
168
- countText: {
169
- color: theme.colors.foregroundMuted,
170
- fontSize: 12,
171
- fontVariant: ["tabular-nums" as const],
172
- },
173
- empty: {
174
- color: theme.colors.foregroundMuted,
175
- fontSize: 13,
176
- paddingVertical: 4,
177
135
  },
178
136
  detailHeader: {
179
137
  flexDirection: "row" as const,
@@ -187,32 +145,20 @@ function UsagePopoverAgent(
187
145
  gap: 4,
188
146
  },
189
147
  back: {
148
+ ...TEXT.back,
190
149
  color: theme.colors.foregroundMuted,
191
- fontSize: 12,
192
- fontWeight: "500" as const,
193
- },
194
- panelButton: {
195
- width: 24,
196
- height: 24,
197
- alignItems: "center" as const,
198
- justifyContent: "center" as const,
199
- borderRadius: 6,
200
- flexShrink: 0,
201
150
  },
202
151
  title: {
152
+ ...sectionTitle(true),
203
153
  color: theme.colors.foreground,
204
- fontSize: 14,
205
- fontWeight: "600" as const,
206
154
  flexShrink: 1,
207
155
  },
208
156
  body: {
157
+ ...TEXT.code,
209
158
  color: theme.colors.foreground,
210
- fontSize: 13,
211
- fontFamily: MONO,
212
- lineHeight: 22,
213
159
  backgroundColor: theme.colors.surface1,
214
160
  padding: 16,
215
- borderRadius: 12,
161
+ borderRadius: RADIUS.block,
216
162
  },
217
163
  }),
218
164
  [theme, compact],
@@ -223,8 +169,8 @@ function UsagePopoverAgent(
223
169
  const showToggle = skillItems.length > 0 && mcpItems.length > 0;
224
170
  const toggleAction =
225
171
  tab === "skills"
226
- ? { icon: "Plug" as const, accessibilityLabel: "Show MCP", next: "mcp" as const }
227
- : { icon: "Sparkles" as const, accessibilityLabel: "Show skills", next: "skills" as const };
172
+ ? { icon: "Plug" as const, accessibilityLabel: m.common.showMcp, next: "mcp" as const }
173
+ : { icon: "Sparkles" as const, accessibilityLabel: m.common.showSkills, next: "skills" as const };
228
174
 
229
175
  if (skillDetail) {
230
176
  return (
@@ -233,32 +179,30 @@ function UsagePopoverAgent(
233
179
  <Pressable
234
180
  accessibilityRole="button"
235
181
  onPress={() => setSkillDetail(null)}
236
- style={styles.backRow}
182
+ style={({ pressed }) => [
183
+ styles.backRow,
184
+ pressed ? { opacity: PRESS.textOpacity } : null,
185
+ ]}
237
186
  >
238
- <Icon name="ChevronLeft" size={14} color={theme.colors.foregroundMuted} />
239
- <Text style={styles.back}>Skills</Text>
187
+ <Icon name="ChevronLeft" size={ICON_SIZE.action} color={theme.colors.foregroundMuted} />
188
+ <Text style={styles.back}>{m.common.skills}</Text>
240
189
  </Pressable>
241
190
  {openSkillInPanel ? (
242
- <Pressable
243
- accessibilityRole="button"
244
- accessibilityLabel="Open SKILL.md in panel"
245
- hitSlop={8}
246
- style={styles.panelButton}
191
+ <IconButton
192
+ icon="ArrowUpRight"
193
+ label={m.common.openSkillInPanel}
194
+ color={theme.colors.foregroundMuted}
247
195
  onPress={() => {
248
196
  openSkillInPanel(skillDetail);
249
197
  close();
250
198
  }}
251
- >
252
- <Icon name="ArrowUpRight" size={16} color={theme.colors.foregroundMuted} />
253
- </Pressable>
199
+ />
254
200
  ) : null}
255
201
  </View>
256
202
  <Text style={styles.title}>{formatDisplayName(skillDetail.skillName)}</Text>
257
- {skillFile.isLoading ? <ActivityIndicator color={theme.colors.accent} /> : null}
203
+ {skillFile.isLoading ? <LoadingState color={theme.colors.accent} /> : null}
258
204
  {skillFile.error ? (
259
- <Text style={{ color: theme.colors.statusDanger }}>
260
- {skillFile.error instanceof Error ? skillFile.error.message : String(skillFile.error)}
261
- </Text>
205
+ <ErrorState error={skillFile.error} onRetry={() => void skillFile.refetch()} colors={theme.colors} />
262
206
  ) : null}
263
207
  {skillFile.data ? (
264
208
  <Text style={styles.body} selectable>
@@ -272,35 +216,23 @@ function UsagePopoverAgent(
272
216
  return (
273
217
  <View style={styles.root}>
274
218
  {tab && (skillItems.length > 0 || mcpItems.length > 0) ? (
275
- <View style={styles.sectionHeaderRow}>
276
- <Text style={styles.sectionTitle}>{tab === "skills" ? "Skills" : "MCP"}</Text>
219
+ <SectionHeader title={tab === "skills" ? m.common.skills : m.common.mcp} colors={theme.colors} compact>
277
220
  {showToggle ? (
278
- <Pressable
279
- accessibilityRole="button"
280
- accessibilityLabel={toggleAction.accessibilityLabel}
281
- hitSlop={8}
221
+ <IconButton
222
+ icon={toggleAction.icon}
223
+ label={toggleAction.accessibilityLabel}
282
224
  onPress={() => setTab(toggleAction.next)}
283
- style={styles.titleAction}
284
- >
285
- <Icon
286
- name={toggleAction.icon}
287
- size={16}
288
- color={theme.colors.foregroundMuted}
289
- />
290
- </Pressable>
225
+ color={theme.colors.foregroundMuted}
226
+ />
291
227
  ) : null}
292
- </View>
228
+ </SectionHeader>
293
229
  ) : null}
294
230
 
295
- {loading ? <ActivityIndicator color={theme.colors.accent} /> : null}
296
- {error ? (
297
- <Text style={{ color: theme.colors.statusDanger }}>
298
- {error instanceof Error ? error.message : String(error)}
299
- </Text>
300
- ) : null}
231
+ {loading ? <LoadingState color={theme.colors.accent} /> : null}
232
+ {error ? <ErrorState error={error} onRetry={() => void usage.refetch()} colors={theme.colors} /> : null}
301
233
 
302
234
  {!loading && !error && skillItems.length === 0 && mcpItems.length === 0 ? (
303
- <Text style={styles.empty}>No skill or MCP calls yet</Text>
235
+ <InlineEmpty text={m.common.noSkillOrMcp} color={theme.colors.foregroundMuted} />
304
236
  ) : null}
305
237
 
306
238
  {tab === "skills" && skillItems.length > 0 ? (
@@ -310,7 +242,7 @@ function UsagePopoverAgent(
310
242
  const rowStyle = [styles.row, index === 0 ? styles.rowFirst : null];
311
243
  const content = (
312
244
  <>
313
- <Icon name="Sparkles" size={16} color={theme.colors.foregroundMuted} />
245
+ <Icon name="Sparkles" size={ICON_SIZE.leading} color={theme.colors.foregroundMuted} />
314
246
  <View style={styles.rowMain}>
315
247
  <Text style={styles.rowTitle} numberOfLines={1}>
316
248
  {name}
@@ -319,7 +251,7 @@ function UsagePopoverAgent(
319
251
  <FormattedTime iso={item.lastUsedAt} style={styles.rowMeta} />
320
252
  ) : null}
321
253
  </View>
322
- <CountBadge value={item.total} styles={styles} />
254
+ <CountText value={item.total} color={theme.colors.foregroundMuted} />
323
255
  </>
324
256
  );
325
257
  if (item.skillPath) {
@@ -327,11 +259,14 @@ function UsagePopoverAgent(
327
259
  <Pressable
328
260
  key={item.skillName}
329
261
  accessibilityRole="link"
330
- accessibilityLabel={`Open ${name} SKILL.md`}
262
+ accessibilityLabel={m.common.openSkillFile(name)}
331
263
  onPress={() =>
332
264
  setSkillDetail({ skillName: item.skillName, path: item.skillPath! })
333
265
  }
334
- style={rowStyle}
266
+ style={({ pressed }) => [
267
+ rowStyle,
268
+ pressed ? { backgroundColor: theme.colors.surface2 } : null,
269
+ ]}
335
270
  >
336
271
  {content}
337
272
  </Pressable>
@@ -353,18 +288,18 @@ function UsagePopoverAgent(
353
288
  key={`${item.server}.${item.tool}`}
354
289
  style={[styles.row, index === 0 ? styles.rowFirst : null]}
355
290
  >
356
- <Icon name="Plug" size={16} color={theme.colors.foregroundMuted} />
357
- <View style={styles.rowMain}>
291
+ <Icon name="Plug" size={ICON_SIZE.leading} color={theme.colors.foregroundMuted} />
292
+ <View style={styles.rowMain}>
358
293
  <Text style={styles.rowTitle} numberOfLines={1}>
359
294
  {formatDisplayName(`${item.server}.${item.tool}`)}
360
295
  </Text>
361
296
  {item.failures > 0 ? (
362
- <Text style={styles.rowMeta}>{item.failures} failed</Text>
297
+ <Text style={styles.rowMeta}>{m.common.failed(item.failures)}</Text>
363
298
  ) : item.lastUsedAt ? (
364
299
  <FormattedTime iso={item.lastUsedAt} style={styles.rowMeta} />
365
300
  ) : null}
366
301
  </View>
367
- <CountBadge value={item.count} styles={styles} />
302
+ <CountText value={item.count} color={theme.colors.foregroundMuted} />
368
303
  </View>
369
304
  ))}
370
305
  </View>
@@ -1,6 +1,8 @@
1
1
  import { useState } from "react";
2
2
  import { Text, View, type TextStyle } from "react-native";
3
3
  import { fitFontSize } from "./fit-text.ts";
4
+ import { FONT_SIZE, FONT_WEIGHT, RADIUS } from "./design-tokens.ts";
5
+ import type { KpiRow } from "../shared/insights.ts";
4
6
 
5
7
  /** Hidden measuring box wide enough to never clamp the text (051). */
6
8
  const MEASURE_WIDTH = 1000;
@@ -20,7 +22,7 @@ function FitText({ text, available, base, min, lineHeight, color, weight, tabula
20
22
  min?: number;
21
23
  lineHeight: number;
22
24
  color: string;
23
- weight?: "500";
25
+ weight?: typeof FONT_WEIGHT.medium;
24
26
  tabular?: boolean;
25
27
  }) {
26
28
  // The hidden copy renders at `fontSize` and reports its width; the fit is
@@ -61,20 +63,24 @@ function FitText({ text, available, base, min, lineHeight, color, weight, tabula
61
63
  }
62
64
 
63
65
  function StatTile({ item, colors, dense, compact, bordered }: {
64
- item: { label: string; value: string };
65
- colors: { border: string; foreground: string; foregroundMuted: string };
66
+ item: KpiRow;
67
+ colors: { border: string; foreground: string; foregroundMuted: string; statusSuccess: string; statusDanger: string };
66
68
  dense: boolean;
67
69
  compact: boolean;
68
70
  bordered: boolean;
69
71
  }) {
70
72
  const [width, setWidth] = useState(0);
71
73
  const padding = dense ? 8 : 12;
72
- const available = Math.max(0, width - padding * 2);
74
+ const available = Math.max(0, width - padding * 2 - (bordered ? 1 : 0));
75
+ const comparisonColor = item.comparison?.direction === "up"
76
+ ? colors.statusSuccess
77
+ : item.comparison?.direction === "down"
78
+ ? colors.statusDanger
79
+ : colors.foregroundMuted;
73
80
  return (
74
81
  <View
75
82
  onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
76
83
  style={{
77
- position: "relative",
78
84
  flexGrow: 1,
79
85
  flexBasis: dense ? 84 : compact ? 100 : 120,
80
86
  minWidth: 0,
@@ -89,33 +95,45 @@ function StatTile({ item, colors, dense, compact, bordered }: {
89
95
  <FitText
90
96
  text={item.value}
91
97
  available={available}
92
- base={18}
98
+ base={FONT_SIZE.metric}
99
+ min={FONT_SIZE.caption}
93
100
  lineHeight={22}
94
101
  color={colors.foreground}
95
- weight="500"
102
+ weight={FONT_WEIGHT.medium}
96
103
  tabular
97
104
  />
98
105
  <FitText
99
106
  text={item.label}
100
107
  available={available}
101
- base={12}
108
+ base={FONT_SIZE.label}
102
109
  min={10}
103
110
  lineHeight={16}
104
111
  color={colors.foregroundMuted}
105
112
  />
113
+ {item.comparison ? (
114
+ <FitText
115
+ text={item.comparison.text}
116
+ available={available}
117
+ base={FONT_SIZE.label}
118
+ min={10}
119
+ lineHeight={16}
120
+ color={comparisonColor}
121
+ tabular
122
+ />
123
+ ) : null}
106
124
  </View>
107
125
  );
108
126
  }
109
127
 
110
128
  export function UsageStats({ items, colors, compact, dense }: {
111
- items: ReadonlyArray<{ label: string; value: string }>;
112
- colors: { border: string; foreground: string; foregroundMuted: string };
129
+ items: ReadonlyArray<KpiRow>;
130
+ colors: { border: string; foreground: string; foregroundMuted: string; statusSuccess: string; statusDanger: string };
113
131
  compact: boolean;
114
132
  /** Smaller numbers / labels / padding (025). */
115
133
  dense?: boolean;
116
134
  }) {
117
135
  return (
118
- <View style={{ flexDirection: "row", flexWrap: "wrap", borderWidth: 1, borderColor: colors.border, borderRadius: 20, paddingVertical: dense ? 6 : compact ? 8 : 18, rowGap: dense ? 10 : 16 }}>
136
+ <View style={{ flexDirection: "row", flexWrap: "wrap", borderWidth: 1, borderColor: colors.border, borderRadius: RADIUS.card, paddingVertical: dense ? 6 : compact ? 8 : 12, rowGap: dense ? 10 : 16 }}>
119
137
  {items.map((item, index) => (
120
138
  <StatTile
121
139
  key={item.label}
@@ -56,7 +56,8 @@ function useActivityRefresh(scope: AgentTurnEndScope, onTurnEnd: () => void): vo
56
56
  : watchAgentDirectory(paseo.agents, (_snapshot, update) => {
57
57
  // Workspace-wide hint only. The 15s query poll owns completeness.
58
58
  // The surface owns its API observation, independently of the entry.
59
- if (!update) { schedule(); return; }
59
+ // Snapshot-only publishes (mount, periodic re-read) are not activity.
60
+ if (!update) return;
60
61
  if (update.kind === "remove") {
61
62
  schedule();
62
63
  return;
@@ -1,10 +1,12 @@
1
1
  import { useSyncExternalStore } from "react";
2
2
  import { Platform } from "react-native";
3
+ import { messagesFor, type Messages } from "../shared/i18n.ts";
3
4
  import { readAppLanguage, subscribeAppLanguage } from "./web.ts";
4
5
 
5
6
  const defaultLanguage = () => "en";
6
7
  const noSubscription = () => () => {};
7
8
 
9
+ /** Paseo app language (Settings → Language, `system` → OS preference); `en` off web. */
8
10
  export function useAppLanguage(): string {
9
11
  return useSyncExternalStore(
10
12
  Platform.OS === "web" ? subscribeAppLanguage : noSubscription,
@@ -12,3 +14,18 @@ export function useAppLanguage(): string {
12
14
  defaultLanguage,
13
15
  );
14
16
  }
17
+
18
+ /** UI copy for the current app language (066). */
19
+ export function useMessages(): Messages {
20
+ return messagesFor(useAppLanguage());
21
+ }
22
+
23
+ /** Non-React read for registrations (Command Center titles, pill titles). */
24
+ export function currentAppLanguage(): string {
25
+ return Platform.OS === "web" ? readAppLanguage() : "en";
26
+ }
27
+
28
+ /** Non-React subscription; no-op off web. */
29
+ export function watchAppLanguage(onChange: () => void): () => void {
30
+ return Platform.OS === "web" ? subscribeAppLanguage(onChange) : () => {};
31
+ }
package/client/web.ts CHANGED
@@ -1,28 +1,83 @@
1
- // Browser-only bridge: the SDK currently does not expose the app locale.
2
- // Keep DOM access here and call it only on the web platform.
3
- type LanguageElement = { lang: string };
4
- type LanguageObserver = {
5
- observe(target: LanguageElement, options: { attributes: boolean; attributeFilter: string[] }): void;
6
- disconnect(): void;
1
+ // Browser-only bridge: the SDK does not expose the app language, and the host
2
+ // leaves `<html lang="en">` static. The Paseo `language` setting lives in the
3
+ // app settings blob in localStorage; keep DOM / storage access here and call it
4
+ // only on the web platform.
5
+ import { resolveAppLanguage, type AppLanguage } from "../shared/i18n.ts";
6
+
7
+ const APP_SETTINGS_KEY = "@paseo:app-settings";
8
+ /** Same-window writes do not fire `storage`; poll the (small) settings string. */
9
+ const POLL_MS = 1_500;
10
+
11
+ type BrowserEventTarget = {
12
+ addEventListener(type: string, listener: () => void): void;
13
+ removeEventListener(type: string, listener: () => void): void;
7
14
  };
8
- const browser = globalThis as unknown as {
9
- document?: { documentElement: LanguageElement };
10
- MutationObserver?: new (callback: () => void) => LanguageObserver;
15
+ const browser = globalThis as unknown as BrowserEventTarget & {
16
+ localStorage?: { getItem(key: string): string | null };
17
+ navigator?: { languages?: readonly string[]; language?: string };
18
+ document?: BrowserEventTarget;
11
19
  };
12
20
 
13
- export function readAppLanguage(): string {
14
- const language = browser.document?.documentElement.lang.trim() || "en";
21
+ function readLanguageSetting(): string | null {
15
22
  try {
16
- return Intl.getCanonicalLocales(language)[0] ?? "en";
23
+ const raw = browser.localStorage?.getItem(APP_SETTINGS_KEY);
24
+ if (!raw) return null;
25
+ const parsed = JSON.parse(raw) as { language?: unknown };
26
+ return typeof parsed.language === "string" ? parsed.language : null;
17
27
  } catch {
18
- return "en";
28
+ return null;
19
29
  }
20
30
  }
21
31
 
32
+ function systemLanguages(): readonly string[] {
33
+ const nav = browser.navigator;
34
+ if (!nav) return [];
35
+ if (nav.languages && nav.languages.length > 0) return nav.languages;
36
+ return nav.language ? [nav.language] : [];
37
+ }
38
+
39
+ let current: AppLanguage = "en";
40
+ let initialized = false;
41
+ const listeners = new Set<() => void>();
42
+ let stopWatching: (() => void) | null = null;
43
+
44
+ function refresh(): void {
45
+ const next = resolveAppLanguage(readLanguageSetting(), systemLanguages());
46
+ if (next === current) return;
47
+ current = next;
48
+ for (const listener of [...listeners]) listener();
49
+ }
50
+
51
+ export function readAppLanguage(): AppLanguage {
52
+ if (!initialized) {
53
+ initialized = true;
54
+ current = resolveAppLanguage(readLanguageSetting(), systemLanguages());
55
+ }
56
+ return current;
57
+ }
58
+
22
59
  export function subscribeAppLanguage(onChange: () => void): () => void {
23
- const root = browser.document?.documentElement;
24
- if (!root || !browser.MutationObserver) return () => {};
25
- const observer = new browser.MutationObserver(onChange);
26
- observer.observe(root, { attributes: true, attributeFilter: ["lang"] });
27
- return () => observer.disconnect();
60
+ listeners.add(onChange);
61
+ if (!stopWatching) {
62
+ readAppLanguage();
63
+ const timer = setInterval(refresh, POLL_MS);
64
+ browser.addEventListener?.("storage", refresh);
65
+ browser.addEventListener?.("focus", refresh);
66
+ browser.addEventListener?.("languagechange", refresh);
67
+ browser.document?.addEventListener?.("visibilitychange", refresh);
68
+ stopWatching = () => {
69
+ clearInterval(timer);
70
+ browser.removeEventListener?.("storage", refresh);
71
+ browser.removeEventListener?.("focus", refresh);
72
+ browser.removeEventListener?.("languagechange", refresh);
73
+ browser.document?.removeEventListener?.("visibilitychange", refresh);
74
+ };
75
+ }
76
+ return () => {
77
+ listeners.delete(onChange);
78
+ if (listeners.size === 0 && stopWatching) {
79
+ stopWatching();
80
+ stopWatching = null;
81
+ }
82
+ };
28
83
  }