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.7 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 {
  AvatarGroup,
  AvatarGroupList,
  AvatarOverflowIndicator,
  DropdownMenuIcon,
  MENU_ITEM_SHORTCUTS,
  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 { LuBookMarked, LuPencil, LuTrash, LuUsers } from "react-icons/lu";
import { useNavigate } from "react-router";
import { Avatar, Hyperlink, New, Table } from "~/components";
import { usePermissions, useUrlParams } from "~/hooks";
import type { Group } from "~/modules/users/types";
import { path } from "~/utils/path";

type GroupsTableProps = {
  data: Group[];
  count: number;
  // The loader read SINGLE_LOAD_LIMIT rows: more may match.
  isTruncated?: boolean;
};

const GroupsTable = memo(({ data, count, isTruncated }: GroupsTableProps) => {
  const { t } = useLingui();
  const navigate = useNavigate();
  const permissions = usePermissions();
  const [params] = useUrlParams();

  const rows = data.map((row) => ({
    id: row.data.id,
    name: row.data.name,
    isEmployeeTypeGroup: row.data.isEmployeeTypeGroup,
    isCustomerTypeGroup: row.data.isCustomerTypeGroup,
    isSupplierTypeGroup: row.data.isSupplierTypeGroup,
    members: row.data.users
      .map((user) => ({
        name: user.fullName,
        avatar: user.avatarUrl
      }))
      .concat(
        row.children.map((child) => ({ name: child.data.name, avatar: null }))
      )
  }));

  const columns = useMemo<ColumnDef<(typeof rows)[number]>[]>(() => {
    return [
      {
        accessorKey: "name",
        header: t`Group Name`,
        cell: ({ row }) =>
          row.original.isEmployeeTypeGroup ||
          row.original.isCustomerTypeGroup ||
          row.original.isSupplierTypeGroup ? (
            <span>{row.original.name}</span>
          ) : (
            <Hyperlink to={path.to.group(row.original.id)}>
              {row.original.name}
            </Hyperlink>
          ),
        meta: {
          mobile: "P1",
          icon: <LuBookMarked />
        }
      },
      {
        header: t`Members`,
        // accessorKey: undefined, // makes the column unsortable
        cell: ({ row }) => (
          <AvatarGroup limit={3}>
            <AvatarGroupList>
              {row.original.members.map(
                (
                  member: { name: string | null; avatar: string | null },
                  index: number
                ) => (
                  <Avatar
                    key={index}
                    name={member.name ?? undefined}
                    title={member.name ?? undefined}
                    path={member.avatar}
                  />
                )
              )}
            </AvatarGroupList>
            <AvatarOverflowIndicator />
          </AvatarGroup>
        ),
        meta: {
          mobile: "P3",
          icon: <LuUsers />
        }
      }
    ];
  }, [t]);

  const renderContextMenu = useCallback(
    (row: (typeof rows)[number]) => {
      return (
        <>
          <MenuItem
            shortcut={MENU_ITEM_SHORTCUTS.edit}
            disabled={
              row.isEmployeeTypeGroup ||
              row.isCustomerTypeGroup ||
              row.isSupplierTypeGroup ||
              !permissions.can("update", "users")
            }
            onClick={() => {
              navigate(path.to.group(row.id));
            }}
          >
            <DropdownMenuIcon icon={<LuPencil />} />
            <Trans>Edit Group</Trans>
          </MenuItem>
          <MenuItem
            shortcut={MENU_ITEM_SHORTCUTS.delete}
            destructive
            disabled={
              row.isEmployeeTypeGroup ||
              row.isCustomerTypeGroup ||
              row.isSupplierTypeGroup ||
              !permissions.can("delete", "users")
            }
            onClick={() => {
              navigate(path.to.deleteGroup(row.id));
            }}
          >
            <DropdownMenuIcon icon={<LuTrash />} />
            <Trans>Delete Group</Trans>
          </MenuItem>
        </>
      );
    },
    [navigate, permissions]
  );

  return (
    <Table<(typeof rows)[number]>
      data={rows}
      count={count}
      isTruncated={isTruncated}
      columns={columns}
      primaryAction={
        permissions.can("create", "users") && (
          <New label={t`Group`} to={`new?${params.toString()}`} />
        )
      }
      renderContextMenu={renderContextMenu}
      title={t`Groups`}
    />
  );
});

GroupsTable.displayName = "GroupsTable";
export default GroupsTable;