@eifi1/ui-kit 0.11.0 → 0.13.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 (397) hide show
  1. package/README.md +59 -34
  2. package/dist/chart.d.ts +3 -1
  3. package/dist/chart.js +2 -0
  4. package/dist/chart.js.map +1 -1
  5. package/dist/components/account-settings-labels.d.ts +105 -0
  6. package/dist/components/account-settings-labels.js +81 -0
  7. package/dist/components/account-settings-labels.js.map +1 -0
  8. package/dist/components/account-settings.d.ts +36 -52
  9. package/dist/components/account-settings.js +44 -8
  10. package/dist/components/account-settings.js.map +1 -1
  11. package/dist/components/amount-input.d.ts +40 -4
  12. package/dist/components/amount-input.js +50 -12
  13. package/dist/components/amount-input.js.map +1 -1
  14. package/dist/components/authed-image.d.ts +68 -0
  15. package/dist/components/authed-image.js +117 -0
  16. package/dist/components/authed-image.js.map +1 -0
  17. package/dist/components/breadcrumbs.d.ts +2 -1
  18. package/dist/components/breadcrumbs.js +5 -2
  19. package/dist/components/breadcrumbs.js.map +1 -1
  20. package/dist/components/bulk-action-bar.d.ts +10 -2
  21. package/dist/components/bulk-action-bar.js.map +1 -1
  22. package/dist/components/button-group.d.ts +51 -2
  23. package/dist/components/button-group.js +5 -1
  24. package/dist/components/button-group.js.map +1 -1
  25. package/dist/components/calculator.d.ts +15 -4
  26. package/dist/components/calendar-heatmap.d.ts +41 -3
  27. package/dist/components/calendar-heatmap.js +48 -7
  28. package/dist/components/calendar-heatmap.js.map +1 -1
  29. package/dist/components/chart.js +14 -8
  30. package/dist/components/chart.js.map +1 -1
  31. package/dist/components/chip.d.ts +2 -1
  32. package/dist/components/chip.js +26 -18
  33. package/dist/components/chip.js.map +1 -1
  34. package/dist/components/choice-card.d.ts +35 -3
  35. package/dist/components/choice-card.js +83 -25
  36. package/dist/components/choice-card.js.map +1 -1
  37. package/dist/components/combobox-core.d.ts +3 -1
  38. package/dist/components/combobox-core.js +18 -5
  39. package/dist/components/combobox-core.js.map +1 -1
  40. package/dist/components/combobox.d.ts +2 -2
  41. package/dist/components/combobox.js +31 -9
  42. package/dist/components/combobox.js.map +1 -1
  43. package/dist/components/copy-button.d.ts +51 -70
  44. package/dist/components/copy-button.js +1 -1
  45. package/dist/components/copy-button.js.map +1 -1
  46. package/dist/components/data-table-cells.d.ts +63 -0
  47. package/dist/components/data-table-cells.js +64 -0
  48. package/dist/components/data-table-cells.js.map +1 -0
  49. package/dist/components/data-table-filter-popover.d.ts +1 -1
  50. package/dist/components/data-table-filter-popover.js +8 -1
  51. package/dist/components/data-table-filter-popover.js.map +1 -1
  52. package/dist/components/data-table-filters.d.ts +1 -1
  53. package/dist/components/data-table-filters.js +31 -1
  54. package/dist/components/data-table-filters.js.map +1 -1
  55. package/dist/components/data-table-labels.d.ts +13 -0
  56. package/dist/components/data-table-labels.js +8 -1
  57. package/dist/components/data-table-labels.js.map +1 -1
  58. package/dist/components/data-table.d.ts +1 -1
  59. package/dist/components/data-table.js +125 -27
  60. package/dist/components/data-table.js.map +1 -1
  61. package/dist/components/date-picker.d.ts +3 -1
  62. package/dist/components/date-picker.js +15 -2
  63. package/dist/components/date-picker.js.map +1 -1
  64. package/dist/components/description-list.d.ts +23 -3
  65. package/dist/components/description-list.js +23 -3
  66. package/dist/components/description-list.js.map +1 -1
  67. package/dist/components/dialog-frame.d.ts +1 -0
  68. package/dist/components/entity-combobox.d.ts +1 -1
  69. package/dist/components/entity-combobox.js +40 -20
  70. package/dist/components/entity-combobox.js.map +1 -1
  71. package/dist/components/error-boundary.d.ts +281 -0
  72. package/dist/components/error-boundary.js +302 -0
  73. package/dist/components/error-boundary.js.map +1 -0
  74. package/dist/components/field-sync.js +90 -83
  75. package/dist/components/field-sync.js.map +1 -1
  76. package/dist/components/field.d.ts +30 -4
  77. package/dist/components/field.js +40 -17
  78. package/dist/components/field.js.map +1 -1
  79. package/dist/components/file-button.d.ts +50 -208
  80. package/dist/components/file-dropzone.d.ts +49 -2
  81. package/dist/components/floating-panel.d.ts +13 -4
  82. package/dist/components/floating-panel.js +7 -3
  83. package/dist/components/floating-panel.js.map +1 -1
  84. package/dist/components/form-actions.d.ts +53 -0
  85. package/dist/components/form-actions.js +98 -0
  86. package/dist/components/form-actions.js.map +1 -0
  87. package/dist/components/full-bleed-dialog.js +4 -0
  88. package/dist/components/full-bleed-dialog.js.map +1 -1
  89. package/dist/components/hover-menu.js +64 -56
  90. package/dist/components/hover-menu.js.map +1 -1
  91. package/dist/components/image-grid.d.ts +67 -0
  92. package/dist/components/image-grid.js +112 -0
  93. package/dist/components/image-grid.js.map +1 -0
  94. package/dist/components/lightbox.d.ts +104 -0
  95. package/dist/components/lightbox.js +229 -0
  96. package/dist/components/lightbox.js.map +1 -0
  97. package/dist/components/line-items.d.ts +114 -0
  98. package/dist/components/line-items.js +238 -0
  99. package/dist/components/line-items.js.map +1 -0
  100. package/dist/components/list.d.ts +37 -4
  101. package/dist/components/list.js +35 -14
  102. package/dist/components/list.js.map +1 -1
  103. package/dist/components/loading-state.d.ts +24 -0
  104. package/dist/components/loading-state.js +32 -0
  105. package/dist/components/loading-state.js.map +1 -0
  106. package/dist/components/menu-item.d.ts +2 -1
  107. package/dist/components/menu-item.js +5 -1
  108. package/dist/components/menu-item.js.map +1 -1
  109. package/dist/components/mini-calendar.d.ts +13 -1
  110. package/dist/components/mini-calendar.js +46 -2
  111. package/dist/components/mini-calendar.js.map +1 -1
  112. package/dist/components/modal.d.ts +20 -1
  113. package/dist/components/modal.js +26 -2
  114. package/dist/components/modal.js.map +1 -1
  115. package/dist/components/month-picker.d.ts +21 -1
  116. package/dist/components/month-picker.js +139 -26
  117. package/dist/components/month-picker.js.map +1 -1
  118. package/dist/components/multi-entity-combobox.d.ts +1 -1
  119. package/dist/components/multi-entity-combobox.js +42 -20
  120. package/dist/components/multi-entity-combobox.js.map +1 -1
  121. package/dist/components/nav-pills.d.ts +2 -1
  122. package/dist/components/nav-pills.js +5 -1
  123. package/dist/components/nav-pills.js.map +1 -1
  124. package/dist/components/number-field.d.ts +24 -5
  125. package/dist/components/number-field.js +5 -7
  126. package/dist/components/number-field.js.map +1 -1
  127. package/dist/components/number-input.d.ts +25 -5
  128. package/dist/components/number-input.js +7 -1
  129. package/dist/components/number-input.js.map +1 -1
  130. package/dist/components/numpad-sheet.d.ts +20 -5
  131. package/dist/components/numpad-sheet.js +3 -2
  132. package/dist/components/numpad-sheet.js.map +1 -1
  133. package/dist/components/page-header.d.ts +35 -6
  134. package/dist/components/page-header.js +7 -1
  135. package/dist/components/page-header.js.map +1 -1
  136. package/dist/components/passkeys-setting.d.ts +67 -0
  137. package/dist/components/passkeys-setting.js +214 -0
  138. package/dist/components/passkeys-setting.js.map +1 -0
  139. package/dist/components/picker-sheet.js +5 -1
  140. package/dist/components/picker-sheet.js.map +1 -1
  141. package/dist/components/pie-chart-labels.d.ts +20 -0
  142. package/dist/components/pie-chart-labels.js +12 -0
  143. package/dist/components/pie-chart-labels.js.map +1 -0
  144. package/dist/components/pie-chart.d.ts +101 -0
  145. package/dist/components/pie-chart.js +283 -0
  146. package/dist/components/pie-chart.js.map +1 -0
  147. package/dist/components/popover.js +3 -0
  148. package/dist/components/popover.js.map +1 -1
  149. package/dist/components/progress-bar.d.ts +44 -3
  150. package/dist/components/progress-bar.js +58 -22
  151. package/dist/components/progress-bar.js.map +1 -1
  152. package/dist/components/qr-code.d.ts +34 -0
  153. package/dist/components/qr-code.js +57 -0
  154. package/dist/components/qr-code.js.map +1 -0
  155. package/dist/components/search-field.d.ts +15 -3
  156. package/dist/components/search-field.js +5 -1
  157. package/dist/components/search-field.js.map +1 -1
  158. package/dist/components/settings-fields.d.ts +49 -1
  159. package/dist/components/signed-amount.d.ts +122 -0
  160. package/dist/components/signed-amount.js +152 -0
  161. package/dist/components/signed-amount.js.map +1 -0
  162. package/dist/components/skeleton.d.ts +10 -3
  163. package/dist/components/skeleton.js +12 -2
  164. package/dist/components/skeleton.js.map +1 -1
  165. package/dist/components/stat-tile.d.ts +2 -1
  166. package/dist/components/stat-tile.js +6 -3
  167. package/dist/components/stat-tile.js.map +1 -1
  168. package/dist/components/swipeable-row.js +4 -3
  169. package/dist/components/swipeable-row.js.map +1 -1
  170. package/dist/components/table.d.ts +39 -5
  171. package/dist/components/table.js +45 -11
  172. package/dist/components/table.js.map +1 -1
  173. package/dist/components/text-link.d.ts +238 -0
  174. package/dist/components/text-link.js +144 -0
  175. package/dist/components/text-link.js.map +1 -0
  176. package/dist/components/text.d.ts +26 -2
  177. package/dist/components/text.js +16 -2
  178. package/dist/components/text.js.map +1 -1
  179. package/dist/components/time-input.d.ts +49 -1
  180. package/dist/components/toggle-group.d.ts +35 -0
  181. package/dist/components/toggle-group.js +43 -14
  182. package/dist/components/toggle-group.js.map +1 -1
  183. package/dist/components/toggle-legend.d.ts +53 -3
  184. package/dist/components/toggle-legend.js +54 -1
  185. package/dist/components/toggle-legend.js.map +1 -1
  186. package/dist/components/tooltip.d.ts +51 -1
  187. package/dist/components/tooltip.js +59 -35
  188. package/dist/components/tooltip.js.map +1 -1
  189. package/dist/components/ui.d.ts +51 -779
  190. package/dist/components/ui.js +364 -34
  191. package/dist/components/ui.js.map +1 -1
  192. package/dist/components/use-table-state.d.ts +3 -52
  193. package/dist/components/use-table-state.js +128 -30
  194. package/dist/components/use-table-state.js.map +1 -1
  195. package/dist/{data-table-filters-Dh9uF_S-.d.ts → data-table-Drun8Fk9.d.ts} +305 -61
  196. package/dist/data-table.d.ts +2 -1
  197. package/dist/data-table.js +5 -1
  198. package/dist/data-table.js.map +1 -1
  199. package/dist/feedback/feedback-attachment.js +4 -6
  200. package/dist/feedback/feedback-attachment.js.map +1 -1
  201. package/dist/feedback/feedback-dialog.d.ts +26 -3
  202. package/dist/feedback/feedback-dialog.js +21 -5
  203. package/dist/feedback/feedback-dialog.js.map +1 -1
  204. package/dist/feedback/feedback-inbox.js +86 -75
  205. package/dist/feedback/feedback-inbox.js.map +1 -1
  206. package/dist/feedback/feedback-thread.d.ts +173 -0
  207. package/dist/feedback/feedback-thread.js +264 -0
  208. package/dist/feedback/feedback-thread.js.map +1 -0
  209. package/dist/feedback.d.ts +2 -1
  210. package/dist/feedback.js +1 -0
  211. package/dist/feedback.js.map +1 -1
  212. package/dist/hooks/use-authed-src.d.ts +71 -0
  213. package/dist/hooks/use-authed-src.js +63 -0
  214. package/dist/hooks/use-authed-src.js.map +1 -0
  215. package/dist/hooks/use-file-drop.d.ts +49 -2
  216. package/dist/hooks/use-hotkey.d.ts +59 -0
  217. package/dist/hooks/use-hotkey.js +105 -0
  218. package/dist/hooks/use-hotkey.js.map +1 -0
  219. package/dist/hooks/use-search-param-state.d.ts +83 -0
  220. package/dist/hooks/use-search-param-state.js +94 -0
  221. package/dist/hooks/use-search-param-state.js.map +1 -0
  222. package/dist/i18n/defaults.d.ts +15 -4
  223. package/dist/i18n/defaults.js +31 -1
  224. package/dist/i18n/defaults.js.map +1 -1
  225. package/dist/i18n/kit-labels.d.ts +46 -312
  226. package/dist/i18n/kit-labels.js +20 -5
  227. package/dist/i18n/kit-labels.js.map +1 -1
  228. package/dist/i18n/locales/de-CH-informal.d.ts +15 -4
  229. package/dist/i18n/locales/de-CH.d.ts +15 -4
  230. package/dist/i18n/locales/de-informal.d.ts +15 -4
  231. package/dist/i18n/locales/de.d.ts +15 -4
  232. package/dist/i18n/locales/de.js +164 -3
  233. package/dist/i18n/locales/de.js.map +1 -1
  234. package/dist/i18n/locales/es.d.ts +15 -4
  235. package/dist/i18n/locales/es.js +161 -3
  236. package/dist/i18n/locales/es.js.map +1 -1
  237. package/dist/i18n/locales/fr.d.ts +15 -4
  238. package/dist/i18n/locales/fr.js +161 -3
  239. package/dist/i18n/locales/fr.js.map +1 -1
  240. package/dist/i18n/locales/hu.d.ts +15 -4
  241. package/dist/i18n/locales/hu.js +161 -3
  242. package/dist/i18n/locales/hu.js.map +1 -1
  243. package/dist/i18n/locales/it.d.ts +15 -4
  244. package/dist/i18n/locales/it.js +161 -3
  245. package/dist/i18n/locales/it.js.map +1 -1
  246. package/dist/i18n/locales/zh.d.ts +15 -4
  247. package/dist/i18n/locales/zh.js +161 -3
  248. package/dist/i18n/locales/zh.js.map +1 -1
  249. package/dist/index.d.ts +39 -19
  250. package/dist/index.js +40 -1
  251. package/dist/index.js.map +1 -1
  252. package/dist/kit-labels-Mz8zZHIF.d.ts +1744 -0
  253. package/dist/lib/format.d.ts +162 -0
  254. package/dist/lib/format.js +151 -0
  255. package/dist/lib/format.js.map +1 -0
  256. package/dist/lib/qr-encode.d.ts +44 -0
  257. package/dist/lib/qr-encode.js +338 -0
  258. package/dist/lib/qr-encode.js.map +1 -0
  259. package/dist/rhf/fields.d.ts +283 -0
  260. package/dist/rhf/fields.js +631 -0
  261. package/dist/rhf/fields.js.map +1 -0
  262. package/dist/rhf/form.d.ts +49 -1
  263. package/dist/rhf/line-items.d.ts +31 -0
  264. package/dist/rhf/line-items.js +59 -0
  265. package/dist/rhf/line-items.js.map +1 -0
  266. package/dist/rhf.d.ts +61 -1
  267. package/dist/rhf.js +2 -0
  268. package/dist/rhf.js.map +1 -1
  269. package/dist/search/command-palette.js +20 -14
  270. package/dist/search/command-palette.js.map +1 -1
  271. package/dist/shell/app-shell.d.ts +99 -2
  272. package/dist/shell/app-shell.js +120 -57
  273. package/dist/shell/app-shell.js.map +1 -1
  274. package/dist/shell/auth-layout.d.ts +74 -0
  275. package/dist/shell/auth-layout.js +97 -0
  276. package/dist/shell/auth-layout.js.map +1 -0
  277. package/dist/shell/top-bar-brand.d.ts +81 -0
  278. package/dist/shell/top-bar-brand.js +47 -0
  279. package/dist/shell/top-bar-brand.js.map +1 -0
  280. package/dist/shell/topbar-action-menu.d.ts +21 -4
  281. package/dist/shell/topbar-action-menu.js +18 -2
  282. package/dist/shell/topbar-action-menu.js.map +1 -1
  283. package/dist/shell.d.ts +56 -1
  284. package/dist/shell.js +2 -0
  285. package/dist/shell.js.map +1 -1
  286. package/dist/tour/tour.js +10 -4
  287. package/dist/tour/tour.js.map +1 -1
  288. package/dist/wizard/stepper-nav.d.ts +48 -1
  289. package/dist/wizard/use-wizard.js +4 -2
  290. package/dist/wizard/use-wizard.js.map +1 -1
  291. package/dist/wizard/wizard-context.d.ts +24 -1
  292. package/dist/wizard/wizard-context.js +19 -2
  293. package/dist/wizard/wizard-context.js.map +1 -1
  294. package/dist/wizard/wizard-step.d.ts +30 -5
  295. package/dist/wizard/wizard-step.js +34 -3
  296. package/dist/wizard/wizard-step.js.map +1 -1
  297. package/dist/wizard.d.ts +50 -3
  298. package/package.json +6 -6
  299. package/src/chart.ts +4 -0
  300. package/src/components/account-settings-labels.ts +190 -0
  301. package/src/components/account-settings.tsx +110 -63
  302. package/src/components/amount-input.tsx +109 -10
  303. package/src/components/authed-image.tsx +190 -0
  304. package/src/components/breadcrumbs.tsx +8 -2
  305. package/src/components/bulk-action-bar.tsx +16 -3
  306. package/src/components/button-group.tsx +7 -2
  307. package/src/components/calendar-heatmap.tsx +105 -10
  308. package/src/components/chart.tsx +19 -8
  309. package/src/components/chip.tsx +14 -4
  310. package/src/components/choice-card.tsx +136 -25
  311. package/src/components/combobox-core.tsx +33 -9
  312. package/src/components/combobox.tsx +33 -9
  313. package/src/components/copy-button.tsx +3 -2
  314. package/src/components/data-table-cells.tsx +119 -0
  315. package/src/components/data-table-filter-popover.tsx +13 -5
  316. package/src/components/data-table-filters.ts +77 -0
  317. package/src/components/data-table-labels.ts +23 -0
  318. package/src/components/data-table.tsx +230 -13
  319. package/src/components/date-picker.tsx +19 -3
  320. package/src/components/description-list.tsx +52 -3
  321. package/src/components/entity-combobox.tsx +25 -7
  322. package/src/components/error-boundary.tsx +640 -0
  323. package/src/components/field-sync.tsx +48 -40
  324. package/src/components/field.tsx +56 -5
  325. package/src/components/floating-panel.tsx +20 -7
  326. package/src/components/form-actions.tsx +191 -0
  327. package/src/components/full-bleed-dialog.tsx +4 -0
  328. package/src/components/hover-menu.tsx +26 -17
  329. package/src/components/image-grid.tsx +181 -0
  330. package/src/components/lightbox.tsx +374 -0
  331. package/src/components/line-items.tsx +385 -0
  332. package/src/components/list.tsx +82 -19
  333. package/src/components/loading-state.tsx +47 -0
  334. package/src/components/menu-item.tsx +7 -2
  335. package/src/components/mini-calendar.tsx +72 -3
  336. package/src/components/modal.tsx +65 -3
  337. package/src/components/month-picker.tsx +181 -26
  338. package/src/components/multi-entity-combobox.tsx +27 -7
  339. package/src/components/nav-pills.tsx +8 -1
  340. package/src/components/number-field.tsx +15 -10
  341. package/src/components/number-input.tsx +17 -1
  342. package/src/components/numpad-sheet.tsx +6 -1
  343. package/src/components/page-header.tsx +42 -5
  344. package/src/components/passkeys-setting.tsx +331 -0
  345. package/src/components/picker-sheet.tsx +10 -1
  346. package/src/components/pie-chart-labels.ts +32 -0
  347. package/src/components/pie-chart.tsx +479 -0
  348. package/src/components/popover.tsx +3 -0
  349. package/src/components/progress-bar.tsx +129 -19
  350. package/src/components/qr-code.tsx +83 -0
  351. package/src/components/search-field.tsx +20 -4
  352. package/src/components/signed-amount.tsx +295 -0
  353. package/src/components/skeleton.tsx +21 -3
  354. package/src/components/stat-tile.tsx +11 -3
  355. package/src/components/swipeable-row.tsx +7 -7
  356. package/src/components/table.tsx +102 -13
  357. package/src/components/text-link.tsx +356 -0
  358. package/src/components/text.tsx +46 -2
  359. package/src/components/toggle-group.tsx +93 -20
  360. package/src/components/toggle-legend.tsx +108 -2
  361. package/src/components/tooltip.tsx +79 -8
  362. package/src/components/ui.tsx +707 -49
  363. package/src/components/use-table-state.ts +265 -33
  364. package/src/data-table.ts +10 -0
  365. package/src/feedback/feedback-attachment.tsx +10 -7
  366. package/src/feedback/feedback-dialog.tsx +49 -7
  367. package/src/feedback/feedback-inbox.tsx +7 -0
  368. package/src/feedback/feedback-thread.tsx +510 -0
  369. package/src/feedback.ts +1 -0
  370. package/src/hooks/use-authed-src.ts +161 -0
  371. package/src/hooks/use-hotkey.ts +172 -0
  372. package/src/hooks/use-search-param-state.ts +187 -0
  373. package/src/i18n/defaults.ts +30 -0
  374. package/src/i18n/kit-labels.tsx +118 -8
  375. package/src/i18n/locales/de.ts +161 -0
  376. package/src/i18n/locales/es.ts +158 -0
  377. package/src/i18n/locales/fr.ts +161 -0
  378. package/src/i18n/locales/hu.ts +159 -0
  379. package/src/i18n/locales/it.ts +159 -0
  380. package/src/i18n/locales/zh.ts +158 -0
  381. package/src/index.ts +59 -0
  382. package/src/lib/format.ts +348 -0
  383. package/src/lib/qr-encode.ts +420 -0
  384. package/src/rhf/fields.tsx +1135 -0
  385. package/src/rhf/line-items.tsx +130 -0
  386. package/src/rhf.ts +6 -0
  387. package/src/search/command-palette.tsx +29 -19
  388. package/src/shell/app-shell.tsx +153 -3
  389. package/src/shell/auth-layout.tsx +190 -0
  390. package/src/shell/top-bar-brand.tsx +76 -0
  391. package/src/shell/topbar-action-menu.tsx +32 -4
  392. package/src/shell.ts +2 -0
  393. package/src/tour/tour.tsx +16 -5
  394. package/src/wizard/use-wizard.ts +8 -4
  395. package/src/wizard/wizard-context.tsx +41 -1
  396. package/src/wizard/wizard-step.tsx +75 -6
  397. package/tokens.css +12 -0
