Skip to content

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

3.6 KB
Raw Blame
import { t } from "ttag";

import { DateTime } from "metabase/common/components/DateTime";
import type { useGetIcon } from "metabase/hooks/use-icon";
import {
  Ellipsified,
  FixedSizeIcon,
  Flex,
  Group,
  SortableHeaderPill,
  Text,
  type TreeTableColumnDef,
} from "metabase/ui";
import { EMPTY_CELL_PLACEHOLDER } from "metabase/utils/constants";
import type { ContentDiagnosticsBaseFinding } from "metabase-types/api";

import { DiagnosticsEntityIcon } from "./DiagnosticsEntityIcon";
import { TOOLTIP_OPEN_DELAY_MS, TYPE_COLUMN_MIN_WIDTH } from "./constants";
import {
  getCollectionName,
  getEntityName,
  getEntityTypeLabel,
  getUserName,
} from "./utils";

type CommonColumns<T extends ContentDiagnosticsBaseFinding> = Record<
  "name" | "entityType" | "collectionName" | "createdBy" | "createdAt",
  TreeTableColumnDef<T>
>;

export function getCommonColumns<T extends ContentDiagnosticsBaseFinding>(
  getIcon: ReturnType<typeof useGetIcon>,
): CommonColumns<T> {
  return {
    name: {
      id: "name",
      header: t`Name`,
      enableSorting: true,
      sortDescFirst: false,
      minWidth: "auto",
      maxAutoWidth: 520,
      accessorFn: getEntityName,
      cell: ({ row }) => (
        <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>
          {getEntityName(row.original)}
        </Ellipsified>
      ),
    },
    entityType: {
      id: "entity-type",
      header: ({ column }) => (
        <Flex justify="center" flex={1}>
          <SortableHeaderPill
            name={t`Type`}
            sort={column.getIsSorted() || undefined}
          />
        </Flex>
      ),
      enableSorting: true,
      sortDescFirst: false,
      width: "auto",
      minWidth: TYPE_COLUMN_MIN_WIDTH,
      accessorFn: (finding) => getEntityTypeLabel(finding),
      cell: ({ row }) => (
        <DiagnosticsEntityIcon
          entity={row.original}
          getIcon={getIcon}
          mx="auto"
        />
      ),
    },
    collectionName: {
      id: "collection-name",
      header: t`Location`,
      enableSorting: true,
      sortDescFirst: false,
      width: "auto",
      minWidth: 120,
      maxAutoWidth: 520,
      accessorFn: (finding) => getCollectionName(finding.details.collection),
      cell: ({ row }) => {
        const collectionName = getCollectionName(
          row.original.details.collection,
        );
        return (
          <Group align="center" gap="sm" miw={0} wrap="nowrap">
            <FixedSizeIcon name="folder" />
            <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>
              {collectionName}
            </Ellipsified>
          </Group>
        );
      },
    },
    createdBy: {
      id: "created-by",
      header: t`Created by`,
      enableSorting: true,
      sortDescFirst: false,
      width: "auto",
      minWidth: 120,
      accessorFn: (finding) => getUserName(finding.details.creator),
      cell: ({ row }) => (
        <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>
          {getUserName(row.original.details.creator)}
        </Ellipsified>
      ),
    },
    createdAt: {
      id: "created-at",
      header: t`Created at`,
      enableSorting: true,
      sortDescFirst: false,
      width: "auto",
      minWidth: 150,
      accessorFn: (finding) => finding.created_at,
      cell: ({ row }) => {
        const { created_at } = row.original;
        return created_at != null ? (
          <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>
            <DateTime value={created_at} unit="day" />
          </Ellipsified>
        ) : (
          <Text c="text-secondary">{EMPTY_CELL_PLACEHOLDER}</Text>
        );
      },
    },
  };
}