1- import React , { useEffect , useState } from 'react'
1+ import React , { useEffect , useState , useRef } from 'react'
22import PropTypes from 'prop-types'
33import classNames from 'classnames'
44import { isEmpty , isNil } from 'lodash'
@@ -12,6 +12,9 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
1212 const [ visibleTables , setVisibleTables ] = useState ( [ ] )
1313 const [ visibleColumns , setVisibleColumns ] = useState ( [ ] )
1414
15+ const refListTables = useRef ( null )
16+ const refListColumns = useRef ( null )
17+
1518 const initBrowser = ( ) => {
1619 if ( ! isEmpty ( schemas ) ) {
1720 const schema = schemas [ 0 ]
@@ -44,40 +47,57 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
4447 }
4548
4649 setOpenTable ( table )
50+ if ( refListTables . current ) {
51+ refListTables . current . scrollTop = 0
52+ }
4753 setVisibleColumns ( ( isNil ( table ) || isNil ( table . columns ) ) ? [ ] : table . columns )
48-
49- } else if ( activeItem . type == 'table' ) {
50- // search for the schema and table
51- const [ schema , table ] = schemas . reduce ( ( result , schema ) => {
52- const table = ( schema . tables || [ ] ) . find ( t => isEqual ( t , activeItem ) )
53- return isNil ( table ) ? result : [ schema , table ]
54- } , [ ] )
55-
56- if ( schema ) {
57- setOpenSchema ( schema )
58- setVisibleTables ( schema . tables )
54+ if ( refListColumns . current ) {
55+ refListColumns . current . scrollTop = 0
5956 }
6057
61- if ( table ) {
62- setOpenTable ( table )
63- setVisibleColumns ( table . columns )
58+ } else if ( activeItem . type == 'table' ) {
59+ if ( ! isNil ( activeItem . schema ) ) {
60+ // this is a newly created table, search for the schema and table
61+ const [ schema , table ] = schemas . reduce ( ( result , schema ) => {
62+ return schema . id == activeItem . schema ? (
63+ [ schema , ( schema . tables || [ ] ) . find ( t => isEqual ( t , activeItem ) ) ]
64+ ) : result
65+ } , [ ] )
66+
67+ if ( schema ) {
68+ setOpenSchema ( schema )
69+ setVisibleTables ( schema . tables )
70+ }
71+
72+ if ( table ) {
73+ setOpenTable ( table )
74+ setVisibleColumns ( table . columns )
75+ }
76+ } else {
77+ setOpenTable ( activeItem )
78+ setVisibleColumns ( activeItem . columns )
79+ if ( refListColumns . current ) {
80+ refListColumns . current . scrollTop = 0
81+ }
6482 }
6583
6684 } else if ( activeItem . type == 'column' ) {
67- // search for the schema and the table for the column
68- const [ schema , table ] = schemas . reduce ( ( result , schema ) => {
69- const table = ( schema . tables || [ ] ) . find ( t => ( t . columns && t . columns . find ( c => isEqual ( c , activeItem ) ) ) )
70- return isNil ( table ) ? result : [ schema , table ]
71- } , [ ] )
72-
73- if ( schema ) {
74- setOpenSchema ( schema )
75- setVisibleTables ( schema . tables )
76- }
77-
78- if ( table ) {
79- setOpenTable ( table )
80- setVisibleColumns ( table . columns )
85+ if ( ! isNil ( activeItem . table ) ) {
86+ // this is a newly created column, search for the schema and table
87+ const [ schema , table ] = schemas . reduce ( ( result , schema ) => {
88+ const table = ( schema . tables || [ ] ) . find ( t => ( t . id == activeItem . table ) )
89+ return isNil ( table ) ? result : [ schema , table ]
90+ } , [ ] )
91+
92+ if ( schema ) {
93+ setOpenSchema ( schema )
94+ setVisibleTables ( schema . tables )
95+ }
96+
97+ if ( table ) {
98+ setOpenTable ( table )
99+ setVisibleColumns ( table . columns )
100+ }
81101 }
82102 }
83103 }
@@ -117,7 +137,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
117137 >
118138 < div > { schema . name } </ div >
119139 {
120- openSchema && ( openSchema . id == schema . id ) && (
140+ openSchema && ( isEqual ( openSchema , schema ) ) && (
121141 < div className = "ms-auto" > < i className = "bi bi-chevron-right" > </ i > </ div >
122142 )
123143 }
@@ -132,7 +152,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
132152 < div className = "dq-browser-title" >
133153 { gettext ( 'Tables' ) }
134154 </ div >
135- < ul className = "dq-browser-list" >
155+ < ul className = "dq-browser-list" ref = { refListTables } >
136156 {
137157 visibleTables . map ( ( table , index ) => (
138158 < li key = { index } >
@@ -144,7 +164,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
144164 >
145165 < div > { table . name } </ div >
146166 {
147- openTable && ( openTable . id == table . id ) && (
167+ openTable && ( isEqual ( openTable , table ) ) && (
148168 < div className = "ms-auto" > < i className = "bi bi-chevron-right" > </ i > </ div >
149169 )
150170 }
@@ -159,7 +179,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
159179 < div className = "dq-browser-title" >
160180 { gettext ( 'Columns' ) }
161181 </ div >
162- < ul className = "dq-browser-list" >
182+ < ul className = "dq-browser-list" ref = { refListColumns } >
163183 {
164184 visibleColumns . map ( ( column , index ) => (
165185 < li key = { index } >
0 commit comments