@@ -0,0 +1,510 @@
1
+ import { useEffect, useImperativeHandle, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
2
+ import type { ReactNode, Ref } from "react";
3
+ import { Paperclip, Send } from "lucide-react";
4
+ import { cn } from "../lib/cn";
5
+ import { formatRelativeTime } from "../lib/format";
6
+ import { Button, EmptyState, Spinner, Textarea } from "../components/ui";
7
+ import { Skeleton } from "../components/skeleton";
8
+ import { Tooltip } from "../components/tooltip";
9
+ import { useKitFileLabels, useKitLabels, useKitLocale } from "../i18n/kit-labels";
10
+ import { FeedbackAttachmentField } from "./feedback-attachment";
11
+ import type { FeedbackNoteAttachment } from "./feedback-inbox";
12
+ import type { FeedbackAttachmentLabels } from "./feedback-dialog";
13
+
14
+ /**
15
+ * The conversation under a report: who said what, when, with what attached — and the
16
+ * box to answer in.
17
+ *
18
+ * Both apps had written it by hand. kastlan's feedback page grew a comment list of
19
+ * bordered boxes with `slate-*` literals (feedback-page.tsx, "keksdose left this
20
+ * unbuilt"), and keksdose built the same thing a second time for support
21
+ * (support-thread.tsx) with sides, bubbles and a composer that clears only what it
22
+ * sent. This is that second one, generalised: the kit owns the look and the
23
+ * behaviour, the app owns the data, the upload and every word it does not share.
24
+ *
25
+ * **Own vs other** is the caller's per message (`own`), not a comparison the kit makes
26
+ * against a user id: whose message sits at the END side depends on who is reading,
27
+ * and only the app knows that (keksdose's support thread labelled a member's question
28
+ * "Du" in the admin panel the first time round, by reading the wrong field).
29
+ *
30
+ * **Redaction.** A thread is somebody's words about their own data — exactly what a
31
+ * host's demo / screen-share mode blurs. Bodies, authors and attachments carry
32
+ * `data-private` (the attribute `Tooltip redact` and `StatTile sensitive` use) unless
33
+ * `redact={false}`.
34
+ *
35
+ * **RTL.** Sides are logical — `justify-end` is the reading end, and the bubble's
36
+ * tucked corner is `rounded-ee` / `rounded-es` — and each body is `dir="auto"`, so a
37
+ * Hebrew reply in an English thread (or the reverse) lays out as its own language.
38
+ */
39
+
40
+ /* ── Labels ───────────────────────────────────────────────────────────────── */
41
+
42
+ /** The `feedbackThread` namespace of `<UiKitProvider labels>`. */
43
+ export interface FeedbackThreadLabels {
44
+ /** Accessible name of the message list. */
45
+ thread: string;
46
+ empty: string;
47
+ loading: string;
48
+ /** Author line of an `own` message that has no `author` of its own. */
49
+ you: string;
50
+ /** The badge beside a `staff` author. */
51
+ staff: string;
52
+ /** Accessible name of a message's attachment list. */
53
+ attachments: string;
54
+ }
55
+
56
+ export const DEFAULT_FEEDBACK_THREAD_LABELS: FeedbackThreadLabels = {
57
+ thread: "Comments",
58
+ empty: "No comments yet",
59
+ loading: "Loading comments…",
60
+ you: "You",
61
+ staff: "Staff",
62
+ attachments: "Attachments",
63
+ };
64
+
65
+ /** The `feedbackComposer` namespace of `<UiKitProvider labels>`. The attach buttons'
66
+ * words are `feedbackAttachment`'s, shared with the dialog and the note editor. */
67
+ export interface FeedbackComposerLabels {
68
+ /** Accessible name of the text box. */
69
+ field: string;
70
+ placeholder: string;
71
+ send: string;
72
+ /** The shortcut line, given the platform's modifier ("Ctrl" or "⌘"). */
73
+ sendHint: (modifier: string) => string;
74
+ }
75
+
76
+ export const DEFAULT_FEEDBACK_COMPOSER_LABELS: FeedbackComposerLabels = {
77
+ field: "Write a comment",
78
+ placeholder: "Write a comment…",
79
+ send: "Send",
80
+ sendHint: (modifier) => `${modifier} + Enter to send`,
81
+ };
82
+
83
+ /* ── Thread ───────────────────────────────────────────────────────────────── */
84
+
85
+ export interface FeedbackThreadAttachment {
86
+ id?: string | number;
87
+ name: string;
88
+ /** Where to open it. Left out, the attachment is listed by name only. */
89
+ url?: string;
90
+ /** MIME type; an `image/*` with a `url` is shown as a thumbnail. */
91
+ type?: string;
92
+ /** Bytes, shown beside a non-image. */
93
+ size?: number;
94
+ }
95
+
96
+ export interface FeedbackThreadMessage {
97
+ id: string | number;
98
+ /** Who wrote it. Left out on an `own` message, it reads "You". */
99
+ author?: ReactNode;
100
+ /** A `Date`, an ISO string or epoch ms. */
101
+ createdAt: Date | string | number;
102
+ body: string;
103
+ attachments?: readonly FeedbackThreadAttachment[];
104
+ /** Written by whoever is reading: sits at the end side, in the brand tint. */
105
+ own?: boolean;
106
+ /** Written by the team rather than a user: gets the "Staff" badge. */
107
+ staff?: boolean;
108
+ }
109
+
110
+ export interface FeedbackThreadProps {
111
+ messages: readonly FeedbackThreadMessage[];
112
+ loading?: boolean;
113
+ /** Replaces the default "No comments yet". */
114
+ empty?: ReactNode;
115
+ /**
116
+ * Draw one attachment yourself — for files behind auth, which a plain `<img src>`
117
+ * cannot fetch (kastlan's `AuthedImage`), or a lightbox. The default is a thumbnail
118
+ * link for an image with a `url`, a file link otherwise.
119
+ */
120
+ renderAttachment?: (attachment: FeedbackThreadAttachment, message: FeedbackThreadMessage) => ReactNode;
121
+ /** Tag bodies, authors and attachments `data-private`. Default true. */
122
+ redact?: boolean;
123
+ /** "Now" for the relative times — pin it in tests and stories. Default: the clock. */
124
+ now?: Date | number;
125
+ /** Overrides the provider's locale for the times. */
126
+ locale?: string;
127
+ labels?: Partial<FeedbackThreadLabels>;
128
+ className?: string;
129
+ }
130
+
131
+ export function FeedbackThread({
132
+ messages,
133
+ loading = false,
134
+ empty,
135
+ renderAttachment,
136
+ redact = true,
137
+ now,
138
+ locale: localeProp,
139
+ labels: labelsProp,
140
+ className,
141
+ }: FeedbackThreadProps) {
142
+ const labels = useKitLabels("feedbackThread", DEFAULT_FEEDBACK_THREAD_LABELS, labelsProp);
143
+ const locale = useKitLocale(localeProp);
144
+ const file = useKitFileLabels();
145
+ const priv = redact ? "" : undefined;
146
+ const clock = useMinuteClock(now === undefined);
147
+ const reference = now === undefined ? clock : +now;
148
+
149
+ if (loading && messages.length === 0) {
150
+ return <Skeleton lines={3} label={labels.loading} className={className} />;
151
+ }
152
+ if (messages.length === 0) {
153
+ return (
154
+ <EmptyState
155
+ variant="inline"
156
+ size="sm"
157
+ title={empty ?? labels.empty}
158
+ className={cn("justify-start", className)}
159
+ />
160
+ );
161
+ }
162
+
163
+ return (
164
+ <ol aria-label={labels.thread} aria-busy={loading || undefined} className={cn("space-y-3", className)}>
165
+ {messages.map((message) => {
166
+ const date = toDate(message.createdAt);
167
+ const author = message.author ?? (message.own ? labels.you : null);
168
+ const attachments = message.attachments ?? [];
169
+ return (
170
+ <li
171
+ key={message.id}
172
+ className={cn("flex", message.own ? "justify-end" : "justify-start")}
173
+ data-own={message.own ? "" : undefined}
174
+ >
175
+ <article
176
+ className={cn(
177
+ "max-w-[85%] min-w-0 rounded-lg px-3 py-2 text-sm",
178
+ message.own
179
+ ? "rounded-ee-sm bg-[var(--brand-bg)]"
180
+ : "rounded-es-sm bg-[var(--bg-surface-2)]",
181
+ )}
182
+ >
183
+ <header className="mb-0.5 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-[11px] text-[var(--text-muted)]">
184
+ {author != null && (
185
+ <span data-private={priv} className="font-medium text-[var(--text-secondary)]">
186
+ {author}
187
+ </span>
188
+ )}
189
+ {message.staff && (
190
+ <span className="rounded border border-[var(--border)] bg-[var(--bg-surface)] px-1 text-[10px] font-medium uppercase tracking-wide text-[var(--text-secondary)]">
191
+ {labels.staff}
192
+ </span>
193
+ )}
194
+ {date && (
195
+ // The kit's Tooltip, not a native title: one tooltip look app-wide
196
+ // (keksdose F0 / dev#523).
197
+ <Tooltip label={new Intl.DateTimeFormat(locale, { dateStyle: "full", timeStyle: "short" }).format(date)}>
198
+ <time dateTime={date.toISOString()} className="tabular-nums">
199
+ {formatRelativeTime(date, {
200
+ now: reference,
201
+ locale,
202
+ // Past the kit's day steps it is a date: "2 weeks ago" makes
203
+ // the reader do arithmetic the date already did.
204
+ absoluteAfterDays: 6.5,
205
+ })}
206
+ </time>
207
+ </Tooltip>
208
+ )}
209
+ </header>
210
+ {message.body && (
211
+ <p
212
+ data-private={priv}
213
+ dir="auto"
214
+ className="whitespace-pre-wrap break-words text-start text-[var(--text-primary)]"
215
+ >
216
+ {message.body}
217
+ </p>
218
+ )}
219
+ {attachments.length > 0 && (
220
+ <ul aria-label={labels.attachments} data-private={priv} className="mt-2 flex flex-wrap gap-2">
221
+ {attachments.map((attachment, index) => (
222
+ <li key={attachment.id ?? `${index}-${attachment.name}`} className="min-w-0">
223
+ {renderAttachment ? (
224
+ renderAttachment(attachment, message)
225
+ ) : (
226
+ <DefaultAttachment attachment={attachment} size={file.size} />
227
+ )}
228
+ </li>
229
+ ))}
230
+ </ul>
231
+ )}
232
+ </article>
233
+ </li>
234
+ );
235
+ })}
236
+ </ol>
237
+ );
238
+ }
239
+
240
+ function DefaultAttachment({
241
+ attachment,
242
+ size,
243
+ }: {
244
+ attachment: FeedbackThreadAttachment;
245
+ size: (bytes: number) => string;
246
+ }) {
247
+ const { name, url, type } = attachment;
248
+ if (url && type?.startsWith("image/")) {
249
+ return (
250
+ <a href={url} target="_blank" rel="noopener noreferrer" className="block">
251
+ <img
252
+ src={url}
253
+ alt={name}
254
+ loading="lazy"
255
+ className="h-20 w-20 rounded border border-[var(--border)] object-cover"
256
+ />
257
+ </a>
258
+ );
259
+ }
260
+ const content = (
261
+ <>
262
+ <Paperclip className="size-3.5 shrink-0" aria-hidden />
263
+ <span className="truncate">{name}</span>
264
+ {attachment.size !== undefined && (
265
+ <span className="shrink-0 text-[var(--text-muted)]">{size(attachment.size)}</span>
266
+ )}
267
+ </>
268
+ );
269
+ const chip =
270
+ "inline-flex max-w-full items-center gap-1 rounded border border-[var(--border)] bg-[var(--bg-surface)] px-2 py-1 text-xs text-[var(--text-secondary)]";
271
+ return url ? (
272
+ <a href={url} target="_blank" rel="noopener noreferrer" className={cn(chip, "hover:bg-[var(--bg-hover)]")}>
273
+ {content}
274
+ </a>
275
+ ) : (
276
+ <span className={chip}>{content}</span>
277
+ );
278
+ }
279
+
280
+ /** The time, refreshed once a minute while `active`, so "2 minutes ago" does not stay
281
+ * "just now" on a page left open. Read in state rather than in render, which must be
282
+ * pure. */
283
+ function useMinuteClock(active: boolean): number {
284
+ const [time, setTime] = useState(() => Date.now());
285
+ useEffect(() => {
286
+ if (!active) return;
287
+ const id = setInterval(() => setTime(Date.now()), 60_000);
288
+ return () => clearInterval(id);
289
+ }, [active]);
290
+ return time;
291
+ }
292
+
293
+ function toDate(value: Date | string | number): Date | null {
294
+ const date = value instanceof Date ? value : new Date(value);
295
+ return Number.isNaN(date.getTime()) ? null : date;
296
+ }
297
+
298
+ /* ── Composer ─────────────────────────────────────────────────────────────── */
299
+
300
+ /** What the composer's attach control takes — {@link FeedbackNoteAttachment} with the
301
+ * labels optional, since the field reads `feedbackAttachment` from the provider. */
302
+ export type FeedbackComposerAttachment = Omit<FeedbackNoteAttachment, "labels"> & {
303
+ labels?: Partial<FeedbackAttachmentLabels>;
304
+ };
305
+
306
+ export interface FeedbackComposerProps {
307
+ /**
308
+ * Send the comment. Return the promise: the box clears when it RESOLVES, and only of
309
+ * what was sent — text typed while it was in flight stays. A rejection keeps
310
+ * everything for a retry (surface the error yourself; the composer stays quiet).
311
+ */
312
+ onSend: (body: string, attachment: File | null) => void | Promise<unknown>;
313
+ /** The send is in flight: a spinner on the button and no second send. */
314
+ pending?: boolean;
315
+ /** Offer one picture with the comment (file, paste or `onCaptureScreenshot`).
316
+ * `true` for the defaults. Left out, the composer is text only. */
317
+ attachment?: boolean | FeedbackComposerAttachment;
318
+ /** Rendered INSTEAD of the composer — the thread is closed, or read-only for this
319
+ * reader. */
320
+ disabledReason?: ReactNode;
321
+ rows?: number;
322
+ /**
323
+ * The box's hint, over the `placeholder` label — keksdose F9: an admin answering a
324
+ * support ticket is asked something else ("Reply to Anna…") than a member adding to
325
+ * their own report, and one provider-wide label cannot say both.
326
+ */
327
+ placeholder?: string;
328
+ /**
329
+ * The draft, controlled — for a host that keeps it itself (saves it per ticket,
330
+ * prefills a template). Left out, the composer keeps its own. Either way a send
331
+ * that resolves clears only what was sent: controlled, that is an
332
+ * `onValueChange("")` the host receives like any keystroke, and only if the draft
333
+ * is still the one that went out.
334
+ */
335
+ value?: string;
336
+ /** Every change to the draft — typing, {@link FeedbackComposerHandle.insertText}, the
337
+ * clear after a send. Required to change a controlled `value`. */
338
+ onValueChange?: (value: string) => void;
339
+ /** {@link FeedbackComposerHandle}: put text into the draft from outside (canned-reply
340
+ * chips) or focus the box. */
341
+ ref?: Ref<FeedbackComposerHandle>;
342
+ labels?: Partial<FeedbackComposerLabels>;
343
+ className?: string;
344
+ }
345
+
346
+ /**
347
+ * What a {@link FeedbackComposer} `ref` holds.
348
+ *
349
+ * keksdose F9: the support admin answers the same five questions all day and wants
350
+ * chips above the box ("Thanks, fixed in the next release") that drop their text in
351
+ * where the caret is. A controlled `value` alone cannot do that well — the host sees
352
+ * the string, not the caret, so it could only append — so the composer, which owns
353
+ * the textarea, does the splice.
354
+ */
355
+ export interface FeedbackComposerHandle {
356
+ /**
357
+ * Insert `text` at the caret, replacing any selection (the caret the box last had:
358
+ * a click on a chip blurs it, and the selection survives the blur), then focus the
359
+ * box with the caret after the inserted text so the reply can go on. Nothing is
360
+ * added around it — put a space or a newline in `text` if the reply needs one. A
361
+ * no-op while `disabledReason` replaces the box.
362
+ */
363
+ insertText: (text: string) => void;
364
+ focus: () => void;
365
+ }
366
+
367
+ const noSubscribe = () => () => {};
368
+ const isApple = () =>
369
+ typeof navigator !== "undefined" && /Mac|iPhone|iPad|iPod/i.test(navigator.platform || navigator.userAgent);
370
+
371
+ /**
372
+ * The answer box under a {@link FeedbackThread}: a textarea, an optional picture,
373
+ * and Send — Ctrl/⌘+Enter sends, the shortcut the report dialog and the note editor
374
+ * use, so the habit carries across the feature. Plain Enter is a new line: feedback is
375
+ * written in paragraphs, unlike a chat.
376
+ */
377
+ export function FeedbackComposer({
378
+ onSend,
379
+ pending = false,
380
+ attachment,
381
+ disabledReason,
382
+ rows = 3,
383
+ placeholder,
384
+ value,
385
+ onValueChange,
386
+ ref,
387
+ labels: labelsProp,
388
+ className,
389
+ }: FeedbackComposerProps) {
390
+ const labels = useKitLabels("feedbackComposer", DEFAULT_FEEDBACK_COMPOSER_LABELS, labelsProp);
391
+ const [ownDraft, setOwnDraft] = useState("");
392
+ const controlled = value !== undefined;
393
+ const draft = controlled ? value : ownDraft;
394
+ const [file, setFile] = useState<File | null>(null);
395
+ const root = useRef<HTMLDivElement>(null);
396
+ const box = useRef<HTMLTextAreaElement>(null);
397
+ // The draft as of the last commit. The send's `.then` and `insertText` run after
398
+ // renders their closure did not see — text typed while the send was in flight, a
399
+ // second chip clicked before the first one's render — and must not act on a stale
400
+ // string (for a controlled draft there is no updater function to lean on).
401
+ const latest = useRef(draft);
402
+ useLayoutEffect(() => {
403
+ latest.current = draft;
404
+ });
405
+ // Where the caret goes once an inserted text has rendered.
406
+ const caret = useRef<number | null>(null);
407
+ useLayoutEffect(() => {
408
+ const el = box.current;
409
+ if (caret.current === null || !el) return;
410
+ el.focus();
411
+ el.setSelectionRange(caret.current, caret.current);
412
+ caret.current = null;
413
+ });
414
+
415
+ const setDraft = (next: string) => {
416
+ latest.current = next;
417
+ if (!controlled) setOwnDraft(next);
418
+ onValueChange?.(next);
419
+ };
420
+
421
+ useImperativeHandle(ref, () => ({
422
+ insertText: (text) => {
423
+ const el = box.current;
424
+ if (!el) return;
425
+ const current = latest.current;
426
+ // A second insert before the first has rendered goes after the first, not at the
427
+ // textarea's not-yet-moved caret. Otherwise the textarea's selection, which may
428
+ // be past the end of a controlled value the host has just shortened: clamp it.
429
+ const pending = caret.current;
430
+ const start = Math.min(pending ?? el.selectionStart ?? current.length, current.length);
431
+ const end = pending ?? Math.max(start, Math.min(el.selectionEnd ?? start, current.length));
432
+ caret.current = start + text.length;
433
+ setDraft(current.slice(0, start) + text + current.slice(end));
434
+ },
435
+ focus: () => box.current?.focus(),
436
+ }));
437
+ // The server snapshot is "Ctrl"; the client corrects it after hydration.
438
+ const modifier = useSyncExternalStore(
439
+ noSubscribe,
440
+ () => (isApple() ? "⌘" : "Ctrl"),
441
+ () => "Ctrl",
442
+ );
443
+
444
+ if (disabledReason != null && disabledReason !== false) {
445
+ return <p className={cn("text-xs text-[var(--text-muted)]", className)}>{disabledReason}</p>;
446
+ }
447
+
448
+ const canSend = draft.trim().length > 0 && !pending;
449
+ const send = () => {
450
+ if (!canSend) return;
451
+ const sentDraft = draft;
452
+ const sentFile = file;
453
+ Promise.resolve(onSend(draft.trim(), file))
454
+ .then(() => {
455
+ if (latest.current === sentDraft) setDraft("");
456
+ setFile((current) => (current === sentFile ? null : current));
457
+ })
458
+ .catch(() => {});
459
+ };
460
+
461
+ const config = attachment === true ? {} : attachment || null;
462
+
463
+ return (
464
+ <div ref={root} className={cn("space-y-2", className)}>
465
+ <Textarea
466
+ rows={rows}
467
+ dir="auto"
468
+ aria-label={labels.field}
469
+ ref={box}
470
+ placeholder={placeholder ?? labels.placeholder}
471
+ value={draft}
472
+ onChange={(event) => setDraft(event.target.value)}
473
+ onKeyDown={(event) => {
474
+ if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
475
+ event.preventDefault();
476
+ send();
477
+ }
478
+ }}
479
+ />
480
+ <div className="flex flex-wrap items-end justify-between gap-2">
481
+ {config ? (
482
+ <FeedbackAttachmentField
483
+ value={file}
484
+ onChange={setFile}
485
+ labels={config.labels}
486
+ accept={config.accept}
487
+ maxBytes={config.maxBytes}
488
+ onError={config.onError}
489
+ onCaptureScreenshot={config.onCaptureScreenshot}
490
+ // The box is this field's sibling; a paste in it bubbles to `root`.
491
+ pasteFrom={root}
492
+ />
493
+ ) : (
494
+ <span />
495
+ )}
496
+ <div className="ms-auto flex items-center gap-2">
497
+ <span className="text-xs text-[var(--text-placeholder)]">{labels.sendHint(modifier)}</span>
498
+ <Button onClick={send} disabled={!canSend} aria-busy={pending || undefined}>
499
+ {pending ? (
500
+ <Spinner label={null} className="size-4" />
501
+ ) : (
502
+ <Send className="size-4 rtl:-scale-x-100" aria-hidden />
503
+ )}
504
+ {labels.send}
505
+ </Button>
506
+ </div>
507
+ </div>
508
+ </div>
509
+ );
510
+ }
package/src/feedback.ts CHANGED
@@ -6,3 +6,4 @@
6
6
  export * from "./feedback/feedback-attachment";
