Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

add alret before rearrange all tables in the diagram

johnnyfishb66758c7ed72
diff --git a/src/components/alert-dialog/alert-dialog.tsx b/src/components/alert-dialog/alert-dialog.tsx
index 3d0b3156..397beb8e 100644
--- a/src/components/alert-dialog/alert-dialog.tsx
+++ b/src/components/alert-dialog/alert-dialog.tsx
@@ -29,17 +29,14 @@ const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
- <AlertDialogPortal>
- <AlertDialogOverlay />
- <AlertDialogPrimitive.Content
- ref={ref}
- className={cn(
- 'fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg',
- className
- )}
- {...props}
- />
- </AlertDialogPortal>
+ <AlertDialogPrimitive.Content
+ ref={ref}
+ className={cn(
+ 'fixed left-[50%] top-[50%] z-50 grid w-[80%] max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:w-full sm:rounded-lg',
+ className
+ )}
+ {...props}
+ />
));
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName;
diff --git a/src/components/toast/toast.tsx b/src/components/toast/toast.tsx
index 9d359642..4fa23f2b 100644
--- a/src/components/toast/toast.tsx
+++ b/src/components/toast/toast.tsx
@@ -14,7 +14,7 @@ const ToastViewport = React.forwardRef<
<ToastPrimitives.Viewport
ref={ref}
className={cn(
- 'fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]',
+ 'fixed bottom-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 right-0 sm:top-auto sm:flex-col max-w-[360px]',
className
)}
{...props}
diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts
index 2265756a..b269bab5 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -50,6 +50,14 @@ export const en = {
clear: 'Clear',
},
+ reorder_diagram_alert: {
+ title: 'Reorder Diagram',
+ description:
+ 'This action will rearrange all tables in the diagram. Do you want to continue?',
+ reorder: 'Reorder',
+ cancel: 'Cancel',
+ },
+
theme: {
system: 'System',
light: 'Light',
@@ -215,6 +223,14 @@ export const en = {
one_to_many: 'One to Many',
many_to_one: 'Many to One',
},
+
+ toast: {
+ reorder: {
+ title: 'Tables reordered',
+ description: 'Click undo to revert changes',
+ undo: 'Undo',
+ },
+ },
},
};
diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.ts
index d85aefca..e5082728 100644
--- a/src/i18n/locales/es.ts
+++ b/src/i18n/locales/es.ts
@@ -50,6 +50,14 @@ export const es: LanguageTranslation = {
clear: 'Limpiar',
},
+ reorder_diagram_alert: {
+ title: 'Reordenar Diagrama',
+ description:
+ 'Esta acción reorganizará todas las tablas en el diagrama. ¿Deseas continuar?',
+ reorder: 'Reordenar',
+ cancel: 'Cancelar',
+ },
+
theme: {
system: 'Sistema',
light: 'Claro',
@@ -216,6 +224,14 @@ export const es: LanguageTranslation = {
one_to_many: 'Uno a Muchos',
many_to_one: 'Muchos a Uno',
},
+
+ toast: {
+ reorder: {
+ title: 'Tablas reordenadas',
+ description: 'Haz clic en deshacer para revertir los cambios',
+ undo: 'Deshacer',
+ },
+ },
},
};
diff --git a/src/lib/domain/db-table.ts b/src/lib/domain/db-table.ts
index 02e5aaeb..d50869f9 100644
--- a/src/lib/domain/db-table.ts
+++ b/src/lib/domain/db-table.ts
@@ -141,10 +141,25 @@ export const createTablesFromMetadata = ({
export const adjustTablePositions = ({
relationships,
tables,
+ filteredSchemas,
}: {
tables: DBTable[];
relationships: DBRelationship[];
+ filteredSchemas?: string[];
}): DBTable[] => {
+ const filteredTables = filteredSchemas
+ ? tables.filter(
+ (table) => !table.schema || filteredSchemas.includes(table.schema)
+ )
+ : tables;
+
+ // Filter relationships to only include those between filtered tables
+ const filteredRelationships = relationships.filter(
+ (rel) =>
+ filteredTables.some((t) => t.id === rel.sourceTableId) &&
+ filteredTables.some((t) => t.id === rel.targetTableId)
+ );
+
const tableWidth = 200;
const tableHeight = 300;
const gapX = 100;
@@ -154,7 +169,7 @@ export const adjustTablePositions = ({
// Create a map of table connections
const tableConnections = new Map<string, Set<string>>();
- relationships.forEach((rel) => {
+ filteredRelationships.forEach((rel) => {
if (!tableConnections.has(rel.sourceTableId)) {
tableConnections.set(rel.sourceTableId, new Set());
}
@@ -166,7 +181,7 @@ export const adjustTablePositions = ({
});
// Sort tables by number of connections
- const sortedTables = [...tables].sort(
+ const sortedTables = [...filteredTables].sort(
(a, b) =>
(tableConnections.get(b.id)?.size || 0) -
(tableConnections.get(a.id)?.size || 0)
@@ -241,7 +256,7 @@ export const adjustTablePositions = ({
connectedTables.forEach((connectedTableId) => {
if (!positionedTables.has(connectedTableId)) {
- const connectedTable = tables.find(
+ const connectedTable = filteredTables.find(
(t) => t.id === connectedTableId
);
if (connectedTable) {
@@ -265,8 +280,8 @@ export const adjustTablePositions = ({
}
});
- // Apply positions to tables
- tables.forEach((table) => {
+ // Apply positions to filtered tables
+ filteredTables.forEach((table) => {
const position = tablePositions.get(table.id);
if (position) {
table.x = position.x;
@@ -274,5 +289,5 @@ export const adjustTablePositions = ({
}
});
- return tables;
+ return tables; // Return all tables, but only filtered ones are positioned
};
diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsx
index 7d81f0cf..7e7e02fa 100644
--- a/src/pages/editor-page/canvas/canvas.tsx
+++ b/src/pages/editor-page/canvas/canvas.tsx
@@ -38,6 +38,7 @@ import {
TooltipTrigger,
TooltipContent,
} from '@/components/tooltip/tooltip';
+import { useDialog } from '@/hooks/use-dialog';
type AddEdgeParams = Parameters<typeof addEdge<TableEdgeType>>[0];
@@ -80,6 +81,7 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {
const { showSidePanel } = useLayout();
const { effectiveTheme } = useTheme();
const { scrollAction } = useLocalConfig();
+ const { showAlert } = useDialog();
const { isMd: isDesktop } = useBreakpoint('md');
const nodeTypes = useMemo(() => ({ table: TableNode }), []);
const edgeTypes = useMemo(() => ({ 'table-edge': TableEdge }), []);
@@ -283,11 +285,80 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {
const isLoadingDOM =
tables.length > 0 ? !getInternalNode(tables[0].id) : false;
+ const showReorderConfirmation = useCallback(() => {
+ showAlert({
+ title: t('reorder_diagram_alert.title'),
+ description: t('reorder_diagram_alert.description'),
+ actionLabel: t('reorder_diagram_alert.reorder'),
+ closeLabel: t('reorder_diagram_alert.cancel'),
+ onAction: () => {
+ const originalTables = tables.map((table) => ({ ...table }));
+ const newTables = adjustTablePositions({
+ relationships,
+ tables,
+ filteredSchemas,
+ });
+ updateTablesState(() => newTables, {
+ updateHistory: false,
+ forceOverride: true,
+ });
+ setNodes(
+ newTables.map((table) =>
+ tableToTableNode(table, filteredSchemas)
+ )
+ );
+ fitView({ padding: 0.2, duration: 1000 });
+
+ const { dismiss } = toast({
+ title: t('toast.reorder.title'),
+ description: t('toast.reorder.description'),
+ duration: 15000,
+ action: (
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => {
+ updateTablesState(() => originalTables, {
+ updateHistory: false,
+ forceOverride: true,
+ });
+ setNodes(
+ originalTables.map((table) =>
+ tableToTableNode(table, filteredSchemas)
+ )
+ );
+ setTimeout(
+ () =>
+ fitView({
+ padding: 0.2,
+ duration: 1000,
+ }),
+ 0
+ );
+ dismiss();
+ }}
+ >
+ {t('toast.reorder.undo')}
+ </Button>
+ ),
+ });
+ },
+ });
+ }, [
+ showAlert,
+ t,
+ relationships,
+ tables,
+ updateTablesState,
+ setNodes,
+ fitView,
+ toast,
+ filteredSchemas,
+ ]);
+
const reorderTables = useCallback(() => {
- const newTables = adjustTablePositions({ relationships, tables });
- setNodes(newTables.map(tableToTableNode));
- fitView({ padding: 0.2, duration: 1000 });
- }, [relationships, tables, setNodes, fitView]);
+ showReorderConfirmation();
+ }, [showReorderConfirmation]);
return (
<div className="flex h-full">
@@ -337,7 +408,7 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {
</TooltipContent>
</Tooltip>
</Controls>
- {isLoadingDOM ? (
+ {isLoadingDOM ? (
<Controls
position="top-center"
orientation="horizontal"