@eifi1/ui-kit 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -37
- package/dist/chart.d.ts +3 -1
- package/dist/chart.js +2 -0
- package/dist/chart.js.map +1 -1
- package/dist/components/account-settings-labels.d.ts +105 -0
- package/dist/components/account-settings-labels.js +81 -0
- package/dist/components/account-settings-labels.js.map +1 -0
- package/dist/components/account-settings.d.ts +36 -52
- package/dist/components/account-settings.js +44 -8
- package/dist/components/account-settings.js.map +1 -1
- package/dist/components/alert-banner.d.ts +26 -1
- package/dist/components/alert-banner.js +10 -7
- package/dist/components/alert-banner.js.map +1 -1
- package/dist/components/amount-input.d.ts +26 -4
- package/dist/components/amount-input.js +5 -2
- package/dist/components/amount-input.js.map +1 -1
- package/dist/components/authed-image.d.ts +68 -0
- package/dist/components/authed-image.js +117 -0
- package/dist/components/authed-image.js.map +1 -0
- package/dist/components/breadcrumbs.d.ts +2 -1
- package/dist/components/breadcrumbs.js +5 -2
- package/dist/components/breadcrumbs.js.map +1 -1
- package/dist/components/bulk-action-bar.d.ts +51 -5
- package/dist/components/bulk-action-bar.js +88 -41
- package/dist/components/bulk-action-bar.js.map +1 -1
- package/dist/components/button-group.d.ts +107 -3
- package/dist/components/button-group.js +60 -5
- package/dist/components/button-group.js.map +1 -1
- package/dist/components/calculator.d.ts +17 -4
- package/dist/components/calendar-heatmap.d.ts +162 -0
- package/dist/components/calendar-heatmap.js +336 -0
- package/dist/components/calendar-heatmap.js.map +1 -0
- package/dist/components/chip.d.ts +2 -1
- package/dist/components/chip.js +6 -3
- package/dist/components/chip.js.map +1 -1
- package/dist/components/choice-card.d.ts +66 -2
- package/dist/components/choice-card.js +108 -1
- package/dist/components/choice-card.js.map +1 -1
- package/dist/components/combobox-core.js +2 -0
- package/dist/components/combobox-core.js.map +1 -1
- package/dist/components/combobox.d.ts +2 -2
- package/dist/components/combobox.js +31 -9
- package/dist/components/combobox.js.map +1 -1
- package/dist/components/copy-button.d.ts +52 -70
- package/dist/components/copy-button.js +1 -1
- package/dist/components/copy-button.js.map +1 -1
- package/dist/components/data-table-cells.d.ts +63 -0
- package/dist/components/data-table-cells.js +64 -0
- package/dist/components/data-table-cells.js.map +1 -0
- package/dist/components/data-table-filter-popover.d.ts +1 -1
- package/dist/components/data-table-filters.d.ts +1 -1
- package/dist/components/data-table-filters.js +31 -1
- package/dist/components/data-table-filters.js.map +1 -1
- package/dist/components/data-table-labels.d.ts +13 -0
- package/dist/components/data-table-labels.js +8 -1
- package/dist/components/data-table-labels.js.map +1 -1
- package/dist/components/data-table.d.ts +1 -1
- package/dist/components/data-table.js +104 -12
- package/dist/components/data-table.js.map +1 -1
- package/dist/components/date-picker.d.ts +3 -1
- package/dist/components/date-picker.js +15 -2
- package/dist/components/date-picker.js.map +1 -1
- package/dist/components/description-list.d.ts +23 -3
- package/dist/components/description-list.js +23 -3
- package/dist/components/description-list.js.map +1 -1
- package/dist/components/dialog-frame.d.ts +1 -0
- package/dist/components/entity-combobox.d.ts +1 -1
- package/dist/components/entity-combobox.js +18 -3
- package/dist/components/entity-combobox.js.map +1 -1
- package/dist/components/error-boundary.d.ts +92 -0
- package/dist/components/error-boundary.js +111 -0
- package/dist/components/error-boundary.js.map +1 -0
- package/dist/components/field.d.ts +69 -0
- package/dist/components/field.js +75 -0
- package/dist/components/field.js.map +1 -0
- package/dist/components/file-button.d.ts +51 -208
- package/dist/components/file-dropzone.d.ts +50 -2
- package/dist/components/floating-panel.d.ts +156 -4
- package/dist/components/floating-panel.js +193 -29
- package/dist/components/floating-panel.js.map +1 -1
- package/dist/components/form-actions.d.ts +53 -0
- package/dist/components/form-actions.js +98 -0
- package/dist/components/form-actions.js.map +1 -0
- package/dist/components/image-grid.d.ts +67 -0
- package/dist/components/image-grid.js +112 -0
- package/dist/components/image-grid.js.map +1 -0
- package/dist/components/lightbox.d.ts +104 -0
- package/dist/components/lightbox.js +229 -0
- package/dist/components/lightbox.js.map +1 -0
- package/dist/components/line-items.d.ts +114 -0
- package/dist/components/line-items.js +238 -0
- package/dist/components/line-items.js.map +1 -0
- package/dist/components/list.d.ts +37 -3
- package/dist/components/list.js +39 -14
- package/dist/components/list.js.map +1 -1
- package/dist/components/loading-state.d.ts +24 -0
- package/dist/components/loading-state.js +32 -0
- package/dist/components/loading-state.js.map +1 -0
- package/dist/components/menu-item.d.ts +13 -2
- package/dist/components/menu-item.js +11 -2
- package/dist/components/menu-item.js.map +1 -1
- package/dist/components/mini-calendar.d.ts +82 -5
- package/dist/components/mini-calendar.js +212 -64
- package/dist/components/mini-calendar.js.map +1 -1
- package/dist/components/modal.d.ts +20 -1
- package/dist/components/modal.js +26 -2
- package/dist/components/modal.js.map +1 -1
- package/dist/components/month-picker.d.ts +21 -1
- package/dist/components/month-picker.js +139 -26
- package/dist/components/month-picker.js.map +1 -1
- package/dist/components/multi-entity-combobox.d.ts +1 -1
- package/dist/components/multi-entity-combobox.js +19 -3
- package/dist/components/multi-entity-combobox.js.map +1 -1
- package/dist/components/nav-pills.d.ts +67 -0
- package/dist/components/nav-pills.js +69 -0
- package/dist/components/nav-pills.js.map +1 -0
- package/dist/components/number-field.d.ts +26 -5
- package/dist/components/number-field.js +5 -7
- package/dist/components/number-field.js.map +1 -1
- package/dist/components/number-input.d.ts +27 -5
- package/dist/components/number-input.js +7 -1
- package/dist/components/number-input.js.map +1 -1
- package/dist/components/numpad-sheet.d.ts +17 -4
- package/dist/components/passkeys-setting.d.ts +67 -0
- package/dist/components/passkeys-setting.js +214 -0
- package/dist/components/passkeys-setting.js.map +1 -0
- package/dist/components/picker-sheet.js +2 -1
- package/dist/components/picker-sheet.js.map +1 -1
- package/dist/components/pie-chart-labels.d.ts +20 -0
- package/dist/components/pie-chart-labels.js +12 -0
- package/dist/components/pie-chart-labels.js.map +1 -0
- package/dist/components/pie-chart.d.ts +101 -0
- package/dist/components/pie-chart.js +283 -0
- package/dist/components/pie-chart.js.map +1 -0
- package/dist/components/progress-bar.d.ts +73 -3
- package/dist/components/progress-bar.js +110 -8
- package/dist/components/progress-bar.js.map +1 -1
- package/dist/components/qr-code.d.ts +34 -0
- package/dist/components/qr-code.js +57 -0
- package/dist/components/qr-code.js.map +1 -0
- package/dist/components/search-field.d.ts +15 -3
- package/dist/components/search-field.js +5 -1
- package/dist/components/search-field.js.map +1 -1
- package/dist/components/series-chart.d.ts +16 -0
- package/dist/components/series-chart.js +4 -1
- package/dist/components/series-chart.js.map +1 -1
- package/dist/components/settings-fields.d.ts +50 -1
- package/dist/components/signed-amount.d.ts +121 -0
- package/dist/components/signed-amount.js +152 -0
- package/dist/components/signed-amount.js.map +1 -0
- package/dist/components/skeleton.d.ts +10 -3
- package/dist/components/skeleton.js +12 -2
- package/dist/components/skeleton.js.map +1 -1
- package/dist/components/stat-tile.d.ts +2 -1
- package/dist/components/stat-tile.js +5 -2
- package/dist/components/stat-tile.js.map +1 -1
- package/dist/components/status-dot.d.ts +6 -2
- package/dist/components/status-dot.js +6 -1
- package/dist/components/status-dot.js.map +1 -1
- package/dist/components/table.d.ts +77 -11
- package/dist/components/table.js +85 -17
- package/dist/components/table.js.map +1 -1
- package/dist/components/text-link.d.ts +176 -0
- package/dist/components/text-link.js +109 -0
- package/dist/components/text-link.js.map +1 -0
- package/dist/components/text.d.ts +8 -1
- package/dist/components/text.js +1 -0
- package/dist/components/text.js.map +1 -1
- package/dist/components/time-input.d.ts +50 -1
- package/dist/components/toggle-group.d.ts +28 -0
- package/dist/components/toggle-group.js +58 -9
- package/dist/components/toggle-group.js.map +1 -1
- package/dist/components/toggle-legend.d.ts +36 -3
- package/dist/components/toggle-legend.js +49 -0
- package/dist/components/toggle-legend.js.map +1 -1
- package/dist/components/tooltip.d.ts +11 -3
- package/dist/components/tooltip.js +2 -0
- package/dist/components/tooltip.js.map +1 -1
- package/dist/components/ui.d.ts +52 -729
- package/dist/components/ui.js +296 -27
- package/dist/components/ui.js.map +1 -1
- package/dist/components/use-table-state.d.ts +3 -52
- package/dist/components/use-table-state.js +128 -30
- package/dist/components/use-table-state.js.map +1 -1
- package/dist/components/user-avatar.d.ts +25 -3
- package/dist/components/user-avatar.js +32 -3
- package/dist/components/user-avatar.js.map +1 -1
- package/dist/{data-table-filters-Dh9uF_S-.d.ts → data-table-Drun8Fk9.d.ts} +305 -61
- package/dist/data-table.d.ts +2 -1
- package/dist/data-table.js +5 -1
- package/dist/data-table.js.map +1 -1
- package/dist/feedback/feedback-dialog.d.ts +26 -3
- package/dist/feedback/feedback-dialog.js +16 -2
- package/dist/feedback/feedback-dialog.js.map +1 -1
- package/dist/feedback/feedback-thread.d.ts +133 -0
- package/dist/feedback/feedback-thread.js +237 -0
- package/dist/feedback/feedback-thread.js.map +1 -0
- package/dist/feedback.d.ts +2 -1
- package/dist/feedback.js +1 -0
- package/dist/feedback.js.map +1 -1
- package/dist/hooks/use-authed-src.d.ts +71 -0
- package/dist/hooks/use-authed-src.js +63 -0
- package/dist/hooks/use-authed-src.js.map +1 -0
- package/dist/hooks/use-file-drop.d.ts +50 -2
- package/dist/hooks/use-hotkey.d.ts +59 -0
- package/dist/hooks/use-hotkey.js +105 -0
- package/dist/hooks/use-hotkey.js.map +1 -0
- package/dist/hooks/use-search-param-state.d.ts +83 -0
- package/dist/hooks/use-search-param-state.js +94 -0
- package/dist/hooks/use-search-param-state.js.map +1 -0
- package/dist/i18n/defaults.d.ts +17 -4
- package/dist/i18n/defaults.js +33 -1
- package/dist/i18n/defaults.js.map +1 -1
- package/dist/i18n/kit-labels.d.ts +47 -310
- package/dist/i18n/kit-labels.js +20 -5
- package/dist/i18n/kit-labels.js.map +1 -1
- package/dist/i18n/locales/de-CH-informal.d.ts +17 -4
- package/dist/i18n/locales/de-CH.d.ts +17 -4
- package/dist/i18n/locales/de-informal.d.ts +17 -4
- package/dist/i18n/locales/de.d.ts +17 -4
- package/dist/i18n/locales/de.js +165 -4
- package/dist/i18n/locales/de.js.map +1 -1
- package/dist/i18n/locales/es.d.ts +17 -4
- package/dist/i18n/locales/es.js +162 -4
- package/dist/i18n/locales/es.js.map +1 -1
- package/dist/i18n/locales/fr.d.ts +17 -4
- package/dist/i18n/locales/fr.js +162 -4
- package/dist/i18n/locales/fr.js.map +1 -1
- package/dist/i18n/locales/hu.d.ts +17 -4
- package/dist/i18n/locales/hu.js +162 -4
- package/dist/i18n/locales/hu.js.map +1 -1
- package/dist/i18n/locales/it.d.ts +17 -4
- package/dist/i18n/locales/it.js +162 -4
- package/dist/i18n/locales/it.js.map +1 -1
- package/dist/i18n/locales/zh.d.ts +17 -4
- package/dist/i18n/locales/zh.js +162 -4
- package/dist/i18n/locales/zh.js.map +1 -1
- package/dist/index.d.ts +45 -21
- package/dist/index.js +43 -1
- package/dist/index.js.map +1 -1
- package/dist/kit-labels-v3biUF1L.d.ts +1645 -0
- package/dist/lib/clipping.d.ts +21 -5
- package/dist/lib/clipping.js +3 -0
- package/dist/lib/clipping.js.map +1 -1
- package/dist/lib/dates.d.ts +17 -1
- package/dist/lib/dates.js +19 -0
- package/dist/lib/dates.js.map +1 -1
- package/dist/lib/format.d.ts +144 -0
- package/dist/lib/format.js +149 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/qr-encode.d.ts +44 -0
- package/dist/lib/qr-encode.js +338 -0
- package/dist/lib/qr-encode.js.map +1 -0
- package/dist/rhf/fields.d.ts +241 -0
- package/dist/rhf/fields.js +601 -0
- package/dist/rhf/fields.js.map +1 -0
- package/dist/rhf/form.d.ts +50 -1
- package/dist/rhf/line-items.d.ts +31 -0
- package/dist/rhf/line-items.js +59 -0
- package/dist/rhf/line-items.js.map +1 -0
- package/dist/rhf/use-rhf-wizard-step.d.ts +22 -0
- package/dist/rhf/use-rhf-wizard-step.js +38 -0
- package/dist/rhf/use-rhf-wizard-step.js.map +1 -0
- package/dist/rhf.d.ts +63 -1
- package/dist/rhf.js +3 -0
- package/dist/rhf.js.map +1 -1
- package/dist/shell/app-shell.d.ts +99 -2
- package/dist/shell/app-shell.js +58 -3
- package/dist/shell/app-shell.js.map +1 -1
- package/dist/shell/auth-layout.d.ts +74 -0
- package/dist/shell/auth-layout.js +97 -0
- package/dist/shell/auth-layout.js.map +1 -0
- package/dist/shell/top-bar-brand.d.ts +81 -0
- package/dist/shell/top-bar-brand.js +47 -0
- package/dist/shell/top-bar-brand.js.map +1 -0
- package/dist/shell/topbar-action-menu.d.ts +84 -3
- package/dist/shell/topbar-action-menu.js +115 -27
- package/dist/shell/topbar-action-menu.js.map +1 -1
- package/dist/shell.d.ts +58 -2
- package/dist/shell.js +2 -0
- package/dist/shell.js.map +1 -1
- package/dist/wizard/stepper-nav.d.ts +49 -1
- package/dist/wizard/wizard-context.d.ts +30 -1
- package/dist/wizard/wizard-context.js +23 -2
- package/dist/wizard/wizard-context.js.map +1 -1
- package/dist/wizard/wizard-step.d.ts +20 -5
- package/dist/wizard/wizard-step.js +16 -3
- package/dist/wizard/wizard-step.js.map +1 -1
- package/dist/wizard.d.ts +51 -3
- package/package.json +1 -1
- package/src/chart.ts +4 -0
- package/src/components/account-settings-labels.ts +190 -0
- package/src/components/account-settings.tsx +110 -63
- package/src/components/alert-banner.tsx +36 -7
- package/src/components/amount-input.tsx +13 -1
- package/src/components/authed-image.tsx +190 -0
- package/src/components/breadcrumbs.tsx +8 -2
- package/src/components/bulk-action-bar.tsx +163 -57
- package/src/components/button-group.tsx +129 -4
- package/src/components/calendar-heatmap.tsx +599 -0
- package/src/components/chip.tsx +8 -4
- package/src/components/choice-card.tsx +208 -1
- package/src/components/combobox-core.tsx +5 -0
- package/src/components/combobox.tsx +31 -9
- package/src/components/copy-button.tsx +3 -2
- package/src/components/data-table-cells.tsx +119 -0
- package/src/components/data-table-filters.ts +77 -0
- package/src/components/data-table-labels.ts +23 -0
- package/src/components/data-table.tsx +231 -13
- package/src/components/date-picker.tsx +19 -3
- package/src/components/description-list.tsx +52 -3
- package/src/components/entity-combobox.tsx +18 -3
- package/src/components/error-boundary.tsx +216 -0
- package/src/components/field.tsx +188 -0
- package/src/components/floating-panel.tsx +380 -16
- package/src/components/form-actions.tsx +191 -0
- package/src/components/image-grid.tsx +181 -0
- package/src/components/lightbox.tsx +374 -0
- package/src/components/line-items.tsx +385 -0
- package/src/components/list.tsx +93 -18
- package/src/components/loading-state.tsx +47 -0
- package/src/components/menu-item.tsx +29 -3
- package/src/components/mini-calendar.tsx +317 -57
- package/src/components/modal.tsx +61 -3
- package/src/components/month-picker.tsx +181 -26
- package/src/components/multi-entity-combobox.tsx +19 -3
- package/src/components/nav-pills.tsx +152 -0
- package/src/components/number-field.tsx +15 -10
- package/src/components/number-input.tsx +16 -1
- package/src/components/passkeys-setting.tsx +331 -0
- package/src/components/picker-sheet.tsx +7 -1
- package/src/components/pie-chart-labels.ts +32 -0
- package/src/components/pie-chart.tsx +479 -0
- package/src/components/progress-bar.tsx +241 -13
- package/src/components/qr-code.tsx +83 -0
- package/src/components/search-field.tsx +20 -4
- package/src/components/series-chart.tsx +21 -0
- package/src/components/signed-amount.tsx +286 -0
- package/src/components/skeleton.tsx +21 -3
- package/src/components/stat-tile.tsx +8 -2
- package/src/components/status-dot.tsx +10 -2
- package/src/components/table.tsx +186 -23
- package/src/components/text-link.tsx +248 -0
- package/src/components/text.tsx +9 -1
- package/src/components/toggle-group.tsx +106 -13
- package/src/components/toggle-legend.tsx +87 -2
- package/src/components/tooltip.tsx +17 -3
- package/src/components/ui.tsx +572 -44
- package/src/components/use-table-state.ts +265 -33
- package/src/components/user-avatar.tsx +57 -3
- package/src/data-table.ts +10 -0
- package/src/feedback/feedback-dialog.tsx +42 -3
- package/src/feedback/feedback-thread.tsx +434 -0
- package/src/feedback.ts +1 -0
- package/src/hooks/use-authed-src.ts +161 -0
- package/src/hooks/use-hotkey.ts +172 -0
- package/src/hooks/use-search-param-state.ts +187 -0
- package/src/i18n/defaults.ts +32 -0
- package/src/i18n/kit-labels.tsx +104 -8
- package/src/i18n/locales/de.ts +162 -0
- package/src/i18n/locales/es.ts +159 -0
- package/src/i18n/locales/fr.ts +162 -0
- package/src/i18n/locales/hu.ts +160 -0
- package/src/i18n/locales/it.ts +160 -0
- package/src/i18n/locales/zh.ts +159 -0
- package/src/index.ts +64 -0
- package/src/lib/clipping.ts +22 -4
- package/src/lib/dates.ts +37 -0
- package/src/lib/format.ts +325 -0
- package/src/lib/qr-encode.ts +420 -0
- package/src/rhf/fields.tsx +1054 -0
- package/src/rhf/line-items.tsx +130 -0
- package/src/rhf/use-rhf-wizard-step.ts +113 -0
- package/src/rhf.ts +8 -0
- package/src/shell/app-shell.tsx +147 -2
- package/src/shell/auth-layout.tsx +190 -0
- package/src/shell/top-bar-brand.tsx +76 -0
- package/src/shell/topbar-action-menu.tsx +260 -49
- package/src/shell.ts +2 -0
- package/src/wizard/wizard-context.tsx +50 -1
- package/src/wizard/wizard-step.tsx +43 -6
- package/tokens.css +12 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { Paperclip, Send } from "lucide-react";
|
|
4
|
+
import { cn } from "../lib/cn";
|
|
5
|
+
import { Button, EmptyState, Spinner, Textarea } from "../components/ui";
|
|
6
|
+
import { Skeleton } from "../components/skeleton";
|
|
7
|
+
import { useKitFileLabels, useKitLabels, useKitLocale } from "../i18n/kit-labels";
|
|
8
|
+
import { FeedbackAttachmentField } from "./feedback-attachment";
|
|
9
|
+
import type { FeedbackNoteAttachment } from "./feedback-inbox";
|
|
10
|
+
import type { FeedbackAttachmentLabels } from "./feedback-dialog";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The conversation under a report: who said what, when, with what attached — and the
|
|
14
|
+
* box to answer in.
|
|
15
|
+
*
|
|
16
|
+
* Both apps had written it by hand. kastlan's feedback page grew a comment list of
|
|
17
|
+
* bordered boxes with `slate-*` literals (feedback-page.tsx, "keksdose left this
|
|
18
|
+
* unbuilt"), and keksdose built the same thing a second time for support
|
|
19
|
+
* (support-thread.tsx) with sides, bubbles and a composer that clears only what it
|
|
20
|
+
* sent. This is that second one, generalised: the kit owns the look and the
|
|
21
|
+
* behaviour, the app owns the data, the upload and every word it does not share.
|
|
22
|
+
*
|
|
23
|
+
* **Own vs other** is the caller's per message (`own`), not a comparison the kit makes
|
|
24
|
+
* against a user id: whose message sits at the END side depends on who is reading,
|
|
25
|
+
* and only the app knows that (keksdose's support thread labelled a member's question
|
|
26
|
+
* "Du" in the admin panel the first time round, by reading the wrong field).
|
|
27
|
+
*
|
|
28
|
+
* **Redaction.** A thread is somebody's words about their own data — exactly what a
|
|
29
|
+
* host's demo / screen-share mode blurs. Bodies, authors and attachments carry
|
|
30
|
+
* `data-private` (the attribute `Tooltip redact` and `StatTile sensitive` use) unless
|
|
31
|
+
* `redact={false}`.
|
|
32
|
+
*
|
|
33
|
+
* **RTL.** Sides are logical — `justify-end` is the reading end, and the bubble's
|
|
34
|
+
* tucked corner is `rounded-ee` / `rounded-es` — and each body is `dir="auto"`, so a
|
|
35
|
+
* Hebrew reply in an English thread (or the reverse) lays out as its own language.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/* ── Labels ───────────────────────────────────────────────────────────────── */
|
|
39
|
+
|
|
40
|
+
/** The `feedbackThread` namespace of `<UiKitProvider labels>`. */
|
|
41
|
+
export interface FeedbackThreadLabels {
|
|
42
|
+
/** Accessible name of the message list. */
|
|
43
|
+
thread: string;
|
|
44
|
+
empty: string;
|
|
45
|
+
loading: string;
|
|
46
|
+
/** Author line of an `own` message that has no `author` of its own. */
|
|
47
|
+
you: string;
|
|
48
|
+
/** The badge beside a `staff` author. */
|
|
49
|
+
staff: string;
|
|
50
|
+
/** Accessible name of a message's attachment list. */
|
|
51
|
+
attachments: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const DEFAULT_FEEDBACK_THREAD_LABELS: FeedbackThreadLabels = {
|
|
55
|
+
thread: "Comments",
|
|
56
|
+
empty: "No comments yet",
|
|
57
|
+
loading: "Loading comments…",
|
|
58
|
+
you: "You",
|
|
59
|
+
staff: "Staff",
|
|
60
|
+
attachments: "Attachments",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The `feedbackComposer` namespace of `<UiKitProvider labels>`. The attach buttons'
|
|
64
|
+
* words are `feedbackAttachment`'s, shared with the dialog and the note editor. */
|
|
65
|
+
export interface FeedbackComposerLabels {
|
|
66
|
+
/** Accessible name of the text box. */
|
|
67
|
+
field: string;
|
|
68
|
+
placeholder: string;
|
|
69
|
+
send: string;
|
|
70
|
+
/** The shortcut line, given the platform's modifier ("Ctrl" or "⌘"). */
|
|
71
|
+
sendHint: (modifier: string) => string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const DEFAULT_FEEDBACK_COMPOSER_LABELS: FeedbackComposerLabels = {
|
|
75
|
+
field: "Write a comment",
|
|
76
|
+
placeholder: "Write a comment…",
|
|
77
|
+
send: "Send",
|
|
78
|
+
sendHint: (modifier) => `${modifier} + Enter to send`,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/* ── Thread ───────────────────────────────────────────────────────────────── */
|
|
82
|
+
|
|
83
|
+
export interface FeedbackThreadAttachment {
|
|
84
|
+
id?: string | number;
|
|
85
|
+
name: string;
|
|
86
|
+
/** Where to open it. Left out, the attachment is listed by name only. */
|
|
87
|
+
url?: string;
|
|
88
|
+
/** MIME type; an `image/*` with a `url` is shown as a thumbnail. */
|
|
89
|
+
type?: string;
|
|
90
|
+
/** Bytes, shown beside a non-image. */
|
|
91
|
+
size?: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface FeedbackThreadMessage {
|
|
95
|
+
id: string | number;
|
|
96
|
+
/** Who wrote it. Left out on an `own` message, it reads "You". */
|
|
97
|
+
author?: ReactNode;
|
|
98
|
+
/** A `Date`, an ISO string or epoch ms. */
|
|
99
|
+
createdAt: Date | string | number;
|
|
100
|
+
body: string;
|
|
101
|
+
attachments?: readonly FeedbackThreadAttachment[];
|
|
102
|
+
/** Written by whoever is reading: sits at the end side, in the brand tint. */
|
|
103
|
+
own?: boolean;
|
|
104
|
+
/** Written by the team rather than a user: gets the "Staff" badge. */
|
|
105
|
+
staff?: boolean;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface FeedbackThreadProps {
|
|
109
|
+
messages: readonly FeedbackThreadMessage[];
|
|
110
|
+
loading?: boolean;
|
|
111
|
+
/** Replaces the default "No comments yet". */
|
|
112
|
+
empty?: ReactNode;
|
|
113
|
+
/**
|
|
114
|
+
* Draw one attachment yourself — for files behind auth, which a plain `<img src>`
|
|
115
|
+
* cannot fetch (kastlan's `AuthedImage`), or a lightbox. The default is a thumbnail
|
|
116
|
+
* link for an image with a `url`, a file link otherwise.
|
|
117
|
+
*/
|
|
118
|
+
renderAttachment?: (attachment: FeedbackThreadAttachment, message: FeedbackThreadMessage) => ReactNode;
|
|
119
|
+
/** Tag bodies, authors and attachments `data-private`. Default true. */
|
|
120
|
+
redact?: boolean;
|
|
121
|
+
/** "Now" for the relative times — pin it in tests and stories. Default: the clock. */
|
|
122
|
+
now?: Date | number;
|
|
123
|
+
/** Overrides the provider's locale for the times. */
|
|
124
|
+
locale?: string;
|
|
125
|
+
labels?: Partial<FeedbackThreadLabels>;
|
|
126
|
+
className?: string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function FeedbackThread({
|
|
130
|
+
messages,
|
|
131
|
+
loading = false,
|
|
132
|
+
empty,
|
|
133
|
+
renderAttachment,
|
|
134
|
+
redact = true,
|
|
135
|
+
now,
|
|
136
|
+
locale: localeProp,
|
|
137
|
+
labels: labelsProp,
|
|
138
|
+
className,
|
|
139
|
+
}: FeedbackThreadProps) {
|
|
140
|
+
const labels = useKitLabels("feedbackThread", DEFAULT_FEEDBACK_THREAD_LABELS, labelsProp);
|
|
141
|
+
const locale = useKitLocale(localeProp);
|
|
142
|
+
const file = useKitFileLabels();
|
|
143
|
+
const priv = redact ? "" : undefined;
|
|
144
|
+
const clock = useMinuteClock(now === undefined);
|
|
145
|
+
const reference = now === undefined ? clock : +now;
|
|
146
|
+
|
|
147
|
+
if (loading && messages.length === 0) {
|
|
148
|
+
return <Skeleton lines={3} label={labels.loading} className={className} />;
|
|
149
|
+
}
|
|
150
|
+
if (messages.length === 0) {
|
|
151
|
+
return (
|
|
152
|
+
<EmptyState
|
|
153
|
+
variant="inline"
|
|
154
|
+
size="sm"
|
|
155
|
+
title={empty ?? labels.empty}
|
|
156
|
+
className={cn("justify-start", className)}
|
|
157
|
+
/>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<ol aria-label={labels.thread} aria-busy={loading || undefined} className={cn("space-y-3", className)}>
|
|
163
|
+
{messages.map((message) => {
|
|
164
|
+
const date = toDate(message.createdAt);
|
|
165
|
+
const author = message.author ?? (message.own ? labels.you : null);
|
|
166
|
+
const attachments = message.attachments ?? [];
|
|
167
|
+
return (
|
|
168
|
+
<li
|
|
169
|
+
key={message.id}
|
|
170
|
+
className={cn("flex", message.own ? "justify-end" : "justify-start")}
|
|
171
|
+
data-own={message.own ? "" : undefined}
|
|
172
|
+
>
|
|
173
|
+
<article
|
|
174
|
+
className={cn(
|
|
175
|
+
"max-w-[85%] min-w-0 rounded-lg px-3 py-2 text-sm",
|
|
176
|
+
message.own
|
|
177
|
+
? "rounded-ee-sm bg-[var(--brand-bg)]"
|
|
178
|
+
: "rounded-es-sm bg-[var(--bg-surface-2)]",
|
|
179
|
+
)}
|
|
180
|
+
>
|
|
181
|
+
<header className="mb-0.5 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-[11px] text-[var(--text-muted)]">
|
|
182
|
+
{author != null && (
|
|
183
|
+
<span data-private={priv} className="font-medium text-[var(--text-secondary)]">
|
|
184
|
+
{author}
|
|
185
|
+
</span>
|
|
186
|
+
)}
|
|
187
|
+
{message.staff && (
|
|
188
|
+
<span className="rounded border border-[var(--border)] bg-[var(--bg-surface)] px-1 text-[10px] font-medium uppercase tracking-wide text-[var(--text-secondary)]">
|
|
189
|
+
{labels.staff}
|
|
190
|
+
</span>
|
|
191
|
+
)}
|
|
192
|
+
{date && (
|
|
193
|
+
<time
|
|
194
|
+
dateTime={date.toISOString()}
|
|
195
|
+
title={new Intl.DateTimeFormat(locale, { dateStyle: "full", timeStyle: "short" }).format(date)}
|
|
196
|
+
className="tabular-nums"
|
|
197
|
+
>
|
|
198
|
+
{formatRelativeTime(date, reference, locale)}
|
|
199
|
+
</time>
|
|
200
|
+
)}
|
|
201
|
+
</header>
|
|
202
|
+
{message.body && (
|
|
203
|
+
<p
|
|
204
|
+
data-private={priv}
|
|
205
|
+
dir="auto"
|
|
206
|
+
className="whitespace-pre-wrap break-words text-start text-[var(--text-primary)]"
|
|
207
|
+
>
|
|
208
|
+
{message.body}
|
|
209
|
+
</p>
|
|
210
|
+
)}
|
|
211
|
+
{attachments.length > 0 && (
|
|
212
|
+
<ul aria-label={labels.attachments} data-private={priv} className="mt-2 flex flex-wrap gap-2">
|
|
213
|
+
{attachments.map((attachment, index) => (
|
|
214
|
+
<li key={attachment.id ?? `${index}-${attachment.name}`} className="min-w-0">
|
|
215
|
+
{renderAttachment ? (
|
|
216
|
+
renderAttachment(attachment, message)
|
|
217
|
+
) : (
|
|
218
|
+
<DefaultAttachment attachment={attachment} size={file.size} />
|
|
219
|
+
)}
|
|
220
|
+
</li>
|
|
221
|
+
))}
|
|
222
|
+
</ul>
|
|
223
|
+
)}
|
|
224
|
+
</article>
|
|
225
|
+
</li>
|
|
226
|
+
);
|
|
227
|
+
})}
|
|
228
|
+
</ol>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function DefaultAttachment({
|
|
233
|
+
attachment,
|
|
234
|
+
size,
|
|
235
|
+
}: {
|
|
236
|
+
attachment: FeedbackThreadAttachment;
|
|
237
|
+
size: (bytes: number) => string;
|
|
238
|
+
}) {
|
|
239
|
+
const { name, url, type } = attachment;
|
|
240
|
+
if (url && type?.startsWith("image/")) {
|
|
241
|
+
return (
|
|
242
|
+
<a href={url} target="_blank" rel="noopener noreferrer" className="block">
|
|
243
|
+
<img
|
|
244
|
+
src={url}
|
|
245
|
+
alt={name}
|
|
246
|
+
loading="lazy"
|
|
247
|
+
className="h-20 w-20 rounded border border-[var(--border)] object-cover"
|
|
248
|
+
/>
|
|
249
|
+
</a>
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
const content = (
|
|
253
|
+
<>
|
|
254
|
+
<Paperclip className="size-3.5 shrink-0" aria-hidden />
|
|
255
|
+
<span className="truncate">{name}</span>
|
|
256
|
+
{attachment.size !== undefined && (
|
|
257
|
+
<span className="shrink-0 text-[var(--text-muted)]">{size(attachment.size)}</span>
|
|
258
|
+
)}
|
|
259
|
+
</>
|
|
260
|
+
);
|
|
261
|
+
const chip =
|
|
262
|
+
"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)]";
|
|
263
|
+
return url ? (
|
|
264
|
+
<a href={url} target="_blank" rel="noopener noreferrer" className={cn(chip, "hover:bg-[var(--bg-hover)]")}>
|
|
265
|
+
{content}
|
|
266
|
+
</a>
|
|
267
|
+
) : (
|
|
268
|
+
<span className={chip}>{content}</span>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The time, refreshed once a minute while `active`, so "2 minutes ago" does not stay
|
|
273
|
+
* "just now" on a page left open. Read in state rather than in render, which must be
|
|
274
|
+
* pure. */
|
|
275
|
+
function useMinuteClock(active: boolean): number {
|
|
276
|
+
const [time, setTime] = useState(() => Date.now());
|
|
277
|
+
useEffect(() => {
|
|
278
|
+
if (!active) return;
|
|
279
|
+
const id = setInterval(() => setTime(Date.now()), 60_000);
|
|
280
|
+
return () => clearInterval(id);
|
|
281
|
+
}, [active]);
|
|
282
|
+
return time;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function toDate(value: Date | string | number): Date | null {
|
|
286
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
287
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const MINUTE = 60_000;
|
|
291
|
+
const HOUR = 60 * MINUTE;
|
|
292
|
+
const DAY = 24 * HOUR;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* "just now", "5 minutes ago", "yesterday", "3 days ago" — `Intl.RelativeTimeFormat`
|
|
296
|
+
* in `locale`, so the grammar is the language's. Past a week it is a date: "12 days
|
|
297
|
+
* ago" makes the reader do arithmetic that the date already did. The full date and
|
|
298
|
+
* time are on the `<time>`'s title.
|
|
299
|
+
*/
|
|
300
|
+
function formatRelativeTime(date: Date, now: number, locale: string | undefined): string {
|
|
301
|
+
const diff = date.getTime() - now;
|
|
302
|
+
const abs = Math.abs(diff);
|
|
303
|
+
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
|
304
|
+
if (abs < 45_000) return rtf.format(0, "second");
|
|
305
|
+
if (abs < 45 * MINUTE) return rtf.format(Math.round(diff / MINUTE), "minute");
|
|
306
|
+
if (abs < 22 * HOUR) return rtf.format(Math.round(diff / HOUR), "hour");
|
|
307
|
+
if (abs < 7 * DAY) return rtf.format(Math.round(diff / DAY), "day");
|
|
308
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(date);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* ── Composer ─────────────────────────────────────────────────────────────── */
|
|
312
|
+
|
|
313
|
+
/** What the composer's attach control takes — {@link FeedbackNoteAttachment} with the
|
|
314
|
+
* labels optional, since the field reads `feedbackAttachment` from the provider. */
|
|
315
|
+
export type FeedbackComposerAttachment = Omit<FeedbackNoteAttachment, "labels"> & {
|
|
316
|
+
labels?: Partial<FeedbackAttachmentLabels>;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
export interface FeedbackComposerProps {
|
|
320
|
+
/**
|
|
321
|
+
* Send the comment. Return the promise: the box clears when it RESOLVES, and only of
|
|
322
|
+
* what was sent — text typed while it was in flight stays. A rejection keeps
|
|
323
|
+
* everything for a retry (surface the error yourself; the composer stays quiet).
|
|
324
|
+
*/
|
|
325
|
+
onSend: (body: string, attachment: File | null) => void | Promise<unknown>;
|
|
326
|
+
/** The send is in flight: a spinner on the button and no second send. */
|
|
327
|
+
pending?: boolean;
|
|
328
|
+
/** Offer one picture with the comment (file, paste or `onCaptureScreenshot`).
|
|
329
|
+
* `true` for the defaults. Left out, the composer is text only. */
|
|
330
|
+
attachment?: boolean | FeedbackComposerAttachment;
|
|
331
|
+
/** Rendered INSTEAD of the composer — the thread is closed, or read-only for this
|
|
332
|
+
* reader. */
|
|
333
|
+
disabledReason?: ReactNode;
|
|
334
|
+
rows?: number;
|
|
335
|
+
labels?: Partial<FeedbackComposerLabels>;
|
|
336
|
+
className?: string;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const noSubscribe = () => () => {};
|
|
340
|
+
const isApple = () =>
|
|
341
|
+
typeof navigator !== "undefined" && /Mac|iPhone|iPad|iPod/i.test(navigator.platform || navigator.userAgent);
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The answer box under a {@link FeedbackThread}: a textarea, an optional picture,
|
|
345
|
+
* and Send — Ctrl/⌘+Enter sends, the shortcut the report dialog and the note editor
|
|
346
|
+
* use, so the habit carries across the feature. Plain Enter is a new line: feedback is
|
|
347
|
+
* written in paragraphs, unlike a chat.
|
|
348
|
+
*/
|
|
349
|
+
export function FeedbackComposer({
|
|
350
|
+
onSend,
|
|
351
|
+
pending = false,
|
|
352
|
+
attachment,
|
|
353
|
+
disabledReason,
|
|
354
|
+
rows = 3,
|
|
355
|
+
labels: labelsProp,
|
|
356
|
+
className,
|
|
357
|
+
}: FeedbackComposerProps) {
|
|
358
|
+
const labels = useKitLabels("feedbackComposer", DEFAULT_FEEDBACK_COMPOSER_LABELS, labelsProp);
|
|
359
|
+
const [draft, setDraft] = useState("");
|
|
360
|
+
const [file, setFile] = useState<File | null>(null);
|
|
361
|
+
const root = useRef<HTMLDivElement>(null);
|
|
362
|
+
// The server snapshot is "Ctrl"; the client corrects it after hydration.
|
|
363
|
+
const modifier = useSyncExternalStore(
|
|
364
|
+
noSubscribe,
|
|
365
|
+
() => (isApple() ? "⌘" : "Ctrl"),
|
|
366
|
+
() => "Ctrl",
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
if (disabledReason != null && disabledReason !== false) {
|
|
370
|
+
return <p className={cn("text-xs text-[var(--text-muted)]", className)}>{disabledReason}</p>;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const canSend = draft.trim().length > 0 && !pending;
|
|
374
|
+
const send = () => {
|
|
375
|
+
if (!canSend) return;
|
|
376
|
+
const sentDraft = draft;
|
|
377
|
+
const sentFile = file;
|
|
378
|
+
Promise.resolve(onSend(draft.trim(), file))
|
|
379
|
+
.then(() => {
|
|
380
|
+
setDraft((current) => (current === sentDraft ? "" : current));
|
|
381
|
+
setFile((current) => (current === sentFile ? null : current));
|
|
382
|
+
})
|
|
383
|
+
.catch(() => {});
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const config = attachment === true ? {} : attachment || null;
|
|
387
|
+
|
|
388
|
+
return (
|
|
389
|
+
<div ref={root} className={cn("space-y-2", className)}>
|
|
390
|
+
<Textarea
|
|
391
|
+
rows={rows}
|
|
392
|
+
dir="auto"
|
|
393
|
+
aria-label={labels.field}
|
|
394
|
+
placeholder={labels.placeholder}
|
|
395
|
+
value={draft}
|
|
396
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
397
|
+
onKeyDown={(event) => {
|
|
398
|
+
if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
|
|
399
|
+
event.preventDefault();
|
|
400
|
+
send();
|
|
401
|
+
}
|
|
402
|
+
}}
|
|
403
|
+
/>
|
|
404
|
+
<div className="flex flex-wrap items-end justify-between gap-2">
|
|
405
|
+
{config ? (
|
|
406
|
+
<FeedbackAttachmentField
|
|
407
|
+
value={file}
|
|
408
|
+
onChange={setFile}
|
|
409
|
+
labels={config.labels}
|
|
410
|
+
accept={config.accept}
|
|
411
|
+
maxBytes={config.maxBytes}
|
|
412
|
+
onError={config.onError}
|
|
413
|
+
onCaptureScreenshot={config.onCaptureScreenshot}
|
|
414
|
+
// The box is this field's sibling; a paste in it bubbles to `root`.
|
|
415
|
+
pasteFrom={root}
|
|
416
|
+
/>
|
|
417
|
+
) : (
|
|
418
|
+
<span />
|
|
419
|
+
)}
|
|
420
|
+
<div className="ms-auto flex items-center gap-2">
|
|
421
|
+
<span className="text-xs text-[var(--text-placeholder)]">{labels.sendHint(modifier)}</span>
|
|
422
|
+
<Button onClick={send} disabled={!canSend} aria-busy={pending || undefined}>
|
|
423
|
+
{pending ? (
|
|
424
|
+
<Spinner label={null} className="size-4" />
|
|
425
|
+
) : (
|
|
426
|
+
<Send className="size-4 rtl:-scale-x-100" aria-hidden />
|
|
427
|
+
)}
|
|
428
|
+
{labels.send}
|
|
429
|
+
</Button>
|
|
430
|
+
</div>
|
|
431
|
+
</div>
|
|
432
|
+
</div>
|
|
433
|
+
);
|
|
434
|
+
}
|
package/src/feedback.ts
CHANGED
|
@@ -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
|
+
}
|