@@ -4,13 +4,14 @@ import styles from "./main.module.sass";
44import { DataSheet , getRowsToDelete } from "../core" ; //getRowsToDelete
55import { LithologyTag , Tag , TagSize } from "@macrostrat/data-components" ;
66import { PostgrestOrder , usePostgRESTLazyLoader } from "./data-loaders" ;
7- import { Spinner } from "@blueprintjs/core" ;
7+ import { Spinner , InputGroup } from "@blueprintjs/core" ;
88
99export * from "./data-loaders" ;
10- import { useCallback } from "react" ;
10+ import { useCallback , useState , useRef } from "react" ;
1111import {
1212 ErrorBoundary ,
1313 ToasterContext ,
14+ useAPIResult ,
1415 useToaster ,
1516} from "@macrostrat/ui-components" ;
1617import { Spec } from "immutability-helper" ;
@@ -40,6 +41,8 @@ interface PostgRESTTableViewProps<T extends object>
4041 columns ?: string ;
4142 editable ?: boolean ;
4243 identityKey ?: string ;
44+ enableFullTableSearch ?: boolean ;
45+ dataSheetActions ?: any ;
4346 filter (
4447 query : PostgrestFilterBuilder < T , any , any > ,
4548 ) : PostgrestFilterBuilder < T , any , any > ;
@@ -61,10 +64,32 @@ function _PostgRESTTableView<T>({
6164 order,
6265 columns,
6366 editable = false ,
64- filter,
67+ filter = undefined ,
68+ enableFullTableSearch = false ,
69+ dataSheetActions,
6570 identityKey = "id" ,
6671 ...rest
6772} : PostgRESTTableViewProps < T > ) {
73+ const [ input , setInput ] = useState ( "" ) ;
74+
75+ if ( enableFullTableSearch ) {
76+ const columnList = columns ?? getColumnList ( endpoint , table ) ;
77+
78+ filter = ( query ) => {
79+ const urlParams = new URLSearchParams ( query . url . search ) ;
80+ urlParams . delete ( "or" ) ;
81+ query . url . search = urlParams . toString ( ) ? `?${ urlParams . toString ( ) } ` : "" ;
82+
83+ if ( input . length > 2 && enableFullTableSearch ) {
84+ const conditions = columnList ?. map ( ( col ) => `${ col } .ilike.*${ input } *` ) ;
85+
86+ return query . or ( conditions . join ( "," ) ) ;
87+ }
88+
89+ return query ;
90+ } ;
91+ }
92+
6893 const { data, onScroll, dispatch, client } = usePostgRESTLazyLoader (
6994 endpoint ,
7095 table ,
@@ -99,6 +124,9 @@ function _PostgRESTTableView<T>({
99124 return h ( "div.data-sheet-outer" , [
100125 h ( DataSheet , {
101126 ...rest ,
127+ dataSheetActions : enableFullTableSearch
128+ ? h ( SearchAction , { input, setInput, dispatch } )
129+ : dataSheetActions ,
102130 data,
103131 columnSpecOptions : columnOptions ?? { } ,
104132 editable,
@@ -231,3 +259,33 @@ export function ExpandedLithologies({ value, onChange }) {
231259 ] ) ,
232260 ] ) ;
233261}
262+
263+ export function SearchAction ( { input, setInput, dispatch } ) {
264+ return h ( InputGroup , {
265+ type : "search" ,
266+ placeholder : "Search table..." ,
267+ value : input ,
268+ onChange ( event ) {
269+ const search = event . target . value ;
270+ if ( search . length > 2 || ( input . length === 3 && search . length < 3 ) ) {
271+ dispatch ( { type : "reset" } ) ;
272+ }
273+ setInput ( search . toLowerCase ( ) ) ;
274+ } ,
275+ } ) ;
276+ }
277+
278+ function getColumnList ( endpoint : string , table : string ) {
279+ const url = `${ endpoint } /${ table } ?select=*&limit=1` ;
280+ const res = useAPIResult ( url ) ;
281+
282+ if ( ! res || ! Array . isArray ( res ) || res . length === 0 ) return [ ] ;
283+
284+ const sampleRow = res [ 0 ] ;
285+
286+ return Object . entries ( sampleRow )
287+ . filter ( ( [ _ , value ] ) => {
288+ return typeof value === "string" ;
289+ } )
290+ . map ( ( [ key ] ) => key ) ;
291+ }
0 commit comments