From 8874cb552d1b87a5eebce3e930f888e1140641b8 Mon Sep 17 00:00:00 2001 From: johnnyfish Date: Thu, 31 Jul 2025 21:27:06 +0300 Subject: [PATCH] fix for schemas --- src/pages/editor-page/canvas/area-utils.ts | 29 +++++++++++++++-- .../canvas/canvas-filter/canvas-filter.tsx | 25 ++++++++------- src/pages/editor-page/canvas/canvas.tsx | 31 +++++++++++++++++-- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/pages/editor-page/canvas/area-utils.ts b/src/pages/editor-page/canvas/area-utils.ts index 7d805951..84aa59c6 100644 --- a/src/pages/editor-page/canvas/area-utils.ts +++ b/src/pages/editor-page/canvas/area-utils.ts @@ -1,6 +1,9 @@ import type { DBTable } from '@/lib/domain/db-table'; import type { Area } from '@/lib/domain/area'; -import { calcTableHeight } from '@/lib/domain/db-table'; +import { + calcTableHeight, + shouldShowTablesBySchemaFilter, +} from '@/lib/domain/db-table'; /** * Check if a table is inside an area based on their positions and dimensions @@ -53,9 +56,31 @@ const findContainingArea = (table: DBTable, areas: Area[]): Area | null => { */ export const updateTablesParentAreas = ( tables: DBTable[], - areas: Area[] + areas: Area[], + hiddenTableIds?: string[], + filteredSchemas?: string[] ): DBTable[] => { return tables.map((table) => { + // Check if table is hidden by direct hiding or schema filter + const isHiddenDirectly = hiddenTableIds?.includes(table.id) ?? false; + const isHiddenBySchema = !shouldShowTablesBySchemaFilter( + table, + filteredSchemas + ); + const isHidden = isHiddenDirectly || isHiddenBySchema; + + // If table is hidden, remove it from any area + if (isHidden) { + if (table.parentAreaId !== null) { + return { + ...table, + parentAreaId: null, + }; + } + return table; + } + + // For visible tables, find containing area as before const containingArea = findContainingArea(table, areas); const newParentAreaId = containingArea?.id || null; diff --git a/src/pages/editor-page/canvas/canvas-filter/canvas-filter.tsx b/src/pages/editor-page/canvas/canvas-filter/canvas-filter.tsx index e0b8e8a9..e3b28295 100644 --- a/src/pages/editor-page/canvas/canvas-filter/canvas-filter.tsx +++ b/src/pages/editor-page/canvas/canvas-filter/canvas-filter.tsx @@ -132,15 +132,13 @@ export const CanvasFilter: React.FC = ({ onClose }) => { // Convert to tree nodes const nodes: TreeNode[] = []; - // Sort areas by order or name - const sortedAreas = areas - .filter((area) => tablesByArea.has(area.id)) - .sort((a, b) => { - if (a.order !== undefined && b.order !== undefined) { - return a.order - b.order; - } - return a.name.localeCompare(b.name); - }); + // Sort all areas by order or name (including empty ones) + const sortedAreas = areas.sort((a, b) => { + if (a.order !== undefined && b.order !== undefined) { + return a.order - b.order; + } + return a.name.localeCompare(b.name); + }); sortedAreas.forEach((area) => { const areaTables = tablesByArea.get(area.id) || []; @@ -400,13 +398,16 @@ export const CanvasFilter: React.FC = ({ onClose }) => { variant="ghost" size="sm" className="size-7 h-fit p-0" + disabled={!node.children || node.children.length === 0} onClick={(e) => { e.stopPropagation(); // Toggle all tables in this area const allHidden = - node.children?.every((child) => - hiddenTableIds?.includes(child.id) - ) ?? false; + (node.children?.length > 0 && + node.children?.every((child) => + hiddenTableIds?.includes(child.id) + )) || + false; node.children?.forEach((child) => { if (child.type === 'table') { diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsx index 48004915..fad61594 100644 --- a/src/pages/editor-page/canvas/canvas.tsx +++ b/src/pages/editor-page/canvas/canvas.tsx @@ -154,6 +154,21 @@ const areaToAreaNode = ( const tablesInArea = tables.filter( (table) => table.parentAreaId === area.id ); + + // Don't hide area if it has no tables (empty area) + if (tablesInArea.length === 0) { + return { + id: area.id, + type: 'area', + position: { x: area.x, y: area.y }, + data: { area }, + width: area.width, + height: area.height, + zIndex: -10, + hidden: false, + }; + } + const allTablesHidden = tablesInArea.every( (table) => hiddenTableIds?.includes(table.id) || @@ -490,7 +505,12 @@ export const Canvas: React.FC = ({ initialTables }) => { useEffect(() => { const checkParentAreas = debounce(() => { - const updatedTables = updateTablesParentAreas(tables, areas); + const updatedTables = updateTablesParentAreas( + tables, + areas, + hiddenTableIds, + filteredSchemas + ); const needsUpdate: Array<{ id: string; parentAreaId: string | null; @@ -530,7 +550,14 @@ export const Canvas: React.FC = ({ initialTables }) => { }, 300); checkParentAreas(); - }, [tablePositions, areas, updateTablesState, tables]); + }, [ + tablePositions, + areas, + updateTablesState, + tables, + hiddenTableIds, + filteredSchemas, + ]); const onConnectHandler = useCallback( async (params: AddEdgeParams) => {