Overview
MkDowner uses custom React hooks to encapsulate state management and business logic. This page documents the two main hooks used in the application.useFileUpload
TheuseFileUpload hook manages file selection, upload state, progress tracking, and API communication.
Location
src/hooks/useFileUpload.ts
Full Implementation
State Management
The hook manages the following state:| State | Type | Description |
|---|---|---|
selectedFiles | File[] | Array of files selected for upload |
isUploading | boolean | Upload in progress flag |
progress | number | Upload progress percentage (0-100) |
showSuccess | boolean | Success message visibility flag |
downloadedFileName | string | Name of the downloaded file |
fileInputRef | RefObject<HTMLInputElement> | Reference to file input element |
Return Values
The hook returns an object with the following properties and methods:State Properties
Methods
handleFilesSelected(files: FileList): void
Handles file selection from the input element.
handleRemoveFile(index: number): void
Removes a file from the selected files array by index.
handleClearAll(): void
Clears all selected files and resets the file input.
handleNewConversion(): void
Resets the hook state for a new conversion (clears files, progress, and success state).
handleUpload(files: FileList): Promise<void>
Uploads files to the backend and handles the download of converted files.
Usage Example
Progress Simulation
The hook simulates upload progress using an interval that increments progress randomly:- Updates every 200ms
- Increments by a random amount (up to 15%)
- Caps at 90% until upload completes
- Sets to 100% when upload finishes
File Download Logic
When the upload completes, the hook automatically triggers a download:- Filename determination: Single file gets
.mdextension, multiple files get.zip - Blob URL creation: Creates a temporary URL for the blob
- Programmatic click: Creates and clicks a temporary anchor element
- Cleanup: Revokes the blob URL and removes the anchor
useConversionMode
TheuseConversionMode hook manages the conversion mode toggle between markdown and Word formats.
Location
src/hooks/useConversionMode.ts
Full Implementation
TypeScript Types
Return Values
mode: ConversionMode
The current conversion mode.
toggleMode(): void
Toggles between ‘to-markdown’ and ‘to-word’ modes.
Usage Example
State Flow
The hook maintains a simple toggle state that switches between two modes.Best Practices
Hook Composition
Hook Composition
Both hooks can be used together in a component:
Error Handling
Error Handling
The
useFileUpload hook uses try-catch blocks and displays errors via alerts. Consider replacing with a toast notification system like SweetAlert2 (already in dependencies).TypeScript Safety
TypeScript Safety
Both hooks are fully typed with TypeScript. Use the exported types:
See Also
- API Integration - Backend communication details
- Development Setup - Environment configuration