diff --git a/src/apps/customer-portal/src/pages/talent-search/TalentSearchPage/TalentSearchPage.tsx b/src/apps/customer-portal/src/pages/talent-search/TalentSearchPage/TalentSearchPage.tsx index e4b3f07fd..d8f052b7b 100644 --- a/src/apps/customer-portal/src/pages/talent-search/TalentSearchPage/TalentSearchPage.tsx +++ b/src/apps/customer-portal/src/pages/talent-search/TalentSearchPage/TalentSearchPage.tsx @@ -32,7 +32,10 @@ type TalentSearchSortOption = 'alphabetical' | 'matching-index' export const TalentSearchPage: FC = () => { const skipNextAutoSearchRef = useRef(false) - const searchGenerationRef = useRef(0) // ← add this + const searchGenerationRef = useRef(0) + const toggleDebounceTimerRef = useRef | undefined>(undefined) + const pendingToggleAutoSearchRef = useRef(false) + const hasMountedRef = useRef(false) const [lastSearchedDescription, setLastSearchedDescription] = useState('') const countryLookup: CountryLookup[] | undefined = useCountryLookup() @@ -91,20 +94,8 @@ export const TalentSearchPage: FC = () => { [hasSkillSearch], ) - const filteredResults = useMemo(() => results.filter(talent => { - if (onlyActive && !talent.isRecentlyActive) { - return false - } - - if (onlyOpenToWork && !talent.openToWork) { - return false - } - - return true - }), [onlyActive, onlyOpenToWork, results]) - // Order comes from reports-api (sortBy/sortOrder on each request) so pagination stays globally consistent. - const displayedResults = filteredResults + const displayedResults = results const foundMembersCount = totalResults || displayedResults.length const displayedResultsWithCountryName = useMemo( @@ -162,6 +153,7 @@ export const TalentSearchPage: FC = () => { }, ): Promise => { const append = overrides?.append === true + const countries = (overrides?.countries ?? selectedCountryCodesList) .filter(Boolean) const generation = overrides?.generation @@ -345,15 +337,72 @@ export const TalentSearchPage: FC = () => { useEffect(() => { if ((shouldShowIntroState) || isExtractingSkills) { + if (toggleDebounceTimerRef.current) { + clearTimeout(toggleDebounceTimerRef.current) + toggleDebounceTimerRef.current = undefined + } + + pendingToggleAutoSearchRef.current = false + + return + } + + if (!hasMountedRef.current) { + hasMountedRef.current = true + + if (skipNextAutoSearchRef.current) { + skipNextAutoSearchRef.current = false + pendingToggleAutoSearchRef.current = false + if (toggleDebounceTimerRef.current) { + clearTimeout(toggleDebounceTimerRef.current) + toggleDebounceTimerRef.current = undefined + } + + return + } + + runMemberSearch(selectedSkills, { generation: searchGenerationRef.current, page: 1 }) return } if (skipNextAutoSearchRef.current) { skipNextAutoSearchRef.current = false + if (toggleDebounceTimerRef.current) { + clearTimeout(toggleDebounceTimerRef.current) + toggleDebounceTimerRef.current = undefined + } + + pendingToggleAutoSearchRef.current = false + return } - runMemberSearch(selectedSkills, { generation: searchGenerationRef.current, page: 1 }) + const runSearch = (): void => { + runMemberSearch(selectedSkills, { + generation: searchGenerationRef.current, + page: 1, + }) + } + + const shouldDebounce = pendingToggleAutoSearchRef.current || Boolean(toggleDebounceTimerRef.current) + + if (shouldDebounce) { + pendingToggleAutoSearchRef.current = false + if (toggleDebounceTimerRef.current) { + clearTimeout(toggleDebounceTimerRef.current) + toggleDebounceTimerRef.current = undefined + } + + toggleDebounceTimerRef.current = setTimeout(() => { + toggleDebounceTimerRef.current = undefined + runSearch() + }, 800) + return + } + + // No debounce requested and no pending timer: execute immediately. + pendingToggleAutoSearchRef.current = false + runSearch() }, [ hasSearched, hasActiveFilters, @@ -366,6 +415,14 @@ export const TalentSearchPage: FC = () => { shouldShowIntroState, ]) + // Cleanup any pending debounced request on unmount. + useEffect(() => (): void => { + if (toggleDebounceTimerRef.current) { + clearTimeout(toggleDebounceTimerRef.current) + toggleDebounceTimerRef.current = undefined + } + }, []) + const handleLoadMore = useCallback((): void => { if (isLoadingMore || isSearchingMembers || !hasMoreResults) { return @@ -472,24 +529,13 @@ export const TalentSearchPage: FC = () => { placeholder='Select country' /> - +