Skip to content

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

Uxw 231 auto scroll to selected entity picker item vertically (#64459)

Jakub Chodorowiczcda1d6cd4681
diff --git a/frontend/src/metabase/common/components/VirtualizedList/VariableHeightVirtualizedList.tsx b/frontend/src/metabase/common/components/VirtualizedList/VariableHeightVirtualizedList.tsx
index 4591e584748..a33381130d8 100644
--- a/frontend/src/metabase/common/components/VirtualizedList/VariableHeightVirtualizedList.tsx
+++ b/frontend/src/metabase/common/components/VirtualizedList/VariableHeightVirtualizedList.tsx
@@ -1,9 +1,11 @@
+import { useMounted } from "@mantine/hooks";
import { type VirtualItem, useVirtualizer } from "@tanstack/react-virtual";
import { useRef } from "react";
-import { useMount } from "react-use";
import { Box } from "metabase/ui";
+import { useEffectOnceIf } from "./use-effect-once-if";
+
/**
* Use this when you need to virtualize a 1-dimensional list of items with variable heights.
* You can override the default wrapper element (just a div) by passing a `Wrapper` prop.
@@ -22,6 +24,7 @@ export function VirtualizedList({
scrollTo?: number;
}) {
const parentRef = useRef(null);
+ const isMounted = useMounted();
const virtualizer = useVirtualizer({
count: children.length,
@@ -32,14 +35,16 @@ export function VirtualizedList({
const items = virtualizer.getVirtualItems();
- useMount(() => {
- if (scrollTo && scrollTo < children.length) {
- // we need to wait for dynamic measurements to be taken before scrolling
- window.requestAnimationFrame(() => {
- virtualizer.scrollToIndex(scrollTo, { align: "center" });
- });
- }
- });
+ useEffectOnceIf(
+ () => {
+ if (scrollTo && scrollTo < children.length) {
+ window.requestAnimationFrame(() => {
+ virtualizer.scrollToIndex(scrollTo, { align: "center" });
+ });
+ }
+ },
+ isMounted && scrollTo !== undefined && scrollTo !== -1,
+ );
return (
<div
diff --git a/frontend/src/metabase/common/components/VirtualizedList/use-effect-once-if.ts b/frontend/src/metabase/common/components/VirtualizedList/use-effect-once-if.ts
new file mode 100644
index 00000000000..e96450e4723
--- /dev/null
+++ b/frontend/src/metabase/common/components/VirtualizedList/use-effect-once-if.ts
@@ -0,0 +1,19 @@
+import type { EffectCallback } from "react";
+import { useEffect, useRef } from "react";
+
+export function useEffectOnceIf(
+ effect: EffectCallback,
+ condition: boolean,
+): void {
+ const hasRunRef = useRef(false);
+
+ useEffect(() => {
+ // Stop if we already called `effect` before or if the condition is not met
+ if (hasRunRef.current || !condition) {
+ return;
+ }
+
+ hasRunRef.current = true;
+ return effect();
+ }, [effect, condition]);
+}
diff --git a/frontend/src/metabase/common/components/VirtualizedList/use-effect-once-if.unit.spec.tsx b/frontend/src/metabase/common/components/VirtualizedList/use-effect-once-if.unit.spec.tsx
new file mode 100644
index 00000000000..73d5842ef78
--- /dev/null
+++ b/frontend/src/metabase/common/components/VirtualizedList/use-effect-once-if.unit.spec.tsx
@@ -0,0 +1,83 @@
+import { renderHook } from "@testing-library/react";
+
+import { useEffectOnceIf } from "./use-effect-once-if";
+
+describe("useEffectOnceIf", () => {
+ it("should not call effect when condition is false", () => {
+ const effect = jest.fn();
+
+ renderHook(() => useEffectOnceIf(effect, false));
+
+ expect(effect).not.toHaveBeenCalled();
+ });
+
+ it("should call effect when condition is true", () => {
+ const effect = jest.fn();
+
+ renderHook(() => useEffectOnceIf(effect, true));
+
+ expect(effect).toHaveBeenCalledTimes(1);
+ });
+
+ it("should only call effect once even if re-rendered with condition still true", () => {
+ const effect = jest.fn();
+
+ const { rerender } = renderHook(() => useEffectOnceIf(effect, true));
+
+ expect(effect).toHaveBeenCalledTimes(1);
+
+ rerender();
+ rerender();
+
+ expect(effect).toHaveBeenCalledTimes(1);
+ });
+
+ it("should call effect once when condition changes from false to true", () => {
+ const effect = jest.fn();
+
+ const { rerender } = renderHook(
+ ({ condition }) => useEffectOnceIf(effect, condition),
+ { initialProps: { condition: false } },
+ );
+
+ expect(effect).not.toHaveBeenCalled();
+
+ rerender({ condition: true });
+
+ expect(effect).toHaveBeenCalledTimes(1);
+
+ rerender({ condition: true });
+
+ expect(effect).toHaveBeenCalledTimes(1);
+ });
+
+ it("should call effect only when condition becomes true for the first time", () => {
+ const effect = jest.fn();
+
+ const { rerender } = renderHook(
+ ({ condition }) => useEffectOnceIf(effect, condition),
+ { initialProps: { condition: true } },
+ );
+
+ expect(effect).toHaveBeenCalledTimes(1);
+
+ rerender({ condition: false });
+ rerender({ condition: true });
+
+ expect(effect).toHaveBeenCalledTimes(1);
+ });
+
+ it("should call cleanup function if effect returns one", () => {
+ const cleanup = jest.fn();
+ const effect = jest.fn(() => cleanup);
+
+ const { unmount } = renderHook(() => useEffectOnceIf(effect, true));
+
+ expect(effect).toHaveBeenCalledTimes(1);
+ expect(cleanup).not.toHaveBeenCalled();
+
+ unmount();
+
+ expect(cleanup).toHaveBeenCalledTimes(1);
+ });
+});