@salesforce/webapp-template-feature-react-file-upload-experimental 1.55.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 (126) hide show
  1. package/LICENSE.txt +82 -0
  2. package/README.md +102 -0
  3. package/dist/.a4drules/README.md +35 -0
  4. package/dist/.a4drules/a4d-webapp-generate.md +27 -0
  5. package/dist/.a4drules/build-validation.md +78 -0
  6. package/dist/.a4drules/code-quality.md +137 -0
  7. package/dist/.a4drules/graphql/tools/knowledge/lds-explore-graphql-schema.md +227 -0
  8. package/dist/.a4drules/graphql/tools/knowledge/lds-generate-graphql-mutationquery.md +212 -0
  9. package/dist/.a4drules/graphql/tools/knowledge/lds-generate-graphql-readquery.md +185 -0
  10. package/dist/.a4drules/graphql/tools/knowledge/lds-guide-graphql.md +205 -0
  11. package/dist/.a4drules/graphql/tools/schemas/shared.graphqls +1150 -0
  12. package/dist/.a4drules/graphql.md +409 -0
  13. package/dist/.a4drules/images.md +13 -0
  14. package/dist/.a4drules/react.md +387 -0
  15. package/dist/.a4drules/react_image_processing.md +45 -0
  16. package/dist/.a4drules/typescript.md +224 -0
  17. package/dist/.a4drules/ui-layout.md +23 -0
  18. package/dist/.a4drules/webapp-nav-and-placeholders.md +33 -0
  19. package/dist/.a4drules/webapp-no-node-e.md +25 -0
  20. package/dist/.a4drules/webapp-ui-first.md +32 -0
  21. package/dist/.a4drules/webapp.md +75 -0
  22. package/dist/.forceignore +15 -0
  23. package/dist/.husky/pre-commit +4 -0
  24. package/dist/.prettierignore +11 -0
  25. package/dist/.prettierrc +17 -0
  26. package/dist/AGENT.md +75 -0
  27. package/dist/CHANGELOG.md +803 -0
  28. package/dist/README.md +18 -0
  29. package/dist/config/project-scratch-def.json +13 -0
  30. package/dist/force-app/main/default/webapplications/feature-react-file-upload/.graphqlrc.yml +2 -0
  31. package/dist/force-app/main/default/webapplications/feature-react-file-upload/.prettierignore +9 -0
  32. package/dist/force-app/main/default/webapplications/feature-react-file-upload/.prettierrc +11 -0
  33. package/dist/force-app/main/default/webapplications/feature-react-file-upload/build/vite.config.d.ts +2 -0
  34. package/dist/force-app/main/default/webapplications/feature-react-file-upload/build/vite.config.js +93 -0
  35. package/dist/force-app/main/default/webapplications/feature-react-file-upload/codegen.yml +94 -0
  36. package/dist/force-app/main/default/webapplications/feature-react-file-upload/e2e/app.spec.ts +17 -0
  37. package/dist/force-app/main/default/webapplications/feature-react-file-upload/eslint.config.js +141 -0
  38. package/dist/force-app/main/default/webapplications/feature-react-file-upload/feature-react-file-upload.webapplication-meta.xml +7 -0
  39. package/dist/force-app/main/default/webapplications/feature-react-file-upload/index.html +13 -0
  40. package/dist/force-app/main/default/webapplications/feature-react-file-upload/package-lock.json +18396 -0
  41. package/dist/force-app/main/default/webapplications/feature-react-file-upload/package.json +66 -0
  42. package/dist/force-app/main/default/webapplications/feature-react-file-upload/playwright.config.ts +24 -0
  43. package/dist/force-app/main/default/webapplications/feature-react-file-upload/scripts/get-graphql-schema.mjs +68 -0
  44. package/dist/force-app/main/default/webapplications/feature-react-file-upload/scripts/rewrite-e2e-assets.mjs +23 -0
  45. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/api/fileUpload.ts +154 -0
  46. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/api/graphql-operations-types.ts +116 -0
  47. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/api/utils/accounts.ts +41 -0
  48. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/api/utils/query/highRevenueAccountsQuery.graphql +29 -0
  49. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/app.tsx +22 -0
  50. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/appLayout.tsx +9 -0
  51. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/icons/book.svg +3 -0
  52. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/icons/copy.svg +4 -0
  53. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/icons/rocket.svg +3 -0
  54. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/icons/star.svg +3 -0
  55. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/images/codey-1.png +0 -0
  56. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/images/codey-2.png +0 -0
  57. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/images/codey-3.png +0 -0
  58. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/images/vibe-codey.svg +194 -0
  59. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/symbols.svg +1 -0
  60. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/assets/utility.svg +1 -0
  61. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUpload.tsx +83 -0
  62. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadDialog.tsx +79 -0
  63. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadDropZone.tsx +82 -0
  64. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadFileItem.tsx +99 -0
  65. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadIcons.tsx +58 -0
  66. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/alert.tsx +69 -0
  67. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/button.tsx +67 -0
  68. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/card.tsx +92 -0
  69. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/dialog.tsx +143 -0
  70. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/field.tsx +222 -0
  71. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/index.ts +84 -0
  72. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/input.tsx +19 -0
  73. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/label.tsx +19 -0
  74. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/pagination.tsx +112 -0
  75. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/select.tsx +183 -0
  76. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/separator.tsx +26 -0
  77. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/skeleton.tsx +14 -0
  78. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/spinner.tsx +15 -0
  79. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/table.tsx +87 -0
  80. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components/ui/tabs.tsx +78 -0
  81. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/components.json +18 -0
  82. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/hooks/useFileUpload.ts +299 -0
  83. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/hooks/useFileUploadDialog.ts +70 -0
  84. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/index.ts +56 -0
  85. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/lib/utils.ts +6 -0
  86. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/navigationMenu.tsx +80 -0
  87. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/pages/Home.tsx +25 -0
  88. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/pages/NotFound.tsx +18 -0
  89. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/router-utils.tsx +35 -0
  90. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/routes.tsx +22 -0
  91. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/styles/global.css +135 -0
  92. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/types/fileUpload.ts +26 -0
  93. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/utils/fileUploadUtils.ts +44 -0
  94. package/dist/force-app/main/default/webapplications/feature-react-file-upload/src/utils/labels.ts +21 -0
  95. package/dist/force-app/main/default/webapplications/feature-react-file-upload/tsconfig.json +36 -0
  96. package/dist/force-app/main/default/webapplications/feature-react-file-upload/tsconfig.node.json +13 -0
  97. package/dist/force-app/main/default/webapplications/feature-react-file-upload/vite-env.d.ts +1 -0
  98. package/dist/force-app/main/default/webapplications/feature-react-file-upload/vite.config.ts +43 -0
  99. package/dist/force-app/main/default/webapplications/feature-react-file-upload/vitest-env.d.ts +2 -0
  100. package/dist/force-app/main/default/webapplications/feature-react-file-upload/vitest.config.ts +11 -0
  101. package/dist/force-app/main/default/webapplications/feature-react-file-upload/vitest.setup.ts +1 -0
  102. package/dist/force-app/main/default/webapplications/feature-react-file-upload/webapplication.json +7 -0
  103. package/dist/jest.config.js +6 -0
  104. package/dist/package.json +38 -0
  105. package/dist/scripts/apex/hello.apex +10 -0
  106. package/dist/scripts/soql/account.soql +6 -0
  107. package/dist/sfdx-project.json +12 -0
  108. package/package.json +53 -0
  109. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/api/fileUpload.ts +154 -0
  110. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/appLayout.tsx +9 -0
  111. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/assets/symbols.svg +1 -0
  112. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/assets/utility.svg +1 -0
  113. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUpload.tsx +83 -0
  114. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadDialog.tsx +79 -0
  115. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadDropZone.tsx +82 -0
  116. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadFileItem.tsx +99 -0
  117. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/components/FileUploadIcons.tsx +58 -0
  118. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/hooks/useFileUpload.ts +299 -0
  119. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/hooks/useFileUploadDialog.ts +70 -0
  120. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/index.ts +56 -0
  121. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/pages/Home.tsx +25 -0
  122. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/routes.tsx +17 -0
  123. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/types/fileUpload.ts +26 -0
  124. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/utils/fileUploadUtils.ts +44 -0
  125. package/src/force-app/main/default/webapplications/feature-react-file-upload/src/utils/labels.ts +21 -0
  126. package/src/force-app/main/default/webapplications/feature-react-file-upload/vite.config.ts +43 -0
