@sixb/app 0.1.12 → 0.1.13

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 (292) hide show
  1. package/README.md +3 -8
  2. package/dist/agent/activity-label.d.ts.map +1 -1
  3. package/dist/agent/agent-chat.d.ts +9 -8
  4. package/dist/agent/agent-chat.d.ts.map +1 -1
  5. package/dist/agent/agent-panel.d.ts +5 -8
  6. package/dist/agent/agent-panel.d.ts.map +1 -1
  7. package/dist/agent/agents-page.d.ts +4 -0
  8. package/dist/agent/agents-page.d.ts.map +1 -0
  9. package/dist/agent/artifact-card.d.ts +9 -0
  10. package/dist/agent/artifact-card.d.ts.map +1 -0
  11. package/dist/agent/artifact-panel-state.d.ts +16 -0
  12. package/dist/agent/artifact-panel-state.d.ts.map +1 -0
  13. package/dist/agent/artifact-panel.d.ts +38 -0
  14. package/dist/agent/artifact-panel.d.ts.map +1 -0
  15. package/dist/agent/components/file-attachment-card.d.ts +3 -3
  16. package/dist/agent/components/file-attachment-card.d.ts.map +1 -1
  17. package/dist/agent/components/message-parts.d.ts +2 -1
  18. package/dist/agent/components/message-parts.d.ts.map +1 -1
  19. package/dist/agent/components/message-view.d.ts +3 -3
  20. package/dist/agent/components/message-view.d.ts.map +1 -1
  21. package/dist/agent/components/thread-artifacts-menu.d.ts +8 -0
  22. package/dist/agent/components/thread-artifacts-menu.d.ts.map +1 -0
  23. package/dist/agent/hooks/use-agent-conversation.d.ts +3 -4
  24. package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
  25. package/dist/agent/i18n/en.d.ts +10 -49
  26. package/dist/agent/i18n/en.d.ts.map +1 -1
  27. package/dist/agent/i18n/fr.d.ts.map +1 -1
  28. package/dist/agent/i18n/index.d.ts +16 -98
  29. package/dist/agent/i18n/index.d.ts.map +1 -1
  30. package/dist/agent/parts.d.ts +4 -4
  31. package/dist/agent/parts.d.ts.map +1 -1
  32. package/dist/agent/publish-artifact/interpret.d.ts +32 -0
  33. package/dist/agent/publish-artifact/interpret.d.ts.map +1 -0
  34. package/dist/agent/publish-artifact/publish-artifact-tool-view.d.ts +9 -0
  35. package/dist/agent/publish-artifact/publish-artifact-tool-view.d.ts.map +1 -0
  36. package/dist/agent/publish-artifact/use-live-publishes.d.ts +10 -0
  37. package/dist/agent/publish-artifact/use-live-publishes.d.ts.map +1 -0
  38. package/dist/agent/react-router.d.ts +1 -2
  39. package/dist/agent/react-router.d.ts.map +1 -1
  40. package/dist/agent/view-file/view-file-tool-view.d.ts +1 -1
  41. package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
  42. package/dist/artifacts/artifact-page.d.ts +7 -0
  43. package/dist/artifacts/artifact-page.d.ts.map +1 -0
  44. package/dist/artifacts/card-details.d.ts +15 -0
  45. package/dist/artifacts/card-details.d.ts.map +1 -0
  46. package/dist/artifacts/card-preview.d.ts +59 -0
  47. package/dist/artifacts/card-preview.d.ts.map +1 -0
  48. package/dist/artifacts/copy-feedback.d.ts +6 -0
  49. package/dist/artifacts/copy-feedback.d.ts.map +1 -0
  50. package/dist/artifacts/file-size.d.ts +11 -0
  51. package/dist/artifacts/file-size.d.ts.map +1 -0
  52. package/dist/artifacts/file-source.d.ts +45 -0
  53. package/dist/artifacts/file-source.d.ts.map +1 -0
  54. package/dist/artifacts/file-view.d.ts +9 -0
  55. package/dist/artifacts/file-view.d.ts.map +1 -0
  56. package/dist/artifacts/frame.d.ts +45 -0
  57. package/dist/artifacts/frame.d.ts.map +1 -0
  58. package/dist/artifacts/gallery.d.ts +6 -0
  59. package/dist/artifacts/gallery.d.ts.map +1 -0
  60. package/dist/artifacts/kind-icon.d.ts +8 -0
  61. package/dist/artifacts/kind-icon.d.ts.map +1 -0
  62. package/dist/artifacts/markdown-tree.d.ts +22 -0
  63. package/dist/artifacts/markdown-tree.d.ts.map +1 -0
  64. package/dist/artifacts/messages.d.ts +301 -0
  65. package/dist/artifacts/messages.d.ts.map +1 -0
  66. package/dist/artifacts/queries.d.ts +21 -0
  67. package/dist/artifacts/queries.d.ts.map +1 -0
  68. package/dist/artifacts/references/embed-tree.d.ts +17 -0
  69. package/dist/artifacts/references/embed-tree.d.ts.map +1 -0
  70. package/dist/artifacts/references/references.d.ts +50 -0
  71. package/dist/artifacts/references/references.d.ts.map +1 -0
  72. package/dist/artifacts/renderers/document-tree.d.ts +11 -0
  73. package/dist/artifacts/renderers/document-tree.d.ts.map +1 -0
  74. package/dist/artifacts/renderers/document.d.ts +17 -0
  75. package/dist/artifacts/renderers/document.d.ts.map +1 -0
  76. package/dist/artifacts/renderers/file.d.ts +10 -0
  77. package/dist/artifacts/renderers/file.d.ts.map +1 -0
  78. package/dist/artifacts/renderers/notice.d.ts +12 -0
  79. package/dist/artifacts/renderers/notice.d.ts.map +1 -0
  80. package/dist/artifacts/renderers/sheet/chart-data.d.ts +33 -0
  81. package/dist/artifacts/renderers/sheet/chart-data.d.ts.map +1 -0
  82. package/dist/artifacts/renderers/sheet/chart.d.ts +15 -0
  83. package/dist/artifacts/renderers/sheet/chart.d.ts.map +1 -0
  84. package/dist/artifacts/renderers/sheet/format.d.ts +35 -0
  85. package/dist/artifacts/renderers/sheet/format.d.ts.map +1 -0
  86. package/dist/artifacts/renderers/sheet/grid.d.ts +25 -0
  87. package/dist/artifacts/renderers/sheet/grid.d.ts.map +1 -0
  88. package/dist/artifacts/renderers/sheet/mini-chart.d.ts +7 -0
  89. package/dist/artifacts/renderers/sheet/mini-chart.d.ts.map +1 -0
  90. package/dist/artifacts/renderers/sheet/model.d.ts +29 -0
  91. package/dist/artifacts/renderers/sheet/model.d.ts.map +1 -0
  92. package/dist/artifacts/renderers/sheet/selection.d.ts +42 -0
  93. package/dist/artifacts/renderers/sheet/selection.d.ts.map +1 -0
  94. package/dist/artifacts/renderers/sheet/sheet.d.ts +17 -0
  95. package/dist/artifacts/renderers/sheet/sheet.d.ts.map +1 -0
  96. package/dist/artifacts/renderers/sheet/virtual.d.ts +13 -0
  97. package/dist/artifacts/renderers/sheet/virtual.d.ts.map +1 -0
  98. package/dist/artifacts/renderers/sheet/workbook.d.ts +24 -0
  99. package/dist/artifacts/renderers/sheet/workbook.d.ts.map +1 -0
  100. package/dist/artifacts/renderers/slides/navigation.d.ts +17 -0
  101. package/dist/artifacts/renderers/slides/navigation.d.ts.map +1 -0
  102. package/dist/artifacts/renderers/slides/slides.d.ts +14 -0
  103. package/dist/artifacts/renderers/slides/slides.d.ts.map +1 -0
  104. package/dist/artifacts/target.d.ts +29 -0
  105. package/dist/artifacts/target.d.ts.map +1 -0
  106. package/dist/artifacts/theme.d.ts +18 -0
  107. package/dist/artifacts/theme.d.ts.map +1 -0
  108. package/dist/artifacts/viewer.d.ts +19 -0
  109. package/dist/artifacts/viewer.d.ts.map +1 -0
  110. package/dist/{chunk-shmw3f8t.js → chunk-3schcf10.js} +1 -1
  111. package/dist/chunk-c7gy61rx.js +276 -0
  112. package/dist/chunk-c7gy61rx.js.map +10 -0
  113. package/dist/{chunk-48pkffj6.js → chunk-g3f4cm8x.js} +1 -1
  114. package/dist/{chunk-zcxrt24m.js → chunk-gn3k23q5.js} +3 -2
  115. package/dist/{chunk-zcxrt24m.js.map → chunk-gn3k23q5.js.map} +3 -3
  116. package/dist/{chunk-3a99tmwg.js → chunk-ng0hdnrq.js} +10 -3
  117. package/dist/chunk-ng0hdnrq.js.map +10 -0
  118. package/dist/{chunk-j97e06rg.js → chunk-xxpdn63v.js} +7224 -2874
  119. package/dist/chunk-xxpdn63v.js.map +105 -0
  120. package/dist/codegen.d.ts +2 -0
  121. package/dist/codegen.d.ts.map +1 -1
  122. package/dist/createCustomApp.d.ts.map +1 -1
  123. package/dist/index.d.ts +0 -2
  124. package/dist/index.d.ts.map +1 -1
  125. package/dist/index.js +34 -31
  126. package/dist/index.js.map +7 -7
  127. package/dist/locale-messages.d.ts +2 -0
  128. package/dist/locale-messages.d.ts.map +1 -1
  129. package/dist/locale.d.ts +2 -0
  130. package/dist/locale.d.ts.map +1 -1
  131. package/dist/locale.js +1 -1
  132. package/dist/pages/observability/activity/calendar.d.ts +9 -0
  133. package/dist/pages/observability/activity/calendar.d.ts.map +1 -0
  134. package/dist/pages/observability/activity/connections.d.ts +7 -0
  135. package/dist/pages/observability/activity/connections.d.ts.map +1 -0
  136. package/dist/pages/observability/activity/format.d.ts +11 -0
  137. package/dist/pages/observability/activity/format.d.ts.map +1 -0
  138. package/dist/pages/observability/activity/messages.d.ts +231 -0
  139. package/dist/pages/observability/activity/messages.d.ts.map +1 -0
  140. package/dist/pages/observability/activity/meta-items.d.ts +10 -0
  141. package/dist/pages/observability/activity/meta-items.d.ts.map +1 -0
  142. package/dist/pages/observability/activity/page.d.ts +6 -0
  143. package/dist/pages/observability/activity/page.d.ts.map +1 -0
  144. package/dist/pages/observability/activity/period.d.ts +31 -0
  145. package/dist/pages/observability/activity/period.d.ts.map +1 -0
  146. package/dist/pages/observability/activity/steps.d.ts +44 -0
  147. package/dist/pages/observability/activity/steps.d.ts.map +1 -0
  148. package/dist/pages/observability/activity/timeline.d.ts +36 -0
  149. package/dist/pages/observability/activity/timeline.d.ts.map +1 -0
  150. package/dist/pages/observability/activity/users.d.ts +18 -0
  151. package/dist/pages/observability/activity/users.d.ts.map +1 -0
  152. package/dist/pages/routes.d.ts +7 -0
  153. package/dist/pages/routes.d.ts.map +1 -1
  154. package/dist/pages/section-ui.d.ts +2 -1
  155. package/dist/pages/section-ui.d.ts.map +1 -1
  156. package/dist/pages.d.ts +4 -1
  157. package/dist/pages.d.ts.map +1 -1
  158. package/dist/pages.js +5852 -3754
  159. package/dist/pages.js.map +20 -7
  160. package/dist/sections/catalog.d.ts.map +1 -1
  161. package/dist/sections/sections.d.ts.map +1 -1
  162. package/dist/server.js +63 -10
  163. package/dist/server.js.map +5 -5
  164. package/dist/shell/search-command.d.ts.map +1 -1
  165. package/dist/shell/user-menu.d.ts +3 -3
  166. package/dist/shell/user-menu.d.ts.map +1 -1
  167. package/package.json +4 -6
  168. package/src/agent/activity-label.ts +7 -0
  169. package/src/agent/agent-chat.tsx +29 -18
  170. package/src/agent/agent-execution-trace.tsx +1 -1
  171. package/src/agent/agent-panel.tsx +8 -13
  172. package/src/agent/agent-surface.tsx +4 -4
  173. package/src/agent/agents-page.ts +7 -0
  174. package/src/agent/artifact-card.tsx +190 -0
  175. package/src/agent/artifact-panel-state.ts +38 -0
  176. package/src/agent/artifact-panel.tsx +320 -0
  177. package/src/agent/components/file-attachment-card.tsx +13 -14
  178. package/src/agent/components/message-parts.tsx +30 -5
  179. package/src/agent/components/message-view.tsx +13 -19
  180. package/src/agent/components/thread-artifacts-menu.tsx +77 -0
  181. package/src/agent/i18n/en.ts +16 -58
  182. package/src/agent/i18n/fr.ts +15 -59
  183. package/src/agent/parts.ts +6 -6
  184. package/src/agent/publish-artifact/interpret.ts +110 -0
  185. package/src/agent/publish-artifact/publish-artifact-tool-view.tsx +62 -0
  186. package/src/agent/publish-artifact/use-live-publishes.ts +45 -0
  187. package/src/agent/react-router.tsx +26 -7
  188. package/src/agent/view-file/view-file-tool-view.tsx +21 -18
  189. package/src/artifacts/artifact-page.tsx +61 -0
  190. package/src/artifacts/artifacts.css +172 -0
  191. package/src/artifacts/card-details.ts +92 -0
  192. package/src/artifacts/card-preview.tsx +612 -0
  193. package/src/artifacts/copy-feedback.ts +25 -0
  194. package/src/artifacts/file-size.ts +24 -0
  195. package/src/artifacts/file-source.ts +109 -0
  196. package/src/artifacts/file-view.ts +57 -0
  197. package/src/artifacts/frame.tsx +288 -0
  198. package/src/artifacts/gallery.tsx +405 -0
  199. package/src/artifacts/kind-icon.tsx +23 -0
  200. package/src/artifacts/markdown-tree.ts +32 -0
  201. package/src/artifacts/messages.ts +317 -0
  202. package/src/artifacts/queries.ts +85 -0
  203. package/src/artifacts/references/embed-tree.ts +86 -0
  204. package/src/artifacts/references/references.tsx +739 -0
  205. package/src/artifacts/renderers/document-tree.ts +157 -0
  206. package/src/artifacts/renderers/document.tsx +334 -0
  207. package/src/artifacts/renderers/file.tsx +160 -0
  208. package/src/artifacts/renderers/notice.tsx +38 -0
  209. package/src/artifacts/renderers/sheet/chart-data.ts +73 -0
  210. package/src/artifacts/renderers/sheet/chart.tsx +288 -0
  211. package/src/artifacts/renderers/sheet/format.ts +142 -0
  212. package/src/artifacts/renderers/sheet/grid.tsx +709 -0
  213. package/src/artifacts/renderers/sheet/mini-chart.tsx +241 -0
  214. package/src/artifacts/renderers/sheet/model.ts +136 -0
  215. package/src/artifacts/renderers/sheet/selection.ts +152 -0
  216. package/src/artifacts/renderers/sheet/sheet.tsx +471 -0
  217. package/src/artifacts/renderers/sheet/virtual.ts +49 -0
  218. package/src/artifacts/renderers/sheet/workbook.ts +57 -0
  219. package/src/artifacts/renderers/slides/navigation.ts +38 -0
  220. package/src/artifacts/renderers/slides/slides.tsx +441 -0
  221. package/src/artifacts/target.ts +76 -0
  222. package/src/artifacts/theme.ts +29 -0
  223. package/src/artifacts/viewer.tsx +476 -0
  224. package/src/build.ts +13 -2
  225. package/src/codegen.ts +38 -2
  226. package/src/createCustomApp.ts +21 -1
  227. package/src/index.ts +0 -5
  228. package/src/locale-messages.ts +9 -2
  229. package/src/pages/observability/activity/calendar.tsx +239 -0
  230. package/src/pages/observability/activity/connections.tsx +144 -0
  231. package/src/pages/observability/activity/format.ts +49 -0
  232. package/src/pages/observability/activity/messages.ts +255 -0
  233. package/src/pages/observability/activity/meta-items.tsx +23 -0
  234. package/src/pages/observability/activity/page.tsx +605 -0
  235. package/src/pages/observability/activity/period.ts +63 -0
  236. package/src/pages/observability/activity/steps.ts +102 -0
  237. package/src/pages/observability/activity/timeline.tsx +411 -0
  238. package/src/pages/observability/activity/users.tsx +133 -0
  239. package/src/pages/observability/messages.ts +2 -2
  240. package/src/pages/routes.ts +15 -0
  241. package/src/pages/section-ui.tsx +3 -1
  242. package/src/pages.ts +4 -1
  243. package/src/sections/catalog.ts +1 -0
  244. package/src/sections/sections.tsx +2 -0
  245. package/src/shell/search-command.tsx +8 -6
  246. package/src/shell/user-menu.tsx +22 -22
  247. package/dist/agent/document-preview/classify.d.ts +0 -4
  248. package/dist/agent/document-preview/classify.d.ts.map +0 -1
  249. package/dist/agent/document-preview/content-policy.d.ts +0 -10
  250. package/dist/agent/document-preview/content-policy.d.ts.map +0 -1
  251. package/dist/agent/document-preview/content.d.ts +0 -26
  252. package/dist/agent/document-preview/content.d.ts.map +0 -1
  253. package/dist/agent/document-preview/delimited.d.ts +0 -19
  254. package/dist/agent/document-preview/delimited.d.ts.map +0 -1
  255. package/dist/agent/document-preview/document-preview-root.d.ts +0 -24
  256. package/dist/agent/document-preview/document-preview-root.d.ts.map +0 -1
  257. package/dist/agent/document-preview/file-size.d.ts +0 -3
  258. package/dist/agent/document-preview/file-size.d.ts.map +0 -1
  259. package/dist/agent/document-preview/html.d.ts +0 -9
  260. package/dist/agent/document-preview/html.d.ts.map +0 -1
  261. package/dist/agent/document-preview/persistence.d.ts +0 -6
  262. package/dist/agent/document-preview/persistence.d.ts.map +0 -1
  263. package/dist/agent/document-preview/presentation.d.ts +0 -3
  264. package/dist/agent/document-preview/presentation.d.ts.map +0 -1
  265. package/dist/agent/document-preview/rendering.d.ts +0 -14
  266. package/dist/agent/document-preview/rendering.d.ts.map +0 -1
  267. package/dist/agent/document-preview/source.d.ts +0 -13
  268. package/dist/agent/document-preview/source.d.ts.map +0 -1
  269. package/dist/agent/document-preview/state.d.ts +0 -27
  270. package/dist/agent/document-preview/state.d.ts.map +0 -1
  271. package/dist/agent/document-preview/types.d.ts +0 -33
  272. package/dist/agent/document-preview/types.d.ts.map +0 -1
  273. package/dist/agent/workspace.d.ts +0 -11
  274. package/dist/agent/workspace.d.ts.map +0 -1
  275. package/dist/chunk-3a99tmwg.js.map +0 -10
  276. package/dist/chunk-j97e06rg.js.map +0 -80
  277. package/src/agent/document-preview/classify.ts +0 -55
  278. package/src/agent/document-preview/content-policy.ts +0 -37
  279. package/src/agent/document-preview/content.ts +0 -82
  280. package/src/agent/document-preview/delimited.ts +0 -118
  281. package/src/agent/document-preview/document-preview-root.tsx +0 -1017
  282. package/src/agent/document-preview/file-size.ts +0 -26
  283. package/src/agent/document-preview/html.ts +0 -67
  284. package/src/agent/document-preview/persistence.ts +0 -115
  285. package/src/agent/document-preview/presentation.ts +0 -13
  286. package/src/agent/document-preview/rendering.ts +0 -48
  287. package/src/agent/document-preview/source.ts +0 -48
  288. package/src/agent/document-preview/state.ts +0 -77
  289. package/src/agent/document-preview/types.ts +0 -36
  290. package/src/agent/workspace.ts +0 -33
  291. /package/dist/{chunk-shmw3f8t.js.map → chunk-3schcf10.js.map} +0 -0
  292. /package/dist/{chunk-48pkffj6.js.map → chunk-g3f4cm8x.js.map} +0 -0
