Skip to content

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

refactor: create CollectionListView component that is used both by UserCollectionList and TenantCollectionList (#66937)

Nicolò Pretto55fb97170c09
diff --git a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.styled.tsx b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.styled.tsx
deleted file mode 100644
index ed332679d40..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.styled.tsx
+++ /dev/null
@@ -1,20 +0,0 @@
-import styled from "@emotion/styled";
-
-import { GridItem } from "metabase/common/components/Grid";
-
-export const ListHeader = styled.div`
- padding: 1rem 0;
-`;
-
-export const ListGridItem = styled(GridItem)`
- width: 33.33%;
-
- &:hover {
- color: var(--mb-color-brand);
- }
-`;
-
-export const CardContent = styled.div`
- display: flex;
- align-items: center;
-`;
diff --git a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.tsx b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.tsx
index ced2a60cd66..3aec8950f32 100644
--- a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantCollectionList/TenantCollectionList.tsx
@@ -1,65 +1,34 @@
import { t } from "ttag";
-import { BrowserCrumbs } from "metabase/common/components/BrowserCrumbs";
-import { Grid } from "metabase/common/components/Grid";
-import Link from "metabase/common/components/Link";
-import CS from "metabase/css/core/index.css";
+import { CollectionListView } from "metabase/common/components/CollectionListView";
import { ROOT_COLLECTION } from "metabase/entities/collections/constants";
import * as Urls from "metabase/lib/urls";
-import { Box, Card, Flex, Group, Icon, Loader } from "metabase/ui";
+import type { IconName } from "metabase/ui";
import { useListTenantsQuery } from "metabase-enterprise/api";
-import { ListGridItem, ListHeader } from "./TenantCollectionList.styled";
-
export const TenantCollectionList = () => {
const { data, isLoading } = useListTenantsQuery({ status: "active" });
const tenants = data?.data ?? [];
- return (
- <Flex direction="column" p="1.5rem" h="100%">
- <ListHeader>
- <BrowserCrumbs
- crumbs={[
- {
- title: ROOT_COLLECTION.name,
- to: Urls.collection({ id: "root", name: "" }),
- },
- { title: t`Tenant collections` },
- ]}
- />
- </ListHeader>
- <Box style={{ flexGrow: 1, overflowY: "auto" }} pr="0.5rem">
- {isLoading ? (
- <Flex justify="center" align="center" h="100%">
- <Loader size="lg" />
- </Flex>
- ) : (
- <Grid>
- {tenants.map(
- (tenant) =>
- tenant.tenant_collection_id && (
- <ListGridItem key={tenant.id} role="list-item">
- <Link to={`/collection/${tenant.tenant_collection_id}`}>
- <Card shadow="none" withBorder>
- <Group gap="xs">
- <Icon
- name="folder"
- className={CS.mr1}
- c="text-medium"
- size={18}
- />
+ const crumbs = [
+ {
+ title: ROOT_COLLECTION.name,
+ to: Urls.collection({ id: "root", name: "" }),
+ },
+ { title: t`Tenant collections` },
+ ];
- <h3>{tenant.name}</h3>
- </Group>
- </Card>
- </Link>
- </ListGridItem>
- ),
- )}
- </Grid>
- )}
- </Box>
- </Flex>
+ const items = tenants
+ .filter((tenant) => tenant.tenant_collection_id)
+ .map((tenant) => ({
+ key: tenant.id,
+ name: tenant.name,
+ icon: "folder" as const satisfies IconName,
+ link: `/collection/${tenant.tenant_collection_id}`,
+ }));
+
+ return (
+ <CollectionListView crumbs={crumbs} loading={isLoading} items={items} />
);
};
diff --git a/frontend/src/metabase/common/components/CollectionListView/CollectionListView.module.css b/frontend/src/metabase/common/components/CollectionListView/CollectionListView.module.css
new file mode 100644
index 00000000000..88f1970d5cc
--- /dev/null
+++ b/frontend/src/metabase/common/components/CollectionListView/CollectionListView.module.css
@@ -0,0 +1,11 @@
+.listGridItem {
+ width: 33.33%;
+}
+
+.card {
+ color: var(--mb-color-text-medium);
+}
+
+.card:hover {
+ color: var(--mb-color-brand);
+}
diff --git a/frontend/src/metabase/common/components/CollectionListView/CollectionListView.tsx b/frontend/src/metabase/common/components/CollectionListView/CollectionListView.tsx
new file mode 100644
index 00000000000..020bb6942cb
--- /dev/null
+++ b/frontend/src/metabase/common/components/CollectionListView/CollectionListView.tsx
@@ -0,0 +1,114 @@
+import type { CSSProperties, ComponentProps, Key, ReactNode } from "react";
+
+import { BrowserCrumbs } from "metabase/common/components/BrowserCrumbs";
+import { Grid, GridItem } from "metabase/common/components/Grid";
+import Link from "metabase/common/components/Link";
+import { PaginationControls } from "metabase/common/components/PaginationControls";
+import CS from "metabase/css/core/index.css";
+import {
+ Box,
+ Card,
+ Flex,
+ Group,
+ Icon,
+ type IconName,
+ Loader,
+ Title,
+} from "metabase/ui";
+
+import styles from "./CollectionListView.module.css";
+
+type Crumb = NonNullable<ComponentProps<typeof BrowserCrumbs>["crumbs"]>;
+
+type CollectionListItem = {
+ key: Key;
+ name: ReactNode;
+ icon: IconName;
+ link: string;
+};
+
+type PaginationProps = {
+ page: number;
+ pageSize: number;
+ total?: number;
+ itemsLength: number;
+ onNextPage: () => void;
+ onPreviousPage: () => void;
+};
+
+type CollectionListViewProps = {
+ crumbs: Crumb;
+ loading?: boolean;
+ items: CollectionListItem[];
+ containerStyle?: CSSProperties;
+ containerClassName?: string;
+ pagination?: PaginationProps;
+};
+
+export const CollectionListView = ({
+ crumbs,
+ loading = false,
+ items,
+ containerStyle,
+ containerClassName,
+ pagination,
+}: CollectionListViewProps) => {
+ return (
+ <Flex
+ direction="column"
+ p="1.5rem"
+ h="100%"
+ style={containerStyle}
+ className={containerClassName}
+ >
+ <Box py="1rem">
+ <BrowserCrumbs crumbs={crumbs} />
+ </Box>
+ <Box style={{ flexGrow: 1, overflowY: "auto" }} pr="0.5rem">
+ {loading ? (
+ <Flex justify="center" align="center" h="100%">
+ <Loader size="lg" />
+ </Flex>
+ ) : (
+ <Grid>
+ {items.map((item) => (
+ <GridItem
+ key={item.key}
+ role="list-item"
+ className={styles.listGridItem}
+ >
+ <Link to={item.link}>
+ <Card
+ shadow="none"
+ withBorder
+ className={styles.card}
+ style={{ cursor: "pointer" }}
+ >
+ <Group gap="xs">
+ <Icon name={item.icon} className={CS.mr1} size={18} />
+ <Title order={6} component="h3">
+ {item.name}
+ </Title>
+ </Group>
+ </Card>
+ </Link>
+ </GridItem>
+ ))}
+ </Grid>
+ )}
+ </Box>
+ {pagination && (
+ <Flex justify="end" className={CS.syncStatusAwarePagination}>
+ <PaginationControls
+ page={pagination.page}
+ pageSize={pagination.pageSize}
+ total={pagination.total}
+ itemsLength={pagination.itemsLength}
+ onNextPage={pagination.onNextPage}
+ onPreviousPage={pagination.onPreviousPage}
+ />
+ </Flex>
+ )}
+ </Flex>
+ );
+};
diff --git a/frontend/src/metabase/common/components/CollectionListView/index.tsx b/frontend/src/metabase/common/components/CollectionListView/index.tsx
new file mode 100644
index 00000000000..d2f382b6e53
--- /dev/null
+++ b/frontend/src/metabase/common/components/CollectionListView/index.tsx
@@ -0,0 +1,2 @@
+export { CollectionListView } from "./CollectionListView";
+export { UserCollectionList } from "../UserCollectionList";
diff --git a/frontend/src/metabase/common/components/UserCollectionList.styled.tsx b/frontend/src/metabase/common/components/UserCollectionList.styled.tsx
deleted file mode 100644
index fa4c800bd18..00000000000
--- a/frontend/src/metabase/common/components/UserCollectionList.styled.tsx
+++ /dev/null
@@ -1,21 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { GridItem } from "metabase/common/components/Grid";
-
-export const ListHeader = styled.div`
- padding: 1rem 0;
-`;
-
-export const ListGridItem = styled(GridItem)`
- width: 33.33%;
-
- &:hover {
- color: var(--mb-color-brand);
- }
-`;
-
-export const CardContent = styled.div`
- display: flex;
- align-items: center;
-`;
diff --git a/frontend/src/metabase/common/components/UserCollectionList.tsx b/frontend/src/metabase/common/components/UserCollectionList.tsx
index 255158c053c..40515f7680e 100644
--- a/frontend/src/metabase/common/components/UserCollectionList.tsx
+++ b/frontend/src/metabase/common/components/UserCollectionList.tsx
@@ -2,20 +2,14 @@ import {
STANDARD_USER_LIST_PAGE_SIZE as PAGE_SIZE,
useListUsersQuery,
} from "metabase/api";
-import { BrowserCrumbs } from "metabase/common/components/BrowserCrumbs";
-import { Grid } from "metabase/common/components/Grid";
-import Link from "metabase/common/components/Link";
-import { PaginationControls } from "metabase/common/components/PaginationControls";
+import { CollectionListView } from "metabase/common/components/CollectionListView";
import { usePagination } from "metabase/common/hooks/use-pagination";
-import CS from "metabase/css/core/index.css";
import {
PERSONAL_COLLECTIONS,
ROOT_COLLECTION,
} from "metabase/entities/collections/constants";
import * as Urls from "metabase/lib/urls";
-import { Box, Card, Flex, Group, Icon, Loader } from "metabase/ui";
-
-import { ListGridItem, ListHeader } from "./UserCollectionList.styled";
+import type { IconName } from "metabase/ui";
export const UserCollectionList = () => {
const { page, handleNextPage, handlePreviousPage } = usePagination();
@@ -28,63 +22,36 @@ export const UserCollectionList = () => {
const users = data?.data ?? [];
const total = data?.total;
- return (
- <Flex direction="column" p="1.5rem" h="100%">
- <ListHeader>
- <BrowserCrumbs
- crumbs={[
- {
- title: ROOT_COLLECTION.name,
- to: Urls.collection({ id: "root", name: "" }),
- },
- { title: PERSONAL_COLLECTIONS.name },
- ]}
- />
- </ListHeader>
- <Box style={{ flexGrow: 1, overflowY: "auto" }} pr="0.5rem">
- {isLoading ? (
- <Flex justify="center" align="center" h="100%">
- <Loader size="lg" />
- </Flex>
- ) : (
- <Grid>
- {users.map(
- (user) =>
- user.personal_collection_id && (
- <ListGridItem
- key={user.personal_collection_id}
- role="list-item"
- >
- <Link to={`/collection/${user.personal_collection_id}`}>
- <Card shadow="none" withBorder>
- <Group gap="xs">
- <Icon
- name="person"
- className={CS.mr1}
- c="text-medium"
- size={18}
- />
+ const crumbs = [
+ {
+ title: ROOT_COLLECTION.name,
+ to: Urls.collection({ id: "root", name: "" }),
+ },
+ { title: PERSONAL_COLLECTIONS.name },
+ ];
- <h3>{user.common_name}</h3>
- </Group>
- </Card>
- </Link>
- </ListGridItem>
- ),
- )}
- </Grid>
- )}
- </Box>
- <Flex justify="end" className={CS.syncStatusAwarePagination}>
- <PaginationControls
- page={page}
- pageSize={PAGE_SIZE}
- total={total}
- itemsLength={PAGE_SIZE}
- onNextPage={handleNextPage}
- onPreviousPage={handlePreviousPage}
- />
- </Flex>
- </Flex>
+ const items = users
+ .filter((user) => user.personal_collection_id)
+ .map((user) => ({
+ key: user.personal_collection_id,
+ name: user.common_name,
+ icon: "person" as const satisfies IconName,
+ link: `/collection/${user.personal_collection_id}`,
+ }));
+
+ return (
+ <CollectionListView
+ crumbs={crumbs}
+ loading={isLoading}
+ items={items}
+ pagination={{
+ page,
+ pageSize: PAGE_SIZE,
+ total,
+ itemsLength: PAGE_SIZE,
+ onNextPage: handleNextPage,
+ onPreviousPage: handlePreviousPage,
+ }}
+ />
);
};