Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear search state when route to Homepage with asPath = / #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/features/messages/MessageSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from 'react';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';

import { Fade } from '../../components/animations/Fade';
import { Card } from '../../components/layout/Card';
Expand Down Expand Up @@ -38,6 +39,9 @@ export function MessageSearch() {
const [startTimeFilter, setStartTimeFilter] = useState<number | null>(null);
const [endTimeFilter, setEndTimeFilter] = useState<number | null>(null);

// Router
const router = useRouter();

// GraphQL query and results
const { isValidInput, isError, isFetching, hasRun, messageList, isMessagesFound } =
useMessageSearchQuery(
Expand Down Expand Up @@ -72,6 +76,11 @@ export function MessageSearch() {
// Keep url in sync
useSyncQueryParam(QUERY_SEARCH_PARAM, isValidInput ? sanitizedInput : '');

// Reset query input when Router to homepage (no params)
useEffect(() => {
if (router.asPath === '/') setSearchInput('');
}, [router.asPath]);

return (
<>
<SearchBar
Expand Down
Loading