Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

4.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { MENU_ITEM_SHORTCUTS, MenuIcon, MenuItem } from "@carbon/react";
import { Trans, useLingui } from "@lingui/react/macro";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useCallback, useMemo } from "react";
import {
  LuAward,
  LuCalendarClock,
  LuPencil,
  LuTrash,
  LuUsers
} from "react-icons/lu";
import { useNavigate } from "react-router";
import { EmployeeAvatarGroup, Hyperlink, New, Table } from "~/components";
import { Enumerable } from "~/components/Enumerable";
import type { InfiniteOptions } from "~/components/Table";
import { useCompanyToday, usePermissions, useUrlParams } from "~/hooks";
import type { Abilities } from "~/modules/resources/types";
import { usePeople } from "~/stores";
import { path } from "~/utils/path";

type Ability = Abilities[number];

type AbilitiesTableProps = {
  data: Abilities;
  count: number;
  infinite: InfiniteOptions;
};

const AbilitiesTable = memo((props: AbilitiesTableProps) => {
  const { data, count, infinite } = props;
  const { t } = useLingui();
  const navigate = useNavigate();
  const [params] = useUrlParams();
  const permissions = usePermissions();
  const [people] = usePeople();
  // The company's day, not the viewer's: the list spans every location.
  const todayDate = useCompanyToday();

  const employeeOptions = useMemo(
    () => people.map((person) => ({ value: person.id, label: person.name })),
    [people]
  );

  const columns = useMemo<ColumnDef<Ability>[]>(() => {
    return [
      {
        accessorKey: "name",
        header: t`Ability`,
        cell: ({ row }) => (
          <Hyperlink to={path.to.ability(row.original.id!)}>
            <Enumerable value={row.original.name} className="cursor-pointer" />
          </Hyperlink>
        ),
        meta: {
          mobile: "P1",
          icon: <LuAward />
        }
      },
      {
        accessorKey: "recertifyEveryDays",
        header: t`Recertify Every (Days)`,
        cell: ({ row }) => (
          <span className="tabular-nums">
            {row.original.recertifyEveryDays ?? t`Never`}
          </span>
        ),
        meta: {
          mobile: "P3",
          mobileLabel: true,
          icon: <LuCalendarClock />
        }
      },
      {
        id: "employees",
        header: t`Qualified Employees`,
        cell: ({ row }) => {
          // Qualified = has an ability row ∧ not expired. Presence of the row
          // is the qualification — there is no separate training gate.
          const employeeIds = (row.original.employeeAbility ?? [])
            .filter(
              (ea) => !ea.expiresAt || ea.expiresAt.slice(0, 10) > todayDate
            )
            .map((ea) => ea.employeeId);
          return employeeIds.length > 0 ? (
            <EmployeeAvatarGroup employeeIds={employeeIds} />
          ) : (
            <span className="text-muted-foreground">&mdash;</span>
          );
        },
        meta: {
          icon: <LuUsers />,
          filter: {
            type: "static",
            options: employeeOptions,
            isArray: true
          }
        }
      }
    ];
  }, [t, employeeOptions, todayDate]);

  const renderContextMenu = useCallback<(row: Ability) => JSX.Element>(
    (row) => (
      <>
        <MenuItem
          shortcut={MENU_ITEM_SHORTCUTS.view}
          onClick={() => {
            navigate(`${path.to.ability(row.id!)}?${params?.toString()}`);
          }}
        >
          <MenuIcon icon={<LuPencil />} />
          <Trans>View Ability</Trans>
        </MenuItem>
        <MenuItem
          destructive
          disabled={!permissions.can("delete", "resources")}
          onClick={() => {
            navigate(`${path.to.deleteAbility(row.id!)}?${params?.toString()}`);
          }}
        >
          <MenuIcon icon={<LuTrash />} />
          <Trans>Deactivate Ability</Trans>
        </MenuItem>
      </>
    ),
    [navigate, params, permissions]
  );

  return (
    <Table<Ability>
      data={data}
      infinite={infinite}
      columns={columns}
      count={count ?? 0}
      primaryAction={
        permissions.can("create", "resources") && (
          <New label={t`Ability`} to={`new?${params.toString()}`} />
        )
      }
      renderContextMenu={renderContextMenu}
      title={t`Abilities`}
      table="ability"
      withSavedView
    />
  );
});

AbilitiesTable.displayName = "AbilitiesTable";
export default AbilitiesTable;