@@ -5,6 +5,7 @@ import ApiInfo from './ApiInfo';
55import ApiAction from './ApiAction' ;
66import useLocalStorage from 'react-use-localstorage' ;
77import shortid from 'shortid' ;
8+ import Fuse from 'fuse.js' ;
89
910interface IAPIRule {
1011 [ key : string ] : string [ ] ;
@@ -24,6 +25,7 @@ interface IAPIInfo {
2425export default function App ( ) {
2526
2627 const [ lrdDocsJson , setLrdDocsJson ] = useState < IAPIInfo [ ] > ( [ ] ) ;
28+ const [ lrdDocsJsonCopy , setLrdDocsJsonCopy ] = useState < IAPIInfo [ ] > ( [ ] ) ;
2729 const [ allParamsRegistry , setAllParamsRegistery ] = useLocalStorage ( 'allParamsRegistry' , "{}" ) ;
2830 const [ apiURL , setApiURL ] = useState < string > ( '' ) ;
2931 const [ host , setHost ] = useState < string > ( '' ) ;
@@ -39,7 +41,10 @@ export default function App() {
3941 const [ showPatch ] = useLocalStorage ( 'showPatch' , 'true' ) ;
4042 const [ showHead ] = useLocalStorage ( 'showHead' , 'true' ) ;
4143
42-
44+ const searchOptions = {
45+ keys : [ 'uri' , 'docBlock' ] ,
46+ threshold : 0.3
47+ } ;
4348
4449 useEffect ( ( ) => {
4550 // get query param named api
@@ -72,13 +77,31 @@ export default function App() {
7277 . then ( ( lrdDocsJson ) => {
7378 setError ( null )
7479 setLrdDocsJson ( lrdDocsJson )
80+ setLrdDocsJsonCopy ( lrdDocsJson )
7581 setSendingRequest ( false )
7682 } ) . catch ( ( error ) => {
7783 setError ( error . message )
7884 setSendingRequest ( false )
7985 } )
8086 }
8187
88+ const handleSearch = ( search : string ) => {
89+ search = search . trim ( )
90+ if ( ! search ) {
91+ setLrdDocsJson ( lrdDocsJsonCopy )
92+ return
93+ }
94+ const fuse = new Fuse ( lrdDocsJson , searchOptions ) ;
95+
96+ const filteredData = fuse . search ( search ) ;
97+ const filteredLrdJson : IAPIInfo [ ] = [ ]
98+ for ( let i = 0 ; i < filteredData . length ; i ++ ) {
99+ filteredLrdJson . push ( filteredData [ i ] . item )
100+ }
101+
102+ setLrdDocsJson ( filteredLrdJson )
103+ }
104+
82105 const handleChangeSettings = ( showGet : string ,
83106 showPost : string ,
84107 showDelete : string ,
@@ -92,7 +115,7 @@ export default function App() {
92115 }
93116 return (
94117 < >
95- < TopNav handleChangeSettings = { handleChangeSettings } />
118+ < TopNav handleChangeSettings = { handleChangeSettings } handleSearch = { handleSearch } />
96119 { sendingRequest && (
97120 < progress className = "progress progress-success w-full" > </ progress >
98121 ) }
0 commit comments