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.

6.5 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 {
  Avatar,
  Drawer,
  DrawerBody,
  DrawerContent,
  DrawerHeader,
  DrawerTitle,
  HStack,
  Popover,
  PopoverContent,
  PopoverTrigger,
  useCloseRoute,
  useMode,
  VStack
} from "@carbon/react";
import data from "@emoji-mart/data";
import { Trans, useLingui } from "@lingui/react/macro";
import { lazy, Suspense, useCallback, useState } from "react";
import { useFetcher } from "react-router";
import z from "zod";
import { DateTime } from "~/components";
import { Tags } from "~/components/Form";
import { useTags } from "~/hooks/useTags";
import type { Suggestion } from "~/modules/resources/types";
import { path } from "~/utils/path";

// Lazy, not a static import: @emoji-mart/react is CommonJS, so under SSR its
// default import is `{ default: Picker }` and dev React warns "type is
// invalid" the moment the element is created, even in a closed popover.
const Picker = lazy(() => import("@emoji-mart/react"));

type SuggestionDetailsProps = {
  suggestion: Suggestion;
  tags: { name: string }[];
};

type EmojiData = {
  native: string;
  id: string;
  name: string;
};

export default function SuggestionDetails({
  suggestion,
  tags
}: SuggestionDetailsProps) {
  const { t } = useLingui();
  const closeRoute = useCloseRoute();
  const onClose = () => closeRoute();
  const fetcher = useFetcher();
  const [emojiPickerOpen, setEmojiPickerOpen] = useState(false);
  const mode = useMode();
  const pickerTheme = mode;

  const { onUpdateTags } = useTags({
    id: suggestion.id ?? "",
    table: "suggestion"
  });

  const onUpdateEmoji = useCallback(
    (emojiData: EmojiData) => {
      if (!suggestion.id) return;
      const formData = new FormData();
      formData.append("emoji", emojiData.native);
      fetcher.submit(formData, {
        method: "post",
        action: path.to.suggestion(suggestion.id)
      });
      setEmojiPickerOpen(false);
    },
    [suggestion.id, fetcher]
  );

  // Use optimistic emoji value
  const currentEmoji =
    fetcher.formData?.get("emoji")?.toString() ??
    (suggestion as { emoji?: string }).emoji ??
    "💡";

  return (
    <Drawer
      open
      onOpenChange={(open) => {
        if (!open) onClose();
      }}
    >
      <DrawerContent>
        <DrawerHeader>
          <DrawerTitle>
            <Trans>Suggestion</Trans>
          </DrawerTitle>
        </DrawerHeader>
        <DrawerBody>
          <VStack spacing={4}>
            <VStack spacing={2} className="w-full">
              <h3 className="text-xs text-muted-foreground">
                <Trans>Emoji</Trans>
              </h3>
              <Popover open={emojiPickerOpen} onOpenChange={setEmojiPickerOpen}>
                <PopoverTrigger asChild>
                  <button
                    type="button"
                    className="inline-flex items-center justify-center rounded-md h-12 w-12 text-3xl hover:bg-accent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
                  >
                    {currentEmoji}
                  </button>
                </PopoverTrigger>
                <PopoverContent
                  className="w-auto p-0 border-0"
                  align="start"
                  sideOffset={8}
                >
                  <Suspense>
                    <Picker
                      data={data}
                      onEmojiSelect={onUpdateEmoji}
                      theme={pickerTheme}
                      previewPosition="none"
                      skinTonePosition="none"
                      navPosition="bottom"
                      perLine={8}
                    />
                  </Suspense>
                </PopoverContent>
              </Popover>
            </VStack>

            <VStack spacing={2} className="w-full">
              <h3 className="text-xs text-muted-foreground">
                <Trans>Suggestion</Trans>
              </h3>
              <div className="whitespace-pre-wrap text-sm">
                {suggestion.suggestion}
              </div>
            </VStack>

            <VStack spacing={2} className="w-full">
              <h3 className="text-xs text-muted-foreground">
                <Trans>Submitted By</Trans>
              </h3>
              <HStack spacing={2}>
                <Avatar
                  size="sm"
                  name={suggestion.employeeName ?? undefined}
                  src={suggestion.employeeAvatarUrl ?? undefined}
                />
                <span>{suggestion.employeeName ?? "Anonymous"}</span>
              </HStack>
            </VStack>

            <VStack spacing={2} className="w-full">
              <h3 className="text-xs text-muted-foreground">
                <Trans>Date</Trans>
              </h3>
              <span>
                <DateTime value={suggestion.createdAt} variant="date" />
              </span>
            </VStack>

            <VStack spacing={2} className="w-full">
              <h3 className="text-xs text-muted-foreground">
                <Trans>Path</Trans>
              </h3>
              <span className="text-sm font-mono">{suggestion.path}</span>
            </VStack>

            {suggestion.attachmentPath && (
              <VStack spacing={2} className="w-full">
                <h3 className="text-xs text-muted-foreground">
                  <Trans>Attachment</Trans>
                </h3>
                <a
                  href={`/file/preview/private/${suggestion.attachmentPath}`}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="text-primary underline text-sm"
                >
                  <Trans>View Attachment</Trans>
                </a>
              </VStack>
            )}

            <ValidatedForm
              defaultValues={{
                tags: suggestion.tags ?? []
              }}
              validator={z.object({
                tags: z.array(z.string()).optional()
              })}
              className="w-full"
            >
              <Tags
                availableTags={tags}
                label={t`Tags`}
                name="tags"
                table="suggestion"
                inline
                onChange={onUpdateTags}
              />
            </ValidatedForm>
          </VStack>
        </DrawerBody>
      </DrawerContent>
    </Drawer>
  );
}