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.

3.3 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 type { JSONContent } from "@carbon/react";
import {
  Card,
  CardContent,
  CardHeader,
  CardTitle,
  generateHTML,
  useDebounce
} from "@carbon/react";
import { Editor } from "@carbon/react/Editor";
import { useLingui } from "@lingui/react/macro";
import { useState } from "react";
import { useFetcher } from "react-router";
import { useImageUpload, usePermissions } from "~/hooks";
import { path } from "~/utils/path";

// The two rich-text columns on the changeOrder header (reasonForChange +
// description). Both are stored as JSON and read/written the same way; the
// debounced writer posts to $id.content so the engineering lock is enforced
// server-side (a direct supabase write had no such gate). Rendered as full Cards
// on the top-level CO detail route; `embedded` drops the Card chrome for callers
// that supply their own frame.
export function ChangeNoticeContentSection({
  id,
  title,
  field,
  content: initialContent,
  isDisabled,
  embedded = false
}: {
  id: string;
  title: string;
  field: "reasonForChange" | "description";
  content: JSONContent;
  isDisabled: boolean;
  // When true, render just the editor/prose with no Card chrome — the caller
  // (e.g. the accordion rail) supplies the title + frame.
  embedded?: boolean;
}) {
  const permissions = usePermissions();
  const fetcher = useFetcher<{}>();

  const [content, setContent] = useState(initialContent ?? {});

  const onUploadImage = useImageUpload("parts");

  const onUpdateContent = useDebounce(
    (value: JSONContent) => {
      fetcher.submit(
        { field, content: JSON.stringify(value) },
        { method: "post", action: path.to.changeNoticeContent(id) }
      );
    },
    2500,
    true
  );

  const body =
    permissions.can("update", "parts") && !isDisabled ? (
      <Editor
        className="[&_.ProseMirror]:text-sm"
        initialValue={(content ?? {}) as JSONContent}
        onUpload={onUploadImage}
        onChange={(value) => {
          setContent(value);
          onUpdateContent(value);
        }}
      />
    ) : (
      <div
        className="prose prose-sm dark:prose-invert"
        dangerouslySetInnerHTML={{
          __html: generateHTML(content as JSONContent)
        }}
      />
    );

  if (embedded) return body;

  return (
    <Card>
      <CardHeader>
        <CardTitle>{title}</CardTitle>
      </CardHeader>
      <CardContent>{body}</CardContent>
    </Card>
  );
}

export function ChangeNoticeContent({
  id,
  reasonForChange,
  description,
  isDisabled
}: {
  id: string;
  reasonForChange: JSONContent;
  description: JSONContent;
  isDisabled: boolean;
}) {
  const { t } = useLingui();

  if (!id) return null;

  return (
    <>
      <ChangeNoticeContentSection
        key={`${id}-reason`}
        id={id}
        title={t`Reason for Change`}
        field="reasonForChange"
        content={reasonForChange}
        isDisabled={isDisabled}
      />
      <ChangeNoticeContentSection
        key={`${id}-description`}
        id={id}
        title={t`Description of Change`}
        field="description"
        content={description}
        isDisabled={isDisabled}
      />
    </>
  );
}