From 43d864f2bdcd70bd537aa590b6354f30fcdde9e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Fri, 24 Apr 2026 08:15:54 +0300 Subject: [PATCH] feat: add MainContent component with SQL editor and interactive table data viewer --- frontend/src/components/MainContent.tsx | 31 +++++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/frontend/src/components/MainContent.tsx b/frontend/src/components/MainContent.tsx index 055a102..e474838 100644 --- a/frontend/src/components/MainContent.tsx +++ b/frontend/src/components/MainContent.tsx @@ -15,6 +15,7 @@ const MainContent: React.FC = () => { const [loadingSchema, setLoadingSchema] = useState(false); const [loadingData, setLoadingData] = useState(false); const [sqlQuery, setSqlQuery] = useState(''); + const [isCustomQuery, setIsCustomQuery] = useState(false); const [paginationModel, setPaginationModel] = useState({ page: 0, pageSize: 100, @@ -34,13 +35,14 @@ const MainContent: React.FC = () => { useEffect(() => { if (activeTable && activeDatabase) { setSqlQuery(`SELECT * FROM ${activeDatabase}.${activeTable} LIMIT 1000;`); + setIsCustomQuery(false); // Reset to table mode when a new table is selected } }, [activeTable, activeDatabase]); // Fetch Schema useEffect(() => { const fetchSchema = async () => { - if (!activeTable || !activeDatabase) return; + if (!activeTable || !activeDatabase || isCustomQuery) return; setLoadingSchema(true); try { @@ -71,11 +73,11 @@ const MainContent: React.FC = () => { }; fetchSchema(); - }, [activeTable, activeDatabase]); + }, [activeTable, activeDatabase, isCustomQuery]); // Fetch Data const fetchData = useCallback(async () => { - if (!activeTable || !activeDatabase) return; + if (!activeTable || !activeDatabase || isCustomQuery) return; setLoadingData(true); try { @@ -100,7 +102,7 @@ const MainContent: React.FC = () => { } finally { setLoadingData(false); } - }, [activeTable, activeDatabase, paginationModel]); + }, [activeTable, activeDatabase, paginationModel, isCustomQuery]); useEffect(() => { fetchData(); @@ -109,12 +111,12 @@ const MainContent: React.FC = () => { const handleExecute = async () => { if (!sqlQuery) return; setLoadingData(true); + setIsCustomQuery(true); // Switch to custom query mode try { const response = await SchemaService.executeQuery(sqlQuery); const rawData = response.data.data; if (rawData && rawData.length > 0) { - // Generate dynamic columns from the result set const fields = Object.keys(rawData[0]); const newCols: GridColDef[] = fields.map(field => ({ field, @@ -136,7 +138,6 @@ const MainContent: React.FC = () => { } } catch (error: any) { console.error('Execution error', error); - // In a real app, we'd use a nicer toast or error panel alert(error.response?.data?.error || 'Execution failed'); } finally { setLoadingData(false); @@ -155,6 +156,7 @@ const MainContent: React.FC = () => { {/* SQL Editor Section */} { SQL EDITOR - {activeDatabase}.sql + + {isCustomQuery ? 'Custom Query' : `${activeDatabase}.${activeTable}`} + @@ -212,13 +216,20 @@ const MainContent: React.FC = () => { - Results + + {isCustomQuery ? 'Query Results' : 'Table Results'} + {rowCount} rows found + {isCustomQuery && ( + + )} - {loadingSchema ? ( + {(loadingSchema && !isCustomQuery) ? ( @@ -228,7 +239,7 @@ const MainContent: React.FC = () => { columns={columns} rowCount={rowCount} loading={loadingData} - paginationMode="server" + paginationMode={isCustomQuery ? 'client' : 'server'} // Client pagination for SQL results paginationModel={paginationModel} onPaginationModelChange={setPaginationModel} pageSizeOptions={[25, 50, 100]}