Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[Metabot] Clicking the Conversations link clears the filters (#83819)

Sloan Sparger87cb11400ba9
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx
index 3afb9f09c3e..852edc5f857 100644
--- a/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/monitor/ai-auditing/metabot-analytics/components/ConversationsPage/ConversationsPage.unit.spec.tsx
@@ -291,6 +291,18 @@ describe("ConversationsPage", () => {
).toBeInTheDocument();
});
+ it("clears the filters when navigated to the unfiltered url", async () => {
+ const { router } = setup({
+ initialRoute: `${CONVERSATIONS_PATH}?user=${ROBERT.id}`,
+ });
+
+ await screen.findByDisplayValue(ROBERT.common_name);
+
+ act(() => router?.navigate(CONVERSATIONS_PATH));
+
+ expect(await screen.findByDisplayValue("All users")).toBeInTheDocument();
+ });
+
it("filters by user", async () => {
const { router } = setup();
diff --git a/frontend/src/metabase/common/hooks/use-url-state/use-url-state.ts b/frontend/src/metabase/common/hooks/use-url-state/use-url-state.ts
index 4151ec4c168..fe5ba7c098a 100644
--- a/frontend/src/metabase/common/hooks/use-url-state/use-url-state.ts
+++ b/frontend/src/metabase/common/hooks/use-url-state/use-url-state.ts
@@ -70,29 +70,40 @@ export function useUrlState<State extends BaseState>(
const updateUrl = useCallback(
(state: State) => {
- const newLocation = {
- ...location,
- search: queryToSearch(serialize(state)),
- };
- navigate(newLocation);
+ const search = queryToSearch(serialize(state));
+ // skip when the URL already carries this state, so adopting a URL below
+ // does not push it onto the history a second time
+ if (search !== location.search) {
+ navigate({ ...location, search });
+ }
},
[location, serialize, navigate],
);
const updateUrlRef = useLatest(updateUrl);
+ const urlStateRef = useLatest(urlState);
useEffectOnce(function cleanInvalidQueryParams() {
- const query = serialize(urlState);
+ const search = queryToSearch(serialize(urlState));
// Replacing to the identical URL notifies the router, which re-renders every
// location consumer on the page for nothing.
- if (!_.isEqual(query, parseSearchQuery(location.search))) {
+ if (search !== location.search) {
navigate(
- { ...location, search: queryToSearch(query) },
+ { ...location, search },
{ replace: true, state: location.state },
);
}
});
+ useEffect(() => {
+ const nextState = parse(parseSearchQuery(location.search));
+ // compare against the last state written to the URL rather than `state`, so
+ // a patch still waiting out the debounce is not reverted.
+ if (!_.isEqual(serialize(nextState), serialize(urlStateRef.current))) {
+ patchUrlState(nextState, { immediate: true });
+ }
+ }, [location.search, parse, serialize, urlStateRef, patchUrlState]);
+
useEffect(() => {
updateUrlRef.current(urlState);
}, [updateUrlRef, urlState]);
diff --git a/frontend/src/metabase/common/hooks/use-url-state/use-url-state.unit.spec.ts b/frontend/src/metabase/common/hooks/use-url-state/use-url-state.unit.spec.ts
index be886a1d994..7314af6d34e 100644
--- a/frontend/src/metabase/common/hooks/use-url-state/use-url-state.unit.spec.ts
+++ b/frontend/src/metabase/common/hooks/use-url-state/use-url-state.unit.spec.ts
@@ -1,6 +1,6 @@
import { createMockLocation } from "__support__/state";
import { act, renderHookWithProviders, waitFor } from "__support__/ui";
-import type { Location } from "metabase/router";
+import { type Location, useLocation } from "metabase/router";
import type { QueryParam } from "./types";
import { type UrlStateConfig, useUrlState } from "./use-url-state";
@@ -41,7 +41,7 @@ const setup = ({ location = createMockLocation() }: SetupOpts = {}) => {
}),
};
- return renderHookWithProviders(() => useUrlState(location, config), {
+ return renderHookWithProviders(() => useUrlState(useLocation(), config), {
initialRoute: `${location.pathname}${location.search}`,
withRouter: true,
});
@@ -148,6 +148,20 @@ describe("useUrlState", () => {
});
});
+ it("follows the url on navigation, back and forward", () => {
+ const location = createLocation("?name=abc&score=123");
+ const { result, router } = setup({ location });
+
+ act(() => router?.navigate(location.pathname));
+ expect(result.current[0]).toEqual({ name: null, score: null });
+
+ act(() => router?.back());
+ expect(result.current[0]).toEqual({ name: "abc", score: 123 });
+
+ act(() => router?.forward());
+ expect(result.current[0]).toEqual({ name: null, score: null });
+ });
+
it("removes query params", async () => {
const location = createLocation("?name=abc&score=123");
const { result, router } = setup({ location });