@bayonai/mui-design 0.2.1 → 1.1.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 (147) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +108 -7
  3. package/dist/cjs/actions/ActionMenu.js +36 -0
  4. package/dist/cjs/actions/DialogActionButtons.js +33 -0
  5. package/dist/cjs/actions/LoadingIconButton.js +10 -0
  6. package/dist/cjs/actions/StickyActionFooter.js +27 -0
  7. package/dist/cjs/actions/StickySubmitButton.js +10 -0
  8. package/dist/cjs/actions/index.js +18 -0
  9. package/dist/cjs/client.js +1 -0
  10. package/dist/cjs/components/BayonFlowVisual.js +6 -6
  11. package/dist/cjs/components/BayonThemeModeToggle.js +34 -0
  12. package/dist/cjs/components/panels.js +7 -7
  13. package/dist/cjs/drawers/BottomDrawer.js +43 -0
  14. package/dist/cjs/drawers/MobileBottomDrawer.js +102 -0
  15. package/dist/cjs/drawers/MobileBottomDrawerHeader.js +36 -0
  16. package/dist/cjs/drawers/ResponsiveDrawer.js +22 -0
  17. package/dist/cjs/drawers/drawerAppearance.js +10 -0
  18. package/dist/cjs/drawers/index.js +17 -0
  19. package/dist/cjs/drawers/useVisualViewportBottomInset.js +33 -0
  20. package/dist/cjs/feedback/StatusNotice.js +62 -0
  21. package/dist/cjs/feedback/index.js +7 -0
  22. package/dist/cjs/links/LinkAddressPreview.js +80 -0
  23. package/dist/cjs/links/index.js +9 -0
  24. package/dist/cjs/links/useLinkAddressPreview.js +152 -0
  25. package/dist/cjs/pending/PendingChangesDialogShell.js +16 -0
  26. package/dist/cjs/pending/PendingChangesDialogView.js +126 -0
  27. package/dist/cjs/pending/PendingChangesIndicatorView.js +27 -0
  28. package/dist/cjs/pending/index.js +9 -0
  29. package/dist/cjs/pending/types.js +2 -0
  30. package/dist/cjs/providers/BayonThemeProvider.js +83 -4
  31. package/dist/cjs/sections/ExpandableSectionToggle.js +32 -0
  32. package/dist/cjs/sections/index.js +7 -0
  33. package/dist/cjs/styles/pageStyles.js +17 -17
  34. package/dist/cjs/swipe/SwipeActionRow.js +123 -0
  35. package/dist/cjs/swipe/index.js +8 -0
  36. package/dist/cjs/theme/components.js +60 -0
  37. package/dist/cjs/theme/createBayonTheme.js +23 -2
  38. package/dist/cjs/toasts/ActionToast.js +12 -0
  39. package/dist/cjs/toasts/Toast.js +55 -0
  40. package/dist/cjs/toasts/index.js +12 -0
  41. package/dist/cjs/toasts/useUpwardToastDismiss.js +73 -0
  42. package/dist/cjs/uploads/CameraCaptureDialog.js +187 -0
  43. package/dist/cjs/uploads/FileRow.js +23 -0
  44. package/dist/cjs/uploads/FileSwipeActionRow.js +11 -0
  45. package/dist/cjs/uploads/FileThumbnail.js +104 -0
  46. package/dist/cjs/uploads/FileUpload.js +98 -0
  47. package/dist/cjs/uploads/FileViewer.js +151 -0
  48. package/dist/cjs/uploads/ImageCropDialog.js +215 -0
  49. package/dist/cjs/uploads/UploadProgressRing.js +32 -0
  50. package/dist/cjs/uploads/cameraCropPresentation.js +12 -0
  51. package/dist/cjs/uploads/index.js +20 -0
  52. package/dist/esm/actions/ActionMenu.js +30 -0
  53. package/dist/esm/actions/DialogActionButtons.js +24 -0
  54. package/dist/esm/actions/LoadingIconButton.js +7 -0
  55. package/dist/esm/actions/StickyActionFooter.js +24 -0
  56. package/dist/esm/actions/StickySubmitButton.js +7 -0
  57. package/dist/esm/actions/index.js +7 -0
  58. package/dist/esm/client.js +1 -0
  59. package/dist/esm/components/BayonFlowVisual.js +6 -6
  60. package/dist/esm/components/BayonThemeModeToggle.js +28 -0
  61. package/dist/esm/components/panels.js +7 -7
  62. package/dist/esm/drawers/BottomDrawer.js +37 -0
  63. package/dist/esm/drawers/MobileBottomDrawer.js +95 -0
  64. package/dist/esm/drawers/MobileBottomDrawerHeader.js +30 -0
  65. package/dist/esm/drawers/ResponsiveDrawer.js +19 -0
  66. package/dist/esm/drawers/drawerAppearance.js +7 -0
  67. package/dist/esm/drawers/index.js +7 -0
  68. package/dist/esm/drawers/useVisualViewportBottomInset.js +29 -0
  69. package/dist/esm/feedback/StatusNotice.js +56 -0
  70. package/dist/esm/feedback/index.js +3 -0
  71. package/dist/esm/links/LinkAddressPreview.js +74 -0
  72. package/dist/esm/links/index.js +4 -0
  73. package/dist/esm/links/useLinkAddressPreview.js +149 -0
  74. package/dist/esm/pending/PendingChangesDialogShell.js +10 -0
  75. package/dist/esm/pending/PendingChangesDialogView.js +120 -0
  76. package/dist/esm/pending/PendingChangesIndicatorView.js +21 -0
  77. package/dist/esm/pending/index.js +4 -0
  78. package/dist/esm/pending/types.js +1 -0
  79. package/dist/esm/providers/BayonThemeProvider.js +83 -5
  80. package/dist/esm/sections/ExpandableSectionToggle.js +26 -0
  81. package/dist/esm/sections/index.js +3 -0
  82. package/dist/esm/styles/pageStyles.js +17 -17
  83. package/dist/esm/swipe/SwipeActionRow.js +116 -0
  84. package/dist/esm/swipe/index.js +3 -0
  85. package/dist/esm/theme/components.js +60 -0
  86. package/dist/esm/theme/createBayonTheme.js +23 -2
  87. package/dist/esm/toasts/ActionToast.js +9 -0
  88. package/dist/esm/toasts/Toast.js +52 -0
  89. package/dist/esm/toasts/index.js +5 -0
  90. package/dist/esm/toasts/useUpwardToastDismiss.js +69 -0
  91. package/dist/esm/uploads/CameraCaptureDialog.js +181 -0
  92. package/dist/esm/uploads/FileRow.js +17 -0
  93. package/dist/esm/uploads/FileSwipeActionRow.js +7 -0
  94. package/dist/esm/uploads/FileThumbnail.js +98 -0
  95. package/dist/esm/uploads/FileUpload.js +92 -0
  96. package/dist/esm/uploads/FileViewer.js +145 -0
  97. package/dist/esm/uploads/ImageCropDialog.js +212 -0
  98. package/dist/esm/uploads/UploadProgressRing.js +29 -0
  99. package/dist/esm/uploads/cameraCropPresentation.js +5 -0
  100. package/dist/esm/uploads/index.js +9 -0
  101. package/dist/types/actions/ActionMenu.d.ts +24 -0
  102. package/dist/types/actions/DialogActionButtons.d.ts +22 -0
  103. package/dist/types/actions/LoadingIconButton.d.ts +11 -0
  104. package/dist/types/actions/StickyActionFooter.d.ts +14 -0
  105. package/dist/types/actions/StickySubmitButton.d.ts +10 -0
  106. package/dist/types/actions/index.d.ts +5 -0
  107. package/dist/types/client.d.ts +1 -0
  108. package/dist/types/components/BayonThemeModeToggle.d.ts +6 -0
  109. package/dist/types/drawers/BottomDrawer.d.ts +16 -0
  110. package/dist/types/drawers/MobileBottomDrawer.d.ts +20 -0
  111. package/dist/types/drawers/MobileBottomDrawerHeader.d.ts +11 -0
  112. package/dist/types/drawers/ResponsiveDrawer.d.ts +25 -0
  113. package/dist/types/drawers/drawerAppearance.d.ts +8 -0
  114. package/dist/types/drawers/index.d.ts +6 -0
  115. package/dist/types/drawers/useVisualViewportBottomInset.d.ts +6 -0
  116. package/dist/types/feedback/StatusNotice.d.ts +13 -0
  117. package/dist/types/feedback/index.d.ts +1 -0
  118. package/dist/types/links/LinkAddressPreview.d.ts +19 -0
  119. package/dist/types/links/index.d.ts +3 -0
  120. package/dist/types/links/useLinkAddressPreview.d.ts +24 -0
  121. package/dist/types/pending/PendingChangesDialogShell.d.ts +2 -0
  122. package/dist/types/pending/PendingChangesDialogView.d.ts +2 -0
  123. package/dist/types/pending/PendingChangesIndicatorView.d.ts +10 -0
  124. package/dist/types/pending/index.d.ts +3 -0
  125. package/dist/types/pending/types.d.ts +48 -0
  126. package/dist/types/providers/BayonThemeProvider.d.ts +10 -1
  127. package/dist/types/sections/ExpandableSectionToggle.d.ts +12 -0
  128. package/dist/types/sections/index.d.ts +2 -0
  129. package/dist/types/swipe/SwipeActionRow.d.ts +8 -0
  130. package/dist/types/swipe/index.d.ts +1 -0
  131. package/dist/types/toasts/ActionToast.d.ts +11 -0
  132. package/dist/types/toasts/Toast.d.ts +16 -0
  133. package/dist/types/toasts/index.d.ts +5 -0
  134. package/dist/types/toasts/useUpwardToastDismiss.d.ts +15 -0
  135. package/dist/types/uploads/CameraCaptureDialog.d.ts +22 -0
  136. package/dist/types/uploads/FileRow.d.ts +10 -0
  137. package/dist/types/uploads/FileSwipeActionRow.d.ts +1 -0
  138. package/dist/types/uploads/FileThumbnail.d.ts +5 -0
  139. package/dist/types/uploads/FileUpload.d.ts +26 -0
  140. package/dist/types/uploads/FileViewer.d.ts +9 -0
  141. package/dist/types/uploads/ImageCropDialog.d.ts +26 -0
  142. package/dist/types/uploads/UploadProgressRing.d.ts +24 -0
  143. package/dist/types/uploads/cameraCropPresentation.d.ts +2 -0
  144. package/dist/types/uploads/index.d.ts +10 -0
  145. package/docs/camera-crop-contract.md +50 -0
  146. package/docs/uploads-contract.md +40 -0
  147. package/package.json +70 -8
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ "use client";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.CameraCaptureDialog = CameraCaptureDialog;
8
+ const jsx_runtime_1 = require("react/jsx-runtime");
9
+ const react_1 = require("react");
10
+ const material_1 = require("@mui/material");
11
+ const CameraAlt_1 = __importDefault(require("@mui/icons-material/CameraAlt"));
12
+ const cameraCropPresentation_js_1 = require("./cameraCropPresentation.js");
13
+ const cameraCropPresentation_js_2 = require("./cameraCropPresentation.js");
14
+ const DEFAULT_LABELS = {
15
+ title: "Take photo",
16
+ close: "Close camera",
17
+ preview: "Camera preview",
18
+ capture: "Take photo",
19
+ opening: "Opening camera…",
20
+ ready: "Take a photo, then adjust its crop.",
21
+ denied: "Camera access was denied. Allow camera access in your browser, then reopen the camera, or choose a photo.",
22
+ unavailable: "The camera is unavailable. Check that it is connected and not in use, then reopen the camera, or choose a photo.",
23
+ previewError: "The camera preview could not start. Close the camera and try again, or choose a photo.",
24
+ captureError: "The photo could not be captured. Close the camera and try again, or choose a photo.",
25
+ };
26
+ /** Owns camera resources only; the host owns crop, upload and persistence. */
27
+ function CameraCaptureDialog({ onClose, onCapture, labels, facingMode = "user", fileName = "camera.jpg", previewMaxHeight = "50dvh", }) {
28
+ const copy = { ...DEFAULT_LABELS, ...labels };
29
+ const copyRef = (0, react_1.useRef)(copy);
30
+ // Publish copy after commit so abandoned renders cannot change async feedback.
31
+ (0, react_1.useEffect)(() => {
32
+ copyRef.current = copy;
33
+ });
34
+ // Product decision: changing copy must never request camera permission again.
35
+ const initialFacingMode = (0, react_1.useRef)(facingMode);
36
+ const titleId = (0, react_1.useId)();
37
+ const video = (0, react_1.useRef)(null);
38
+ const stream = (0, react_1.useRef)(null);
39
+ const active = (0, react_1.useRef)(true);
40
+ const capturing = (0, react_1.useRef)(false);
41
+ const [ready, setReady] = (0, react_1.useState)(false);
42
+ const [busy, setBusy] = (0, react_1.useState)(false);
43
+ const [error, setError] = (0, react_1.useState)(null);
44
+ const attachPreview = (0, react_1.useCallback)((preview) => {
45
+ video.current = preview;
46
+ const acquired = stream.current;
47
+ if (!preview || !acquired || !active.current)
48
+ return;
49
+ // Dialog portals can mount after permission resolves. Attach from both sides
50
+ // of that race so an accepted camera never waits forever for its preview.
51
+ preview.srcObject = acquired;
52
+ void preview
53
+ .play()
54
+ .then(() => {
55
+ if (active.current &&
56
+ stream.current === acquired &&
57
+ video.current === preview &&
58
+ preview.readyState >= 2)
59
+ setReady(true);
60
+ })
61
+ .catch((cause) => {
62
+ if (!active.current ||
63
+ stream.current !== acquired ||
64
+ video.current !== preview)
65
+ return;
66
+ console.error("[CameraCaptureDialog] Could not play camera preview.", {
67
+ cause,
68
+ });
69
+ acquired.getTracks().forEach((track) => track.stop());
70
+ stream.current = null;
71
+ preview.srcObject = null;
72
+ setReady(false);
73
+ setError(copyRef.current.previewError);
74
+ });
75
+ }, []);
76
+ function stop() {
77
+ var _a;
78
+ (_a = stream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach((track) => track.stop());
79
+ stream.current = null;
80
+ if (video.current)
81
+ video.current.srcObject = null;
82
+ }
83
+ function close() {
84
+ active.current = false;
85
+ stop();
86
+ onClose();
87
+ }
88
+ (0, react_1.useEffect)(() => {
89
+ let cancelled = false;
90
+ active.current = true;
91
+ // Camera consent follows the explicit menu action. Every owned stream, including
92
+ // a permission result arriving after dismissal, must stop before leaving capture.
93
+ async function start() {
94
+ var _a;
95
+ try {
96
+ if (!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia))
97
+ throw new Error("Camera API unavailable");
98
+ const acquired = await navigator.mediaDevices.getUserMedia({
99
+ video: { facingMode: initialFacingMode.current },
100
+ audio: false,
101
+ });
102
+ if (cancelled || !active.current) {
103
+ acquired.getTracks().forEach((track) => track.stop());
104
+ return;
105
+ }
106
+ stream.current = acquired;
107
+ attachPreview(video.current);
108
+ }
109
+ catch (cause) {
110
+ if (cancelled || !active.current)
111
+ return;
112
+ console.error("[CameraCaptureDialog] Could not start camera.", {
113
+ cause,
114
+ });
115
+ stop();
116
+ const denied = cause instanceof DOMException &&
117
+ (cause.name === "NotAllowedError" || cause.name === "SecurityError");
118
+ setError(denied ? copyRef.current.denied : copyRef.current.unavailable);
119
+ }
120
+ }
121
+ void start();
122
+ return () => {
123
+ cancelled = true;
124
+ active.current = false;
125
+ stop();
126
+ };
127
+ }, [attachPreview]);
128
+ function capture() {
129
+ if (!ready || error || capturing.current || !active.current)
130
+ return;
131
+ capturing.current = true;
132
+ setBusy(true);
133
+ try {
134
+ const preview = video.current;
135
+ if (!preview || !preview.videoWidth || !preview.videoHeight)
136
+ throw new Error("Camera frame is not ready");
137
+ const canvas = document.createElement("canvas");
138
+ canvas.width = preview.videoWidth;
139
+ canvas.height = preview.videoHeight;
140
+ const context = canvas.getContext("2d");
141
+ if (!context)
142
+ throw new Error("Camera capture canvas is unavailable");
143
+ context.drawImage(preview, 0, 0);
144
+ canvas.toBlob((blob) => {
145
+ if (!active.current)
146
+ return;
147
+ if (!blob) {
148
+ console.error("[CameraCaptureDialog] Camera capture returned no image.");
149
+ setError(copyRef.current.captureError);
150
+ setBusy(false);
151
+ capturing.current = false;
152
+ return;
153
+ }
154
+ active.current = false;
155
+ stop();
156
+ // Capture only prepares the existing crop flow; it never uploads or publishes.
157
+ onCapture(new File([blob], fileName, { type: "image/jpeg" }));
158
+ }, "image/jpeg", 0.92);
159
+ }
160
+ catch (cause) {
161
+ console.error("[CameraCaptureDialog] Could not capture photo.", {
162
+ cause,
163
+ });
164
+ setError(copyRef.current.captureError);
165
+ setBusy(false);
166
+ capturing.current = false;
167
+ }
168
+ }
169
+ return ((0, jsx_runtime_1.jsxs)(material_1.Dialog, { open: true, fullWidth: true, maxWidth: "xs", onClose: close, "aria-labelledby": titleId, onKeyDown: (event) => {
170
+ if (event.key === "Enter") {
171
+ event.stopPropagation();
172
+ if (!(event.target instanceof HTMLButtonElement))
173
+ event.preventDefault();
174
+ }
175
+ }, children: [(0, jsx_runtime_1.jsxs)(material_1.DialogTitle, { id: titleId, sx: { pr: 7 }, children: [copy.title, (0, jsx_runtime_1.jsx)(cameraCropPresentation_js_1.DialogCloseButton, { label: copy.close, onClose: close })] }), (0, jsx_runtime_1.jsxs)(material_1.DialogContent, { children: [(0, jsx_runtime_1.jsx)(material_1.Box, { component: "video", ref: attachPreview, autoPlay: true, muted: true, playsInline: true, "aria-label": copy.preview, onLoadedData: () => {
176
+ var _a;
177
+ if (active.current &&
178
+ stream.current &&
179
+ ((_a = video.current) === null || _a === void 0 ? void 0 : _a.srcObject) === stream.current)
180
+ setReady(true);
181
+ }, sx: {
182
+ width: "100%",
183
+ maxHeight: previewMaxHeight,
184
+ bgcolor: "action.hover",
185
+ borderRadius: 1,
186
+ } }), error ? ((0, jsx_runtime_1.jsx)(cameraCropPresentation_js_2.StatusNotice, { tone: "error", message: error })) : ((0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", color: "text.secondary", role: "status", children: ready ? copy.ready : copy.opening }))] }), (0, jsx_runtime_1.jsx)(cameraCropPresentation_js_1.DialogActionButtons, { confirmLabel: copy.capture, confirmDisabled: !ready || Boolean(error) || busy, confirmIcon: busy ? (0, jsx_runtime_1.jsx)(cameraCropPresentation_js_1.DialogActionProgress, {}) : (0, jsx_runtime_1.jsx)(CameraAlt_1.default, {}), onConfirm: capture })] }));
187
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ "use client";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.FileRow = FileRow;
8
+ const jsx_runtime_1 = require("react/jsx-runtime");
9
+ const DeleteOutlineRounded_1 = __importDefault(require("@mui/icons-material/DeleteOutlineRounded"));
10
+ const material_1 = require("@mui/material");
11
+ const FileSwipeActionRow_js_1 = require("./FileSwipeActionRow.js");
12
+ function FileRow({ attachment, canDelete, onDelete, onPreview, thumbnail, sizeLabel, }) {
13
+ return ((0, jsx_runtime_1.jsx)(FileSwipeActionRow_js_1.SwipeActionRow, { canDelete: canDelete, onDelete: () => onDelete(attachment), dataComponent: "attachment-row-swipe-container", children: (0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: "row", sx: { alignItems: "center", gap: 1, minHeight: 56, px: 1.5 }, children: [(0, jsx_runtime_1.jsxs)(material_1.ButtonBase, { "aria-label": `Preview attachment ${attachment.fileName}`, "data-component": "attachment-preview-target", onClick: () => onPreview(attachment), sx: {
14
+ alignItems: "center",
15
+ borderRadius: 1,
16
+ flex: 1,
17
+ gap: 1,
18
+ justifyContent: "flex-start",
19
+ minWidth: 0,
20
+ p: 0.5,
21
+ textAlign: "left",
22
+ }, children: [thumbnail, (0, jsx_runtime_1.jsxs)(material_1.Box, { sx: { flex: 1, minWidth: 0 }, children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { noWrap: true, sx: { color: "text.primary", fontSize: 14 }, children: attachment.fileName }), (0, jsx_runtime_1.jsx)(material_1.Typography, { color: "text.secondary", sx: { fontSize: 12 }, children: sizeLabel })] })] }), canDelete ? ((0, jsx_runtime_1.jsx)(material_1.IconButton, { "aria-label": `Delete attachment ${attachment.fileName}`, onClick: () => onDelete(attachment), size: "small", children: (0, jsx_runtime_1.jsx)(DeleteOutlineRounded_1.default, { fontSize: "small" }) })) : null] }) }));
23
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ "use client";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.SwipeActionRow = void 0;
5
+ // The shared MUI row preserves the file adapter's error.main/common.white delete
6
+ // surface, background.paper content, spacing(2), Delete label, 160ms easing and
7
+ // reduced-motion behavior. Its gesture contract is the same: 12px direction lock,
8
+ // 72px delete threshold, 104px cap, pan-y, cancellation and click suppression.
9
+ // Keep one gesture implementation and the host's existing MUI theme context.
10
+ var SwipeActionRow_js_1 = require("../swipe/SwipeActionRow.js");
11
+ Object.defineProperty(exports, "SwipeActionRow", { enumerable: true, get: function () { return SwipeActionRow_js_1.SwipeActionRow; } });
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ "use client";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.FileThumbnail = FileThumbnail;
8
+ const jsx_runtime_1 = require("react/jsx-runtime");
9
+ const InsertDriveFileRounded_1 = __importDefault(require("@mui/icons-material/InsertDriveFileRounded"));
10
+ const material_1 = require("@mui/material");
11
+ const react_1 = require("react");
12
+ const rasterTypes = new Set([
13
+ "image/jpeg",
14
+ "image/png",
15
+ "image/gif",
16
+ "image/webp",
17
+ ]);
18
+ function FileThumbnail({ attachment, loadFile, }) {
19
+ var _a;
20
+ const { blobUrl, contentType, id } = attachment;
21
+ const [preview, setPreview] = (0, react_1.useState)(null);
22
+ const [failedSource, setFailedSource] = (0, react_1.useState)(null);
23
+ const source = `${id}/${blobUrl}`;
24
+ const isRaster = rasterTypes.has(contentType);
25
+ const url = failedSource !== source && isRaster
26
+ ? blobUrl.startsWith("blob:")
27
+ ? blobUrl
28
+ : (preview === null || preview === void 0 ? void 0 : preview.source) === source
29
+ ? preview.url
30
+ : null
31
+ : null;
32
+ const loadingPreview = isRaster &&
33
+ !blobUrl.startsWith("blob:") &&
34
+ failedSource !== source &&
35
+ (preview === null || preview === void 0 ? void 0 : preview.source) !== source;
36
+ (0, react_1.useEffect)(() => {
37
+ if (!rasterTypes.has(contentType) || blobUrl.startsWith("blob:"))
38
+ return;
39
+ const controller = new AbortController();
40
+ let objectUrl;
41
+ // Only raster decoders receive authenticated bytes; never embed documents or SVG.
42
+ // Host loaders may fail before returning a promise; both failures use the fallback.
43
+ void (async () => {
44
+ try {
45
+ const blob = await loadFile(attachment, controller.signal);
46
+ if (controller.signal.aborted)
47
+ return;
48
+ objectUrl = URL.createObjectURL(new Blob([blob], { type: contentType }));
49
+ // A successful current attempt supersedes an earlier failure for this source.
50
+ setFailedSource(null);
51
+ setPreview({ source, url: objectUrl });
52
+ }
53
+ catch (error) {
54
+ if (!controller.signal.aborted) {
55
+ console.error("[AttachmentPreview] Unable to load image thumbnail.", {
56
+ attachmentId: id,
57
+ error,
58
+ });
59
+ setFailedSource(source);
60
+ }
61
+ }
62
+ })();
63
+ return () => {
64
+ controller.abort();
65
+ if (objectUrl)
66
+ URL.revokeObjectURL(objectUrl);
67
+ };
68
+ }, [attachment, blobUrl, contentType, id, source, loadFile]);
69
+ return url ? ((0, jsx_runtime_1.jsx)(material_1.Box, { component: "img", src: url, alt: "", onError: () => {
70
+ console.error("[AttachmentPreview] Unable to decode image thumbnail.", {
71
+ attachmentId: id,
72
+ });
73
+ setFailedSource(source);
74
+ }, sx: {
75
+ width: 40,
76
+ height: 40,
77
+ flexShrink: 0,
78
+ objectFit: "cover",
79
+ borderRadius: 1,
80
+ outline: "1px solid",
81
+ outlineColor: (theme) => theme.palette.mode === "dark"
82
+ ? "rgba(255,255,255,0.1)"
83
+ : "rgba(0,0,0,0.1)",
84
+ } })) : loadingPreview ? ((0, jsx_runtime_1.jsx)(material_1.Stack, { "aria-label": "Loading attachment preview", sx: {
85
+ alignItems: "center",
86
+ bgcolor: "action.hover",
87
+ borderRadius: 1,
88
+ flexShrink: 0,
89
+ height: 40,
90
+ justifyContent: "center",
91
+ width: 40,
92
+ }, children: (0, jsx_runtime_1.jsx)(material_1.CircularProgress, { size: 18 }) })) : ((0, jsx_runtime_1.jsxs)(material_1.Stack, { "aria-hidden": true, sx: {
93
+ width: 40,
94
+ height: 40,
95
+ flexShrink: 0,
96
+ alignItems: "center",
97
+ justifyContent: "center",
98
+ bgcolor: "action.hover",
99
+ borderRadius: 1,
100
+ color: "text.secondary",
101
+ }, children: [(0, jsx_runtime_1.jsx)(InsertDriveFileRounded_1.default, { sx: { fontSize: 20 } }), (0, jsx_runtime_1.jsx)(material_1.Typography, { sx: { fontSize: 9, fontWeight: 600, maxWidth: 36 }, noWrap: true, children: attachment.fileName.includes(".")
102
+ ? (_a = attachment.fileName.split(".").pop()) === null || _a === void 0 ? void 0 : _a.slice(0, 5).toUpperCase()
103
+ : "FILE" })] }));
104
+ }
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ "use client";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.FileUpload = FileUpload;
8
+ const jsx_runtime_1 = require("react/jsx-runtime");
9
+ const hooks_1 = require("@bayonai/file-uploads/hooks");
10
+ const AddRounded_1 = __importDefault(require("@mui/icons-material/AddRounded"));
11
+ const AttachFileRounded_1 = __importDefault(require("@mui/icons-material/AttachFileRounded"));
12
+ const material_1 = require("@mui/material");
13
+ const react_1 = require("react");
14
+ function FileUpload({ attachments, canUpload = true, onUploadAttachment, label, accept, errorMessage, renderFile, renderViewer, RowShell, FieldIcon, colors, }) {
15
+ const inputRef = (0, react_1.useRef)(null);
16
+ const [previewAttachment, setPreviewAttachment] = (0, react_1.useState)(null);
17
+ const [uploadError, setUploadError] = (0, react_1.useState)(null);
18
+ const { busy: uploading, tryStart, finish } = (0, hooks_1.useUploadLock)();
19
+ const countLabel = attachments.length
20
+ ? `${attachments.length} attached file${attachments.length === 1 ? "" : "s"}`
21
+ : canUpload
22
+ ? "Add a file"
23
+ : "No files";
24
+ if (!canUpload && attachments.length === 0) {
25
+ return null;
26
+ }
27
+ async function handleFileChange(fileList) {
28
+ if (!canUpload)
29
+ return;
30
+ const file = fileList === null || fileList === void 0 ? void 0 : fileList[0];
31
+ if (!file) {
32
+ return;
33
+ }
34
+ if (!tryStart())
35
+ return;
36
+ setUploadError(null);
37
+ try {
38
+ await onUploadAttachment(file);
39
+ }
40
+ catch (error) {
41
+ console.error("[AttachmentSection] Attachment upload failed.", {
42
+ error,
43
+ fileName: file.name,
44
+ });
45
+ setUploadError(errorMessage(error));
46
+ }
47
+ finally {
48
+ finish();
49
+ if (inputRef.current) {
50
+ inputRef.current.value = "";
51
+ }
52
+ }
53
+ }
54
+ function openFilePicker() {
55
+ var _a;
56
+ if (!canUpload || uploading)
57
+ return;
58
+ (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.click();
59
+ }
60
+ function handlePreviewAttachment(attachment) {
61
+ setUploadError(null);
62
+ setPreviewAttachment(attachment);
63
+ }
64
+ return ((0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsxs)(material_1.Box, { sx: { position: "relative" }, children: [(0, jsx_runtime_1.jsxs)(RowShell, { hasBottomBorder: attachments.length === 0, children: [(0, jsx_runtime_1.jsx)(FieldIcon, { ariaLabel: `Add a file to ${label}`, onClick: canUpload && !uploading ? openFilePicker : undefined, children: (0, jsx_runtime_1.jsx)(AttachFileRounded_1.default, {}) }), canUpload && uploading ? ((0, jsx_runtime_1.jsx)(material_1.CircularProgress, { "aria-label": "Uploading attachment", size: 16, sx: { color: "primary.main" } })) : null, canUpload ? ((0, jsx_runtime_1.jsx)(material_1.ButtonBase, { "aria-label": `Add a file to ${label}`, component: "button", disabled: uploading, onClick: openFilePicker, sx: {
65
+ color: colors.text,
66
+ display: "block",
67
+ flex: 1,
68
+ fontSize: 13,
69
+ minWidth: 0,
70
+ overflow: "hidden",
71
+ textAlign: "left",
72
+ textOverflow: "ellipsis",
73
+ transition: `color ${colors.transition}`,
74
+ whiteSpace: "nowrap",
75
+ "&:hover": { color: "text.primary" },
76
+ "&.Mui-disabled": {
77
+ color: colors.text,
78
+ },
79
+ }, children: uploading ? "Uploading file..." : countLabel })) : ((0, jsx_runtime_1.jsx)(material_1.Typography, { noWrap: true, sx: {
80
+ color: colors.text,
81
+ flex: 1,
82
+ fontSize: 13,
83
+ }, children: countLabel })), canUpload ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.IconButton, { "aria-label": `Upload attachment to ${label}`, disabled: uploading, onClick: openFilePicker, size: "small", sx: { color: colors.text }, children: (0, jsx_runtime_1.jsx)(AddRounded_1.default, { fontSize: "small" }) }), (0, jsx_runtime_1.jsx)(material_1.Box, { accept: accept, component: "input", onChange: (event) => void handleFileChange(event.target.files), ref: inputRef, sx: { display: "none" }, type: "file" })] })) : null] }), canUpload && uploading ? ((0, jsx_runtime_1.jsx)(material_1.LinearProgress, { "aria-label": "Attachment upload progress", "data-component": "attachment-upload-progress", sx: {
84
+ bottom: 0,
85
+ height: 2,
86
+ left: 0,
87
+ position: "absolute",
88
+ right: 0,
89
+ } })) : null] }), attachments.length ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Box, { "aria-hidden": true, sx: {
90
+ borderTop: 1,
91
+ borderColor: colors.divider,
92
+ mx: "10%",
93
+ } }), (0, jsx_runtime_1.jsx)(material_1.Stack, { sx: {
94
+ borderBottom: 1,
95
+ borderColor: colors.divider,
96
+ py: 0.5,
97
+ }, children: attachments.map((attachment) => ((0, jsx_runtime_1.jsx)(material_1.Box, { children: renderFile(attachment, handlePreviewAttachment) }, attachment.id))) })] })) : null, uploadError ? ((0, jsx_runtime_1.jsx)(material_1.Typography, { color: "error", sx: { fontSize: 12, px: 1.5, py: 0.75 }, children: uploadError })) : null, renderViewer(previewAttachment, () => setPreviewAttachment(null))] }));
98
+ }
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ "use client";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.FileViewer = FileViewer;
8
+ const jsx_runtime_1 = require("react/jsx-runtime");
9
+ const react_1 = require("react");
10
+ const CloseRounded_1 = __importDefault(require("@mui/icons-material/CloseRounded"));
11
+ const DownloadRounded_1 = __importDefault(require("@mui/icons-material/DownloadRounded"));
12
+ const material_1 = require("@mui/material");
13
+ const react_2 = require("react");
14
+ const rasterImageTypes = new Set([
15
+ "image/jpeg",
16
+ "image/png",
17
+ "image/gif",
18
+ "image/webp",
19
+ ]);
20
+ function getPreviewKind(contentType) {
21
+ if (rasterImageTypes.has(contentType))
22
+ return "image";
23
+ if (contentType === "application/pdf")
24
+ return "pdf";
25
+ if (contentType.startsWith("text/") || contentType === "application/json") {
26
+ return "text";
27
+ }
28
+ if (contentType.startsWith("audio/"))
29
+ return "audio";
30
+ if (contentType.startsWith("video/"))
31
+ return "video";
32
+ return null;
33
+ }
34
+ function FileViewer(props) {
35
+ const { attachment } = props;
36
+ const fileKey = attachment
37
+ ? `${attachment.id}/${attachment.blobUrl}`
38
+ : "closed";
39
+ // Product decision: file-specific loading and error state belongs to the
40
+ // viewer. Consumers may replace the selected file without coordinating a
41
+ // React key or clearing state from the previous selection.
42
+ return (0, react_1.createElement)(FileViewerState, { ...props, key: fileKey });
43
+ }
44
+ function FileViewerState({ attachment, onClose, loadFile, onDownload, }) {
45
+ const [downloadError, setDownloadError] = (0, react_2.useState)(null);
46
+ const [downloading, setDownloading] = (0, react_2.useState)(false);
47
+ async function download() {
48
+ if (!attachment || downloading)
49
+ return;
50
+ setDownloading(true);
51
+ setDownloadError(null);
52
+ try {
53
+ await onDownload(attachment);
54
+ }
55
+ catch (error) {
56
+ console.error("[FileViewer] Download failed.", {
57
+ fileId: attachment.id,
58
+ error,
59
+ });
60
+ setDownloadError("Could not download this file. Check your connection and try downloading again.");
61
+ }
62
+ finally {
63
+ setDownloading(false);
64
+ }
65
+ }
66
+ const previewKind = attachment
67
+ ? getPreviewKind(attachment.contentType)
68
+ : null;
69
+ return ((0, jsx_runtime_1.jsxs)(material_1.Dialog, { fullWidth: true, maxWidth: "md", onClose: onClose, open: Boolean(attachment), children: [(0, jsx_runtime_1.jsxs)(material_1.DialogTitle, { sx: { pr: 6 }, children: [attachment === null || attachment === void 0 ? void 0 : attachment.fileName, (0, jsx_runtime_1.jsx)(material_1.IconButton, { "aria-label": "Close attachment preview", onClick: onClose, sx: { position: "absolute", right: 8, top: 8 }, children: (0, jsx_runtime_1.jsx)(CloseRounded_1.default, {}) })] }), (0, jsx_runtime_1.jsx)(material_1.DialogContent, { dividers: true, sx: { minHeight: 240 }, children: attachment ? ((0, jsx_runtime_1.jsx)(AttachmentViewerContent, { attachment: attachment, loadFile: loadFile, previewKind: previewKind })) : null }), downloadError ? ((0, jsx_runtime_1.jsx)(material_1.Typography, { role: "alert", color: "error", sx: { px: 3 }, children: downloadError })) : null, (0, jsx_runtime_1.jsx)(material_1.DialogActions, { children: attachment ? ((0, jsx_runtime_1.jsx)(material_1.IconButton, { "aria-label": "Download attachment", disabled: downloading, onClick: () => void download(), children: (0, jsx_runtime_1.jsx)(DownloadRounded_1.default, {}) })) : null })] }));
70
+ }
71
+ function AttachmentViewerContent({ attachment, previewKind, loadFile, }) {
72
+ if (!previewKind) {
73
+ return ((0, jsx_runtime_1.jsx)(material_1.Typography, { color: "text.secondary", children: "This file type can\u2019t be displayed here. Download it to open it in a compatible app." }));
74
+ }
75
+ return ((0, jsx_runtime_1.jsx)(LoadableAttachmentPreview, { attachment: attachment, previewKind: previewKind, loadFile: loadFile }));
76
+ }
77
+ function LoadableAttachmentPreview({ attachment, previewKind, loadFile, }) {
78
+ const [error, setError] = (0, react_2.useState)(null);
79
+ const [loading, setLoading] = (0, react_2.useState)(true);
80
+ const [previewUrl, setPreviewUrl] = (0, react_2.useState)(null);
81
+ const [text, setText] = (0, react_2.useState)(null);
82
+ function handleMediaDecodeError() {
83
+ console.error("[FileViewer] Browser could not decode the file preview.", {
84
+ contentType: attachment.contentType,
85
+ fileId: attachment.id,
86
+ previewKind,
87
+ });
88
+ setError("Unable to display this file. You can download it instead.");
89
+ }
90
+ (0, react_2.useEffect)(() => {
91
+ let objectUrl = null;
92
+ let cancelled = false;
93
+ const controller = new AbortController();
94
+ void (async () => {
95
+ try {
96
+ const blob = attachment.blobUrl.startsWith("blob:")
97
+ ? await fetch(attachment.blobUrl).then((response) => response.blob())
98
+ : await loadFile(attachment, controller.signal);
99
+ if (cancelled)
100
+ return;
101
+ if (previewKind === "text") {
102
+ const content = await blob.text();
103
+ if (!cancelled)
104
+ setText(content);
105
+ return;
106
+ }
107
+ objectUrl = attachment.blobUrl.startsWith("blob:")
108
+ ? attachment.blobUrl
109
+ : URL.createObjectURL(new Blob([blob], { type: attachment.contentType }));
110
+ setPreviewUrl(objectUrl);
111
+ }
112
+ catch (caughtError) {
113
+ if (!cancelled) {
114
+ console.error("[FileViewer] Unable to load the file preview.", {
115
+ contentType: attachment.contentType,
116
+ error: caughtError,
117
+ fileId: attachment.id,
118
+ });
119
+ setError("Unable to display this file. You can download it instead.");
120
+ }
121
+ }
122
+ finally {
123
+ if (!cancelled)
124
+ setLoading(false);
125
+ }
126
+ })();
127
+ return () => {
128
+ cancelled = true;
129
+ controller.abort();
130
+ if (objectUrl && !attachment.blobUrl.startsWith("blob:")) {
131
+ URL.revokeObjectURL(objectUrl);
132
+ }
133
+ };
134
+ }, [attachment, previewKind, loadFile]);
135
+ return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: loading ? ((0, jsx_runtime_1.jsx)(material_1.Box, { sx: { display: "grid", minHeight: 200, placeItems: "center" }, children: (0, jsx_runtime_1.jsx)(material_1.CircularProgress, { "aria-label": "Loading attachment preview" }) })) : error ? ((0, jsx_runtime_1.jsx)(material_1.Typography, { color: "error", role: "alert", children: error })) : previewKind === "image" ? ((0, jsx_runtime_1.jsx)(material_1.Box, { alt: attachment.fileName, component: "img", onError: handleMediaDecodeError, src: previewUrl !== null && previewUrl !== void 0 ? previewUrl : undefined, sx: {
136
+ display: "block",
137
+ maxHeight: "65vh",
138
+ maxWidth: "100%",
139
+ mx: "auto",
140
+ } })) : previewKind === "pdf" ? ((0, jsx_runtime_1.jsx)(material_1.Box, { component: "iframe", referrerPolicy: "no-referrer",
141
+ // Product decision: PDFs are collaborator-controlled active-format
142
+ // documents. Keep their browsing context opaque and block scripts,
143
+ // forms, popups and top-level navigation while retaining preview.
144
+ sandbox: "", src: previewUrl !== null && previewUrl !== void 0 ? previewUrl : undefined, title: "Attachment preview", sx: { border: 0, height: "65vh", width: "100%" } })) : previewKind === "text" ? ((0, jsx_runtime_1.jsx)(material_1.Box, { component: "pre", sx: {
145
+ fontFamily: "monospace",
146
+ fontSize: 13,
147
+ m: 0,
148
+ overflow: "auto",
149
+ whiteSpace: "pre-wrap",
150
+ }, children: text })) : previewKind === "audio" ? ((0, jsx_runtime_1.jsx)(material_1.Box, { component: "audio", controls: true, onError: handleMediaDecodeError, src: previewUrl !== null && previewUrl !== void 0 ? previewUrl : undefined, sx: { width: "100%" } })) : ((0, jsx_runtime_1.jsx)(material_1.Box, { component: "video", controls: true, onError: handleMediaDecodeError, src: previewUrl !== null && previewUrl !== void 0 ? previewUrl : undefined, sx: { maxHeight: "65vh", maxWidth: "100%", width: "100%" } })) }));
151
+ }