@@ -14,62 +14,46 @@ import { getTimeCellContent } from "src/shared/cell-content/time-content";
1414import { getDateCellContent } from "src/shared/cell-content/date-cell-content" ;
1515import { getCurrencyCellContent } from "src/shared/cell-content/currency-cell-content" ;
1616
17+ type CellWithReferences = {
18+ cell : BodyCell ;
19+ column : Column ;
20+ row : BodyRow ;
21+ tags : Tag [ ] ;
22+ } ;
23+
1724export const filterBodyRowsBySearch = (
18- LoomState : LoomState ,
25+ state : LoomState ,
1926 filteredBodyRows : BodyRow [ ] ,
2027 searchText : string
2128) : BodyRow [ ] => {
22- const { columns, bodyCells, bodyRows } = LoomState . model ;
23- const columnMap = new Map < string , Column > ( ) ;
24- columns . forEach ( ( column ) => columnMap . set ( column . id , column ) ) ;
25-
26- const rowMap = new Map < string , BodyRow > ( ) ;
27- bodyRows . forEach ( ( row ) => rowMap . set ( row . id , row ) ) ;
29+ const { columns, bodyCells, bodyRows } = state . model ;
2830
29- const cellToTagMap = new Map < string , Tag [ ] > ( ) ;
30- bodyCells . forEach ( ( cell ) => {
31- const column = columnMap . get ( cell . columnId ) ;
31+ const cells : CellWithReferences [ ] = bodyCells . map ( ( cell ) => {
32+ const column = columns . find ( ( c ) => c . id === cell . columnId ) ;
3233 if ( ! column ) throw new ColumNotFoundError ( cell . columnId ) ;
3334
34- const cellTags = column . tags . filter ( ( tag ) =>
35- cell . tagIds . includes ( tag . id )
36- ) ;
37- cellToTagMap . set ( cell . id , cellTags ) ;
35+ const row = bodyRows . find ( ( r ) => r . id === cell . rowId ) ;
36+ if ( ! row ) throw new RowNotFoundError ( cell . rowId ) ;
37+
38+ const tags = column . tags . filter ( ( tag ) => cell . tagIds . includes ( tag . id ) ) ;
39+
40+ return { cell, column, row, tags } ;
3841 } ) ;
3942
43+ if ( searchText === "" ) return filteredBodyRows ;
44+
4045 return filteredBodyRows . filter ( ( row ) => {
41- const rowCells = bodyCells . filter ( ( cell ) => cell . rowId === row . id ) ;
42- return rowCells . some ( ( cell ) => {
43- const cellTags = cellToTagMap . get ( cell . id ) ;
44- if ( ! cellTags )
45- throw new Error ( `Tags not found for cell ${ cell . id } ` ) ;
46-
47- return doesCellMatch (
48- cell ,
49- columnMap ,
50- rowMap ,
51- cellTags ,
52- searchText . toLowerCase ( )
53- ) ;
46+ const filteredCells = cells . filter ( ( cell ) => cell . row . id === row . id ) ;
47+ return filteredCells . some ( ( cell ) => {
48+ return doesCellMatch ( cell , searchText . toLowerCase ( ) ) ;
5449 } ) ;
5550 } ) ;
5651} ;
5752
58- const doesCellMatch = (
59- cell : BodyCell ,
60- columnMap : Map < string , Column > ,
61- rowMap : Map < string , BodyRow > ,
62- cellTags : Tag [ ] ,
63- searchText : string
64- ) => {
65- const column = columnMap . get ( cell . columnId ) ;
66- if ( ! column ) throw new ColumNotFoundError ( cell . columnId ) ;
67- const row = rowMap . get ( cell . rowId ) ;
68- if ( ! row ) throw new RowNotFoundError ( cell . rowId ) ;
69-
70- const { dateTime, markdown } = cell ;
71- const { currencyType, type, dateFormat } = column ;
72- const { lastEditedTime, creationTime } = row ;
53+ const doesCellMatch = ( cell : CellWithReferences , searchText : string ) => {
54+ const { dateTime, markdown } = cell . cell ;
55+ const { currencyType, type, dateFormat } = cell . column ;
56+ const { lastEditedTime, creationTime } = cell . row ;
7357
7458 switch ( type ) {
7559 case CellType . TEXT :
@@ -92,7 +76,7 @@ const doesCellMatch = (
9276 ) ;
9377 case CellType . TAG :
9478 case CellType . MULTI_TAG :
95- return matchTags ( cellTags , searchText ) ;
79+ return matchTags ( cell . tags , searchText ) ;
9680 default :
9781 throw new Error ( "Unsupported cell type" ) ;
9882 }
0 commit comments