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.8 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,
  useDisclosure
} from "@carbon/react";
import { Trans, useLingui } from "@lingui/react/macro";
import type { ColumnDef } from "@tanstack/react-table";
import type { ReactNode } from "react";
import { memo, useCallback, useMemo, useState } from "react";
import { LuCalendar, LuEye, LuHash, LuStar, LuTrash } from "react-icons/lu";
import { useNavigate } from "react-router";
import { DateTime, Hyperlink, Table } from "~/components";
import { ConfirmDelete } from "~/components/Modals";
import type { InfiniteOptions } from "~/components/Table";
import { usePermissions } from "~/hooks";
import { path } from "~/utils/path";
import type { DepreciationRunListItem } from "../../types";
import DepreciationRunStatus from "./DepreciationRunStatus";

type DepreciationRunTableProps = {
  data: DepreciationRunListItem[];
  count: number;
  infinite: InfiniteOptions;
  primaryAction?: ReactNode;
};

const DepreciationRunTable = memo(
  ({ data, count, primaryAction, infinite }: DepreciationRunTableProps) => {
    const { t } = useLingui();
    const navigate = useNavigate();
    const permissions = usePermissions();
    const [selectedRun, setSelectedRun] =
      useState<DepreciationRunListItem | null>(null);
    const deleteModal = useDisclosure();

    const columns = useMemo<ColumnDef<DepreciationRunListItem>[]>(
      () => [
        {
          accessorKey: "depreciationRunId",
          header: t`Run ID`,
          cell: ({ row }) => (
            <Hyperlink to={path.to.depreciationRun(row.original.id)}>
              {row.original.depreciationRunId}
            </Hyperlink>
          ),
          meta: {
            mobile: "P1",
            icon: <LuHash />
          }
        },
        {
          accessorKey: "periodEnd",
          header: t`Period End`,
          cell: ({ row }) => (
            <DateTime value={row.original.periodEnd} variant="date" />
          ),
          meta: {
            mobile: "P3",
            filter: {
              type: "dateRange"
            },
            icon: <LuCalendar />
          }
        },
        {
          accessorKey: "status",
          header: t`Status`,
          cell: ({ row }) => (
            <DepreciationRunStatus status={row.original.status} />
          ),
          meta: {
            mobile: "P2",
            icon: <LuStar />
          }
        },
        {
          accessorKey: "postedAt",
          header: t`Posted At`,
          cell: ({ row }) =>
            row.original.postedAt ? (
              <DateTime value={row.original.postedAt} variant="date" />
            ) : (
              "—"
            ),
          meta: {
            icon: <LuCalendar />
          }
        }
      ],
      [t]
    );

    const renderContextMenu = useCallback(
      (row: DepreciationRunListItem) => (
        <>
          <MenuItem
            shortcut={MENU_ITEM_SHORTCUTS.view}
            disabled={!permissions.can("view", "accounting")}
            onClick={() => navigate(path.to.depreciationRun(row.id))}
          >
            <MenuIcon icon={<LuEye />} />
            <Trans>View Run</Trans>
          </MenuItem>
          {row.status === "Draft" && (
            <MenuItem
              shortcut={MENU_ITEM_SHORTCUTS.delete}
              disabled={!permissions.can("delete", "accounting")}
              destructive
              onClick={() => {
                setSelectedRun(row);
                deleteModal.onOpen();
              }}
            >
              <MenuIcon icon={<LuTrash />} />
              <Trans>Delete</Trans>
            </MenuItem>
          )}
        </>
      ),
      [deleteModal, navigate, permissions]
    );

    return (
      <>
        <Table<DepreciationRunListItem>
          infinite={infinite}
          data={data}
          columns={columns}
          count={count}
          primaryAction={primaryAction}
          renderContextMenu={renderContextMenu}
          title={t`Depreciation`}
        />
        {selectedRun && (
          <ConfirmDelete
            action={path.to.deleteDepreciationRun(selectedRun.id)}
            isOpen={deleteModal.isOpen}
            name={selectedRun.depreciationRunId}
            text={t`Are you sure you want to delete ${selectedRun.depreciationRunId}? This cannot be undone.`}
            onCancel={() => {
              deleteModal.onClose();
              setSelectedRun(null);
            }}
            onSubmit={() => {
              deleteModal.onClose();
              setSelectedRun(null);
            }}
          />
        )}
      </>
    );
  }
);

DepreciationRunTable.displayName = "DepreciationRunTable";
export default DepreciationRunTable;