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.2 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 { ValidatedForm } from "@carbon/form";
import { Button, HStack, VStack } from "@carbon/react";
import { HTML } from "@carbon/react/HTML";
import { Trans } from "@lingui/react/macro";
import { Fragment } from "react";
import { Form } from "react-router";
import { Avatar } from "~/components";
import { Hidden, Submit } from "~/components/Form";
import RichTextForm from "~/components/Form/RichText";
import { usePermissions, useUser } from "~/hooks";
import { noteValidator } from "~/modules/shared/shared.models";
import type { Note } from "~/modules/shared/types";
import { path } from "~/utils/path";
import { DateTime } from "./DateTime";

type RichTextProps = {
  documentId: string;
  notes: Note[];
};

const RichText = ({ documentId, notes }: RichTextProps) => {
  const user = useUser();
  const permissions = usePermissions();
  const isEmployee = permissions.is("employee");

  if (!isEmployee) return null;

  return (
    <>
      {notes.length > 0 ? (
        <div className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-8 w-full">
          {notes.map((note) => {
            if (!note.user || Array.isArray(note.user))
              throw new Error("Invalid user");
            return (
              <Fragment key={note.id}>
                {/* @ts-expect-error */}
                <Avatar path={note.user.avatarUrl} name={note.user?.fullName} />
                <VStack spacing={1}>
                  <p className="font-bold">{note.user?.fullName!}</p>
                  <HTML text={note.note} />
                  <HStack spacing={4}>
                    <span className="text-sm text-muted-foreground">
                      <DateTime value={note.createdAt} variant="relative" />
                    </span>
                    {user.id === note.user.id && (
                      <Form method="post" action={path.to.deleteNote(note.id)}>
                        <Button type="submit" variant="link" size="md">
                          <Trans>Delete</Trans>
                        </Button>
                      </Form>
                    )}
                  </HStack>
                </VStack>
              </Fragment>
            );
          })}
        </div>
      ) : (
        <div className="text-muted-foreground p-4 w-full text-center">
          <Trans>No notes</Trans>
        </div>
      )}

      <div className="pt-8 w-full">
        <ValidatedForm
          method="post"
          action={path.to.newNote}
          resetAfterSubmit
          validator={noteValidator}
        >
          <Hidden name="documentId" value={documentId} />
          <VStack spacing={3}>
            <div className="w-full border border-border rounded-md">
              <RichTextForm name="note" className="min-h-[160px]" />
            </div>
            <div className="flex justify-end w-full">
              <Submit withBlocker={false}>
                <Trans>Add Note</Trans>
              </Submit>
            </div>
          </VStack>
        </ValidatedForm>
      </div>
    </>
  );
};

export default RichText;