1- import React , {
2- useCallback ,
3- useEffect ,
4- useMemo ,
5- useRef ,
6- useState ,
7- } from 'react' ;
1+ import React , { useCallback , useMemo , useRef , useState } from 'react' ;
2+ import {
3+ getFilteredRowModel ,
4+ type Row as ReactTableRow ,
5+ } from '@tanstack/table-core' ;
86import {
97 Table ,
108 TableBody ,
@@ -20,6 +18,7 @@ import {
2018 flexRender ,
2119 type HeaderGroup ,
2220 SearchInput ,
21+ type LGTableDataType ,
2322} from '@mongodb-js/compass-components' ;
2423import type { ShardZoneData } from '../store/reducer' ;
2524
@@ -79,66 +78,61 @@ const parseData = (shardZones: ShardZoneData[]): ShardZoneExpandableRow[] => {
7978 return Object . values ( grouppedZones ) ;
8079} ;
8180
81+ const hasFilteredChildren = (
82+ row : ReactTableRow < LGTableDataType < ShardZoneRow > >
83+ ) =>
84+ row . subRows . filter (
85+ ( subRow ) => Object . values ( subRow . columnFilters ) . includes ( true ) // columnFilters: e.g. { __global__: true }
86+ ) . length > 0 ;
87+
8288export function ShardZonesTable ( {
8389 shardZones,
8490} : {
8591 shardZones : ShardZoneData [ ] ;
8692} ) {
8793 const tableContainerRef = useRef < HTMLDivElement > ( null ) ;
8894 const [ searchText , setSearchText ] = useState < string > ( '' ) ;
89- const handleSearchTextChange = useCallback (
90- ( e : React . ChangeEvent < HTMLInputElement > ) => {
91- setSearchText ( e . currentTarget . value ) ;
92- } ,
93- [ setSearchText ]
94- ) ;
95+ const [ expanded , setExpanded ] = useState < true | Record < string , boolean > > ( { } ) ;
9596
9697 const data = useMemo < ShardZoneExpandableRow [ ] > (
9798 ( ) => parseData ( shardZones ) ,
9899 [ shardZones ]
99100 ) ;
100101
101- const filteredData = useMemo < ShardZoneExpandableRow [ ] > (
102- ( ) =>
103- data . reduce < ShardZoneExpandableRow [ ] > ( ( filtered , next ) => {
104- const { searchable, subRows } = next ;
105- if ( searchable . includes ( searchText ) ) {
106- filtered . push ( next ) ;
107- return filtered ;
108- }
109- const matchingSubRows = subRows . filter ( ( subRow ) =>
110- subRow . searchable . includes ( searchText )
111- ) ;
112- if ( matchingSubRows . length > 0 ) {
113- filtered . push ( {
114- ...next ,
115- subRows : matchingSubRows ,
116- } ) ;
117- }
118- return filtered ;
119- } , [ ] ) ,
120- [ data , searchText ]
121- ) ;
122-
123102 const table = useLeafyGreenTable ( {
124103 containerRef : tableContainerRef ,
125- data : filteredData ,
104+ data,
126105 columns,
106+ state : {
107+ globalFilter : searchText ,
108+ expanded,
109+ } ,
110+ onGlobalFilterChange : setSearchText ,
111+ onExpandedChange : setExpanded ,
112+ enableGlobalFilter : true ,
113+ getFilteredRowModel : getFilteredRowModel ( ) ,
114+ getIsRowExpanded : ( row ) => {
115+ return (
116+ ( searchText && hasFilteredChildren ( row ) ) ||
117+ ( typeof expanded !== 'boolean' && expanded [ row . id ] )
118+ ) ;
119+ } ,
120+ globalFilterFn : 'auto' ,
121+ filterFromLeafRows : true ,
122+ maxLeafRowFilterDepth : 2 ,
127123 } ) ;
128124
129- const { rows } = table . getRowModel ( ) ;
125+ const tableRef = useRef ( table ) ;
126+ tableRef . current = table ;
130127
131- const rowsRef = useRef ( rows ) ;
132- rowsRef . current = rows ;
133- useEffect ( ( ) => {
134- if ( ! searchText ) return ;
135- for ( const row of rowsRef . current ) {
136- if ( row . subRows . length && ! row . getIsExpanded ( ) ) {
137- console . log ( 'expanding row' , row . original , row . getIsExpanded ( ) ) ;
138- row . toggleExpanded ( ) ;
139- }
140- }
141- } , [ searchText , rowsRef ] ) ;
128+ const handleSearchTextChange = useCallback (
129+ ( e : React . ChangeEvent < HTMLInputElement > ) => {
130+ tableRef . current . setGlobalFilter ( e . currentTarget . value ) ;
131+ } ,
132+ [ tableRef ]
133+ ) ;
134+
135+ const { rows } = table . getRowModel ( ) ;
142136
143137 return (
144138 < >
@@ -187,13 +181,10 @@ export function ShardZonesTable({
187181 { subRow . getVisibleCells ( ) . map ( ( cell ) => {
188182 return (
189183 < Cell key = { cell . id } >
190- { ( ( ) => {
191- // console.log({ cell, context: cell.getContext() });
192- return flexRender (
193- cell . column . columnDef . cell ,
194- cell . getContext ( )
195- ) ;
196- } ) ( ) }
184+ { flexRender (
185+ cell . column . columnDef . cell ,
186+ cell . getContext ( )
187+ ) }
197188 </ Cell >
198189 ) ;
199190 } ) }
0 commit comments