Skip to content

Commit

Permalink
Maintain the tab state on Chat component between AI stream and filter…
Browse files Browse the repository at this point in the history
…ed results, when a user is actively filtering content
  • Loading branch information
adamjarling committed Jul 1, 2024
1 parent 40833ba commit 74453ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 1 addition & 2 deletions components/Facets/Filter/Submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const FacetsFilterSubmit: React.FC<FacetsFilterSubmitProps> = ({
const {
query: { q },
} = router;
const { ai, urlFacets } = useQueryParams();
const { urlFacets } = useQueryParams();

const {
filterDispatch,
Expand All @@ -34,7 +34,6 @@ const FacetsFilterSubmit: React.FC<FacetsFilterSubmitProps> = ({
...(q && { q }),
...urlFacets,
...userFacetsUnsubmitted,
...(ai && { ai }),
};

router.push({
Expand Down
21 changes: 18 additions & 3 deletions pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ResultsWrapper,
StyledResponseWrapper,
} from "@/components/Search/Search.styled";
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";

import { ActiveTab } from "@/types/context/search-context";
import { ApiSearchRequestBody } from "@/types/api/request";
Expand Down Expand Up @@ -47,7 +47,9 @@ type RequestState = {

const SearchPage: NextPage = () => {
const size = 40;

const router = useRouter();
const prevRouterQuery = useRef(router.query);

const { user } = React.useContext(UserContext);
const { isChecked } = useGenerativeAISearchToggle();
Expand Down Expand Up @@ -152,9 +154,22 @@ const SearchPage: NextPage = () => {
})();
}, [pageQueryUrl]);

/**
* Maintain tab state when filtering in the Gen AI Chat component
*/
useEffect(() => {
setActiveTab(isChecked ? "stream" : "results");
}, [isChecked]);
const routerQueryParamsChanged =
JSON.stringify(prevRouterQuery.current) !== JSON.stringify(router.query);

if (routerQueryParamsChanged) {
setActiveTab(
routerQueryParamsChanged ? "results" : isChecked ? "stream" : "results",
);

// Update the previous value for the next render
prevRouterQuery.current = router.query;
}
}, [router.query]);

/**
* Handle any network errors
Expand Down

0 comments on commit 74453ef

Please sign in to comment.