Skip to content

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

indicate dirty transforms nodes (#76301)

Aleksandr Lesnenkoe4f821e59318
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncedCollectionsSidebarSection/CollectionSyncStatusBadge.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncedCollectionsSidebarSection/CollectionSyncStatusBadge.tsx
index f4414e49750..a98f0ca29e0 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncedCollectionsSidebarSection/CollectionSyncStatusBadge.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncedCollectionsSidebarSection/CollectionSyncStatusBadge.tsx
@@ -10,6 +10,7 @@ export const CollectionSyncStatusBadge = () => (
h="0.5rem"
w="0.5rem"
mr="xs"
+ flex="0 0 auto"
data-testid="remote-sync-status"
/>
</Tooltip>
diff --git a/frontend/src/metabase/transforms/pages/TransformListPage/TransformListPage.tsx b/frontend/src/metabase/transforms/pages/TransformListPage/TransformListPage.tsx
index 954c4ee7579..01fbdc7ac7c 100644
--- a/frontend/src/metabase/transforms/pages/TransformListPage/TransformListPage.tsx
+++ b/frontend/src/metabase/transforms/pages/TransformListPage/TransformListPage.tsx
@@ -55,6 +55,7 @@ import { type TreeNode, getCollectionNodeId, isCollectionNode } from "./types";
import {
buildTreeData,
getDefaultExpandedIds,
+ useGetNodeSyncColor,
useGetTransformWarnings,
} from "./utils";
@@ -144,6 +145,7 @@ export const TransformListPage = ({
);
const warningsByTransformId = useGetTransformWarnings(transforms);
+ const getNodeSyncColor = useGetNodeSyncColor();
const treeData = useMemo(() => {
const data = buildTreeData(collections, transforms);
@@ -192,6 +194,7 @@ export const TransformListPage = ({
row,
hasPythonTransformsFeature,
warningsByTransformId,
+ syncColor: getNodeSyncColor(row.original),
}),
},
{
@@ -265,7 +268,7 @@ export const TransformListPage = ({
) : null,
},
];
- }, [hasPythonTransformsFeature, warningsByTransformId]);
+ }, [hasPythonTransformsFeature, warningsByTransformId, getNodeSyncColor]);
const getRowHref = useCallback((row: Row<TreeNode>) => {
if (isRowDisabled(row)) {
@@ -375,10 +378,12 @@ function getNameCell({
row,
hasPythonTransformsFeature,
warningsByTransformId,
+ syncColor,
}: {
row: Row<TreeNode>;
hasPythonTransformsFeature: boolean;
warningsByTransformId: Map<number, string>;
+ syncColor: ColorName | undefined;
}) {
const getTooltipProps = (message: string | undefined) => {
if (!message) {
@@ -409,13 +414,17 @@ function getNameCell({
row.original.nodeType === "library" && !hasPythonTransformsFeature;
const hasWarning = !!getWarningMessage();
+ const iconColor = hasWarning
+ ? "warning"
+ : (syncColor ?? getNodeIconColor(row.original));
return (
<Group gap="sm" wrap="nowrap" miw={0}>
<EntityNameCell
data-testid="tree-node-name"
icon={hasWarning ? "warning" : row.original.icon}
- iconColor={hasWarning ? "warning" : getNodeIconColor(row.original)}
+ iconColor={iconColor}
+ nameColor={syncColor}
name={row.original.name}
ellipsifiedProps={{ ...getTooltipProps(getWarningMessage()) }}
/>
diff --git a/frontend/src/metabase/transforms/pages/TransformListPage/utils.ts b/frontend/src/metabase/transforms/pages/TransformListPage/utils.ts
index 5e15b136437..2c092e94dd6 100644
--- a/frontend/src/metabase/transforms/pages/TransformListPage/utils.ts
+++ b/frontend/src/metabase/transforms/pages/TransformListPage/utils.ts
@@ -1,18 +1,28 @@
-import { useMemo, useRef } from "react";
+import { useCallback, useMemo, useRef } from "react";
import { t } from "ttag";
import { getCollectionIcon } from "metabase/common/collections/utils";
+import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";
import { useSelector } from "metabase/redux";
import { getMetadata } from "metabase/selectors/metadata";
import { getLibQuery } from "metabase/transforms/utils";
+import type { ColorName } from "metabase/ui/colors/types";
import * as Lib from "metabase-lib";
import type Metadata from "metabase-lib/v1/metadata/Metadata";
-import type { Collection, CollectionId, Transform } from "metabase-types/api";
+import type {
+ Collection,
+ CollectionId,
+ RemoteSyncEntity,
+ RemoteSyncEntityModel,
+ RemoteSyncEntityStatus,
+ Transform,
+} from "metabase-types/api";
import {
type TreeNode,
getCollectionNodeId,
getTransformNodeId,
+ isCollectionNode,
} from "./types";
export function getIncrementalWarning(
@@ -170,3 +180,114 @@ export function getDefaultExpandedIds(
return expandedIds;
}
+
+export function getDescendantCollectionIds(node: TreeNode): Set<number> {
+ const ids = new Set<number>();
+ const visit = (current: TreeNode) => {
+ if (isCollectionNode(current)) {
+ ids.add(current.collection.id as number);
+ }
+ current.children?.forEach(visit);
+ };
+ visit(node);
+ return ids;
+}
+
+const TRANSFORM_MODEL = "transform" satisfies RemoteSyncEntityModel;
+const COLLECTION_MODEL = "collection" satisfies RemoteSyncEntityModel;
+const PYTHON_LIBRARY_MODEL = "pythonlibrary" satisfies RemoteSyncEntityModel;
+
+const SYNC_STATUS_COLOR: Record<RemoteSyncEntityStatus, ColorName> = {
+ create: "success",
+ update: "warning",
+ touch: "warning",
+ delete: "danger",
+ removed: "danger",
+};
+
+export function getSyncColorForEntities(
+ entities: RemoteSyncEntity[],
+): ColorName | undefined {
+ const colors = new Set(
+ entities.map((entity) => SYNC_STATUS_COLOR[entity.sync_status]),
+ );
+ if (colors.size === 0) {
+ return undefined;
+ }
+ if (colors.size === 1) {
+ const [color] = colors;
+ return color;
+ }
+ return SYNC_STATUS_COLOR.update;
+}
+
+export function getFolderSyncColor(
+ subtreeEntities: RemoteSyncEntity[],
+ folderCollectionId: number,
+): ColorName | undefined {
+ if (subtreeEntities.length === 0) {
+ return undefined;
+ }
+ const folderEntity = subtreeEntities.find(
+ (entity) =>
+ entity.model === COLLECTION_MODEL && entity.id === folderCollectionId,
+ );
+ const isNewFolder = folderEntity?.sync_status === "create";
+ return isNewFolder ? SYNC_STATUS_COLOR.create : SYNC_STATUS_COLOR.update;
+}
+
+export function useGetNodeSyncColor(): (
+ node: TreeNode,
+) => ColorName | undefined {
+ const { isVisible: isRemoteSyncVisible } =
+ PLUGIN_REMOTE_SYNC.useGitSyncVisible();
+ const { dirty } = PLUGIN_REMOTE_SYNC.useRemoteSyncDirtyState();
+
+ const { dirtyTransformById, dirtyPythonLibraries } = useMemo(() => {
+ const transformById = new Map<number, RemoteSyncEntity>();
+ const pythonLibraries: RemoteSyncEntity[] = [];
+ if (isRemoteSyncVisible) {
+ for (const entity of dirty) {
+ if (entity.model === TRANSFORM_MODEL) {
+ transformById.set(entity.id, entity);
+ } else if (entity.model === PYTHON_LIBRARY_MODEL) {
+ pythonLibraries.push(entity);
+ }
+ }
+ }
+ return {
+ dirtyTransformById: transformById,
+ dirtyPythonLibraries: pythonLibraries,
+ };
+ }, [isRemoteSyncVisible, dirty]);
+
+ return useCallback(
+ (node: TreeNode): ColorName | undefined => {
+ if (!isRemoteSyncVisible) {
+ return undefined;
+ }
+ if (node.nodeType === "transform") {
+ const entity =
+ node.transformId != null
+ ? dirtyTransformById.get(node.transformId)
+ : undefined;
+ return getSyncColorForEntities(entity ? [entity] : []);
+ }
+ if (node.nodeType === "library") {
+ return getSyncColorForEntities(dirtyPythonLibraries);
+ }
+ if (isCollectionNode(node)) {
+ const collectionIds = getDescendantCollectionIds(node);
+ const entities = dirty.filter(
+ (entity) =>
+ (entity.collection_id != null &&
+ collectionIds.has(entity.collection_id)) ||
+ (entity.model === COLLECTION_MODEL && collectionIds.has(entity.id)),
+ );
+ return getFolderSyncColor(entities, node.collection.id as number);
+ }
+ return undefined;
+ },
+ [isRemoteSyncVisible, dirty, dirtyTransformById, dirtyPythonLibraries],
+ );
+}
diff --git a/frontend/src/metabase/transforms/pages/TransformListPage/utils.unit.spec.ts b/frontend/src/metabase/transforms/pages/TransformListPage/utils.unit.spec.ts
index 6773ebd78b3..9674a13ee08 100644
--- a/frontend/src/metabase/transforms/pages/TransformListPage/utils.unit.spec.ts
+++ b/frontend/src/metabase/transforms/pages/TransformListPage/utils.unit.spec.ts
@@ -2,7 +2,13 @@ import { createMockMetadata } from "__support__/metadata";
import { getLibQuery } from "metabase/transforms/utils";
import * as Lib from "metabase-lib";
import { DEFAULT_TEST_QUERY, SAMPLE_PROVIDER } from "metabase-lib/test-helpers";
+import type {
+ RemoteSyncEntity,
+ RemoteSyncEntityStatus,
+} from "metabase-types/api";
import {
+ createMockCollection,
+ createMockRemoteSyncEntity,
createMockStructuredDatasetQuery,
createMockTemplateTag,
createMockTransform,
@@ -10,7 +16,23 @@ import {
createMockTransformTarget,
} from "metabase-types/api/mocks";
-import { buildTreeData, getIncrementalWarning } from "./utils";
+import {
+ buildTreeData,
+ getDescendantCollectionIds,
+ getFolderSyncColor,
+ getIncrementalWarning,
+ getSyncColorForEntities,
+} from "./utils";
+
+const dirtyEntity = (
+ status: RemoteSyncEntityStatus,
+ overrides: Partial<RemoteSyncEntity> = {},
+): RemoteSyncEntity =>
+ createMockRemoteSyncEntity({
+ model: "transform",
+ sync_status: status,
+ ...overrides,
+ });
jest.mock("metabase/transforms/utils", () => ({
getLibQuery: jest.fn(),
@@ -123,6 +145,120 @@ describe("buildTreeData", () => {
});
});
+describe("getDescendantCollectionIds", () => {
+ it("includes the folder's own collection id and all nested folder ids", () => {
+ const collections = [
+ createMockCollection({
+ id: 1,
+ name: "Parent",
+ children: [
+ createMockCollection({
+ id: 2,
+ name: "Child",
+ children: [createMockCollection({ id: 3, name: "Grandchild" })],
+ }),
+ ],
+ }),
+ ];
+
+ const [parentNode] = buildTreeData(collections, []);
+
+ expect(getDescendantCollectionIds(parentNode)).toEqual(new Set([1, 2, 3]));
+ });
+
+ it("ignores transform leaves and counts only collection ids", () => {
+ const collections = [
+ createMockCollection({ id: 10, name: "Folder", children: [] }),
+ ];
+ const transforms = [
+ createMockTransform({ id: 99, name: "T", collection_id: 10 }),
+ ];
+
+ const [folderNode] = buildTreeData(collections, transforms);
+
+ expect(getDescendantCollectionIds(folderNode)).toEqual(new Set([10]));
+ });
+});
+
+describe("getSyncColorForEntities", () => {
+ it("returns undefined when there are no changes", () => {
+ expect(getSyncColorForEntities([])).toBeUndefined();
+ });
+
+ it("colors a created entity green (success)", () => {
+ expect(getSyncColorForEntities([dirtyEntity("create")])).toBe("success");
+ });
+
+ it("colors an updated entity amber (warning)", () => {
+ expect(getSyncColorForEntities([dirtyEntity("update")])).toBe("warning");
+ });
+
+ it("colors a removed entity red (danger)", () => {
+ expect(getSyncColorForEntities([dirtyEntity("removed")])).toBe("danger");
+ });
+});
+
+describe("getFolderSyncColor", () => {
+ const collection = (id: number, status: RemoteSyncEntityStatus) =>
+ dirtyEntity(status, { id, model: "collection" });
+ const childTransform = (
+ collectionId: number,
+ status: RemoteSyncEntityStatus,
+ ) =>
+ dirtyEntity(status, {
+ id: 100 + collectionId,
+ collection_id: collectionId,
+ });
+
+ it("returns undefined when the subtree has no changes", () => {
+ expect(getFolderSyncColor([], 1)).toBeUndefined();
+ });
+
+ it("colors a brand-new folder green", () => {
+ expect(getFolderSyncColor([collection(1, "create")], 1)).toBe("success");
+ });
+
+ it("colors a renamed or moved folder amber, not green", () => {
+ expect(getFolderSyncColor([collection(1, "update")], 1)).toBe("warning");
+ });
+
+ it("colors an existing folder amber when it only contains new children", () => {
+ expect(getFolderSyncColor([childTransform(1, "create")], 1)).toBe(
+ "warning",
+ );
+ });
+
+ it("colors an existing folder amber when a child was removed", () => {
+ expect(getFolderSyncColor([childTransform(1, "removed")], 1)).toBe(
+ "warning",
+ );
+ });
+
+ it("keeps a brand-new folder green even with new children", () => {
+ expect(
+ getFolderSyncColor(
+ [collection(1, "create"), childTransform(1, "create")],
+ 1,
+ ),
+ ).toBe("success");
+ });
+
+ it("keeps nested brand-new folders green (new folder > new folder > new transform)", () => {
+ const subtree = [
+ collection(1, "create"),
+ collection(2, "create"),
+ childTransform(2, "create"),
+ ];
+ expect(getFolderSyncColor(subtree, 1)).toBe("success");
+ expect(
+ getFolderSyncColor(
+ [collection(2, "create"), childTransform(2, "create")],
+ 2,
+ ),
+ ).toBe("success");
+ });
+});
+
describe("getIncrementalWarning", () => {
const metadata = createMockMetadata({});
diff --git a/frontend/src/metabase/ui/components/data-display/TreeTable/EntityNameCell/EntityNameCell.tsx b/frontend/src/metabase/ui/components/data-display/TreeTable/EntityNameCell/EntityNameCell.tsx
index 06b5207d8b2..069f0081db6 100644
--- a/frontend/src/metabase/ui/components/data-display/TreeTable/EntityNameCell/EntityNameCell.tsx
+++ b/frontend/src/metabase/ui/components/data-display/TreeTable/EntityNameCell/EntityNameCell.tsx
@@ -10,6 +10,7 @@ interface EntityNameCellProps {
icon?: IconName;
name: React.ReactNode;
iconColor?: ColorName;
+ nameColor?: ColorName;
wrap?: boolean;
ellipsifiedProps?: ComponentProps<typeof Ellipsified>;
tooltipOpenDelay?: number;
@@ -20,6 +21,7 @@ export const EntityNameCell = memo(function EntityNameCell({
icon,
name,
iconColor = "core-brand",
+ nameColor,
wrap = false,
tooltipOpenDelay = 600,
ellipsifiedProps,
@@ -29,11 +31,12 @@ export const EntityNameCell = memo(function EntityNameCell({
<Group data-testid={testId} gap="sm" wrap="nowrap" miw={0}>
{icon && <Icon name={icon} c={iconColor} className={CS.flexNoShrink} />}
{wrap ? (
- <Text flex={1} miw={0}>
+ <Text c={nameColor} flex={1} miw={0}>
{name}
</Text>
) : (
<Ellipsified
+ c={nameColor}
flex={1}
miw={0}
tooltipProps={{ openDelay: tooltipOpenDelay }}