Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔍 fix: Race Condition in Search Bar Clear Text Handler (#9718)

Danny Avila99135a3dc1a2
diff --git a/client/src/components/Nav/SearchBar.tsx b/client/src/components/Nav/SearchBar.tsx
index 891d667e15..1e23f757e8 100644
--- a/client/src/components/Nav/SearchBar.tsx
+++ b/client/src/components/Nav/SearchBar.tsx
@@ -24,35 +24,45 @@ const SearchBar = forwardRef((props: SearchBarProps, ref: React.Ref<HTMLDivEleme
const inputRef = useRef<HTMLInputElement>(null);
const [showClearIcon, setShowClearIcon] = useState(false);
- const { newConversation } = useNewConvo();
+ const { newConversation: newConvo } = useNewConvo();
const [search, setSearchState] = useRecoilState(store.search);
- const clearSearch = useCallback(() => {
- if (location.pathname.includes('/search')) {
- newConversation({ disableFocus: true });
- navigate('/c/new', { replace: true });
- }
- }, [newConversation, location.pathname, navigate]);
+ const clearSearch = useCallback(
+ (pathname?: string) => {
+ if (pathname?.includes('/search') || pathname === '/c/new') {
+ queryClient.removeQueries([QueryKeys.messages]);
+ newConvo({ disableFocus: true });
+ navigate('/c/new');
+ }
+ },
+ [newConvo, navigate, queryClient],
+ );
- const clearText = useCallback(() => {
- setShowClearIcon(false);
- setText('');
- setSearchState((prev) => ({
- ...prev,
- query: '',
- debouncedQuery: '',
- isTyping: false,
- }));
- clearSearch();
- inputRef.current?.focus();
- }, [setSearchState, clearSearch]);
+ const clearText = useCallback(
+ (pathname?: string) => {
+ setShowClearIcon(false);
+ setText('');
+ setSearchState((prev) => ({
+ ...prev,
+ query: '',
+ debouncedQuery: '',
+ isTyping: false,
+ }));
+ clearSearch(pathname);
+ inputRef.current?.focus();
+ },
+ [setSearchState, clearSearch],
+ );
- const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
- const { value } = e.target as HTMLInputElement;
- if (e.key === 'Backspace' && value === '') {
- clearText();
- }
- };
+ const handleKeyUp = useCallback(
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
+ const { value } = e.target as HTMLInputElement;
+ if (e.key === 'Backspace' && value === '') {
+ clearText(location.pathname);
+ }
+ },
+ [clearText, location.pathname],
+ );
const sendRequest = useCallback(
(value: string) => {
@@ -85,8 +95,6 @@ const SearchBar = forwardRef((props: SearchBarProps, ref: React.Ref<HTMLDivEleme
debouncedSetDebouncedQuery(value);
if (value.length > 0 && location.pathname !== '/search') {
navigate('/search', { replace: true });
- } else if (value.length === 0 && location.pathname === '/search') {
- navigate('/c/new', { replace: true });
}
};
@@ -132,7 +140,7 @@ const SearchBar = forwardRef((props: SearchBarProps, ref: React.Ref<HTMLDivEleme
showClearIcon ? 'opacity-100' : 'opacity-0',
isSmallScreen === true ? 'right-[16px]' : '',
)}
- onClick={clearText}
+ onClick={() => clearText(location.pathname)}
tabIndex={showClearIcon ? 0 : -1}
disabled={!showClearIcon}
>