import React, { useState, useMemo } from 'react'; import { Loader2, X } from 'lucide-react'; import { useAllTables } from '@/features/tables/hooks/useAllTables'; interface FilesSourceConfig { tableId: number; tableName: string; tableIcon?: string; projectId: number; } interface FilesSourceInlineSelectorProps { defaultSpaceId?: number; onSelect: (config: FilesSourceConfig) => void; onCancel: () => void; showHeader?: boolean; } export function FilesSourceInlineSelector({ defaultSpaceId, onSelect, onCancel, showHeader = true }: FilesSourceInlineSelectorProps) { const [selectedProjectId, setSelectedProjectId] = useState(null); const [selectedTableId, setSelectedTableId] = useState(null); const { data: allTablesData, isLoading } = useAllTables(); // Filter spaces const filteredSpaces = useMemo(() => { if (!allTablesData?.spacesWithTables) return []; if (!defaultSpaceId) return allTablesData.spacesWithTables; return allTablesData.spacesWithTables.filter(s => s.id === defaultSpaceId); }, [allTablesData, defaultSpaceId]); // Get tables for selected project (filter for Files-like tables) const projectTables = useMemo(() => { if (!selectedProjectId || !allTablesData?.spacesWithTables) return []; for (const space of allTablesData.spacesWithTables) { const project = space.projects.find(p => p.id === selectedProjectId); if (project) return project.tables || []; } return []; }, [selectedProjectId, allTablesData]); // Get selected table info const selectedTable = useMemo(() => { if (!selectedTableId) return null; return allTablesData?.flat?.find(t => t.id === String(selectedTableId)); }, [selectedTableId, allTablesData]); const handleSave = () => { if (!selectedTable || !selectedProjectId) return; onSelect({ tableId: Number(selectedTable.id), tableName: selectedTable.displayName || selectedTable.name, tableIcon: selectedTable.icon || '📁', projectId: selectedProjectId }); }; if (isLoading) { return (
); } return (
{/* Header with close button */} {showHeader && (
)} {/* Project selector with label if no header */} {!showHeader && } {/* Project selector */} {/* Table selector */}
{/* Save button only */}
); }