uploader.tsx
Overview
uploader.tsx defines a React functional component FileUploadDirectUpload that provides a user interface and logic for uploading files directly within a client-side React application. This component leverages a custom file upload UI library (FileUpload and related components) to enable drag-and-drop or button-based file selection and manages uploading files via an asynchronous API hook (useUploadCanvasFile).
The primary purpose of this component is to allow users to upload a single file at a time, handle the upload process with feedback on success or failure, and notify the parent component of changes to the uploaded file data. It includes error handling for rejected files and upload failures, displaying notifications via the toast system.
Detailed Explanation
Component: FileUploadDirectUpload
Description
A React component for uploading files with a direct upload mechanism. It supports drag-and-drop and manual browsing of files, limits the upload to one file at a time, and integrates with an upload API hook to process the file upload asynchronously.
Props
Name | Type | Description |
|---|---|---|
|
| (Unused in implementation) Placeholder for current value state to be managed externally. |
| Callback function invoked when upload completes successfully with the returned data. |
Internal State
files:File[]
Tracks the currently selected files in the upload component.
Hooks Used
useUploadCanvasFile()
Custom hook providing an uploadCanvasFile method that takes an array of files and returns a promise resolving with upload result data.
Methods
onUpload
Parameters:
files:File[]- Files to be uploaded.An object with two callbacks:
onSuccess(file: File): Called when a file upload succeeds.onError(file: File, error: Error): Called when a file upload fails.
Returns: Promise (async)
Description:
Handles the uploading process for files using uploadCanvasFile. It uploads each file individually, calls onSuccess or onError accordingly, and invokes the externalonChangecallback with the returned data on success. All uploads run concurrently withPromise.all. Unexpected errors are logged to the console.
onFileReject
Parameters:
file: The rejected file.message: Reason for rejection.
Description:
Displays a toast notification informing the user that a file has been rejected, truncating the filename if longer than 20 characters.
Render Structure
Uses
FileUploadas the root component managing file selection and upload lifecycle.FileUploadDropzoneprovides a drag-and-drop area with an icon and instructions.FileUploadTriggerwraps a button to open the file browser.FileUploadListrenders a list of current files, each with preview, metadata, delete button, and upload progress indicator.
Usage Example
import React from 'react';
import { FileUploadDirectUpload } from './uploader';
function MyUploaderWrapper() {
const [uploadedData, setUploadedData] = React.useState<Record<string, any>>({});
return (
<FileUploadDirectUpload
value={uploadedData}
onChange={(data) => setUploadedData(data)}
/>
);
}
Important Implementation Details
Single File Limitation:
The component restricts uploads to a single file (maxFiles={1},multiple={false}) but internally managesfilesas an array for compatibility with the underlyingFileUploadcomponent.Concurrent Upload Handling:
UsesPromise.allto upload all files concurrently, although only one file is allowed here, allowing easy future extension for multiple uploads.Error Handling:
Upload errors per file are handled individually with callbacks. Any unexpected errors outside of these are caught and logged.File Rejection Feedback:
Uses thetoastnotification system to inform users of rejected files, enhancing UX.External Upload Logic:
The actual upload functionality is abstracted away in theuseUploadCanvasFilehook, which is responsible for interacting with the backend or storage service.
Interaction with Other Parts of the System
UI Components:
Relies on a custom file upload UI library (FileUpload,FileUploadDropzone,FileUploadItem, etc.) for the drag-drop and file list UI.Hooks:
UsesuseUploadCanvasFileto perform the backend upload request. This hook encapsulates the API call or agent request logic.Iconography:
Uses icons fromlucide-react(UploadandX).Notifications:
Usestoastfromsonnerto display user feedback.Parent Components:
TheonChangeprop allows parent components to receive upload results and update their state accordingly, enabling integration with broader application logic.
Diagram: Component Interaction and Structure
componentDiagram
component FileUploadDirectUpload {
+files: File[]
+onUpload(files, callbacks)
+onFileReject(file, message)
--
renders FileUpload
uses useUploadCanvasFile
uses toast for notifications
}
component FileUpload {
+FileUploadDropzone
+FileUploadTrigger
+FileUploadList
}
FileUploadDirectUpload --> FileUpload
FileUpload --> FileUploadDropzone
FileUpload --> FileUploadTrigger
FileUpload --> FileUploadList
FileUploadDirectUpload ..> useUploadCanvasFile : uses
FileUploadDirectUpload ..> toast : triggers notifications
FileUploadList --> FileUploadItem
Summary
uploader.tsx implements a user-friendly, single-file upload React component that integrates UI components, asynchronous upload logic, and user feedback mechanisms. It cleanly separates UI from upload logic via hooks and callbacks, making it reusable and maintainable within a larger React application.