react-spreadsheet-mapper
Version:
A headless React library for mapping spreadsheet data.
63 lines (62 loc) • 2.76 kB
TypeScript
import type { SpreadsheetData, SpreadsheetConfig, MappedField, MappingError, MappingOption, PerformanceMetrics } from './types';
/**
* Props for the useSpreadsheetMapper hook.
* @interface UseSpreadsheetMapperProps
* @property {MappingOption[]} options - An array of target options for mapping.
* @property {(data: SpreadsheetData & { map: { field: string; value: string }[] }) => void} onFinish - Callback function to be called when the mapping process is finished.
* @property {SpreadsheetConfig} [config] - Optional configuration for spreadsheet processing.
* @property {string} [clientId] - Optional client identifier for rate limiting (defaults to 'default').
* @property {(message: string, type?: 'success' | 'error' | 'info') => void} [onAnnounce] - Optional callback for screen reader announcements.
*/
interface UseSpreadsheetMapperProps {
options: MappingOption[];
onFinish: (data: SpreadsheetData & {
map: {
field: string;
value: string;
}[];
}) => void;
config?: SpreadsheetConfig;
clientId?: string;
onAnnounce?: (message: string, type?: 'success' | 'error' | 'info') => void;
}
/**
* File processing state interface
*/
interface FileProcessingState {
file: File;
status: 'pending' | 'processing' | 'completed' | 'error';
error?: string;
data?: SpreadsheetData;
}
/**
* A headless React hook for mapping spreadsheet data with enhanced security, performance, and accessibility features.
* Provides state and functions for file processing, field mapping, and error handling.
* @function useSpreadsheetMapper
* @param {UseSpreadsheetMapperProps} { options, onFinish, config, clientId, onAnnounce } - Props for the hook.
* @returns Enhanced return object with performance metrics and accessibility features
*/
declare const useSpreadsheetMapper: ({ options, onFinish, config, clientId, onAnnounce }: UseSpreadsheetMapperProps) => {
map: MappedField[];
errors: MappingError[];
processedFiles: SpreadsheetData[];
fileProcessingStates: FileProcessingState[];
isProcessing: boolean;
performanceMetrics: PerformanceMetrics[];
updateOrCreate: (record: MappedField) => void;
save: (value: string, fileName?: string) => void;
finish: () => void;
handleFiles: (files: File[]) => void;
handleFileFinish: (data: SpreadsheetData) => void;
reset: () => void;
getPerformanceSummary: () => {
totalFiles: number;
totalSize: number;
totalProcessingTime: number;
averageProcessingTime: number;
totalRows: number;
averageFileSize: number;
} | null;
announce: (message: string, type?: "success" | "error" | "info") => void;
};
export default useSpreadsheetMapper;