@@ -0,0 +1,241 @@
1
+ import type { WorkbookChart } from "@sixb/core/artifacts"
2
+ import { useLocale } from "@sixb/ui/lib/i18n"
3
+ import { useMemo } from "react"
4
+ import { chartData } from "./chart-data"
5
+ import type { SheetModel } from "./model"
6
+
7
+ // A chart at a glance, as a card previews it: the shape of the data in the theme's chart colors,
8
+ // with no axes to read. Drawn as plain SVG, so a card never loads the charting library.
9
+ const WIDTH = 200
10
+ const HEIGHT = 64
11
+
12
+ export function MiniChart({
13
+ chart,
14
+ model,
15
+ }: {
16
+ readonly chart: WorkbookChart
17
+ readonly model: SheetModel
18
+ }) {
19
+ const data = useMemo(() => chartData(chart, model), [chart, model])
20
+ if (!data) return null
21
+ const series = data.series.map((entry) => ({
22
+ color: entry.color,
23
+ values: data.rows.map((row) => {
24
+ const value = row[entry.key]
25
+ return typeof value === "number" ? value : null
26
+ }),
27
+ }))
28
+ if (chart.type === "pie") {
29
+ const [first] = series
30
+ return first ? (
31
+ <MiniPie labels={data.rows.map((row) => String(row.x ?? ""))} values={first.values} />
32
+ ) : null
33
+ }
34
+ return (
35
+ <svg
36
+ viewBox={`0 0 ${WIDTH} ${HEIGHT}`}
37
+ preserveAspectRatio="none"
38
+ className="block size-full overflow-visible"
39
+ aria-hidden="true"
40
+ >
41
+ {chart.type === "bar" ? (
42
+ <Bars series={series} stacked={chart.stacked} />
43
+ ) : (
44
+ <Lines series={series} area={chart.type === "area"} stacked={chart.stacked} />
45
+ )}
46
+ </svg>
47
+ )
48
+ }
49
+
50
+ interface Series {
51
+ readonly color: string
52
+ readonly values: readonly (number | null)[]
53
+ }
54
+
55
+ function Bars({
56
+ series,
57
+ stacked,
58
+ }: {
59
+ readonly series: readonly Series[]
60
+ readonly stacked: boolean
61
+ }) {
62
+ const count = series[0]?.values.length ?? 0
63
+ if (count === 0) return null
64
+ const totals = Array.from({ length: count }, (_, index) =>
65
+ stacked
66
+ ? series.reduce((sum, entry) => sum + Math.max(0, entry.values[index] ?? 0), 0)
67
+ : Math.max(0, ...series.map((entry) => entry.values[index] ?? 0))
68
+ )
69
+ const max = Math.max(...totals) || 1
70
+ const slot = WIDTH / count
71
+ const group = slot * 0.64
72
+ const width = stacked ? group : group / series.length
73
+ const y = (value: number) => HEIGHT * (1 - value / max)
74
+ return (
75
+ <>
76
+ {Array.from({ length: count }, (_, index) => {
77
+ let base = 0
78
+ return series.map((entry, position) => {
79
+ const value = Math.max(0, entry.values[index] ?? 0)
80
+ const x = index * slot + (slot - group) / 2 + (stacked ? 0 : position * width)
81
+ const top = stacked ? base + value : value
82
+ const rect = (
83
+ <rect
84
+ key={`${index}:${position}`}
85
+ x={x + (stacked ? 0 : 0.6)}
86
+ y={y(top)}
87
+ width={Math.max(0.5, width - (stacked ? 0 : 1.2))}
88
+ height={Math.max(0, y(stacked ? base : 0) - y(top))}
89
+ rx={1}
90
+ fill={entry.color}
91
+ />
92
+ )
93
+ base = top
94
+ return rect
95
+ })
96
+ })}
97
+ </>
98
+ )
99
+ }
100
+
101
+ function Lines({
102
+ series,
103
+ area,
104
+ stacked,
105
+ }: {
106
+ readonly series: readonly Series[]
107
+ readonly area: boolean
108
+ readonly stacked: boolean
109
+ }) {
110
+ const count = series[0]?.values.length ?? 0
111
+ if (count === 0) return null
112
+ // Stacked areas sit on the series before them.
113
+ const layers = series.map((entry, position) =>
114
+ entry.values.map((value, index) =>
115
+ value === null
116
+ ? null
117
+ : stacked
118
+ ? series
119
+ .slice(0, position + 1)
120
+ .reduce((sum, layer) => sum + Math.max(0, layer.values[index] ?? 0), 0)
121
+ : value
122
+ )
123
+ )
124
+ const all = layers.flat().filter((value): value is number => value !== null)
125
+ if (all.length === 0) return null
126
+ // An area measures from zero; a line shows change, so it fits its values.
127
+ const low = area ? Math.min(0, ...all) : Math.min(...all)
128
+ const high = Math.max(...all)
129
+ const pad = area ? 0 : (high - low) * 0.12 || 1
130
+ const min = low - pad
131
+ const max = high + pad
132
+ const x = (index: number) => (count === 1 ? WIDTH / 2 : (index / (count - 1)) * WIDTH)
133
+ const y = (value: number) => HEIGHT * (1 - (value - min) / (max - min || 1))
134
+ return (
135
+ <>
136
+ {layers.map((values, position) => {
137
+ const color = series[position]?.color ?? "currentColor"
138
+ // An empty cell is a gap in the line, not a zero.
139
+ const runs: { index: number; value: number }[][] = [[]]
140
+ values.forEach((value, index) => {
141
+ if (value === null) runs.push([])
142
+ else runs.at(-1)?.push({ index, value })
143
+ })
144
+ return runs
145
+ .filter((run) => run.length > 0)
146
+ .map((run, runIndex) => {
147
+ const points = run.map((point) => `${x(point.index)},${y(point.value)}`).join(" ")
148
+ const first = run[0]
149
+ const last = run.at(-1)
150
+ return (
151
+ <g key={`${position}:${runIndex}`}>
152
+ {area && first && last ? (
153
+ <polygon
154
+ points={`${x(first.index)},${y(Math.max(min, 0))} ${points} ${x(last.index)},${y(Math.max(min, 0))}`}
155
+ fill={color}
156
+ fillOpacity={0.14}
157
+ />
158
+ ) : null}
159
+ <polyline
160
+ points={points}
161
+ fill="none"
162
+ stroke={color}
163
+ strokeWidth={1.75}
164
+ strokeLinejoin="round"
165
+ strokeLinecap="round"
166
+ vectorEffect="non-scaling-stroke"
167
+ />
168
+ </g>
169
+ )
170
+ })
171
+ })}
172
+ </>
173
+ )
174
+ }
175
+
176
+ /** A donut of the slices, beside the largest of them with their shares. */
177
+ function MiniPie({
178
+ labels,
179
+ values,
180
+ }: {
181
+ readonly labels: readonly string[]
182
+ readonly values: readonly (number | null)[]
183
+ }) {
184
+ const locale = useLocale()
185
+ const slices = values
186
+ .map((value, index) => ({
187
+ label: labels[index] ?? "",
188
+ value: Math.max(0, value ?? 0),
189
+ color: `var(--chart-${(index % 5) + 1})`,
190
+ }))
191
+ .filter((slice) => slice.value > 0)
192
+ const total = slices.reduce((sum, slice) => sum + slice.value, 0)
193
+ if (total === 0) return null
194
+ const share = new Intl.NumberFormat(locale, { style: "percent", maximumFractionDigits: 0 })
195
+ const radius = 24
196
+ const circumference = 2 * Math.PI * radius
197
+ let offset = 0
198
+ return (
199
+ <span className="flex h-full items-center gap-4">
200
+ <svg viewBox="0 0 64 64" className="h-full max-h-16 shrink-0" aria-hidden="true">
201
+ <g transform="rotate(-90 32 32)">
202
+ {slices.map((slice) => {
203
+ const length = (slice.value / total) * circumference
204
+ const circle = (
205
+ <circle
206
+ key={slice.label}
207
+ cx={32}
208
+ cy={32}
209
+ r={radius}
210
+ fill="none"
211
+ stroke={slice.color}
212
+ strokeWidth={11}
213
+ strokeDasharray={`${Math.max(0, length - 1.2)} ${circumference}`}
214
+ strokeDashoffset={-offset}
215
+ />
216
+ )
217
+ offset += length
218
+ return circle
219
+ })}
220
+ </g>
221
+ </svg>
222
+ <span className="flex min-w-0 flex-1 flex-col gap-0.5 text-[11px] leading-4">
223
+ {[...slices]
224
+ .sort((a, b) => b.value - a.value)
225
+ .slice(0, 3)
226
+ .map((slice) => (
227
+ <span key={slice.label} className="flex min-w-0 items-center gap-1.5">
228
+ <span
229
+ className="size-1.5 shrink-0 rounded-full"
230
+ style={{ background: slice.color }}
231
+ />
232
+ <span className="min-w-0 flex-1 truncate text-muted-foreground">{slice.label}</span>
233
+ <span className="text-foreground tabular-nums">
234
+ {share.format(slice.value / total)}
235
+ </span>
236
+ </span>
237
+ ))}
238
+ </span>
239
+ </span>
240
+ )
241
+ }
@@ -0,0 +1,136 @@
1
+ import {
2
+ type CsvTable,
3
+ NUMERIC_COLUMN_TYPES,
4
+ parseSheetNumber,
5
+ type SheetColumnFormat,
6
+ type WorkbookSheet,
7
+ } from "@sixb/core/artifacts"
8
+
9
+ /** One column as the grid draws it. */
10
+ export interface SheetColumn {
11
+ readonly name: string
12
+ readonly format: SheetColumnFormat
13
+ readonly numeric: boolean
14
+ /** Digits after the decimal point: the format's, else as many as the column's values need. */
15
+ readonly decimals: number
16
+ readonly width: number
17
+ }
18
+
19
+ /** A sheet ready to draw: its columns, and its data rows each as wide as the columns. */
20
+ export interface SheetModel {
21
+ readonly name: string
22
+ readonly columns: readonly SheetColumn[]
23
+ readonly rows: readonly (readonly string[])[]
24
+ /** Grid rows that stay in view, the header row included. */
25
+ readonly frozenRows: number
26
+ readonly frozenColumns: number
27
+ }
28
+
29
+ type SheetLayout = Pick<
30
+ WorkbookSheet,
31
+ "name" | "frozenRows" | "frozenColumns" | "columns" | "widths"
32
+ >
33
+
34
+ /** A sheet of a workbook, laid out as its workbook says. */
35
+ export function sheetModel(sheet: SheetLayout, table: CsvTable): SheetModel {
36
+ const columns = table.header.map((name, index) => {
37
+ const format = sheet.columns[name] ?? { type: "text" as const }
38
+ return column(name, format, table.rows, index, sheet.widths[name])
39
+ })
40
+ return {
41
+ name: sheet.name,
42
+ columns,
43
+ rows: table.rows.map((row) => pad(row, columns.length)),
44
+ frozenRows: Math.min(sheet.frozenRows, table.rows.length + 1),
45
+ frozenColumns: Math.min(sheet.frozenColumns, columns.length),
46
+ }
47
+ }
48
+
49
+ /**
50
+ * A plain CSV file as a sheet: a column whose values are all numbers reads as numbers, any other
51
+ * as text, and rows longer than the header add unnamed columns.
52
+ */
53
+ export function fileSheetModel(name: string, table: CsvTable): SheetModel {
54
+ // A loop, not a spread: a long file's rows are more arguments than a call can take.
55
+ let width = table.header.length
56
+ for (const row of table.rows) width = Math.max(width, row.length)
57
+ const header = Array.from({ length: width }, (_, index) => table.header[index] ?? "")
58
+ const columns = header.map((columnName, index) => {
59
+ const values = table.rows.map((row) => row[index] ?? "").filter((value) => value.trim())
60
+ const numeric =
61
+ values.length > 0 && values.every((value) => parseSheetNumber(value) !== undefined)
62
+ return column(columnName, { type: numeric ? "number" : "text" }, table.rows, index, undefined)
63
+ })
64
+ return {
65
+ name,
66
+ columns,
67
+ rows: table.rows.map((row) => pad(row, width)),
68
+ frozenRows: 1,
69
+ frozenColumns: 0,
70
+ }
71
+ }
72
+
73
+ const SAMPLE_ROWS = 200
74
+ const CHARACTER_WIDTH = 7.4
75
+ const HEADER_CHARACTER_WIDTH = 7.8
76
+ const MIN_WIDTH = 72
77
+ const MAX_WIDTH = 320
78
+
79
+ function column(
80
+ name: string,
81
+ format: SheetColumnFormat,
82
+ rows: readonly (readonly string[])[],
83
+ index: number,
84
+ width: number | undefined
85
+ ): SheetColumn {
86
+ const numeric = NUMERIC_COLUMN_TYPES.includes(format.type)
87
+ const sample = rows.slice(0, SAMPLE_ROWS).map((row) => row[index] ?? "")
88
+ return {
89
+ name,
90
+ format,
91
+ numeric,
92
+ decimals: format.decimals ?? (numeric ? neededDecimals(sample, format.type) : 0),
93
+ width: width ?? estimatedWidth(name, sample, format),
94
+ }
95
+ }
96
+
97
+ /** As many decimals as the values carry, capped where more would be noise. */
98
+ function neededDecimals(values: readonly string[], type: SheetColumnFormat["type"]): number {
99
+ const cap = type === "number" ? 4 : 2
100
+ let decimals = 0
101
+ for (const value of values) {
102
+ const number = parseSheetNumber(value)
103
+ if (number === undefined) continue
104
+ const scaled = type === "percent" ? number * 100 : number
105
+ const text = String(Math.abs(Number(scaled.toFixed(cap))))
106
+ const point = text.indexOf(".")
107
+ if (point >= 0) decimals = Math.max(decimals, text.length - point - 1)
108
+ if (decimals === cap) break
109
+ }
110
+ return decimals
111
+ }
112
+
113
+ /** A width that fits the header and most values without measuring text. */
114
+ function estimatedWidth(
115
+ name: string,
116
+ sample: readonly string[],
117
+ format: SheetColumnFormat
118
+ ): number {
119
+ // Formatting adds currency signs, grouping, and month names.
120
+ const extra =
121
+ format.type === "currency" ? 4 : format.type === "number" || format.type === "date" ? 2 : 0
122
+ const longest = Math.max(
123
+ ...sample.map((value) => value.length + extra),
124
+ format.type === "image" ? 4 : 0
125
+ )
126
+ // The header is semibold, beside its type glyph and sort control.
127
+ const header = name.length * HEADER_CHARACTER_WIDTH + (format.type === "text" ? 0 : 18) + 54
128
+ const values = longest * CHARACTER_WIDTH + 28
129
+ return Math.round(Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, header, values)))
130
+ }
131
+
132
+ function pad(row: readonly string[], width: number): readonly string[] {
133
+ return row.length >= width
134
+ ? row
135
+ : [...row, ...Array.from({ length: width - row.length }, () => "")]
136
+ }
@@ -0,0 +1,152 @@
1
+ import { parseSheetNumber } from "@sixb/core/artifacts"
2
+ import type { SheetModel } from "./model"
3
+
4
+ // Grid coordinates: row 0 is the header row, row 1 the first data row as currently ordered;
5
+ // column 0 is the sheet's first column. A1 names count rows from 1, so grid row 0 is row 1.
6
+
7
+ export interface CellPoint {
8
+ readonly row: number
9
+ readonly column: number
10
+ }
11
+
12
+ export interface CellRange {
13
+ readonly top: number
14
+ readonly left: number
15
+ readonly bottom: number
16
+ readonly right: number
17
+ }
18
+
19
+ export function cellRange(anchor: CellPoint, focus: CellPoint): CellRange {
20
+ return {
21
+ top: Math.min(anchor.row, focus.row),
22
+ left: Math.min(anchor.column, focus.column),
23
+ bottom: Math.max(anchor.row, focus.row),
24
+ right: Math.max(anchor.column, focus.column),
25
+ }
26
+ }
27
+
28
+ export function inRange(range: CellRange, row: number, column: number): boolean {
29
+ return row >= range.top && row <= range.bottom && column >= range.left && column <= range.right
30
+ }
31
+
32
+ /** `A`, `Z`, `AA`, `AB`… */
33
+ export function columnLetter(index: number): string {
34
+ let letters = ""
35
+ for (let rest = index + 1; rest > 0; rest = Math.floor((rest - 1) / 26)) {
36
+ letters = String.fromCharCode(65 + ((rest - 1) % 26)) + letters
37
+ }
38
+ return letters
39
+ }
40
+
41
+ /** `B2`, or `B2:D5` for more than one cell. */
42
+ export function rangeLabel(range: CellRange): string {
43
+ const start = `${columnLetter(range.left)}${range.top + 1}`
44
+ const end = `${columnLetter(range.right)}${range.bottom + 1}`
45
+ return start === end ? start : `${start}:${end}`
46
+ }
47
+
48
+ export interface RangeStats {
49
+ /** Cells with a value. */
50
+ readonly count: number
51
+ /** Cells of numeric columns that hold a number; the sum and average are theirs. */
52
+ readonly numericCount: number
53
+ readonly sum: number
54
+ readonly average?: number
55
+ }
56
+
57
+ /** What a spreadsheet's status bar says about a selection. Header cells are labels, not values. */
58
+ export function rangeStats(
59
+ range: CellRange,
60
+ model: SheetModel,
61
+ order: readonly number[]
62
+ ): RangeStats {
63
+ let count = 0
64
+ let numericCount = 0
65
+ let sum = 0
66
+ for (let row = Math.max(range.top, 1); row <= range.bottom; row += 1) {
67
+ const values = model.rows[order[row - 1] ?? -1]
68
+ if (!values) continue
69
+ for (let column = range.left; column <= range.right; column += 1) {
70
+ const value = values[column] ?? ""
71
+ if (!value.trim()) continue
72
+ count += 1
73
+ if (!model.columns[column]?.numeric) continue
74
+ const number = parseSheetNumber(value)
75
+ if (number === undefined) continue
76
+ numericCount += 1
77
+ sum += number
78
+ }
79
+ }
80
+ return { count, numericCount, sum, ...(numericCount > 0 ? { average: sum / numericCount } : {}) }
81
+ }
82
+
83
+ /** The selection as tab-separated values, the shape every spreadsheet pastes. */
84
+ export function rangeTsv(range: CellRange, model: SheetModel, order: readonly number[]): string {
85
+ const lines: string[] = []
86
+ for (let row = range.top; row <= range.bottom; row += 1) {
87
+ const values =
88
+ row === 0 ? model.columns.map((column) => column.name) : model.rows[order[row - 1] ?? -1]
89
+ if (!values) continue
90
+ lines.push(
91
+ values
92
+ .slice(range.left, range.right + 1)
93
+ .map((value) => value.replace(/[\t\r\n]+/g, " "))
94
+ .join("\t")
95
+ )
96
+ }
97
+ return lines.join("\n")
98
+ }
99
+
100
+ export type MoveKey = "ArrowUp" | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "Home" | "End"
101
+
102
+ /** Where a key moves the focus cell; `jump` goes to the edge, as with Cmd or Ctrl held. */
103
+ export function moveFocus(
104
+ point: CellPoint,
105
+ key: MoveKey,
106
+ bounds: { readonly rows: number; readonly columns: number },
107
+ jump = false
108
+ ): CellPoint {
109
+ const lastRow = bounds.rows - 1
110
+ const lastColumn = bounds.columns - 1
111
+ switch (key) {
112
+ case "ArrowUp":
113
+ return { ...point, row: jump ? 0 : Math.max(0, point.row - 1) }
114
+ case "ArrowDown":
115
+ return { ...point, row: jump ? lastRow : Math.min(lastRow, point.row + 1) }
116
+ case "ArrowLeft":
117
+ return { ...point, column: jump ? 0 : Math.max(0, point.column - 1) }
118
+ case "ArrowRight":
119
+ return { ...point, column: jump ? lastColumn : Math.min(lastColumn, point.column + 1) }
120
+ case "Home":
121
+ return jump ? { row: 0, column: 0 } : { ...point, column: 0 }
122
+ case "End":
123
+ return jump ? { row: lastRow, column: lastColumn } : { ...point, column: lastColumn }
124
+ }
125
+ }
126
+
127
+ export type SortDirection = "ascending" | "descending"
128
+
129
+ /** The order of the data rows sorted by one column: numbers as numbers, the rest as text. */
130
+ export function sortedOrder(
131
+ model: SheetModel,
132
+ sort: { readonly column: number; readonly direction: SortDirection } | null,
133
+ locale: string
134
+ ): number[] {
135
+ const order = model.rows.map((_, index) => index)
136
+ if (!sort) return order
137
+ const column = model.columns[sort.column]
138
+ const collator = new Intl.Collator(locale, { numeric: true, sensitivity: "base" })
139
+ const key = (index: number) => model.rows[index]?.[sort.column] ?? ""
140
+ const sign = sort.direction === "ascending" ? 1 : -1
141
+ return order.sort((a, b) => {
142
+ const left = key(a)
143
+ const right = key(b)
144
+ // Empty cells go last whichever way the column sorts.
145
+ if (!left.trim() || !right.trim()) return (left.trim() ? 0 : 1) - (right.trim() ? 0 : 1)
146
+ if (column?.numeric) {
147
+ const difference = (parseSheetNumber(left) ?? 0) - (parseSheetNumber(right) ?? 0)
148
+ if (difference !== 0) return sign * difference
149
+ }
150
+ return sign * collator.compare(left, right) || a - b
151
+ })
152
+ }