@@ -2,11 +2,11 @@ import { useEffect, useRef } from 'react';
22import { useEventListener } from 'hooks/useEventListener' ;
33import { Result } from './Result' ;
44import { Spinner } from 'shared/components/Spinner/Spinner' ;
5- import { addHistoryEntry } from '../search-history' ;
65import './ResultsList.scss' ;
76import { useTranslation } from 'react-i18next' ;
87import { LOADING_INDICATOR } from '../types' ;
98import { useFormNavigation } from 'shared/hooks/useFormNavigation' ;
9+ import { activateResult , isResultGoingToRedirect } from '../helpers' ;
1010
1111function scrollInto ( element : Element ) {
1212 element . scrollIntoView ( {
@@ -38,6 +38,22 @@ export function ResultsList({
3838 //todo 2
3939 const isLoading = results . find ( ( r : any ) => r . type === LOADING_INDICATOR ) ;
4040 results = results . filter ( ( r : any ) => r . type !== LOADING_INDICATOR ) ;
41+
42+ const activateResultWithSafeNavigation = ( ) => {
43+ if ( isResultGoingToRedirect ( results [ activeIndex ] . query ) ) {
44+ navigateSafely ( ( ) =>
45+ activateResult (
46+ results [ activeIndex ] . query ,
47+ results [ activeIndex ] . onActivate ,
48+ ) ,
49+ ) ;
50+ } else
51+ activateResult (
52+ results [ activeIndex ] . query ,
53+ results [ activeIndex ] . onActivate ,
54+ ) ;
55+ } ;
56+
4157 useEffect ( ( ) => {
4258 if ( results ?. length <= activeIndex ) {
4359 setActiveIndex ( 0 ) ;
@@ -65,10 +81,7 @@ export function ResultsList({
6581 scrollInto ( listRef . current ! . children [ activeIndex - 1 ] ) ;
6682 } else if ( key === 'Enter' && results ?. [ activeIndex ] ) {
6783 e . preventDefault ( ) ;
68- navigateSafely ( ( ) => {
69- addHistoryEntry ( results [ activeIndex ] . query ) ;
70- results [ activeIndex ] . onActivate ( ) ;
71- } ) ;
84+ activateResultWithSafeNavigation ( ) ;
7285 }
7386 } ,
7487 [ activeIndex , results , isHistoryMode ] ,
@@ -85,10 +98,7 @@ export function ResultsList({
8598 activeIndex = { activeIndex }
8699 setActiveIndex = { setActiveIndex }
87100 onItemClick = { ( ) => {
88- navigateSafely ( ( ) => {
89- addHistoryEntry ( result . query ) ;
90- result . onActivate ( ) ;
91- } ) ;
101+ activateResultWithSafeNavigation ( ) ;
92102 } }
93103 />
94104 ) )
0 commit comments