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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// 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 { useRevalidator } from "@carbon/query";
import { ActionMenu, toast } from "@carbon/react";
import { useLingui } from "@lingui/react/macro";
import type { PostgrestSingleResponse } from "@supabase/supabase-js";
import type { ColumnDef } from "@tanstack/react-table";
import type { ReactNode } from "react";
import { useCallback } from "react";
/** The setup grids' Table has a container with `contain: strict`, so it
* takes its height from its parent: a header row and the footer row, each
* `h-11` (44px), the rows and the borders. A row is 44px, 49px with a row
* menu (its button), 53px when a cell stacks two lines. */
export function setupGridHeight(rowCount: number, rowHeight = 44) {
return 88 + rowHeight * Math.max(rowCount, 1) + 4;
}
/**
* The row menu as an ordinary last column. The Table's own `renderContextMenu`
* pins its menu column to the right edge, so when a grid is wider than the
* page it sits on top of the Total column; this one scrolls with the row.
*/
export function rowMenuColumn<T>(
render: (row: T) => ReactNode,
label: string
): ColumnDef<T> {
return {
id: "rowMenu",
header: () => <span className="sr-only">{label}</span>,
cell: ({ row }) => {
const items = render(row.original);
return items ? (
<div className="flex justify-end">
<ActionMenu>{items}</ActionMenu>
</div>
) : null;
},
size: 60
};
}
/**
* Saves one grid cell: posts `fields` to `action` with `quiet` set, so the
* route answers `{ error }` instead of a redirect and a flash. A failure
* toasts and the cell reverts (the editable cells read `error`); a success
* revalidates the page so totals and footers follow.
*/
export function useCellSave() {
const { t } = useLingui();
const revalidator = useRevalidator();
return useCallback(
async (
action: string,
fields: Record<string, string>
): Promise<PostgrestSingleResponse<unknown>> => {
const formData = new FormData();
formData.set("quiet", "true");
for (const [key, value] of Object.entries(fields)) {
formData.set(key, value);
}
const response = await fetch(action, {
method: "post",
body: formData
}).catch(() => null);
const body = (await response?.json().catch(() => null)) as {
error?: string | null;
} | null;
const message =
!response?.ok || body?.error
? (body?.error ?? t`Failed to save the change`)
: null;
if (message) {
toast.error(message);
} else {
revalidator.revalidate();
}
return {
data: null,
error: message ? { message } : null
} as unknown as PostgrestSingleResponse<unknown>;
},
[revalidator, t]
);
}