@@ -0,0 +1,83 @@
1
+ import { FileUploadDropZone } from "./FileUploadDropZone";
2
+ import { FileUploadDialog } from "./FileUploadDialog";
3
+ import { LABELS } from "../utils/labels";
4
+ import { useFileUpload } from "../hooks/useFileUpload";
5
+ import { useFileUploadDialog } from "../hooks/useFileUploadDialog";
6
+
7
+ export interface FileUploadProps {
8
+ /** MIME types to accept (e.g. image/*). Omit for all files. */
9
+ accept?: string;
10
+ /** Whether to allow multiple file selection. Default: false */
11
+ multiple?: boolean;
12
+ /** Record Id for FirstPublishLocationId (e.g. Account, Opportunity). When provided, files are linked to this record. Otherwise, current user Id is used. */
13
+ recordId?: string;
14
+ /** Called when uploads complete. Receives array of successfully uploaded files with name, size, and contentVersionId. */
15
+ onUploadComplete?: (files: { name: string; size: number; contentVersionId?: string }[]) => void;
16
+ onUploadError?: (file: File, error: string) => void;
17
+ /** Optional CSS class for the wrapper div */
18
+ className?: string;
19
+ }
20
+
21
+ /**
22
+ * File upload component. Renders a drop zone for selecting files, a modal dialog
23
+ * showing upload progress, and a list of successfully uploaded files. Supports
24
+ * click-to-select and drag-and-drop. Composes FileUploadDropZone and FileUploadDialog.
25
+ */
26
+ export function FileUpload({
27
+ accept,
28
+ multiple = false,
29
+ recordId,
30
+ onUploadComplete,
31
+ onUploadError,
32
+ className = "",
33
+ }: FileUploadProps) {
34
+ const { fileItems, getInputProps, getDropZoneProps, isDragging, reset, cancelFile, allDone } =
35
+ useFileUpload({
36
+ accept,
37
+ multiple,
38
+ recordId,
39
+ onUploadComplete,
40
+ onUploadError,
41
+ });
42
+
43
+ const { dialogOpen, uploadedFileNames, handleOpenChange } = useFileUploadDialog({
44
+ fileItems,
45
+ reset,
46
+ });
47
+
48
+ const inputProps = getInputProps();
49
+ const dropZoneProps = getDropZoneProps();
50
+
51
+ return (
52
+ <div className={["w-fit", className].filter(Boolean).join(" ")}>
53
+ <div className="flex w-fit flex-col gap-1">
54
+ <FileUploadDropZone
55
+ inputProps={inputProps}
56
+ dropZoneProps={dropZoneProps}
57
+ isDragging={isDragging}
58
+ />
59
+
60
+ <FileUploadDialog
61
+ open={dialogOpen}
62
+ onOpenChange={handleOpenChange}
63
+ fileItems={fileItems}
64
+ onCancelFile={cancelFile}
65
+ allDone={allDone}
66
+ />
67
+
68
+ {uploadedFileNames.length > 0 && (
69
+ <ul
70
+ className="mt-2 list-inside list-disc space-y-1 text-sm text-gray-700"
71
+ aria-label={LABELS.uploadedFiles}
72
+ >
73
+ {uploadedFileNames.map((name, i) => (
74
+ <li key={`${name}-${i}`}>{name}</li>
75
+ ))}
76
+ </ul>
77
+ )}
78
+ </div>
79
+ </div>
80
+ );
81
+ }
82
+
83
+ export default FileUpload;
@@ -0,0 +1,79 @@
1
+ import {
2
+ Button,
3
+ Dialog,
4
+ DialogClose,
5
+ DialogContent,
6
+ DialogFooter,
7
+ DialogHeader,
8
+ DialogTitle,
9
+ } from "@/components/ui";
10
+ import { FileUploadFileItem } from "./FileUploadFileItem";
11
+ import { formatUploadSummary } from "../utils/fileUploadUtils";
12
+ import { LABELS } from "../utils/labels";
13
+ import type { FileUploadItem } from "../types/fileUpload";
14
+
15
+ export interface FileUploadDialogProps {
16
+ /** Whether the dialog is open */
17
+ open: boolean;
18
+ /** Called when the dialog open state changes (e.g. close button, Done) */
19
+ onOpenChange: (open: boolean) => void;
20
+ /** File items with upload state and progress */
21
+ fileItems: FileUploadItem[];
22
+ /** Called when the user cancels an in-progress upload */
23
+ onCancelFile: (fileName: string) => void;
24
+ /** Whether all uploads are complete (success, error, or cancelled) */
25
+ allDone: boolean;
26
+ }
27
+
28
+ /**
29
+ * Modal dialog showing upload progress. Lists each file with FileUploadFileItem,
30
+ * displays upload summary (e.g. "1 of 2 files uploaded"), and a Done button
31
+ * disabled until all uploads complete.
32
+ */
33
+ export function FileUploadDialog({
34
+ open,
35
+ onOpenChange,
36
+ fileItems,
37
+ onCancelFile,
38
+ allDone,
39
+ }: FileUploadDialogProps) {
40
+ const successCount = fileItems.filter((i) => i.state === "success").length;
41
+ const summaryText = formatUploadSummary(successCount, fileItems.length);
42
+
43
+ return (
44
+ <Dialog open={open} onOpenChange={onOpenChange}>
45
+ <DialogContent showCloseButton>
46
+ <DialogHeader>
47
+ <DialogTitle className="text-blue-900">{LABELS.uploadFilesDialogTitle}</DialogTitle>
48
+ </DialogHeader>
49
+
50
+ <ul className="max-h-60 min-w-0 space-y-2 overflow-y-auto" aria-label={LABELS.uploadStatus}>
51
+ {fileItems.map((item, i) => (
52
+ <FileUploadFileItem
53
+ key={`${item.file.name}-${i}`}
54
+ item={item}
55
+ onCancel={onCancelFile}
56
+ />
57
+ ))}
58
+ </ul>
59
+
60
+ <DialogFooter className="grid min-w-0 grid-cols-[auto_minmax(0,4fr)_minmax(0,5fr)] items-center px-2 pb-1 pt-2">
61
+ <div aria-hidden />
62
+ <p className="min-w-0 text-sm font-medium text-blue-900">{summaryText}</p>
63
+ <div className="flex min-w-0 justify-end">
64
+ <DialogClose asChild>
65
+ <Button
66
+ variant="outline"
67
+ className="flex-shrink-0"
68
+ disabled={!allDone}
69
+ aria-label={LABELS.doneButton}
70
+ >
71
+ {LABELS.done}
72
+ </Button>
73
+ </DialogClose>
74
+ </div>
75
+ </DialogFooter>
76
+ </DialogContent>
77
+ </Dialog>
78
+ );
79
+ }
@@ -0,0 +1,82 @@
1
+ import { UtilityIcon } from "./FileUploadIcons";
2
+ import { LABELS } from "../utils/labels";
3
+
4
+ export interface FileUploadDropZoneProps {
5
+ /** Props for the hidden file input (ref, type, accept, multiple, onChange) */
6
+ inputProps: {
7
+ ref: React.RefObject<HTMLInputElement | null>;
8
+ type: "file";
9
+ accept?: string;
10
+ multiple: boolean;
11
+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
12
+ };
13
+ /** Props for the drop zone (onClick, onDragOver, onDragLeave, onDrop, onKeyDown) */
14
+ dropZoneProps: {
15
+ onClick: () => void;
16
+ onDragOver: (e: React.DragEvent) => void;
17
+ onDragLeave: (e: React.DragEvent) => void;
18
+ onDrop: (e: React.DragEvent) => void;
19
+ onKeyDown: (e: React.KeyboardEvent) => void;
20
+ };
21
+ /** Whether the user is currently dragging over the drop zone */
22
+ isDragging: boolean;
23
+ }
24
+
25
+ const DROP_ZONE_BASE_CLASSES =
26
+ "mb-2 flex cursor-pointer flex-wrap items-center gap-3 rounded-[4px] border border-dashed p-1 transition-colors";
27
+ const DROP_ZONE_DRAGGING_CLASSES = "border-blue-500 bg-blue-50";
28
+ const DROP_ZONE_IDLE_CLASSES = "hover:bg-gray-50";
29
+ const BORDER_COLOR_IDLE = "rgb(116, 116, 116)";
30
+
31
+ /**
32
+ * Drop zone for file selection. Renders a dashed border area with "Upload Files"
33
+ * button and "Or drop files" / "Drop files here" text. Accepts click and drag-and-drop.
34
+ * Uses a hidden file input; props come from useFileUpload.
35
+ */
36
+ export function FileUploadDropZone({
37
+ inputProps,
38
+ dropZoneProps,
39
+ isDragging,
40
+ }: FileUploadDropZoneProps) {
41
+ const dropZoneClassName = [
42
+ DROP_ZONE_BASE_CLASSES,
43
+ isDragging ? DROP_ZONE_DRAGGING_CLASSES : DROP_ZONE_IDLE_CLASSES,
44
+ ].join(" ");
45
+
46
+ return (
47
+ <>
48
+ <p className="mb-1 text-sm font-semibold text-gray-900">{LABELS.attach}</p>
49
+ <div
50
+ role="button"
51
+ tabIndex={0}
52
+ onClick={dropZoneProps.onClick}
53
+ onDragOver={dropZoneProps.onDragOver}
54
+ onDragLeave={dropZoneProps.onDragLeave}
55
+ onDrop={dropZoneProps.onDrop}
56
+ onKeyDown={dropZoneProps.onKeyDown}
57
+ className={dropZoneClassName}
58
+ style={!isDragging ? { borderColor: BORDER_COLOR_IDLE } : undefined}
59
+ aria-label={LABELS.dropZone}
60
+ data-testid="file-upload-drop-zone"
61
+ >
62
+ <input
63
+ id="file-upload-input-id"
64
+ ref={inputProps.ref}
65
+ type="file"
66
+ accept={inputProps.accept}
67
+ multiple={inputProps.multiple}
68
+ onChange={inputProps.onChange}
69
+ className="sr-only"
70
+ aria-hidden
71
+ />
72
+ <span className="inline-flex items-center gap-2 rounded-[4px] border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-blue-600 hover:bg-gray-50">
73
+ <UtilityIcon id="upload" size="sm" />
74
+ {LABELS.uploadFiles}
75
+ </span>
76
+ <span className="text-sm text-gray-900">
77
+ {isDragging ? LABELS.dropFilesHere : LABELS.orDropFiles}
78
+ </span>
79
+ </div>
80
+ </>
81
+ );
82
+ }
@@ -0,0 +1,99 @@
1
+ import { Button } from "@/components/ui";
2
+ import { FileTypeIcon, UtilityIcon } from "./FileUploadIcons";
3
+ import {
4
+ formatFileSize,
5
+ getFileExtension,
6
+ getProgressWidth,
7
+ isUploading,
8
+ } from "../utils/fileUploadUtils";
9
+ import { LABELS } from "../utils/labels";
10
+ import type { FileUploadItem } from "../types/fileUpload";
11
+
12
+ export interface FileUploadFileItemProps {
13
+ /** The file item with upload state, progress, and optional error */
14
+ item: FileUploadItem;
15
+ /** Called when the user cancels an in-progress upload */
16
+ onCancel: (fileName: string) => void;
17
+ }
18
+
19
+ /**
20
+ * Renders a single file upload item with file type icon, name, size, progress bar,
21
+ * cancel button (when uploading), and status icon (success, error, or cancelled).
22
+ */
23
+ export function FileUploadFileItem({ item, onCancel }: FileUploadFileItemProps) {
24
+ const extension = getFileExtension(item.file.name);
25
+ const progressWidth = getProgressWidth(item.state, item.progress);
26
+
27
+ return (
28
+ <li
29
+ className="grid min-w-0 grid-cols-[auto_minmax(0,4fr)_minmax(0,5fr)] items-center gap-2 rounded-lg border border-gray-200 bg-gray-100 p-2"
30
+ data-testid="file-upload-item"
31
+ >
32
+ <FileTypeIcon extension={extension} />
33
+ <div className="min-w-0 overflow-hidden">
34
+ <p
35
+ className="truncate text-sm font-medium text-gray-900"
36
+ title={item.file.name}
37
+ aria-label={LABELS.fileName(item.file.name)}
38
+ >
39
+ {item.file.name}
40
+ </p>
41
+ <p
42
+ className="truncate text-xs text-gray-600"
43
+ aria-label={LABELS.fileSize(formatFileSize(item.file.size))}
44
+ >
45
+ {formatFileSize(item.file.size)}
46
+ </p>
47
+ </div>
48
+ <div className="flex min-w-0 items-center gap-1.5">
49
+ <div
50
+ className="h-1.5 min-w-0 flex-1 overflow-hidden rounded-full bg-gray-200"
51
+ role="progressbar"
52
+ aria-label={LABELS.uploadProgress(item.file.name)}
53
+ aria-valuenow={progressWidth}
54
+ aria-valuemin={0}
55
+ aria-valuemax={100}
56
+ >
57
+ <div
58
+ className="h-full bg-blue-600 transition-all duration-300"
59
+ style={{ width: `${progressWidth}%` }}
60
+ />
61
+ </div>
62
+ {isUploading(item.state) && (
63
+ <Button
64
+ type="button"
65
+ variant="ghost"
66
+ size="icon"
67
+ className="h-7 w-7 shrink-0 text-gray-600 hover:text-red-600"
68
+ onClick={() => onCancel(item.file.name)}
69
+ aria-label={LABELS.cancelUpload(item.file.name)}
70
+ >
71
+ <UtilityIcon id="clear" fill="rgb(116, 116, 116)" />
72
+ </Button>
73
+ )}
74
+ {renderStatusIcon(item.state)}
75
+ {item.state === "error" && item.error && <p className="sr-only">{item.error}</p>}
76
+ </div>
77
+ </li>
78
+ );
79
+ }
80
+
81
+ function renderStatusIcon(state: FileUploadItem["state"]): React.ReactNode {
82
+ if (isUploading(state)) return null;
83
+ if (state === "success") return <UtilityIcon id="success" fill="rgb(46, 132, 74)" />;
84
+ if (state === "cancelled") {
85
+ return (
86
+ <span className="shrink-0 text-xs text-gray-500" aria-label={LABELS.cancelled}>
87
+ {LABELS.cancelled}
88
+ </span>
89
+ );
90
+ }
91
+ return (
92
+ <span
93
+ className="inline-flex h-4 w-4 shrink-0 items-center justify-center text-red-600"
94
+ aria-label={LABELS.uploadFailed}
95
+ >
96
+ ×
97
+ </span>
98
+ );
99
+ }
@@ -0,0 +1,58 @@
1
+ import symbolsUrl from "@assets/symbols.svg?url";
2
+
3
+ import utilitySvg from "@assets/utility.svg?url";
4
+
5
+ const IMAGE_EXTENSIONS = new Set([
6
+ "jpg",
7
+ "jpeg",
8
+ "png",
9
+ "gif",
10
+ "webp",
11
+ "svg",
12
+ "bmp",
13
+ "ico",
14
+ "tiff",
15
+ "tif",
16
+ ]);
17
+
18
+ /**
19
+ * Renders a file-type icon based on extension. Uses symbols.svg: "unknown" for
20
+ * no extension, "image" for image types, or the extension (e.g. pdf) for others.
21
+ */
22
+ export function FileTypeIcon({ extension }: { extension: string }) {
23
+ /** Symbol ID in symbols.svg: unknown (no ext), image (image types), or extension (e.g. pdf). */
24
+ const ext = extension.toLowerCase();
25
+ const symbolId = extension === "FILE" ? "unknown" : IMAGE_EXTENSIONS.has(ext) ? "image" : ext;
26
+ return (
27
+ <span
28
+ className="inline-flex h-8 w-8 shrink-0 items-center justify-center"
29
+ aria-hidden
30
+ title={extension}
31
+ >
32
+ <svg className="h-full w-full" aria-hidden>
33
+ <use href={`${symbolsUrl}#${symbolId}`} />
34
+ </svg>
35
+ </span>
36
+ );
37
+ }
38
+ /** Symbol ID in utility.svg (e.g. clear, success, upload) */
39
+ export interface UtilityIconProps {
40
+ id: string;
41
+ size?: "sm" | "md";
42
+ fill?: string;
43
+ }
44
+
45
+ /**
46
+ * Renders a utility icon from utility.svg by symbol id. Supports size (sm/md)
47
+ * and fill color. Used for clear, success, upload, etc.
48
+ */
49
+ export function UtilityIcon({ id, size = "md", fill = "currentColor" }: UtilityIconProps) {
50
+ const sizeClass = size === "sm" ? "h-4 w-4" : "h-6 w-6";
51
+ return (
52
+ <span className={`inline-flex ${sizeClass} shrink-0 items-center justify-center`} aria-hidden>
53
+ <svg className="h-full w-full" fill={fill} aria-hidden>
54
+ <use href={`${utilitySvg}#${id}`} />
55
+ </svg>
56
+ </span>
57
+ );
58
+ }
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Hook: useFileUpload
3
+ *
4
+ * Manages file upload state and logic: config fetch, upload to URL, ContentVersion creation.
5
+ * Separates upload logic from UI. Use for custom upload UIs or with the FileUpload component.
6
+ *
7
+ * @param options - Upload options (accept, multiple, recordId, onUploadComplete, onUploadError)
8
+ * @returns Object containing fileItems, allDone, cancelFile, getInputProps, getDropZoneProps, openFilePicker, isDragging, reset
9
+ *
10
+ * @remarks
11
+ * Coordinates: (1) getUploadConfig for token/URL, (2) uploadToUrl for file body, (3) createContentVersion
12
+ * for record creation. Supports cancel, progress tracking, and sequential multi-file uploads.
13
+ */
14
+ import * as React from "react";
15
+ import { flushSync } from "react-dom";
16
+ import {
17
+ getUploadConfig,
18
+ uploadToUrl,
19
+ createContentVersion,
20
+ getCurrentUserId,
21
+ } from "../api/fileUpload";
22
+ import type { FileUploadItem, UploadedFile, UploadState } from "../types/fileUpload";
23
+ import { isFileTooLarge, MAX_FILE_SIZE_BYTES, formatFileSize } from "../utils/fileUploadUtils";
24
+ import { LABELS } from "../utils/labels";
25
+
26
+ export interface UseFileUploadOptions {
27
+ /** MIME types or file extensions to accept (e.g. image/*, .pdf). Omit for all files. */
28
+ accept?: string;
29
+ /** Whether to allow multiple file selection. Default: false */
30
+ multiple?: boolean;
31
+ /** Record Id for FirstPublishLocationId (e.g. Account, Opportunity). When provided, files are linked to this record. Otherwise, current user Id is used. */
32
+ recordId?: string;
33
+ /** Called when uploads complete. Receives array of successfully uploaded files with name, size, and contentVersionId. */
34
+ onUploadComplete?: (files: UploadedFile[]) => void;
35
+ /** Called when an upload fails. Receives the file and error message. */
36
+ onUploadError?: (file: File, error: string) => void;
37
+ }
38
+
39
+ function updateItem(
40
+ items: FileUploadItem[],
41
+ fileName: string,
42
+ update: Partial<FileUploadItem>,
43
+ ): FileUploadItem[] {
44
+ return items.map((item) => (item.file.name === fileName ? { ...item, ...update } : item));
45
+ }
46
+
47
+ interface UseFileUploadReturn {
48
+ fileItems: FileUploadItem[];
49
+ allDone: boolean;
50
+ cancelFile: (fileName: string) => void;
51
+ getInputProps: () => {
52
+ ref: React.RefObject<HTMLInputElement | null>;
53
+ type: "file";
54
+ accept?: string;
55
+ multiple: boolean;
56
+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
57
+ };
58
+ getDropZoneProps: () => {
59
+ onClick: () => void;
60
+ onDragOver: (e: React.DragEvent) => void;
61
+ onDragLeave: (e: React.DragEvent) => void;
62
+ onDrop: (e: React.DragEvent) => void;
63
+ onKeyDown: (e: React.KeyboardEvent) => void;
64
+ };
65
+ /** Programmatically open the native file picker. Use with a custom trigger (e.g. button). */
66
+ openFilePicker: () => void;
67
+ isDragging: boolean;
68
+ reset: () => void;
69
+ }
70
+
71
+ export function useFileUpload(options: UseFileUploadOptions = {}): UseFileUploadReturn {
72
+ const { accept, multiple = false, recordId, onUploadComplete, onUploadError } = options;
73
+
74
+ const [fileItems, setFileItems] = React.useState<FileUploadItem[]>([]);
75
+ const [isDragging, setIsDragging] = React.useState(false);
76
+ const inputRef = React.useRef<HTMLInputElement>(null);
77
+ const cancelledFilesRef = React.useRef<Set<string>>(new Set());
78
+ const currentAbortControllerRef = React.useRef<AbortController | null>(null);
79
+ const currentFileNameRef = React.useRef<string | null>(null);
80
+
81
+ const cancelFile = React.useCallback((fileName: string) => {
82
+ cancelledFilesRef.current.add(fileName);
83
+ if (currentFileNameRef.current === fileName && currentAbortControllerRef.current) {
84
+ currentAbortControllerRef.current.abort();
85
+ }
86
+ }, []);
87
+
88
+ React.useEffect(() => {
89
+ return () => {
90
+ currentAbortControllerRef.current?.abort();
91
+ };
92
+ }, []);
93
+
94
+ const handleChange = React.useCallback(
95
+ async (e: React.ChangeEvent<HTMLInputElement>) => {
96
+ const files = e.target.files ? Array.from(e.target.files) : [];
97
+ if (files.length === 0) return;
98
+
99
+ const maxSizeLabel = formatFileSize(MAX_FILE_SIZE_BYTES);
100
+ const errorMessage = LABELS.fileTooLarge(maxSizeLabel);
101
+
102
+ const items: FileUploadItem[] = files.map((file) => {
103
+ if (isFileTooLarge(file)) {
104
+ onUploadError?.(file, errorMessage);
105
+ return {
106
+ file,
107
+ state: "error" as UploadState,
108
+ progress: 0,
109
+ error: errorMessage,
110
+ };
111
+ }
112
+ return {
113
+ file,
114
+ state: "loading_config" as UploadState,
115
+ progress: 0,
116
+ };
117
+ });
118
+ setFileItems(items);
119
+ cancelledFilesRef.current.clear();
120
+
121
+ const itemsToUpload = items.filter((item) => item.state !== "error");
122
+ if (itemsToUpload.length === 0) {
123
+ e.target.value = "";
124
+ return;
125
+ }
126
+
127
+ let config: Awaited<ReturnType<typeof getUploadConfig>> | null = null;
128
+ let publishLocationId: string | null = null;
129
+ const uploadedFiles: UploadedFile[] = [];
130
+
131
+ for (const item of itemsToUpload) {
132
+ const { file } = item;
133
+
134
+ if (cancelledFilesRef.current.has(file.name)) {
135
+ setFileItems((prev) => updateItem(prev, file.name, { state: "cancelled", progress: 0 }));
136
+ continue;
137
+ }
138
+
139
+ try {
140
+ if (!config) {
141
+ config = await getUploadConfig();
142
+ }
143
+ setFileItems((prev) => updateItem(prev, file.name, { state: "loading_config" }));
144
+
145
+ if (cancelledFilesRef.current.has(file.name)) {
146
+ setFileItems((prev) =>
147
+ updateItem(prev, file.name, { state: "cancelled", progress: 0 }),
148
+ );
149
+ continue;
150
+ }
151
+
152
+ const abortController = new AbortController();
153
+ currentAbortControllerRef.current = abortController;
154
+ currentFileNameRef.current = file.name;
155
+
156
+ setFileItems((prev) => updateItem(prev, file.name, { state: "uploading", progress: 0 }));
157
+ const contentBodyId = await uploadToUrl(
158
+ file,
159
+ config.token,
160
+ config.uploadUrl,
161
+ (percent) => {
162
+ setFileItems((prev) => updateItem(prev, file.name, { progress: percent }));
163
+ },
164
+ abortController.signal,
165
+ );
166
+
167
+ currentAbortControllerRef.current = null;
168
+ currentFileNameRef.current = null;
169
+
170
+ if (cancelledFilesRef.current.has(file.name)) {
171
+ continue;
172
+ }
173
+
174
+ if (!publishLocationId) {
175
+ publishLocationId = recordId ?? (await getCurrentUserId());
176
+ }
177
+ setFileItems((prev) =>
178
+ updateItem(prev, file.name, { state: "creating_record", progress: 100 }),
179
+ );
180
+ const contentVersionId = await createContentVersion(
181
+ file,
182
+ contentBodyId,
183
+ publishLocationId,
184
+ );
185
+
186
+ flushSync(() => {
187
+ setFileItems((prev) =>
188
+ updateItem(prev, file.name, {
189
+ state: "success",
190
+ progress: 100,
191
+ contentVersionId,
192
+ }),
193
+ );
194
+ });
195
+ uploadedFiles.push({
196
+ name: file.name,
197
+ size: file.size,
198
+ contentVersionId,
199
+ });
200
+ } catch (err) {
201
+ currentAbortControllerRef.current = null;
202
+ currentFileNameRef.current = null;
203
+ const message = err instanceof Error ? err.message : String(err);
204
+ const isCancelled =
205
+ message === "Upload aborted" || cancelledFilesRef.current.has(file.name);
206
+ setFileItems((prev) =>
207
+ updateItem(prev, file.name, {
208
+ state: isCancelled ? "cancelled" : "error",
209
+ error: isCancelled ? undefined : message,
210
+ }),
211
+ );
212
+ if (!isCancelled) {
213
+ onUploadError?.(file, message);
214
+ }
215
+ // Continue with remaining files; do not return early
216
+ }
217
+ }
218
+
219
+ if (uploadedFiles.length > 0) {
220
+ onUploadComplete?.(uploadedFiles);
221
+ }
222
+
223
+ e.target.value = "";
224
+ },
225
+ [recordId, onUploadComplete, onUploadError],
226
+ );
227
+
228
+ const handleDragOver = React.useCallback((e: React.DragEvent) => {
229
+ e.preventDefault();
230
+ e.stopPropagation();
231
+ setIsDragging(true);
232
+ }, []);
233
+
234
+ const handleDragLeave = React.useCallback((e: React.DragEvent) => {
235
+ e.preventDefault();
236
+ e.stopPropagation();
237
+ setIsDragging(false);
238
+ }, []);
239
+
240
+ const handleDrop = React.useCallback(
241
+ (e: React.DragEvent) => {
242
+ e.preventDefault();
243
+ e.stopPropagation();
244
+ setIsDragging(false);
245
+ const files = e.dataTransfer.files ? Array.from(e.dataTransfer.files) : [];
246
+ if (files.length > 0 && inputRef.current) {
247
+ const dt = new DataTransfer();
248
+ files.forEach((f) => dt.items.add(f));
249
+ inputRef.current.files = dt.files;
250
+ handleChange({ target: inputRef.current } as React.ChangeEvent<HTMLInputElement>);
251
+ }
252
+ },
253
+ [handleChange],
254
+ );
255
+
256
+ const handleClick = React.useCallback(() => {
257
+ inputRef.current?.click();
258
+ }, []);
259
+
260
+ const handleKeyDown = React.useCallback((e: React.KeyboardEvent) => {
261
+ if (e.key === "Enter" || e.key === " ") {
262
+ e.preventDefault();
263
+ inputRef.current?.click();
264
+ }
265
+ }, []);
266
+
267
+ const reset = React.useCallback(() => {
268
+ setFileItems([]);
269
+ }, []);
270
+
271
+ const allDone =
272
+ fileItems.length > 0 &&
273
+ fileItems.every(
274
+ (item) => item.state === "success" || item.state === "error" || item.state === "cancelled",
275
+ );
276
+
277
+ return {
278
+ fileItems,
279
+ allDone,
280
+ isDragging,
281
+ reset,
282
+ cancelFile,
283
+ openFilePicker: handleClick,
284
+ getInputProps: () => ({
285
+ ref: inputRef,
286
+ type: "file" as const,
287
+ accept,
288
+ multiple,
289
+ onChange: handleChange,
290
+ }),
291
+ getDropZoneProps: () => ({
292
+ onClick: handleClick,
293
+ onDragOver: handleDragOver,
294
+ onDragLeave: handleDragLeave,
295
+ onDrop: handleDrop,
296
+ onKeyDown: handleKeyDown,
297
+ }),
298
+ };
299
+ }