* phase 0: token foundation * phase 1: motion language * phase 2: primitives and semantic tokens * phase 3: depth and rhythm polish * phase 4: enforce design system lint rules * phase 5: split ui primitives into modules * phase 7: refactor app surfaces to ui layer * phase 9: enforce ui architecture imports * phase 10: add ui system harness * fix compact reader auth control * fix pdf loader flash * Converge sidebar and reader controls * refactor: remove initial loader state and related logic from PDFViewerPage * Remove legacy UI shim imports * Converge modal and drawer frames * Migrate secondary modals to shared frame * Move settings modal onto shared frame * Use shared cards in audiobook settings * Converge choice and popover surfaces * Converge reader navigation buttons * Refactor UI components to use consistent button and icon styles across the application * refactor(ui): unify button usage in settings, admin, and doclist components Replace native button elements with shared Button, ChoiceTile, and IconButton components for consistent UI behavior and styling. Update classNames and props to match new component APIs. Adjust FinderSidebar to use utility function for conditional class merging. * feat(ui): introduce shared Listbox components for unified select and dropdown styling Replace direct usage of Headless UI Listbox primitives with new SharedListboxButton, SharedListboxOption, and SharedListboxOptions components across AudiobookExportModal, FinderToolbar, VoicesControlBase, and select UI. Refactor related imports and classNames to centralize dropdown styling and logic. Simplify UserMenu button markup for improved consistency. This change consolidates dropdown/select UI patterns, reduces duplication, and improves maintainability by providing a single source of truth for Listbox styling and behavior. * refactor(ui): consolidate button, menu, popover, and range primitives for unified usage Remove legacy UI harness and dev/demo files. Replace scattered button, menu, popover, and range input utilities with shared, composable primitives: Button, ButtonLink, ButtonAnchor, MenuActionItem, MenuItemsSurface, PopoverSurface, PopoverTrigger, and RangeInput. Update all usages across app, admin, player, and document components to use these new primitives, eliminating duplicated class logic and improving consistency. Remove obsolete utility files and class exports. This change streamlines UI code, centralizes styling, and reduces maintenance overhead. * feat(ui): redesign range input with dynamic progress styling and improved accessibility Revamp the range input component to support dynamic progress indication using CSS custom properties and linear gradients. Add logic to compute and set the progress percentage based on current value, min, and max. Refine focus and disabled states for better accessibility and usability. Update styling for both WebKit and Mozilla engines to ensure consistent appearance. This change enhances visual feedback and modernizes the range slider UI. * style(ui): update range input to use secondary accent color for progress Switch range input progress styling from primary to secondary accent color for both WebKit and Mozilla engines. Remove drop shadow from slider thumb for a cleaner appearance. This change aligns the component with the updated design palette and simplifies visual effects. * refactor(app): remove unused Link imports from public pages and components Eliminate redundant imports of the Link component from Next.js in several public-facing pages and components. These imports were no longer in use after recent UI refactoring and consolidation of navigation elements. This cleanup reduces bundle size and improves code clarity. * chore(ui): remove unused export of segmented control classes from select component Eliminate unnecessary export statements for segmentedButtonClass and segmentedGroupClass in the select component to streamline the module's public API and reduce potential confusion. * test(accessibility): improve confirm dialog test coverage and refactor media state helper Expand accessibility tests for ConfirmDialog to assert dialog semantics, ARIA attributes, and visible destructive actions using test IDs. Refactor expectMediaState helper to check both UI control state and underlying media signals for more robust playback state detection. * refactor(ui): improve segmented control accessibility and update danger color tokens Update SegmentedControl to support full keyboard navigation and focus management, enhancing accessibility. Replace string indicator in Select with icon, and update button danger variant to use new --danger-strong variable for hover states. Add danger-strong token to Tailwind config and globals. Refine dropzone disabled behavior, adjust focus ring for better contrast, and apply minor UI consistency tweaks across components. * feat(ui): convert SidebarNavLink to forwardRef component Refactor SidebarNavLink to use React.forwardRef, enabling parent components to access the underlying anchor element's ref. Update prop typing and function signature accordingly for improved composability and integration with higher-order components.
245 lines
8.4 KiB
TypeScript
245 lines
8.4 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useCallback, useId, type ReactNode } from 'react';
|
|
import { useDropzone } from 'react-dropzone';
|
|
import { UploadIcon } from '@/components/icons/Icons';
|
|
import { useDocuments } from '@/contexts/DocumentContext';
|
|
import { uploadDocxAsPdf } from '@/lib/client/api/documents';
|
|
import { useFeatureFlag } from '@/contexts/RuntimeConfigContext';
|
|
import { dropzoneSurfaceClass } from '@/components/ui';
|
|
|
|
interface DocumentUploaderProps {
|
|
className?: string;
|
|
variant?: 'default' | 'compact' | 'overlay';
|
|
children?: ReactNode;
|
|
onUploadBatchChange?: (state: UploadBatchState) => void;
|
|
}
|
|
|
|
export interface UploadBatchState {
|
|
uploaderId: string;
|
|
isActive: boolean;
|
|
totalFiles: number;
|
|
completedFiles: number;
|
|
phase: 'uploading' | 'converting';
|
|
currentFileName: string | null;
|
|
}
|
|
|
|
export function DocumentUploader({
|
|
className = '',
|
|
variant = 'default',
|
|
children,
|
|
onUploadBatchChange,
|
|
}: DocumentUploaderProps) {
|
|
const uploaderId = useId();
|
|
const enableDocx = useFeatureFlag('enableDocxConversion');
|
|
const {
|
|
addPDFDocument: addPDF,
|
|
addEPUBDocument: addEPUB,
|
|
addHTMLDocument: addHTML,
|
|
refreshDocuments,
|
|
} = useDocuments();
|
|
const [isUploading, setIsUploading] = useState(false);
|
|
const [isConverting, setIsConverting] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const emitBatchState = useCallback((state: Omit<UploadBatchState, 'uploaderId'>) => {
|
|
onUploadBatchChange?.({ uploaderId, ...state });
|
|
}, [onUploadBatchChange, uploaderId]);
|
|
|
|
const onDrop = useCallback(async (acceptedFiles: File[]) => {
|
|
if (!acceptedFiles || acceptedFiles.length === 0) return;
|
|
|
|
const totalFiles = acceptedFiles.length;
|
|
let completedFiles = 0;
|
|
|
|
setIsUploading(true);
|
|
setError(null);
|
|
emitBatchState({
|
|
isActive: true,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'uploading',
|
|
currentFileName: acceptedFiles[0]?.name ?? null,
|
|
});
|
|
|
|
try {
|
|
for (const file of acceptedFiles) {
|
|
if (file.type === 'application/pdf') {
|
|
emitBatchState({
|
|
isActive: true,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'uploading',
|
|
currentFileName: file.name,
|
|
});
|
|
await addPDF(file);
|
|
completedFiles += 1;
|
|
} else if (file.type === 'application/epub+zip') {
|
|
emitBatchState({
|
|
isActive: true,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'uploading',
|
|
currentFileName: file.name,
|
|
});
|
|
await addEPUB(file);
|
|
completedFiles += 1;
|
|
} else if (file.type === 'text/plain' || file.type === 'text/markdown' || file.name.endsWith('.md')) {
|
|
emitBatchState({
|
|
isActive: true,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'uploading',
|
|
currentFileName: file.name,
|
|
});
|
|
await addHTML(file);
|
|
completedFiles += 1;
|
|
} else if (enableDocx && file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
|
|
// Preserve prior UX: show "Converting DOCX..." state rather than generic uploading.
|
|
setIsUploading(false);
|
|
setIsConverting(true);
|
|
emitBatchState({
|
|
isActive: true,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'converting',
|
|
currentFileName: file.name,
|
|
});
|
|
// Convert+upload directly on the server. Use sha(docx) as stable ID to avoid duplicates.
|
|
await uploadDocxAsPdf(file);
|
|
await refreshDocuments();
|
|
setIsConverting(false);
|
|
setIsUploading(true);
|
|
completedFiles += 1;
|
|
} else {
|
|
continue;
|
|
}
|
|
|
|
emitBatchState({
|
|
isActive: true,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'uploading',
|
|
currentFileName: null,
|
|
});
|
|
}
|
|
} catch (err) {
|
|
setError('Failed to upload file. Please try again.');
|
|
console.error('Upload error:', err);
|
|
} finally {
|
|
setIsUploading(false);
|
|
setIsConverting(false);
|
|
emitBatchState({
|
|
isActive: false,
|
|
totalFiles,
|
|
completedFiles,
|
|
phase: 'uploading',
|
|
currentFileName: null,
|
|
});
|
|
}
|
|
}, [addHTML, addPDF, addEPUB, refreshDocuments, enableDocx, emitBatchState]);
|
|
|
|
const { getRootProps, getInputProps, isDragActive } = useDropzone({
|
|
onDrop,
|
|
accept: {
|
|
'application/pdf': ['.pdf'],
|
|
'application/epub+zip': ['.epub'],
|
|
'text/plain': ['.txt'],
|
|
'text/markdown': ['.md'],
|
|
...(enableDocx ? {
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx']
|
|
} : {})
|
|
},
|
|
multiple: true,
|
|
disabled: isUploading || isConverting,
|
|
noClick: variant === 'overlay',
|
|
noKeyboard: variant === 'overlay'
|
|
});
|
|
|
|
const isDisabled = isUploading || isConverting;
|
|
|
|
if (variant === 'overlay') {
|
|
const rootProps = getRootProps();
|
|
return (
|
|
<div {...rootProps} className={`relative w-full h-full ${className}`}>
|
|
<input {...getInputProps()} />
|
|
{children}
|
|
{isDragActive && (
|
|
<div className="absolute inset-0 z-50 flex flex-col items-center justify-center bg-surface backdrop-blur-md pointer-events-none p-6">
|
|
<div className="w-full h-full border-2 border-dashed border-accent rounded-lg flex flex-col items-center justify-center bg-surface-solid text-center p-4">
|
|
<UploadIcon className="w-14 h-14 text-accent mb-4" />
|
|
<p className="text-xl font-bold text-foreground mb-1.5">
|
|
Drop files here to upload
|
|
</p>
|
|
<p className="text-sm text-soft">
|
|
{enableDocx
|
|
? 'Accepts PDF, EPUB, TXT, MD, or DOCX'
|
|
: 'Accepts PDF, EPUB, TXT, or MD'}
|
|
</p>
|
|
{error && (
|
|
<p className="mt-3 text-sm text-danger">
|
|
Upload failed: {error} — try again.
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
{!isDragActive && error && (
|
|
<div className="absolute inset-x-4 bottom-4 z-40 rounded-md border border-danger bg-danger-wash px-3 py-2 text-center text-sm text-danger pointer-events-none">
|
|
Upload failed: {error} — try again.
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div
|
|
{...getRootProps()}
|
|
className={dropzoneSurfaceClass({
|
|
variant: variant === 'compact' ? 'compact' : 'default',
|
|
active: isDragActive,
|
|
disabled: isDisabled,
|
|
className,
|
|
})}
|
|
>
|
|
<input {...getInputProps()} />
|
|
{variant === 'compact' ? (
|
|
<div className="flex items-center gap-2 text-left w-full min-w-0">
|
|
<UploadIcon className="w-3.5 h-3.5 text-soft group-hover:text-accent shrink-0 transition-colors duration-base" />
|
|
{isUploading ? (
|
|
<p className="text-[12px] font-medium truncate flex-1">Uploading…</p>
|
|
) : isConverting ? (
|
|
<p className="text-[12px] font-medium truncate flex-1">Converting DOCX…</p>
|
|
) : (
|
|
<div className="flex items-center gap-2 min-w-0 flex-1">
|
|
<p className="text-[12px] truncate flex-1">
|
|
{isDragActive ? 'Drop files here' : 'Upload documents'}
|
|
</p>
|
|
{error && <p className="text-[10px] text-danger truncate shrink-0">{error}</p>}
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col items-center justify-center text-center">
|
|
<UploadIcon className="w-7 h-7 sm:w-10 sm:h-10 mb-2 text-soft" />
|
|
{isUploading ? (
|
|
<p className="text-sm sm:text-lg font-semibold text-foreground">Uploading file...</p>
|
|
) : isConverting ? (
|
|
<p className="text-sm sm:text-lg font-semibold text-foreground">Converting DOCX to PDF...</p>
|
|
) : (
|
|
<>
|
|
<p className="mb-2 text-sm sm:text-lg font-semibold text-foreground">
|
|
{isDragActive ? 'Drop your file(s) here' : 'Drop your file(s) here, or click to select'}
|
|
</p>
|
|
<p className="text-xs sm:text-sm text-soft">
|
|
{enableDocx ? 'PDF, EPUB, TXT, MD, or DOCX files are accepted' : 'PDF, EPUB, TXT, or MD files are accepted'}
|
|
</p>
|
|
{error && <p className="mt-2 text-sm text-danger">{error}</p>}
|
|
</>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|