import { useState, type SyntheticEvent } from 'react'; import { useNavigate } from 'react-router-dom'; import { LoadingState, Table, type Column } from '../components'; import { useGetImportDirectory, usePostImport } from '../generated/anthoLumeAPIV1'; import type { DirectoryItem } from '../generated/model'; import { Button } from '../components/Button'; import { FolderOpenIcon } from '../icons'; import { useMutationWithToast } from '../hooks/useMutationWithToast'; import { dataForStatus } from '../utils/apiResponses'; export default function AdminImportPage() { const [currentPath, setCurrentPath] = useState(''); const [selectedDirectory, setSelectedDirectory] = useState(''); const [importType, setImportType] = useState<'DIRECT' | 'COPY'>('DIRECT'); const navigate = useNavigate(); const toastMutationOptions = useMutationWithToast(); const { data: directoryData, isLoading } = useGetImportDirectory( currentPath ? { directory: currentPath } : {} ); const postImport = usePostImport(); const directoryResponse = dataForStatus(directoryData, 200); const directories = directoryResponse?.items ?? []; const currentPathDisplay = directoryResponse?.current_path ?? currentPath ?? '/data'; const handleSelectDirectory = (directory: string) => { setSelectedDirectory(`${currentPath}/${directory}`); }; const handleNavigateUp = () => { if (currentPathDisplay !== '/') { const parts = currentPathDisplay.split('/'); parts.pop(); setCurrentPath(parts.join('/') || ''); } }; const handleImport = (e: SyntheticEvent) => { e.preventDefault(); if (!selectedDirectory) return; postImport.mutate( { data: { directory: selectedDirectory, type: importType, }, }, toastMutationOptions({ success: 'Import completed successfully', error: 'Import failed', onSuccess: () => navigate('/admin/import-results'), }) ); }; const handleCancel = () => { setSelectedDirectory(''); }; if (isLoading && !currentPath) { return ; } if (selectedDirectory) { return (

Selected Import Directory

{selectedDirectory}

setImportType('DIRECT')} />
setImportType('COPY')} />
); } const directoryColumns: Column[] = [ { id: 'select', header: '', className: 'w-12', render: item => ( ), }, { id: 'name', header: currentPathDisplay, render: item => item.name ?? '' }, ]; return (
{currentPathDisplay !== '/' && ( )} item.name ?? ''} /> ); }