2025-08-21 17:30:26 +01:00
|
|
|
import React, { createContext, useContext, useState, useCallback, useMemo } from 'react';
|
2025-08-04 15:01:36 +01:00
|
|
|
import { useFileHandler } from '../hooks/useFileHandler';
|
2025-08-21 17:30:26 +01:00
|
|
|
import { FileMetadata } from '../types/file';
|
2025-08-28 10:56:07 +01:00
|
|
|
import { FileId } from '../types/file';
|
2025-08-04 15:01:36 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
interface FilesModalContextType {
|
|
|
|
isFilesModalOpen: boolean;
|
2025-08-26 15:30:58 +01:00
|
|
|
openFilesModal: (options?: { insertAfterPage?: number; customHandler?: (files: File[], insertAfterPage?: number) => void }) => void;
|
2025-08-08 15:15:09 +01:00
|
|
|
closeFilesModal: () => void;
|
|
|
|
onFileSelect: (file: File) => void;
|
|
|
|
onFilesSelect: (files: File[]) => void;
|
2025-08-28 10:56:07 +01:00
|
|
|
onStoredFilesSelect: (filesWithMetadata: Array<{ file: File; originalId: FileId; metadata: FileMetadata }>) => void;
|
2025-08-11 09:16:16 +01:00
|
|
|
onModalClose?: () => void;
|
2025-08-08 15:15:09 +01:00
|
|
|
setOnModalClose: (callback: () => void) => void;
|
|
|
|
}
|
2025-08-04 15:01:36 +01:00
|
|
|
|
|
|
|
const FilesModalContext = createContext<FilesModalContextType | null>(null);
|
|
|
|
|
|
|
|
export const FilesModalProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
2025-08-21 17:30:26 +01:00
|
|
|
const { addToActiveFiles, addMultipleFiles, addStoredFiles } = useFileHandler();
|
2025-08-08 15:15:09 +01:00
|
|
|
const [isFilesModalOpen, setIsFilesModalOpen] = useState(false);
|
|
|
|
const [onModalClose, setOnModalClose] = useState<(() => void) | undefined>();
|
2025-08-26 15:30:58 +01:00
|
|
|
const [insertAfterPage, setInsertAfterPage] = useState<number | undefined>();
|
|
|
|
const [customHandler, setCustomHandler] = useState<((files: File[], insertAfterPage?: number) => void) | undefined>();
|
2025-08-08 15:15:09 +01:00
|
|
|
|
2025-08-26 15:30:58 +01:00
|
|
|
const openFilesModal = useCallback((options?: { insertAfterPage?: number; customHandler?: (files: File[], insertAfterPage?: number) => void }) => {
|
|
|
|
setInsertAfterPage(options?.insertAfterPage);
|
|
|
|
setCustomHandler(() => options?.customHandler);
|
2025-08-08 15:15:09 +01:00
|
|
|
setIsFilesModalOpen(true);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const closeFilesModal = useCallback(() => {
|
|
|
|
setIsFilesModalOpen(false);
|
2025-08-26 15:30:58 +01:00
|
|
|
setInsertAfterPage(undefined); // Clear insertion position
|
|
|
|
setCustomHandler(undefined); // Clear custom handler
|
2025-08-08 15:15:09 +01:00
|
|
|
onModalClose?.();
|
|
|
|
}, [onModalClose]);
|
|
|
|
|
|
|
|
const handleFileSelect = useCallback((file: File) => {
|
2025-08-26 15:30:58 +01:00
|
|
|
if (customHandler) {
|
|
|
|
// Use custom handler for special cases (like page insertion)
|
|
|
|
customHandler([file], insertAfterPage);
|
|
|
|
} else {
|
|
|
|
// Use normal file handling
|
|
|
|
addToActiveFiles(file);
|
|
|
|
}
|
2025-08-08 15:15:09 +01:00
|
|
|
closeFilesModal();
|
2025-08-26 15:30:58 +01:00
|
|
|
}, [addToActiveFiles, closeFilesModal, insertAfterPage, customHandler]);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
|
|
|
const handleFilesSelect = useCallback((files: File[]) => {
|
2025-08-26 15:30:58 +01:00
|
|
|
if (customHandler) {
|
|
|
|
// Use custom handler for special cases (like page insertion)
|
|
|
|
customHandler(files, insertAfterPage);
|
|
|
|
} else {
|
|
|
|
// Use normal file handling
|
|
|
|
addMultipleFiles(files);
|
|
|
|
}
|
2025-08-08 15:15:09 +01:00
|
|
|
closeFilesModal();
|
2025-08-26 15:30:58 +01:00
|
|
|
}, [addMultipleFiles, closeFilesModal, insertAfterPage, customHandler]);
|
2025-08-08 15:15:09 +01:00
|
|
|
|
2025-08-28 10:56:07 +01:00
|
|
|
const handleStoredFilesSelect = useCallback((filesWithMetadata: Array<{ file: File; originalId: FileId; metadata: FileMetadata }>) => {
|
2025-08-26 15:30:58 +01:00
|
|
|
if (customHandler) {
|
|
|
|
// Use custom handler for special cases (like page insertion)
|
|
|
|
const files = filesWithMetadata.map(item => item.file);
|
|
|
|
customHandler(files, insertAfterPage);
|
|
|
|
} else {
|
|
|
|
// Use normal file handling
|
|
|
|
addStoredFiles(filesWithMetadata);
|
|
|
|
}
|
2025-08-21 17:30:26 +01:00
|
|
|
closeFilesModal();
|
2025-08-26 15:30:58 +01:00
|
|
|
}, [addStoredFiles, closeFilesModal, insertAfterPage, customHandler]);
|
2025-08-21 17:30:26 +01:00
|
|
|
|
2025-08-08 15:15:09 +01:00
|
|
|
const setModalCloseCallback = useCallback((callback: () => void) => {
|
|
|
|
setOnModalClose(() => callback);
|
|
|
|
}, []);
|
|
|
|
|
2025-08-21 17:30:26 +01:00
|
|
|
const contextValue: FilesModalContextType = useMemo(() => ({
|
2025-08-08 15:15:09 +01:00
|
|
|
isFilesModalOpen,
|
|
|
|
openFilesModal,
|
|
|
|
closeFilesModal,
|
|
|
|
onFileSelect: handleFileSelect,
|
|
|
|
onFilesSelect: handleFilesSelect,
|
2025-08-21 17:30:26 +01:00
|
|
|
onStoredFilesSelect: handleStoredFilesSelect,
|
2025-08-08 15:15:09 +01:00
|
|
|
onModalClose,
|
|
|
|
setOnModalClose: setModalCloseCallback,
|
2025-08-21 17:30:26 +01:00
|
|
|
}), [
|
|
|
|
isFilesModalOpen,
|
|
|
|
openFilesModal,
|
|
|
|
closeFilesModal,
|
|
|
|
handleFileSelect,
|
|
|
|
handleFilesSelect,
|
|
|
|
handleStoredFilesSelect,
|
|
|
|
onModalClose,
|
|
|
|
setModalCloseCallback,
|
|
|
|
]);
|
2025-08-04 15:01:36 +01:00
|
|
|
|
|
|
|
return (
|
2025-08-08 15:15:09 +01:00
|
|
|
<FilesModalContext.Provider value={contextValue}>
|
2025-08-04 15:01:36 +01:00
|
|
|
{children}
|
|
|
|
</FilesModalContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useFilesModalContext = () => {
|
|
|
|
const context = useContext(FilesModalContext);
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useFilesModalContext must be used within FilesModalProvider');
|
|
|
|
}
|
|
|
|
return context;
|
2025-08-11 09:16:16 +01:00
|
|
|
};
|