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 { DateTimePicker, Hidden, Select, ValidatedForm } from "@carbon/form";
import { useAction } from "@carbon/query";
import {
Button,
Copy,
HStack,
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
ModalTitle,
Subheading,
Tooltip,
TooltipContent,
TooltipTrigger,
toast,
useDisclosure,
VStack
} from "@carbon/react";
import { getLocalTimeZone, toZoned } from "@internationalized/date";
import { Trans, useLingui } from "@lingui/react/macro";
import { useCallback, useState } from "react";
import { LuCopy, LuKeySquare, LuLink } from "react-icons/lu";
import { useParams } from "react-router";
import { z } from "zod";
import {
Assignee,
EmployeeAvatar,
useOptimisticAssignment
} from "~/components";
import { Location, Procedure, WorkCenter } from "~/components/Form";
import { usePermissions, useRouteData } from "~/hooks";
import type { action as eventsAction } from "~/routes/x+/maintenance+/$dispatchId.events";
import type { action as updateAction } from "~/routes/x+/maintenance+/update";
import { path } from "~/utils/path";
import { copyToClipboard } from "~/utils/string";
import {
isMaintenanceDispatchLocked,
maintenanceDispatchEventValidator,
maintenanceDispatchPriority,
maintenanceSeverity,
maintenanceSource,
oeeImpact
} from "../../resources.models";
import type {
MaintenanceDispatchDetail,
MaintenanceDispatchEvent
} from "../../types";
import MaintenanceOeeImpact from "./MaintenanceOeeImpact";
import MaintenancePriority from "./MaintenancePriority";
import MaintenanceSeverity from "./MaintenanceSeverity";
import MaintenanceSource from "./MaintenanceSource";
import MaintenanceStatus from "./MaintenanceStatus";
const MaintenanceDispatchProperties = () => {
const { t } = useLingui();
const { dispatchId } = useParams();
if (!dispatchId) throw new Error("dispatchId not found");
const permissions = usePermissions();
const eventModal = useDisclosure();
const [selectedEvent, setSelectedEvent] =
useState<MaintenanceDispatchEvent | null>(null);
const routeData = useRouteData<{
dispatch: MaintenanceDispatchDetail;
events: MaintenanceDispatchEvent[];
failureModes: { id: string; name: string }[];
}>(path.to.maintenanceDispatch(dispatchId));
const optimisticAssignment = useOptimisticAssignment({
id: dispatchId,
table: "maintenanceDispatch"
});
const assignee =
optimisticAssignment !== undefined
? optimisticAssignment
: (routeData?.dispatch?.assignee?.id ?? null);
const fetcher = useAction<typeof updateAction>({
onSuccess: ({ data }) => {
if (data?.warning) toast.error(data.warning);
}
});
const eventFetcher = useAction<typeof eventsAction>({
onSuccess: ({ data }) => {
toast.success(t`Event saved`);
if (data && "warning" in data && typeof data.warning === "string") {
toast.error(data.warning);
}
eventModal.onClose();
setSelectedEvent(null);
}
});
const onUpdate = useCallback(
(field: string, value: string | null) => {
const formData = new FormData();
formData.append("ids", dispatchId);
formData.append("field", field);
formData.append("value", value?.toString() ?? "");
fetcher.submit(formData, {
method: "post",
action: path.to.maintenanceDispatchUpdate
});
},
[dispatchId, fetcher]
);
const isLocked = isMaintenanceDispatchLocked(routeData?.dispatch?.status);
const [currentOeeImpact, setCurrentOeeImpact] = useState<string>(
routeData?.dispatch?.oeeImpact ?? "No Impact"
);
const showFailureModes =
currentOeeImpact === "Down" || currentOeeImpact === "Impact";
return (
<VStack
spacing={4}
className="w-96 bg-background/30 h-full overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-accent border-l border-border px-4 py-2 text-sm"
>
<VStack spacing={2}>
<HStack className="w-full justify-between">
<Subheading as="h3" variant="light">
<Trans>Properties</Trans>
</Subheading>
<HStack spacing={1}>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
aria-label="Link"
size="sm"
className="p-1"
onClick={() =>
copyToClipboard(
window.location.origin +
path.to.maintenanceDispatch(dispatchId)
)
}
>
<LuLink className="w-3 h-3" />
</Button>
</TooltipTrigger>
<TooltipContent>
<span>
<Trans>Copy link to dispatch</Trans>
</span>
</TooltipContent>
</Tooltip>
<Copy
text={routeData?.dispatch?.id ?? ""}
label={t`Copy dispatch ID`}
icon={<LuKeySquare className="size-3" />}
variant="ghost"
className="w-auto"
/>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
aria-label="Copy"
size="sm"
className="p-1"
onClick={() =>
copyToClipboard(
routeData?.dispatch?.maintenanceDispatchId ?? ""
)
}
>
<LuCopy className="w-3 h-3" />
</Button>
</TooltipTrigger>
<TooltipContent>
<span>
<Trans>Copy dispatch number</Trans>
</span>
</TooltipContent>
</Tooltip>
</HStack>
</HStack>
<span className="text-sm tracking-tight">
{routeData?.dispatch?.maintenanceDispatchId}
</span>
</VStack>
<VStack spacing={2}>
<h3 className="text-xs text-muted-foreground">
<Trans>Status</Trans>
</h3>
<MaintenanceStatus status={routeData?.dispatch?.status} />
</VStack>
<VStack spacing={2}>
<h3 className="text-xs text-muted-foreground">
<Trans>Assignee</Trans>
</h3>
<Assignee
id={dispatchId}
table="maintenanceDispatch"
size="sm"
value={assignee ?? ""}
isReadOnly={!permissions.can("update", "resources")}
/>
</VStack>
<ValidatedForm
defaultValues={{
locationId: routeData?.dispatch?.locationId ?? ""
}}
validator={z.object({
locationId: z.string().optional()
})}
className="w-full"
>
<Location
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Location`}
name="locationId"
inline
isClearable
onChange={(value) => {
onUpdate("locationId", value?.value ?? null);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
workCenterId: routeData?.dispatch?.workCenterId ?? ""
}}
validator={z.object({
workCenterId: z.string().optional()
})}
className="w-full"
>
<WorkCenter
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Work Center`}
name="workCenterId"
termId="work-center"
inline
isClearable
onChange={(value) => {
onUpdate("workCenterId", value?.value ?? null);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
priority: routeData?.dispatch?.priority ?? ""
}}
validator={z.object({
priority: z.string().optional()
})}
className="w-full"
>
<Select
options={maintenanceDispatchPriority.map((priority) => ({
value: priority,
label: (
<div className="flex gap-2 items-center">
<MaintenancePriority priority={priority} />
</div>
)
}))}
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Priority`}
name="priority"
inline={(value) => {
return (
<MaintenancePriority
priority={value as (typeof maintenanceDispatchPriority)[number]}
/>
);
}}
onChange={(value) => {
if (value) {
onUpdate("priority", value.value);
}
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
severity: routeData?.dispatch?.severity ?? ""
}}
validator={z.object({
severity: z.string().optional()
})}
className="w-full"
>
<Select
options={maintenanceSeverity.map((severity) => ({
value: severity,
label: severity
}))}
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Severity`}
name="severity"
inline={(value) => {
return (
<MaintenanceSeverity
severity={value as (typeof maintenanceSeverity)[number]}
/>
);
}}
onChange={(value) => {
if (value) {
onUpdate("severity", value.value);
}
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
source: routeData?.dispatch?.source ?? ""
}}
validator={z.object({
source: z.string().optional()
})}
className="w-full"
>
<Select
options={maintenanceSource.map((source) => ({
value: source,
label: <MaintenanceSource source={source} />
}))}
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Source`}
name="source"
inline={(value) => {
return (
<MaintenanceSource
source={value as (typeof maintenanceSource)[number]}
/>
);
}}
onChange={(value) => {
if (value) {
onUpdate("source", value.value);
}
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
procedureId: (routeData?.dispatch as any)?.procedureId ?? ""
}}
validator={z.object({
procedureId: z.string().optional()
})}
className="w-full"
>
<Procedure
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Procedure`}
name="procedureId"
inline={(value, options) => {
const procedure = options.find((o) => o.value === value);
return procedure?.label ?? null;
}}
isClearable
onChange={(value) => {
onUpdate("procedureId", value?.value ?? null);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
oeeImpact: routeData?.dispatch?.oeeImpact ?? "No Impact"
}}
validator={z.object({
oeeImpact: z.string().optional()
})}
className="w-full"
>
<Select
options={oeeImpact.map((impact) => ({
value: impact,
label: <MaintenanceOeeImpact oeeImpact={impact} />
}))}
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`OEE Impact`}
name="oeeImpact"
inline={(value) => {
return (
<MaintenanceOeeImpact
oeeImpact={value as (typeof oeeImpact)[number]}
/>
);
}}
onChange={(value) => {
if (value) {
setCurrentOeeImpact(value.value);
onUpdate("oeeImpact", value.value);
}
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
plannedStartTime: routeData?.dispatch?.plannedStartTime ?? ""
}}
validator={z.object({
plannedStartTime: z.string().optional()
})}
className="w-full"
>
<DateTimePicker
name="plannedStartTime"
label={t`Planned Start`}
inline
isDisabled={!permissions.can("update", "resources") || isLocked}
onChange={(date) => {
onUpdate(
"plannedStartTime",
date ? toZoned(date, getLocalTimeZone()).toAbsoluteString() : null
);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
plannedEndTime: routeData?.dispatch?.plannedEndTime ?? ""
}}
validator={z.object({
plannedEndTime: z.string().optional()
})}
className="w-full"
>
<DateTimePicker
name="plannedEndTime"
label={t`Planned End`}
inline
isDisabled={!permissions.can("update", "resources") || isLocked}
onChange={(date) => {
onUpdate(
"plannedEndTime",
date ? toZoned(date, getLocalTimeZone()).toAbsoluteString() : null
);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
actualStartTime: routeData?.dispatch?.actualStartTime ?? ""
}}
validator={z.object({
actualStartTime: z.string().optional()
})}
className="w-full"
>
<DateTimePicker
name="actualStartTime"
label={t`Actual Start`}
inline
isDisabled={!permissions.can("update", "resources") || isLocked}
onChange={(date) => {
onUpdate(
"actualStartTime",
date ? toZoned(date, getLocalTimeZone()).toAbsoluteString() : null
);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
actualEndTime: routeData?.dispatch?.actualEndTime ?? ""
}}
validator={z.object({
actualEndTime: z.string().optional()
})}
className="w-full"
>
<DateTimePicker
name="actualEndTime"
label={t`Actual End`}
inline
isDisabled={!permissions.can("update", "resources") || isLocked}
onChange={(date) => {
onUpdate(
"actualEndTime",
date ? toZoned(date, getLocalTimeZone()).toAbsoluteString() : null
);
}}
/>
</ValidatedForm>
{showFailureModes && (
<>
<ValidatedForm
defaultValues={{
suspectedFailureModeId:
routeData?.dispatch?.suspectedFailureModeId ?? ""
}}
validator={z.object({
suspectedFailureModeId: z.string().optional()
})}
className="w-full"
>
<Select
options={(routeData?.failureModes ?? []).map((mode) => ({
value: mode.id,
label: mode.name
}))}
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Suspected Failure Mode`}
name="suspectedFailureModeId"
inline={(value) => {
return (
<span>
{routeData?.failureModes.find((mode) => mode.id === value)
?.name ?? ""}
</span>
);
}}
isClearable
onChange={(value) => {
onUpdate("suspectedFailureModeId", value?.value ?? null);
}}
/>
</ValidatedForm>
<ValidatedForm
defaultValues={{
actualFailureModeId:
routeData?.dispatch?.actualFailureModeId ?? ""
}}
validator={z.object({
actualFailureModeId: z.string().optional()
})}
className="w-full"
>
<Select
options={(routeData?.failureModes ?? []).map((mode) => ({
value: mode.id,
label: mode.name
}))}
isReadOnly={isLocked || !permissions.can("update", "resources")}
label={t`Actual Failure Mode`}
name="actualFailureModeId"
inline={(value) => {
return (
<span>
{routeData?.failureModes.find((mode) => mode.id === value)
?.name ?? ""}
</span>
);
}}
isClearable
onChange={(value) => {
onUpdate("actualFailureModeId", value?.value ?? null);
}}
/>
</ValidatedForm>
</>
)}
<VStack spacing={2}>
<h3 className="text-xs text-muted-foreground">
<Trans>Created By</Trans>
</h3>
<EmployeeAvatar
employeeId={routeData?.dispatch?.createdBy!}
size="xxs"
/>
</VStack>
{/* Timecard Edit Modal */}
{eventModal.isOpen && selectedEvent && (
<Modal
open={eventModal.isOpen}
onOpenChange={(open) => {
if (!open) {
eventModal.onClose();
setSelectedEvent(null);
}
}}
>
<ModalContent>
<ValidatedForm
method="post"
action={path.to.maintenanceDispatchEvents(dispatchId)}
validator={maintenanceDispatchEventValidator}
fetcher={eventFetcher}
defaultValues={{
id: selectedEvent.id,
maintenanceDispatchId: dispatchId,
employeeId: selectedEvent.employee.id,
workCenterId:
selectedEvent.workCenter.id ??
routeData?.dispatch?.workCenterId ??
"",
startTime: selectedEvent.startTime,
endTime: selectedEvent.endTime ?? ""
}}
>
<ModalHeader>
<ModalTitle>
<Trans>Edit Timecard</Trans>
</ModalTitle>
</ModalHeader>
<ModalBody>
<VStack spacing={4}>
<HStack spacing={2}>
<EmployeeAvatar
employeeId={selectedEvent.employee.id}
size="sm"
/>
<span className="text-sm font-medium">
{selectedEvent.employee?.fullName ?? "Unknown"}
</span>
</HStack>
<Hidden name="id" value={selectedEvent.id} />
<Hidden name="maintenanceDispatchId" value={dispatchId} />
<Hidden name="employeeId" value={selectedEvent.employee.id} />
<Hidden
name="workCenterId"
value={
selectedEvent.workCenter.id ??
routeData?.dispatch?.workCenterId ??
""
}
/>
<DateTimePicker
name="startTime"
label={t`Start Time`}
isDisabled={!permissions.can("update", "resources")}
/>
<DateTimePicker
name="endTime"
label={t`End Time`}
isDisabled={!permissions.can("update", "resources")}
/>
</VStack>
</ModalBody>
<ModalFooter>
<Button
variant="secondary"
onClick={() => {
eventModal.onClose();
setSelectedEvent(null);
}}
>
<Trans>Cancel</Trans>
</Button>
<Button
type="submit"
isLoading={eventFetcher.state !== "idle"}
isDisabled={!permissions.can("update", "resources")}
>
<Trans>Save</Trans>
</Button>
</ModalFooter>
</ValidatedForm>
</ModalContent>
</Modal>
)}
</VStack>
);
};
export default MaintenanceDispatchProperties;