11import { useSigma } from '@react-sigma/core' ;
22import { Attributes } from 'graphology-types' ;
3- import MiniSearch from 'minisearch' ;
3+ import MiniSearch , { Options } from 'minisearch' ;
44import React from 'react' ;
55import { FC , PropsWithChildren , createContext , useEffect , useState } from 'react' ;
66
7+ import { Document } from './types' ;
78import { edgeToDocument , nodeToDocument } from './utils' ;
89
910export interface GraphSearchContextType {
@@ -18,27 +19,58 @@ const initialContext: GraphSearchContextType = {
1819 */
1920export const GraphSearchContext = createContext ( initialContext ) ;
2021
22+ export interface GraphSearchContextProviderProps {
23+ /**
24+ * The minisearch options for its construtor.
25+ */
26+ minisearchOptions ?: Partial < Options < Document > > ;
27+ }
2128/**
2229 * Search context provider.
2330 * It exposes the minisearch instance to search in the graph and
2431 * is also responsible to keep the index up to date with the graph.
2532 *
33+ * You can override the minisearch options by passing them in the `minisearchOptions` prop.
34+ *
35+ * Documents in the index have the type {@link Document} :
36+ * - `id` is node or edge key in the graph
37+ * - `type` is "nodes" or "edges"
38+ * - `itemId` is the unique id in the index (concatenation of type and id)
39+ * - `label` is the label of the node or edge in the graph
40+ * - every attributes of the node or edge are also in the index, prefixed by `prop_`
41+ *
42+ *
2643 * @category Component
2744 */
28- export const GraphSearchContextProvider : FC < PropsWithChildren > = ( { children } ) => {
45+ export const GraphSearchContextProvider : FC < PropsWithChildren < GraphSearchContextProviderProps > > = ( {
46+ children,
47+ minisearchOptions,
48+ } ) => {
2949 const [ context , setContext ] = useState ( initialContext ) ;
3050 const sigma = useSigma ( ) ;
3151
3252 useEffect ( ( ) => {
33- const index = new MiniSearch ( {
53+ const index = new MiniSearch < Document > ( {
54+ ...minisearchOptions ,
3455 idField : 'itemId' ,
35- fields : [ 'label' ] ,
36- storeFields : [ 'itemId' , 'id' , 'type' ] ,
37- processTerm : ( term , _fieldName ) =>
38- term
39- . normalize ( 'NFD' )
40- . replace ( / [ \u0300 - \u036f ] / g, '' )
41- . toLowerCase ( ) ,
56+ fields : minisearchOptions ?. fields
57+ ? [ 'label' , ...minisearchOptions . fields . filter ( ( e ) => e !== 'label' ) ]
58+ : [ 'label' ] ,
59+ storeFields : minisearchOptions ?. storeFields
60+ ? [
61+ 'itemId' ,
62+ 'id' ,
63+ 'type' ,
64+ ...minisearchOptions . storeFields . filter ( ( e ) => ! [ 'itemId' , 'id' , 'type' ] . includes ( e ) ) ,
65+ ]
66+ : [ 'itemId' , 'id' , 'type' ] ,
67+ processTerm : minisearchOptions ?. processTerm
68+ ? minisearchOptions ?. processTerm
69+ : ( term , _fieldName ) =>
70+ term
71+ . normalize ( 'NFD' )
72+ . replace ( / [ \u0300 - \u036f ] / g, '' )
73+ . toLowerCase ( ) ,
4274 } ) ;
4375
4476 // index all
@@ -88,7 +120,7 @@ export const GraphSearchContextProvider: FC<PropsWithChildren> = ({ children })
88120 graph . off ( 'edgeAttributesUpdated' , indexEdge ) ;
89121 graph . off ( 'edgeDropped' , deleteEdge ) ;
90122 } ;
91- } , [ sigma ] ) ;
123+ } , [ sigma , minisearchOptions ] ) ;
92124
93125 return < GraphSearchContext . Provider value = { context } > { children } </ GraphSearchContext . Provider > ;
94126} ;
0 commit comments