Skip to content

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

chore: cleanup relationships which we never show (#69786)

Uladzimir Havenchyk085fc52dca85
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.tsx
index c124de1610d..e872a79c88a 100644
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.tsx
+++ b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.tsx
@@ -1,4 +1,3 @@
-import type ForeignKey from "metabase-lib/v1/metadata/ForeignKey";
import type {
DatasetColumn,
RowValue,
@@ -7,35 +6,22 @@ import type {
import { ObjectDetailBodyWrapper } from "./ObjectDetailBody.styled";
import { DetailsTable } from "./ObjectDetailsTable";
-import { Relationships } from "./ObjectRelationships";
import type { OnVisualizationClickType } from "./types";
export interface ObjectDetailBodyProps {
columns: DatasetColumn[];
- objectName: string;
zoomedRow: RowValue[];
settings: VisualizationSettings;
- hasRelationships: boolean;
onVisualizationClick: OnVisualizationClickType;
visualizationIsClickable: (clicked: unknown) => boolean;
- tableForeignKeys?: ForeignKey[];
- tableForeignKeyReferences?: {
- [key: number]: { status: number; value: number };
- };
- followForeignKey?: (fk: ForeignKey) => void;
}
export function ObjectDetailBody({
columns,
- objectName,
zoomedRow,
settings,
- hasRelationships = false,
onVisualizationClick,
visualizationIsClickable,
- tableForeignKeys,
- tableForeignKeyReferences,
- followForeignKey,
}: ObjectDetailBodyProps): JSX.Element {
return (
<ObjectDetailBodyWrapper>
@@ -46,14 +32,6 @@ export function ObjectDetailBody({
onVisualizationClick={onVisualizationClick}
visualizationIsClickable={visualizationIsClickable}
/>
- {hasRelationships && (
- <Relationships
- objectName={objectName}
- tableForeignKeys={tableForeignKeys}
- tableForeignKeyReferences={tableForeignKeyReferences}
- foreignKeyClicked={followForeignKey}
- />
- )}
</ObjectDetailBodyWrapper>
);
}
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.unit.spec.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.unit.spec.tsx
index b37c0af8255..f681b383a01 100644
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.unit.spec.tsx
+++ b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailBody.unit.spec.tsx
@@ -8,17 +8,12 @@ describe("ObjectDetailBody", () => {
render(
<ObjectDetailBody
columns={testDataset.cols}
- objectName="Large Sandstone Socks"
zoomedRow={testDataset.rows[2]}
settings={{
column: () => null,
}}
- hasRelationships={false}
onVisualizationClick={() => null}
visualizationIsClickable={() => false}
- tableForeignKeys={[]}
- tableForeignKeyReferences={{}}
- followForeignKey={() => null}
/>,
);
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.tsx
index af805821a5a..62182e1d78d 100644
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.tsx
+++ b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectDetailView.tsx
@@ -13,7 +13,6 @@ import { runQuestionQuery } from "metabase/query_builder/actions";
import { ActionsApi, MetabaseApi } from "metabase/services";
import { Modal } from "metabase/ui";
import * as Lib from "metabase-lib";
-import type ForeignKey from "metabase-lib/v1/metadata/ForeignKey";
import { isVirtualCardId } from "metabase-lib/v1/metadata/utils/saved-questions";
import { isPK } from "metabase-lib/v1/types/utils/isa";
import type {
@@ -74,7 +73,6 @@ export function ObjectDetailView({
zoomedRow: passedZoomedRow,
zoomedRowID,
tableForeignKeys,
- tableForeignKeyReferences,
settings,
canZoom,
canZoomPreviousRow,
@@ -86,7 +84,6 @@ export function ObjectDetailView({
visualizationIsClickable,
fetchTableFks,
loadObjectDetailFKReferences,
- followForeignKey,
viewPreviousObjectDetail,
viewNextObjectDetail,
closeObjectDetail,
@@ -247,15 +244,6 @@ export function ObjectDetailView({
}
}, [hasFks, data, prevData, prevTableForeignKeys, loadFKReferences]);
- const onFollowForeignKey = useCallback(
- (fk: ForeignKey) => {
- if (zoomedRowID !== undefined) {
- followForeignKey({ objectId: zoomedRowID, fk });
- }
- },
- [zoomedRowID, followForeignKey],
- );
-
const areImplicitActionsEnabled = Boolean(
question &&
question.canWrite() &&
@@ -363,15 +351,10 @@ export function ObjectDetailView({
)}
<ObjectDetailBody
columns={passedData.cols}
- objectName={objectName}
zoomedRow={zoomedRow ?? []}
settings={settings}
- hasRelationships={hasRelationships}
onVisualizationClick={onVisualizationClick}
visualizationIsClickable={visualizationIsClickable}
- tableForeignKeys={tableForeignKeys}
- tableForeignKeyReferences={tableForeignKeyReferences}
- followForeignKey={onFollowForeignKey}
/>
</ObjectDetailWrapperDiv>
)}
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.styled.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.styled.tsx
deleted file mode 100644
index 0d00be676fe..00000000000
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.styled.tsx
+++ /dev/null
@@ -1,30 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { color } from "metabase/ui/utils/colors";
-
-export const ObjectRelationships = styled.div`
- overflow-y: auto;
- flex: 0 0 100%;
- padding: 2rem;
- background-color: var(--mb-color-background-secondary);
-`;
-
-interface ObjectRelationshipContentProps {
- isClickable: boolean;
-}
-
-export const ObjectRelationContent = styled.div<ObjectRelationshipContentProps>`
- display: flex;
- align-items: center;
- margin: 1rem 0;
- padding-bottom: 1rem;
- border-bottom: 1px solid var(--mb-color-border);
- color: ${(props) =>
- color(props.isClickable ? "text-primary" : "text-secondary")};
- cursor: ${(props) => props.isClickable && "pointer"};
-
- &:hover {
- color: ${(props) => props.isClickable && "var(--mb-color-brand)"};
- }
-`;
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.tsx
deleted file mode 100644
index e7e13b2c4e5..00000000000
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.tsx
+++ /dev/null
@@ -1,133 +0,0 @@
-import cx from "classnames";
-import { inflect } from "inflection";
-import { jt, t } from "ttag";
-
-import { IconBorder } from "metabase/common/components/IconBorder";
-import CS from "metabase/css/core/index.css";
-import { foreignKeyCountsByOriginTable } from "metabase/lib/schema_metadata";
-import { Icon, Loader } from "metabase/ui";
-import type ForeignKey from "metabase-lib/v1/metadata/ForeignKey";
-
-import {
- ObjectRelationContent,
- ObjectRelationships,
-} from "./ObjectRelationships.styled";
-import type { ForeignKeyReferences } from "./types";
-
-export interface RelationshipsProps {
- objectName?: string;
- tableForeignKeys?: ForeignKey[];
- tableForeignKeyReferences?: ForeignKeyReferences;
- foreignKeyClicked?: (fk: ForeignKey) => void;
-}
-
-export function Relationships({
- objectName,
- tableForeignKeys,
- tableForeignKeyReferences,
- foreignKeyClicked,
-}: RelationshipsProps): JSX.Element | null {
- if (!tableForeignKeys || !tableForeignKeys?.length) {
- return null;
- }
-
- const fkCountsByTable = foreignKeyCountsByOriginTable(tableForeignKeys);
-
- const sortedForeignTables = tableForeignKeys.sort((a, b) =>
- (a.origin?.table?.displayName() ?? "").localeCompare(
- b.origin?.table?.displayName() ?? "",
- ),
- );
-
- return (
- <ObjectRelationships>
- <div className={cx(CS.textBold, CS.textMedium)}>
- {jt`${(
- <span className={CS.textDark} key={objectName}>
- {objectName}
- </span>
- )} is connected to:`}
- </div>
-
- <ul>
- {sortedForeignTables.map((fk) => (
- <Relationship
- key={`${fk.origin_id}-${fk.destination_id}`}
- fk={fk}
- fkCountInfo={
- fk.origin?.id != null
- ? tableForeignKeyReferences?.[Number(fk.origin.id)]
- : undefined
- }
- fkCount={
- (fk.origin?.table != null &&
- fkCountsByTable?.[fk.origin.table?.id]) ||
- 0
- }
- foreignKeyClicked={foreignKeyClicked}
- />
- ))}
- </ul>
- </ObjectRelationships>
- );
-}
-
-interface RelationshipProps {
- fk: ForeignKey;
- fkCountInfo:
- | {
- status: number;
- value: number;
- }
- | undefined;
- fkCount: number;
- foreignKeyClicked?: (fk: ForeignKey) => void;
-}
-
-function Relationship({
- fk,
- fkCountInfo,
- fkCount,
- foreignKeyClicked,
-}: RelationshipProps) {
- const fkCountValue = fkCountInfo?.value || 0;
- const isLoaded = fkCountInfo?.status === 1;
- const fkClickable = isLoaded && Boolean(fkCountInfo.value);
- const originTableName = fk.origin?.table?.displayName() ?? "";
-
- const relationName = inflect(originTableName, fkCountValue);
-
- const via =
- fkCount > 1 ? (
- <span className={cx(CS.textMedium, CS.textNormal)}>
- {" "}
- {t`via ${fk.origin?.displayName()}`}
- </span>
- ) : null;
-
- return (
- <li data-testid={`fk-relation-${originTableName.toLowerCase()}`}>
- <ObjectRelationContent
- isClickable={fkClickable}
- onClick={
- fkClickable && foreignKeyClicked
- ? () => foreignKeyClicked(fk)
- : undefined
- }
- >
- <div>
- <h2>{isLoaded ? fkCountValue : <Loader size="xs" />}</h2>
- <h5 className={CS.block}>
- {relationName}
- {via}
- </h5>
- </div>
- {fkClickable && foreignKeyClicked && (
- <IconBorder className={CS.flexAlignRight}>
- <Icon data-testid="click-icon" name="chevronright" size={10} />
- </IconBorder>
- )}
- </ObjectRelationContent>
- </li>
- );
-}
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.unit.spec.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.unit.spec.tsx
deleted file mode 100644
index 4ffb788bc2c..00000000000
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/ObjectRelationships.unit.spec.tsx
+++ /dev/null
@@ -1,108 +0,0 @@
-import { createMockEntitiesState } from "__support__/store";
-import { renderWithProviders, screen } from "__support__/ui";
-import { getMetadata } from "metabase/selectors/metadata";
-import type { Database, TableId } from "metabase-types/api";
-import { createMockForeignKey } from "metabase-types/api/mocks";
-import {
- ORDERS,
- PRODUCTS,
- PRODUCTS_ID,
- REVIEWS,
- createOrdersProductIdField,
- createOrdersTable,
- createProductsIdField,
- createProductsTable,
- createReviewsProductIdField,
- createReviewsTable,
- createSampleDatabase,
-} from "metabase-types/api/mocks/presets";
-import { createMockState } from "metabase-types/store/mocks";
-
-import { Relationships } from "./ObjectRelationships";
-import type { ForeignKeyReferences } from "./types";
-
-const SINGLE_TABLE_DATABASE = createSampleDatabase({
- tables: [createProductsTable()],
-});
-
-const MULTI_TABLE_DATABASE = createSampleDatabase({
- tables: [
- createProductsTable({
- fks: [
- createMockForeignKey({
- origin_id: ORDERS.PRODUCT_ID,
- origin: createOrdersProductIdField(),
- destination_id: PRODUCTS.ID,
- destination: createProductsIdField(),
- }),
- createMockForeignKey({
- origin_id: REVIEWS.PRODUCT_ID,
- origin: createReviewsProductIdField(),
- destination_id: PRODUCTS.ID,
- destination: createProductsIdField(),
- }),
- ],
- }),
- createOrdersTable(),
- createReviewsTable(),
- ],
-});
-
-const FOREIGN_KEY_REFERENCES = {
- [ORDERS.PRODUCT_ID]: { status: 1, value: 771 },
- [REVIEWS.PRODUCT_ID]: { status: 1, value: 881 },
-};
-
-interface SetupOpts {
- database: Database;
- tableId: TableId;
- tableForeignKeyReferences?: ForeignKeyReferences;
-}
-
-const setup = ({
- database,
- tableId,
- tableForeignKeyReferences = {},
-}: SetupOpts) => {
- const state = createMockState({
- entities: createMockEntitiesState({
- databases: [database],
- }),
- });
-
- const metadata = getMetadata(state);
- const table = metadata.table(tableId);
-
- renderWithProviders(
- <div data-testid="container">
- <Relationships
- objectName="Large Sandstone Socks"
- tableForeignKeys={table?.fks ?? []}
- tableForeignKeyReferences={tableForeignKeyReferences}
- foreignKeyClicked={() => null}
- />
- </div>,
- { storeInitialState: state },
- );
-};
-
-describe("Object Relationships", () => {
- it("renders null if no foreign keys are provided", () => {
- setup({ database: SINGLE_TABLE_DATABASE, tableId: PRODUCTS_ID });
- expect(screen.getByTestId("container")).toBeEmptyDOMElement();
- });
-
- it("renders a list of relationships", () => {
- setup({
- database: MULTI_TABLE_DATABASE,
- tableId: PRODUCTS_ID,
- tableForeignKeyReferences: FOREIGN_KEY_REFERENCES,
- });
-
- expect(screen.getByText(/Large Sandstone Socks/i)).toBeInTheDocument();
- expect(screen.getByText("771")).toBeInTheDocument();
- expect(screen.getByText(/Orders/i)).toBeInTheDocument();
- expect(screen.getByText("881")).toBeInTheDocument();
- expect(screen.getByText(/Reviews/i)).toBeInTheDocument();
- });
-});