7
7
  export * from "./feedback/feedback-dialog";
8
8
  export * from "./feedback/feedback-inbox";
9
+ export * from "./feedback/feedback-thread";
@@ -0,0 +1,161 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+
3
+ /**
4
+ * Fetch a file with the app's own credentials and hand back an object URL for it.
5
+ *
6
+ * A plain `<img src="/api/v1/…">` cannot carry a Bearer token, so the browser gets a
7
+ * 401 and the image — and any "open it" link built on the same path — fails. Both apps
8
+ * solved this once each, by hand: kastlan's `shared/components/authed-image.tsx` (ported
9
+ * from keksdose) and its `useDocumentBlobUrl` for floor plans. This is that code, once,
10
+ * with the two things neither copy did: a loading/error state a caller can render, and
11
+ * an abort when the URL changes mid-flight (a stale response used to overwrite the new
12
+ * one's object URL — and leak the old one, since nothing revoked it).
13
+ *
14
+ * The kit does no auth of its own: the caller hands over a {@link AuthedFetcher} — its
15
+ * axios client, its `fetch` with the header — and the kit only manages the blob's life:
16
+ *
17
+ * - created when the bytes arrive,
18
+ * - revoked when the URL changes, the fetcher reports an error, or the component
19
+ * unmounts — so a long gallery session does not accumulate blobs,
20
+ * - never exposed as a token-bearing URL: the object URL is `blob:` and local.
21
+ */
22
+
23
+ /**
24
+ * The app's authenticated GET. Resolve to the file's bytes: a `Blob` (axios with
25
+ * `responseType: "blob"`), or a `Response` (plain `fetch`), whose `ok` is checked here.
26
+ *
27
+ * Honour `signal` if you can — a thumbnail scrolled past, or a lightbox paged on, then
28
+ * stops downloading. Ignoring it is safe; the result of a superseded call is discarded
29
+ * either way.
30
+ */
31
+ export type AuthedFetcher = (url: string, init: { signal: AbortSignal }) => Promise<Blob | Response>;
32
+
33
+ /** `idle`: no URL. `loading`: the fetch is in flight. `ready`: `src` is usable.
34
+ * `error`: the fetch failed (see `error`). */
35
+ export type AuthedSrcStatus = "idle" | "loading" | "ready" | "error";
36
+
37
+ export interface UseAuthedSrcOptions {
38
+ /**
39
+ * How to fetch. Left out (or `null`), the URL is used as it is — `src` is the URL and
40
+ * the status `ready` at once — so a component that takes an OPTIONAL fetcher (the
41
+ * image grid, the lightbox) has one code path for public and protected images.
42
+ */
43
+ fetcher?: AuthedFetcher | null;
44
+ /** `false` holds the fetch — a lightbox slide that is not on screen. Default `true`. */
45
+ enabled?: boolean;
46
+ }
47
+
48
+ export interface UseAuthedSrcResult {
49
+ /** The object URL (or, without a fetcher, the URL itself); `null` until ready. */
50
+ src: string | null;
51
+ status: AuthedSrcStatus;
52
+ /** What the fetcher threw, when `status` is `error`. */
53
+ error: unknown;
54
+ /** The blob's MIME type, once fetched (`""` when the server sent none). `undefined`
55
+ * without a fetcher — nothing was fetched to ask. Lets a caller that does not know
56
+ * whether a URL is an image or a PDF find out. */
57
+ type: string | undefined;
58
+ /** Fetch again — after an `error`, or to pick up a changed file at the same URL. */
59
+ retry: () => void;
60
+ }
61
+
62
+ interface State {
63
+ /** Which request this state belongs to — see the effect. */
64
+ key: string | null;
65
+ src: string | null;
66
+ status: AuthedSrcStatus;
67
+ error: unknown;
68
+ type: string | undefined;
69
+ }
70
+
71
+ async function toBlob(res: Blob | Response): Promise<Blob> {
72
+ if (typeof Response !== "undefined" && res instanceof Response) {
73
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
74
+ return res.blob();
75
+ }
76
+ return res as Blob;
77
+ }
78
+
79
+ /**
80
+ * `useAuthedSrc(url, { fetcher })` — see the module note.
81
+ *
82
+ * ```tsx
83
+ * const fetcher: AuthedFetcher = (url, { signal }) =>
84
+ * apiClient.get(url, { responseType: "blob", signal }).then((r) => r.data);
85
+ * const { src, status } = useAuthedSrc(photo.file_path, { fetcher });
86
+ * ```
87
+ *
88
+ * The fetcher is read through a ref, so an inline arrow does not refetch on every
89
+ * render; a change of `url` (or `retry`) does.
90
+ */
91
+ export function useAuthedSrc(
92
+ url: string | null | undefined,
93
+ { fetcher, enabled = true }: UseAuthedSrcOptions = {},
94
+ ): UseAuthedSrcResult {
95
+ const fetcherRef = useRef(fetcher);
96
+ useEffect(() => {
97
+ fetcherRef.current = fetcher;
98
+ });
99
+ const hasFetcher = !!fetcher;
100
+ const [attempt, setAttempt] = useState(0);
101
+ const key = url && enabled && hasFetcher ? `${attempt}|${url}` : null;
102
+ const [state, setState] = useState<State>({
103
+ key: null,
104
+ src: null,
105
+ status: "idle",
106
+ error: undefined,
107
+ type: undefined,
108
+ });
109
+
110
+ useEffect(() => {
111
+ const fetchFn = fetcherRef.current;
112
+ if (!key || !url || !fetchFn) return;
113
+ const controller = new AbortController();
114
+ let created: string | null = null;
115
+ let active = true;
116
+ // Settled: the response is in (or failed). Aborting then would signal a request
117
+ // that is long over — a fetcher that listens for `abort` (to log, to cancel a
118
+ // retry it scheduled, to count cancellations) would see a cancellation that
119
+ // never happened on every URL change and unmount.
120
+ let settled = false;
121
+ // No "loading" state is set here: a key the state does not describe yet already
122
+ // reads as loading (see the end of the hook).
123
+ fetchFn(url, { signal: controller.signal })
124
+ .then(toBlob)
125
+ .then((blob) => {
126
+ settled = true;
127
+ if (!active) return;
128
+ created = URL.createObjectURL(blob);
129
+ setState({ key, src: created, status: "ready", error: undefined, type: blob.type });
130
+ })
131
+ .catch((error: unknown) => {
132
+ settled = true;
133
+ if (!active) return;
134
+ setState({ key, src: null, status: "error", error, type: undefined });
135
+ });
136
+ return () => {
137
+ // `active` first: an in-flight response that lands after this must neither set
138
+ // state on an unmounted component nor create a blob nobody will revoke.
139
+ active = false;
140
+ // Only a request still in flight is aborted; `active` above is the stale-response
141
+ // guard either way.
142
+ if (!settled) controller.abort();
143
+ if (created) URL.revokeObjectURL(created);
144
+ };
145
+ }, [key, url]);
146
+
147
+ const retry = useCallback(() => setAttempt((n) => n + 1), []);
148
+
149
+ if (!url || !enabled) {
150
+ return { src: null, status: "idle", error: undefined, type: undefined, retry };
151
+ }
152
+ if (!hasFetcher) {
153
+ return { src: url, status: "ready", error: undefined, type: undefined, retry };
154
+ }
155
+ // Until the effect for THIS key has run, the state still describes the previous URL
156
+ // — a render in between must not show the old picture under the new name.
157
+ if (state.key !== key) {
158
+ return { src: null, status: "loading", error: undefined, type: undefined, retry };
159
+ }
160
+ return { src: state.src, status: state.status, error: state.error, type: state.type, retry };
161
+ }