Skip to content

Commit 57a3ae3

Browse files
committed
center on newly added collection
1 parent b8a5705 commit 57a3ae3

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

packages/compass-components/src/components/drawer-portal.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,14 @@ export function useDrawerActions() {
319319
});
320320
return stableActions.current;
321321
}
322+
323+
export const useDrawerState = () => {
324+
const drawerToolbarContext = useDrawerToolbarContext();
325+
const drawerState = useContext(DrawerStateContext);
326+
return {
327+
isOpen:
328+
drawerToolbarContext.isDrawerOpen &&
329+
// the second check is a workaround, because LG doesn't set isDrawerOpen to false when it's empty
330+
drawerState.length > 0,
331+
};
332+
};

packages/compass-data-modeling/src/components/diagram-editor.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
Button,
2727
useDarkMode,
2828
useDrawerActions,
29+
useDrawerState,
2930
rafraf,
3031
} from '@mongodb-js/compass-components';
3132
import { cancelAnalysis, retryAnalysis } from '../store/analysis-process';
@@ -108,6 +109,7 @@ const DiagramContent: React.FunctionComponent<{
108109
model: StaticModel | null;
109110
isInRelationshipDrawingMode: boolean;
110111
editErrors?: string[];
112+
newCollection?: string;
111113
onMoveCollection: (ns: string, newPosition: [number, number]) => void;
112114
onCollectionSelect: (namespace: string) => void;
113115
onRelationshipSelect: (rId: string) => void;
@@ -119,6 +121,7 @@ const DiagramContent: React.FunctionComponent<{
119121
diagramLabel,
120122
model,
121123
isInRelationshipDrawingMode,
124+
newCollection,
122125
onMoveCollection,
123126
onCollectionSelect,
124127
onRelationshipSelect,
@@ -130,6 +133,7 @@ const DiagramContent: React.FunctionComponent<{
130133
const isDarkMode = useDarkMode();
131134
const diagram = useRef(useDiagram());
132135
const { openDrawer } = useDrawerActions();
136+
const { isOpen: isDrawerOpen } = useDrawerState();
133137

134138
const setDiagramContainerRef = useCallback((ref: HTMLDivElement | null) => {
135139
if (ref) {
@@ -184,6 +188,31 @@ const DiagramContent: React.FunctionComponent<{
184188
});
185189
}, []);
186190

191+
// Center on a new collection when it is added
192+
const previouslyOpenedDrawer = useRef<boolean>(false);
193+
useEffect(() => {
194+
if (newCollection) {
195+
const node = nodes.find((n) => n.id === newCollection);
196+
if (node) {
197+
const zoom = diagram.current.getViewport().zoom;
198+
const drawerOffset = previouslyOpenedDrawer.current ? 0 : 240;
199+
const newNodeWidth = 244;
200+
const newNodeHeight = 64;
201+
return rafraf(() => {
202+
void diagram.current.setCenter(
203+
((node.position.x + newNodeWidth / 2) * zoom + drawerOffset) / zoom,
204+
node.position.y + newNodeHeight / 2,
205+
{
206+
duration: 500,
207+
zoom,
208+
}
209+
);
210+
});
211+
}
212+
}
213+
previouslyOpenedDrawer.current = !!isDrawerOpen;
214+
}, [newCollection, nodes, isDrawerOpen]);
215+
187216
const handleNodesConnect = useCallback(
188217
(source: string, target: string) => {
189218
onCreateNewRelationship(source, target);
@@ -242,6 +271,7 @@ const ConnectedDiagramContent = connect(
242271
model: diagram ? selectCurrentModelFromState(state) : null,
243272
diagramLabel: diagram?.name || 'Schema Preview',
244273
selectedItems: state.diagram?.selectedItems ?? null,
274+
newCollection: diagram?.draftCollection,
245275
};
246276
},
247277
{

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export type DiagramState =
4747
};
4848
editErrors?: string[];
4949
selectedItems: SelectedItems | null;
50-
draftCollection?: string | null;
50+
draftCollection?: string;
5151
})
5252
| null; // null when no diagram is currently open
5353

0 commit comments

Comments
 (0)