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 { CARBON_SLACK_ENABLED, useCarbon } from "@carbon/auth";
import { getCompanyPrivateBucket, storage } from "@carbon/files";
import { convertHeicToJpeg, isHeic } from "@carbon/files/media";
import {
Hidden,
Submit,
TextAreaControlled,
ValidatedForm
} from "@carbon/form";
import { getLogger } from "@carbon/logger";
import { useAction } from "@carbon/query";
import {
Badge,
BadgeCloseButton,
Button,
Checkbox,
File,
HStack,
Popover,
PopoverContent,
PopoverTrigger,
toast,
useMode,
VStack
} from "@carbon/react";
import data from "@emoji-mart/data";
import { Trans, useLingui } from "@lingui/react/macro";
import { nanoid } from "nanoid";
import type { ChangeEvent } from "react";
import React, { Suspense, useEffect, useState } from "react";
import { LuImage, LuMegaphone } from "react-icons/lu";
import { useLocation } from "react-router";
import { useUser } from "~/hooks";
import { suggestionValidator } from "~/modules/shared/shared.models";
import type { action } from "~/routes/x+/resources+/suggestions.new";
import { useUIStore } from "~/stores/ui";
import { path } from "~/utils/path";
const logger = getLogger("erp", "suggestion");
const Picker = React.lazy(() => import("@emoji-mart/react"));
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB in bytes
const defaultEmoji = "\u{1F4A1}";
type EmojiData = {
native: string;
id: string;
name: string;
};
const Suggestion = () => {
const { t } = useLingui();
const location = useLocation();
const [open, setOpen] = useState(false);
const [suggestion, setSuggestion] = useState("");
const [emoji, setEmoji] = useState(defaultEmoji);
const [emojiPickerOpen, setEmojiPickerOpen] = useState(false);
const [anonymous, setAnonymous] = useState(true);
const [sendToCarbon, setSendToCarbon] = useState(CARBON_SLACK_ENABLED);
const mode = useMode();
const pickerTheme = mode;
const [attachment, setAttachment] = useState<{
name: string;
path: string;
} | null>(null);
const { carbon } = useCarbon();
const user = useUser();
const companyId = user.company.id;
const suggestionPrefill = useUIStore((state) => state.suggestionPrefill);
const clearSuggestionRequest = useUIStore(
(state) => state.clearSuggestionRequest
);
const fetcher = useAction<typeof action>({
onSuccess: () => {
toast.success(t`Suggestion submitted`);
setSuggestion("");
setEmoji(defaultEmoji);
setAttachment(null);
setAnonymous(true);
setSendToCarbon(CARBON_SLACK_ENABLED);
setOpen(false);
}
});
useEffect(() => {
if (!suggestionPrefill) return;
setSuggestion(suggestionPrefill.suggestion);
setAnonymous(suggestionPrefill.anonymous);
setSendToCarbon(suggestionPrefill.sendToCarbon);
setEmoji(defaultEmoji);
setAttachment(null);
setOpen(true);
clearSuggestionRequest();
}, [suggestionPrefill, clearSuggestionRequest]);
const uploadImage = async (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files && carbon) {
let file = e.target.files[0];
const fileName = file.name;
toast.info(t`Uploading ${fileName}`);
if (file.size > MAX_FILE_SIZE) {
toast.error(t`File size exceeds 10MB limit`);
return;
}
if (isHeic(file.name, file.type)) {
try {
file = await convertHeicToJpeg(carbon, {
bucket: getCompanyPrivateBucket(companyId),
directory: `${companyId}/tmp`,
file
});
} catch {
toast.error(t`Failed to convert image`);
return;
}
}
const fileExtension = file.name.substring(file.name.lastIndexOf(".") + 1);
const storagePath = `${companyId}/suggestions/${nanoid()}.${fileExtension}`;
const imageUpload = await storage(carbon)
.company(companyId)
.upload(storagePath, file, {
cacheControl: `${12 * 60 * 60}`,
upsert: true
});
if (imageUpload.error) {
logger.error(imageUpload.error);
toast.error(t`Failed to upload image`);
}
if (imageUpload.data?.path) {
setAttachment({
name: file.name,
path: imageUpload.data.path
});
}
}
};
const onEmojiSelect = (emojiData: EmojiData) => {
setEmoji(emojiData.native);
setEmojiPickerOpen(false);
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="ghost"
className="hover:scale-100"
leftIcon={<LuMegaphone />}
>
<Trans>Suggestion</Trans>
</Button>
</PopoverTrigger>
<PopoverContent align="end" className="w-[380px] ">
<ValidatedForm
method="post"
action={path.to.newSuggestion}
validator={suggestionValidator}
fetcher={fetcher}
>
<Hidden name="path" value={location.pathname} />
<Hidden name="emoji" value={emoji} />
<Hidden name="attachmentPath" value={attachment?.path ?? ""} />
<Hidden name="userId" value={anonymous ? "" : user.id} />
<Hidden name="sendToCarbon" value={sendToCarbon ? "true" : ""} />
<VStack spacing={2}>
<VStack spacing={2} className="w-full">
<TextAreaControlled
name="suggestion"
label=""
value={suggestion}
onChange={(value) => setSuggestion(value)}
placeholder={t`Ideas, suggestions or problems?`}
/>
{attachment && (
<Badge className="-mt-2 truncate" variant="secondary">
{attachment.name}
<BadgeCloseButton
type="button"
onClick={() => {
setAttachment(null);
}}
/>
</Badge>
)}
</VStack>
<HStack className="w-full justify-between">
<VStack spacing={2}>
<HStack spacing={2}>
<Checkbox
isChecked={anonymous}
onCheckedChange={(checked) =>
setAnonymous(checked === true)
}
/>
<span className="text-sm">
<Trans>Submit anonymously</Trans>
</span>
</HStack>
{CARBON_SLACK_ENABLED && (
<HStack spacing={2}>
<Checkbox
isChecked={sendToCarbon}
onCheckedChange={(checked) =>
setSendToCarbon(checked === true)
}
/>
<span className="text-sm">
<Trans>Send to Carbon</Trans>
</span>
</HStack>
)}
</VStack>
<Popover open={emojiPickerOpen} onOpenChange={setEmojiPickerOpen}>
<PopoverTrigger asChild>
<button
type="button"
className="inline-flex items-center justify-center rounded-md h-10 w-10 text-2xl hover:bg-accent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 shrink-0"
>
{emoji}
</button>
</PopoverTrigger>
<PopoverContent
className="w-auto p-0 border-0"
align="end"
sideOffset={8}
>
<Suspense>
<Picker
data={data}
onEmojiSelect={onEmojiSelect}
theme={pickerTheme}
previewPosition="none"
skinTonePosition="none"
navPosition="bottom"
perLine={8}
/>
</Suspense>
</PopoverContent>
</Popover>
</HStack>
<HStack className="w-full justify-between">
<Button
variant="secondary"
onClick={() => {
setSuggestion("");
setEmoji(defaultEmoji);
setAttachment(null);
setOpen(false);
}}
>
<Trans>Cancel</Trans>
</Button>
<HStack spacing={1}>
<Button
isDisabled={suggestion.length === 0}
variant="secondary"
onClick={() => setSuggestion("")}
>
<Trans>Clear</Trans>
</Button>
<File
accept="image/*"
aria-label={t`Attach File`}
className="px-2"
isDisabled={!!attachment}
variant="secondary"
onChange={uploadImage}
>
<LuImage />
</File>
<Submit isDisabled={suggestion.length < 3}>
<Trans>Send</Trans>
</Submit>
</HStack>
</HStack>
</VStack>
</ValidatedForm>
</PopoverContent>
</Popover>
);
};
export default Suggestion;