@@ -26,6 +26,7 @@ import {
2626 Button ,
2727 useDarkMode ,
2828 useDrawerActions ,
29+ useDrawerState ,
2930 rafraf ,
3031} from '@mongodb-js/compass-components' ;
3132import { 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 {
0 commit comments