@@ -5,7 +5,7 @@ import Downshift, { DownshiftState, StateChangeOptions } from 'downshift';
55import Fuse , { FuseOptions } from 'fuse.js' ;
66import { document } from 'global' ;
77import { transparentize } from 'polished' ;
8- import React , { useEffect , useMemo , useRef , useState , useCallback } from 'react' ;
8+ import React , { useMemo , useRef , useState , useCallback } from 'react' ;
99
1010import { DEFAULT_REF_ID } from './data' ;
1111import {
@@ -21,7 +21,6 @@ import {
2121 isCloseType ,
2222} from './types' ;
2323import { searchItem } from './utils' ;
24- import { matchesKeyCode , matchesModifiers } from '../../keybinding' ;
2524
2625const DEFAULT_MAX_SEARCH_RESULTS = 50 ;
2726
@@ -173,26 +172,6 @@ export const Search = React.memo<{
173172 [ api , inputRef , showAllComponents , DEFAULT_REF_ID ]
174173 ) ;
175174
176- useEffect ( ( ) => {
177- const focusSearch = ( event : KeyboardEvent ) => {
178- if ( ! enableShortcuts || isLoading || event . repeat ) return ;
179- if ( ! inputRef . current || inputRef . current === document . activeElement ) return ;
180- if (
181- // Shift is required to type `/` on some keyboard layouts
182- matchesModifiers ( { ctrl : false , alt : false , meta : false } , event ) &&
183- matchesKeyCode ( 'Slash' , event )
184- ) {
185- inputRef . current . focus ( ) ;
186- inputRef . current . select ( ) ;
187- event . preventDefault ( ) ;
188- }
189- } ;
190-
191- // Keyup prevents slashes from ending up in the input field when held down
192- document . addEventListener ( 'keyup' , focusSearch ) ;
193- return ( ) => document . removeEventListener ( 'keyup' , focusSearch ) ;
194- } , [ inputRef , isLoading , enableShortcuts ] ) ;
195-
196175 const list : SearchItem [ ] = useMemo ( ( ) => {
197176 return dataset . entries . reduce ( ( acc : SearchItem [ ] , [ refId , { stories } ] ) => {
198177 if ( stories ) {
@@ -209,17 +188,20 @@ export const Search = React.memo<{
209188 if ( ! input ) return [ ] ;
210189
211190 let results : DownshiftItem [ ] = [ ] ;
212- const componentResults = ( fuse . search ( input ) as SearchResult [ ] ) . filter (
213- ( { item } ) => item . isComponent
214- ) ;
191+ const resultIds : Set < string > = new Set ( ) ;
192+ const distinctResults = ( fuse . search ( input ) as SearchResult [ ] ) . filter ( ( { item } ) => {
193+ if ( ! ( item . isComponent || item . isLeaf ) || resultIds . has ( item . parent ) ) return false ;
194+ resultIds . add ( item . id ) ;
195+ return true ;
196+ } ) ;
215197
216- if ( componentResults . length ) {
217- results = componentResults . slice ( 0 , allComponents ? 1000 : DEFAULT_MAX_SEARCH_RESULTS ) ;
218- if ( componentResults . length > DEFAULT_MAX_SEARCH_RESULTS && ! allComponents ) {
198+ if ( distinctResults . length ) {
199+ results = distinctResults . slice ( 0 , allComponents ? 1000 : DEFAULT_MAX_SEARCH_RESULTS ) ;
200+ if ( distinctResults . length > DEFAULT_MAX_SEARCH_RESULTS && ! allComponents ) {
219201 results . push ( {
220202 showAll : ( ) => showAllComponents ( true ) ,
221- totalCount : componentResults . length ,
222- moreCount : componentResults . length - DEFAULT_MAX_SEARCH_RESULTS ,
203+ totalCount : distinctResults . length ,
204+ moreCount : distinctResults . length - DEFAULT_MAX_SEARCH_RESULTS ,
223205 } ) ;
224206 }
225207 }
0 commit comments