Skip to content

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

Loading Skeletons in Entity Picker (#78160)

Nick Fitzpatrickd188001d3a59
diff --git a/frontend/src/metabase/common/components/DelayedLoading/DelayedLoading.tsx b/frontend/src/metabase/common/components/DelayedLoading/DelayedLoading.tsx
new file mode 100644
index 00000000000..f29cab3a646
--- /dev/null
+++ b/frontend/src/metabase/common/components/DelayedLoading/DelayedLoading.tsx
@@ -0,0 +1,76 @@
+import { useMemo, useState } from "react";
+import { useMount } from "react-use";
+import _ from "underscore";
+
+import { Flex, Loader, Skeleton, Stack, Text } from "metabase/ui";
+
+export const LoadingSpinner = ({ text }: { text?: string }) => (
+ <Flex align="center" justify="center" h="100%" gap="md">
+ <Loader size="lg" />
+ {!!text && <Text color="text-secondary">{text}</Text>}
+ </Flex>
+);
+
+// sometimes showing a loading spinner can make things feel slow. This loading spinner
+// will only appear if the component is still loading after a certain delay
+export const DelayedLoadingSpinner = ({
+ text,
+ delay = 100,
+}: {
+ text?: string;
+ delay?: number;
+}) => {
+ const [show, setShow] = useState(false);
+
+ useMount(() => {
+ const timeout = setTimeout(() => {
+ setShow(true);
+ }, delay);
+ return () => clearTimeout(timeout);
+ });
+
+ if (!show) {
+ // make tests aware that things are loading
+ return <span data-testid="loading-indicator" />;
+ }
+
+ return <LoadingSpinner text={text} />;
+};
+
+// Uneven label widths keep the skeleton rows from looking mechanically uniform.
+const SKELETON_ROW_WIDTHS = [
+ "70%",
+ "55%",
+ "80%",
+ "45%",
+ "65%",
+ "75%",
+ "50%",
+ "60%",
+];
+
+export const DelayedSkeleton = ({ delay = 300 }: { delay?: number }) => {
+ const [show, setShow] = useState(false);
+ const widths = useMemo(() => _.shuffle(SKELETON_ROW_WIDTHS), []);
+
+ useMount(() => {
+ const timeout = setTimeout(() => setShow(true), delay);
+ return () => clearTimeout(timeout);
+ });
+
+ if (!show) {
+ // make tests aware that things are loading
+ return <span data-testid="loading-indicator" />;
+ }
+
+ return (
+ <Stack gap="md" data-testid="loading-indicator">
+ {widths.map((width, index) => (
+ <Flex key={index} align="center" gap="sm">
+ <Skeleton height="1rem" width="1rem" radius="sm" />
+ <Skeleton height="0.75rem" width={width} radius="sm" />
+ </Flex>
+ ))}
+ </Stack>
+ );
+};
diff --git a/frontend/src/metabase/common/components/DelayedLoading/index.ts b/frontend/src/metabase/common/components/DelayedLoading/index.ts
new file mode 100644
index 00000000000..a2d199dd29f
--- /dev/null
+++ b/frontend/src/metabase/common/components/DelayedLoading/index.ts
@@ -0,0 +1 @@
+export * from "./DelayedLoading";
diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemListLoader.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemListLoader.tsx
new file mode 100644
index 00000000000..2d1659d750b
--- /dev/null
+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemListLoader.tsx
@@ -0,0 +1,10 @@
+import { t } from "ttag";
+
+import { DelayedSkeleton } from "metabase/common/components/DelayedLoading";
+import { Box } from "metabase/ui";
+
+export const ItemListLoader = () => (
+ <Box w={365} h="100%" p="1rem" aria-label={t`Loading...`}>
+ <DelayedSkeleton />
+ </Box>
+);
diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemListLoader.unit.spec.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemListLoader.unit.spec.tsx
new file mode 100644
index 00000000000..3c5f85a1e06
--- /dev/null
+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemListLoader.unit.spec.tsx
@@ -0,0 +1,26 @@
+import { act, render, screen } from "__support__/ui";
+
+import { ItemListLoader } from "./ItemListLoader";
+
+describe("ItemListLoader (metabase#UXW-4025)", () => {
+ beforeAll(() => {
+ jest.useFakeTimers();
+ });
+
+ afterAll(() => {
+ jest.useRealTimers();
+ });
+ it("shows loading skeletons after a short delay instead of a spinner", () => {
+ render(<ItemListLoader />);
+
+ // The loading indicator is present the whole time
+ expect(screen.getByTestId("loading-indicator")).toBeEmptyDOMElement();
+
+ act(() => {
+ jest.advanceTimersByTime(300);
+ });
+
+ // Once the delay elapses, skeleton rows fill the indicator.
+ expect(screen.getByTestId("loading-indicator")).not.toBeEmptyDOMElement();
+ });
+});
diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/ItemList.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/ItemList.tsx
index 86acf84a754..79669750a35 100644
--- a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/ItemList.tsx
+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/ItemList.tsx
@@ -21,7 +21,7 @@ import {
import { useOmniPickerContext } from "../../context";
import type { OmniPickerItem } from "../../types";
import { isSelectedItem, useGetEntityPickerIcon } from "../../utils";
-import { ItemListLoader } from "../LoadingSpinner";
+import { ItemListLoader } from "../ItemListLoader";
const PickerColumn = ({
children,
diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/SearchResults.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/SearchResults.tsx
index 7c67893653a..6d410cd679c 100644
--- a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/SearchResults.tsx
+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/ItemLists/SearchResults.tsx
@@ -1,5 +1,6 @@
import { t } from "ttag";
+import { DelayedSkeleton } from "metabase/common/components/DelayedLoading";
import { EmptyState } from "metabase/common/components/EmptyState";
import { EntityIcon } from "metabase/common/components/EntityIcon";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
@@ -46,10 +47,18 @@ export const SearchResults = ({
const selectedItem = path?.[path.length - 1];
const getEntityPickerIcon = useGetEntityPickerIcon();
- if (isLoading || error) {
+ if (error) {
return (
<Box h="100%" w="40rem">
- <LoadingAndErrorWrapper loading={isLoading} error={error} />
+ <LoadingAndErrorWrapper error={error} />
+ </Box>
+ );
+ }
+
+ if (isLoading) {
+ return (
+ <Box h="100%" w="40rem" p="md">
+ <DelayedSkeleton />
</Box>
);
}
diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/LoadingSpinner.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/LoadingSpinner.tsx
deleted file mode 100644
index e14b1edede6..00000000000
--- a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/LoadingSpinner.tsx
+++ /dev/null
@@ -1,46 +0,0 @@
-import { useState } from "react";
-import { useMount } from "react-use";
-import { t } from "ttag";
-
-import { Box, Center, Flex, Loader, Text } from "metabase/ui";
-
-export const LoadingSpinner = ({ text }: { text?: string }) => (
- <Flex align="center" justify="center" h="100%" gap="md">
- <Loader size="lg" />
- {!!text && <Text color="text-secondary">{text}</Text>}
- </Flex>
-);
-
-// sometimes showing a loading spinner can make things feel slow. This loading spinner
-// will only appear if the component is still loading after a certain delay
-export const DelayedLoadingSpinner = ({
- text,
- delay = 100,
-}: {
- text?: string;
- delay?: number;
-}) => {
- const [show, setShow] = useState(false);
-
- useMount(() => {
- const timeout = setTimeout(() => {
- setShow(true);
- }, delay);
- return () => clearTimeout(timeout);
- });
-
- if (!show) {
- // make tests aware that things are loading
- return <span data-testid="loading-indicator" />;
- }
-
- return <LoadingSpinner text={text} />;
-};
-
-export const ItemListLoader = () => (
- <Box w={365} h="100%" aria-label={t`Loading...`}>
- <Center p="lg" h="100%">
- <DelayedLoadingSpinner delay={300} />
- </Center>
- </Box>
-);
diff --git a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/index.tsx b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/index.tsx
index 243c500aeca..cc168e90172 100644
--- a/frontend/src/metabase/common/components/Pickers/EntityPicker/components/index.tsx
+++ b/frontend/src/metabase/common/components/Pickers/EntityPicker/components/index.tsx
@@ -1,5 +1,5 @@
export * from "./AutoScrollBox";
export * from "./EntityPickerModal";
export * from "./ItemLists";
-export * from "./LoadingSpinner";
+export * from "./ItemListLoader";
export * from "./NestedItemPicker";
diff --git a/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.tsx b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.tsx
index c6e54f96545..2203dc25c62 100644
--- a/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.tsx
+++ b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.tsx
@@ -2,7 +2,7 @@ import { useCallback } from "react";
import { t } from "ttag";
import { useGetNativeDatasetQuery } from "metabase/api";
-import { DelayedLoadingSpinner } from "metabase/common/components/Pickers/EntityPicker/components/LoadingSpinner";
+import { DelayedLoadingSpinner } from "metabase/common/components/DelayedLoading";
import { getEngineNativeType } from "metabase/databases/utils/engine";
import { Box, Button, Flex, Icon, rem } from "metabase/ui";
import * as Lib from "metabase-lib";