The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Read the reference lists from their own responses (#82473)
Romeo Van Snickd46d7a47e439
diff --git a/frontend/src/metabase/reference/databases/DatabaseDetail.tsx b/frontend/src/metabase/reference/databases/DatabaseDetail.tsxindex d8fedcb1c45..e1b0ee7b04c 100644--- a/frontend/src/metabase/reference/databases/DatabaseDetail.tsx+++ b/frontend/src/metabase/reference/databases/DatabaseDetail.tsx@@ -5,7 +5,6 @@ import { t } from "ttag";import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";import CS from "metabase/css/core/index.css";-import { getShallowFields as getFields } from "metabase/metadata-store";import { connect } from "metabase/redux";import Detail from "metabase/reference/components/Detail";import { EditHeader } from "metabase/reference/components/EditHeader";@@ -32,11 +31,9 @@ const mapStateToProps = (props: ReferenceRouteProps,) => {const entity = getDatabase(state, props) || {};- const fields = getFields(state);return {entity,- metadataFields: fields,user: getUser(state),isEditing: getIsEditing(state),isFormulaExpanded: getIsFormulaExpanded(state),diff --git a/frontend/src/metabase/reference/databases/DatabaseList.tsx b/frontend/src/metabase/reference/databases/DatabaseList.tsxindex aaebd12c5e0..8ce760dd766 100644--- a/frontend/src/metabase/reference/databases/DatabaseList.tsx+++ b/frontend/src/metabase/reference/databases/DatabaseList.tsx@@ -5,31 +5,24 @@ import { t } from "ttag";import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";import { NoDatabasesEmptyState } from "metabase/common/components/NoDatabasesEmptyState";import CS from "metabase/css/core/index.css";-import { getShallowDatabases as getDatabases } from "metabase/metadata-store";-import { connect } from "metabase/redux";import { List } from "metabase/reference/components/List";import S from "metabase/reference/components/List/List.module.css";import { ListItem } from "metabase/reference/components/ListItem";-import type { NormalizedDatabase } from "metabase-types/api";+import type { Database } from "metabase-types/api";import ReferenceHeader from "../components/ReferenceHeader";-import type { StateWithReference } from "../selectors";interface DatabaseListProps {- entities: Record<string, NormalizedDatabase>;+ entities: Database[];loading?: boolean;loadingError?: unknown;}-const mapStateToProps = (state: StateWithReference) => ({- entities: getDatabases(state),-});-class DatabaseList extends Component<DatabaseListProps> {render() {const { entities, loadingError, loading } = this.props;- const databases = Object.values(entities)+ const databases = entities.filter((database) => {const exists = Boolean(database?.id && database?.name);return exists && !database.is_saved_questions;@@ -49,7 +42,7 @@ class DatabaseList extends Component<DatabaseListProps> {error={loadingError}>{() =>- Object.keys(entities).length > 0 ? (+ entities.length > 0 ? (<div className={cx(CS.wrapper, CS.wrapperTrim)}><List>{databases.map((database) => (@@ -76,4 +69,4 @@ class DatabaseList extends Component<DatabaseListProps> {}// eslint-disable-next-line import/no-default-export -- deprecated usage-export default connect(mapStateToProps)(DatabaseList);+export default DatabaseList;diff --git a/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx b/frontend/src/metabase/reference/databases/DatabaseListContainer.tsxindex 1379d2ab9f9..8aa95a9ad40 100644--- a/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx+++ b/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx@@ -23,7 +23,9 @@ function DatabaseListContainer(props: DatabaseListContainerProps) {const { pathname } = useLocation();const previousPathname = usePrevious(pathname);- const { isFetching, error } = useListDatabasesQuery({ include: "tables" });+ const { data, isFetching, error } = useListDatabasesQuery({+ include: "tables",+ });useEffect(() => {const pathnameChanged =@@ -38,7 +40,11 @@ function DatabaseListContainer(props: DatabaseListContainerProps) {className={cx(CS.flexFull, CS.relative)}sidebar={<BaseSidebar />}>- <DatabaseList loading={isFetching} loadingError={error} />+ <DatabaseList+ entities={data?.data ?? []}+ loading={isFetching}+ loadingError={error}+ /></SidebarLayout>);}diff --git a/frontend/src/metabase/reference/databases/TableDetail.tsx b/frontend/src/metabase/reference/databases/TableDetail.tsxindex b21a0df9af4..b8c2bbac861 100644--- a/frontend/src/metabase/reference/databases/TableDetail.tsx+++ b/frontend/src/metabase/reference/databases/TableDetail.tsx@@ -5,10 +5,7 @@ import { t } from "ttag";import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";import CS from "metabase/css/core/index.css";-import {- getShallowFields as getFields,- selectMetadataProvider,-} from "metabase/metadata-store";+import { selectMetadataProvider } from "metabase/metadata-store";import { connect } from "metabase/redux";import S from "metabase/reference/Reference.module.css";import Detail from "metabase/reference/components/Detail";@@ -69,12 +66,10 @@ const mapStateToProps = (props: ReferenceRouteProps,) => {const entity = getTable(state, props) || {};- const fields = getFields(state);return {entity,table: getTable(state, props),- metadataFields: fields,metadataProvider: selectMetadataProvider(state,getTable(state, props)?.db_id ?? null,diff --git a/frontend/src/metabase/reference/fetch-data.ts b/frontend/src/metabase/reference/fetch-data.tsindex da73e59f1ea..e56c2c388c7 100644--- a/frontend/src/metabase/reference/fetch-data.ts+++ b/frontend/src/metabase/reference/fetch-data.ts@@ -21,9 +21,6 @@ const fetchQuestions = (dispatch: Dispatch) =>export const fetchTableData = (dispatch: Dispatch, tableId: TableId) =>dispatch(fetchTableMetadataAndForeignKeys({ id: tableId }));-export const fetchSegmentListData = (dispatch: Dispatch) =>- dispatch(fetchSegments());-export const fetchSegmentDetailData = (dispatch: Dispatch,segmentId: SegmentId,diff --git a/frontend/src/metabase/reference/segments/SegmentDetail.tsx b/frontend/src/metabase/reference/segments/SegmentDetail.tsxindex a39b7860bc7..b57a115b0cc 100644--- a/frontend/src/metabase/reference/segments/SegmentDetail.tsx+++ b/frontend/src/metabase/reference/segments/SegmentDetail.tsx@@ -7,10 +7,7 @@ import { Link } from "metabase/common/components/Link";import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";import { modelIconMap } from "metabase/common/utils/icon";import CS from "metabase/css/core/index.css";-import {- getShallowFields as getFields,- selectMetadataProvider,-} from "metabase/metadata-store";+import { selectMetadataProvider } from "metabase/metadata-store";import { connect } from "metabase/redux";import Detail from "metabase/reference/components/Detail";import { EditHeader } from "metabase/reference/components/EditHeader";@@ -77,12 +74,10 @@ const mapStateToProps = (props: ReferenceRouteProps,) => {const entity = getSegment(state, props) || {};- const fields = getFields(state);return {entity,table: getTable(state, props),- metadataFields: fields,metadataProvider: selectMetadataProvider(state,getTable(state, props)?.db_id ?? null,diff --git a/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx b/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsxindex 1f3bd33cd7e..993fe7e9462 100644--- a/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx+++ b/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx@@ -2,11 +2,11 @@ import cx from "classnames";import type { CSSProperties } from "react";import { t } from "ttag";+import { useListSegmentsQuery } from "metabase/api";import { AdminAwareEmptyState } from "metabase/common/components/AdminAwareEmptyState";import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";import { modelIconMap } from "metabase/common/utils/icon";import CS from "metabase/css/core/index.css";-import { getShallowSegments } from "metabase/metadata-store";import { useSelector } from "metabase/redux";import { List } from "metabase/reference/components/List";import S from "metabase/reference/components/List/List.module.css";@@ -33,17 +33,11 @@ const emptyStateData = {};interface SegmentListProps {- loading?: boolean;- loadingError?: unknown;style?: CSSProperties;}-export function SegmentList({- style,- loading,- loadingError,-}: SegmentListProps) {- const entities = useSelector(getShallowSegments);+export function SegmentList({ style }: SegmentListProps) {+ const { data: segments = [], isLoading, error } = useListSegmentsQuery();const adminLink = useSelector((state) =>getDocsUrl(state, {page: "data-modeling/segments",@@ -54,28 +48,20 @@ export function SegmentList({return (<div style={style} className={CS.full}><ReferenceHeader name={t`Segments`} />- <LoadingAndErrorWrapper- loading={!loadingError && loading}- error={loadingError}- >+ <LoadingAndErrorWrapper loading={!error && isLoading} error={error}>{() =>- Object.keys(entities).length > 0 ? (+ segments.length > 0 ? (<div className={cx(CS.wrapper, CS.wrapperTrim)}><List>- {Object.values(entities).map(- (entity) =>- entity &&- entity.id &&- entity.name && (- <ListItem- key={entity.id}- name={entity.name}- description={entity.description}- url={`/reference/segments/${entity.id}`}- icon={modelIconMap.segment}- />- ),- )}+ {segments.map((segment) => (+ <ListItem+ key={segment.id}+ name={segment.name}+ description={segment.description}+ url={`/reference/segments/${segment.id}`}+ icon={modelIconMap.segment}+ />+ ))}</List></div>) : (diff --git a/frontend/src/metabase/reference/segments/SegmentList/tests/common.unit.spec.ts b/frontend/src/metabase/reference/segments/SegmentList/tests/common.unit.spec.tsindex e8e6939a4c8..7a72af5651d 100644--- a/frontend/src/metabase/reference/segments/SegmentList/tests/common.unit.spec.ts+++ b/frontend/src/metabase/reference/segments/SegmentList/tests/common.unit.spec.ts@@ -9,8 +9,8 @@ describe("SegmentList (OSS)", () => {is_superuser: true,});- it("should render help link when `show-metabase-links: true`", () => {- setup({ user: adminUser, showMetabaseLinks: true });+ it("should render help link when `show-metabase-links: true`", async () => {+ await setup({ user: adminUser, showMetabaseLinks: true });expect(screen.getByText("Segments are interesting subsets of tables"),@@ -20,8 +20,8 @@ describe("SegmentList (OSS)", () => {).toBeInTheDocument();});- it("should render help link when `show-metabase-links: false`", () => {- setup({ user: adminUser, showMetabaseLinks: false });+ it("should render help link when `show-metabase-links: false`", async () => {+ await setup({ user: adminUser, showMetabaseLinks: false });expect(screen.getByText("Segments are interesting subsets of tables"),@@ -35,8 +35,8 @@ describe("SegmentList (OSS)", () => {describe("Non-admins", () => {const user = createMockUser();- it("should not render help link", () => {- setup({ user });+ it("should not render help link", async () => {+ await setup({ user });expect(screen.getByText("Segments are interesting subsets of tables"),diff --git a/frontend/src/metabase/reference/segments/SegmentList/tests/enterprise.unit.spec.ts b/frontend/src/metabase/reference/segments/SegmentList/tests/enterprise.unit.spec.tsindex 928be4e4d17..07080a21827 100644--- a/frontend/src/metabase/reference/segments/SegmentList/tests/enterprise.unit.spec.ts+++ b/frontend/src/metabase/reference/segments/SegmentList/tests/enterprise.unit.spec.ts@@ -5,7 +5,7 @@ import type { SetupOpts } from "./setup";import { setup as baseSetup } from "./setup";function setup(opts: SetupOpts) {- baseSetup({+ return baseSetup({...opts,});}@@ -16,8 +16,8 @@ describe("SegmentList (EE without token)", () => {is_superuser: true,});- it("should render help link when `show-metabase-links: true`", () => {- setup({ user: adminUser, showMetabaseLinks: true });+ it("should render help link when `show-metabase-links: true`", async () => {+ await setup({ user: adminUser, showMetabaseLinks: true });expect(screen.getByText("Segments are interesting subsets of tables"),@@ -27,8 +27,8 @@ describe("SegmentList (EE without token)", () => {).toBeInTheDocument();});- it("should render help link when `show-metabase-links: false`", () => {- setup({ user: adminUser, showMetabaseLinks: false });+ it("should render help link when `show-metabase-links: false`", async () => {+ await setup({ user: adminUser, showMetabaseLinks: false });expect(screen.getByText("Segments are interesting subsets of tables"),@@ -42,8 +42,8 @@ describe("SegmentList (EE without token)", () => {describe("Non-admins", () => {const user = createMockUser();- it("should not render help link", () => {- setup({ user });+ it("should not render help link", async () => {+ await setup({ user });expect(screen.getByText("Segments are interesting subsets of tables"),diff --git a/frontend/src/metabase/reference/segments/SegmentList/tests/premium.unit.spec.ts b/frontend/src/metabase/reference/segments/SegmentList/tests/premium.unit.spec.tsindex febe127925c..199ef39157a 100644--- a/frontend/src/metabase/reference/segments/SegmentList/tests/premium.unit.spec.ts+++ b/frontend/src/metabase/reference/segments/SegmentList/tests/premium.unit.spec.ts@@ -5,7 +5,7 @@ import type { SetupOpts } from "./setup";import { setup as baseSetup } from "./setup";function setup(opts: SetupOpts) {- baseSetup({+ return baseSetup({tokenFeatures: { whitelabel: true },enterprisePlugins: ["whitelabel"],...opts,@@ -18,8 +18,8 @@ describe("SegmentList (EE with token)", () => {is_superuser: true,});- it("should render help link when `show-metabase-links: true`", () => {- setup({ user: adminUser, showMetabaseLinks: true });+ it("should render help link when `show-metabase-links: true`", async () => {+ await setup({ user: adminUser, showMetabaseLinks: true });expect(screen.getByText("Segments are interesting subsets of tables"),@@ -29,8 +29,8 @@ describe("SegmentList (EE with token)", () => {).toBeInTheDocument();});- it("should not render help link when `show-metabase-links: false`", () => {- setup({ user: adminUser, showMetabaseLinks: false });+ it("should not render help link when `show-metabase-links: false`", async () => {+ await setup({ user: adminUser, showMetabaseLinks: false });expect(screen.getByText("Segments are interesting subsets of tables"),@@ -44,8 +44,8 @@ describe("SegmentList (EE with token)", () => {describe("Non-admins", () => {const user = createMockUser();- it("should not render help link", () => {- setup({ user });+ it("should not render help link", async () => {+ await setup({ user });expect(screen.getByText("Segments are interesting subsets of tables"),diff --git a/frontend/src/metabase/reference/segments/SegmentList/tests/setup.tsx b/frontend/src/metabase/reference/segments/SegmentList/tests/setup.tsxindex 8d79726e70f..ab2f4f46fe1 100644--- a/frontend/src/metabase/reference/segments/SegmentList/tests/setup.tsx+++ b/frontend/src/metabase/reference/segments/SegmentList/tests/setup.tsx@@ -1,8 +1,9 @@import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";+import { setupSegmentsEndpoints } from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { createMockState } from "__support__/state";-import { renderWithProviders } from "__support__/ui";-import type { TokenFeatures, User } from "metabase-types/api";+import { renderWithProviders, waitForLoaderToBeRemoved } from "__support__/ui";+import type { Segment, TokenFeatures, User } from "metabase-types/api";import {createMockTokenFeatures,createMockUser,@@ -12,17 +13,21 @@ import { SegmentList } from "../SegmentList";export interface SetupOpts {user: User;+ segments?: Segment[];showMetabaseLinks?: boolean;tokenFeatures?: Partial<TokenFeatures>;enterprisePlugins?: Parameters<typeof setupEnterpriseOnlyPlugin>[0][];}-export const setup = ({+export const setup = async ({user,+ segments = [],showMetabaseLinks = true,tokenFeatures = {},enterprisePlugins = [],}: SetupOpts) => {+ setupSegmentsEndpoints(segments);+const state = createMockState({currentUser: createMockUser(user),settings: mockSettings({@@ -36,4 +41,5 @@ export const setup = ({});renderWithProviders(<SegmentList />, { storeInitialState: state });+ await waitForLoaderToBeRemoved();};diff --git a/frontend/src/metabase/reference/segments/SegmentListContainer.tsx b/frontend/src/metabase/reference/segments/SegmentListContainer.tsxindex d096ee3ccd7..c580183c8d4 100644--- a/frontend/src/metabase/reference/segments/SegmentListContainer.tsx+++ b/frontend/src/metabase/reference/segments/SegmentListContainer.tsx@@ -3,13 +3,11 @@ import { useEffect } from "react";import { usePrevious } from "react-use";import CS from "metabase/css/core/index.css";-import { connect, useDispatch, useSelector } from "metabase/redux";+import { connect, useSelector } from "metabase/redux";import { SidebarLayout } from "metabase/reference/components/SidebarLayout";-import { fetchSegmentListData } from "metabase/reference/fetch-data";import BaseSidebar from "metabase/reference/guide/BaseSidebar";import * as actions from "metabase/reference/reference";import { SegmentList } from "metabase/reference/segments/SegmentList";-import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";import { useLocation } from "metabase/router";import type { ClearStateProps } from "../reference";@@ -25,13 +23,8 @@ function SegmentListContainer(props: SegmentListContainerProps) {const { pathname } = useLocation();const previousPathname = usePrevious(pathname);- const dispatch = useDispatch();const isEditing = useSelector(getIsEditing);- const { loading, loadingError } = useReferenceFetch(() =>- fetchSegmentListData(dispatch),- );-useEffect(() => {const pathnameChanged =previousPathname !== undefined && previousPathname !== pathname;@@ -46,7 +39,7 @@ function SegmentListContainer(props: SegmentListContainerProps) {style={isEditing ? { paddingTop: "43px" } : {}}sidebar={<BaseSidebar />}>- <SegmentList loading={loading} loadingError={loadingError} />+ <SegmentList /></SidebarLayout>);}