From 1212d98e759af7b03dc46eada7fdd03200152e95 Mon Sep 17 00:00:00 2001 From: yyh Date: Sat, 27 Dec 2025 22:46:59 +0800 Subject: [PATCH] feat: improve search UX by adding `keepPreviousData` to `useQuery` and clearing results for empty queries. --- web/app/components/goto-anything/index.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/web/app/components/goto-anything/index.tsx b/web/app/components/goto-anything/index.tsx index b4c71c62f2..1147174af2 100644 --- a/web/app/components/goto-anything/index.tsx +++ b/web/app/components/goto-anything/index.tsx @@ -4,7 +4,7 @@ import type { FC } from 'react' import type { Plugin } from '../plugins/types' import type { SearchResult } from './actions' import { RiSearchLine } from '@remixicon/react' -import { useQuery } from '@tanstack/react-query' +import { keepPreviousData, useQuery } from '@tanstack/react-query' import { useDebounce, useKeyPress } from 'ahooks' import { Command } from 'cmdk' import { useRouter } from 'next/navigation' @@ -122,6 +122,7 @@ const GotoAnything: FC = ({ enabled: !!searchQueryDebouncedValue && !isCommandsMode, staleTime: 30000, gcTime: 300000, + placeholderData: keepPreviousData, }, ) @@ -197,6 +198,9 @@ const GotoAnything: FC = ({ }, [router]) const dedupedResults = useMemo(() => { + if (!searchQuery.trim()) + return [] + const seen = new Set() return searchResults.filter((result) => { const key = `${result.type}-${result.id}` @@ -205,7 +209,7 @@ const GotoAnything: FC = ({ seen.add(key) return true }) - }, [searchResults]) + }, [searchResults, searchQuery]) // Group results by type const groupedResults = useMemo(() => dedupedResults.reduce((acc, result) => {