Your favorite shift management application!
"use client";
import { useId, useMemo, useRef, useState } from "react";
import { useTranslations } from "next-intl";
import { ChevronDown, ChevronUp, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { ConfirmationDialog } from "@/components/ui/confirmation-dialog";
import { PanelBody, PanelDialog, PanelFooter } from "@/components/panel-dialog";
import { ReadOnlyBanner } from "@/components/read-only-banner";
import { ArchivedPresetSection, PresetSection } from "@/components/preset-list";
import {
EMPTY_PRESET_FORM,
PresetFormCard,
presetToFormData,
samePresetForm,
} from "@/components/preset-form";
import { ShiftPreset } from "@/lib/db/schema";
import { activePresets, groupPresetsByName } from "@/lib/shift-display";
import { usePresets, type PresetFormData } from "@/hooks/usePresets";
import { useCalendarPermission } from "@/hooks/useCalendarPermission";
import { useCalendars } from "@/hooks/useCalendars";
import { useReportDirty } from "@/hooks/useDirtyState";
import { DESKTOP_QUERY, useMediaQuery } from "@/hooks/useMediaQuery";
import { validateTimeRanges, toTimeRanges } from "@/lib/time-ranges";
interface PresetsPanelProps {
calendarId: string;
onClose: () => void;
readOnly?: boolean;
/** Called after every successful mutation, for callers holding their own copy of the presets. */
onPresetsChange?: () => void;
/** Lets the surrounding frame guard its own close button against unsaved form input. */
onDirtyChange?: (dirty: boolean) => void;
}
export function PresetsPanel({
calendarId,
onClose,
readOnly = false,
onPresetsChange,
onDirtyChange,
}: PresetsPanelProps) {
const t = useTranslations();
const permission = useCalendarPermission(calendarId);
const desktop = useMediaQuery(DESKTOP_QUERY, false);
// Creating a new preset and reordering the shared list are calendar-wide;
// editing/deleting an existing one depends on who created it (Own/Any).
const canCreate = !readOnly && permission.can("createPreset");
const canReorder = !readOnly && permission.can("manageAnyPresets");
const canEditPreset = (preset: ShiftPreset) =>
!readOnly && permission.canOwned("manageOwnPresets", "manageAnyPresets", preset.createdBy);
// Banner-level "no preset capability at all" — individual actions still gate themselves.
const isReadOnly =
readOnly || (!permission.can("createPreset") && !permission.can("manageOwnPresets"));
const {
presets,
loading,
createPreset,
updatePreset,
deletePreset,
archivePreset,
reorderPresets,
} = usePresets(calendarId);
const { calendars } = useCalendars();
const splitShiftsEnabled = calendars.find((c) => c.id === calendarId)?.splitShiftsEnabled ?? false;
const formId = useId();
const formRef = useRef<HTMLFormElement>(null);
const titleRef = useRef<HTMLInputElement>(null);
const [editingPreset, setEditingPreset] = useState<ShiftPreset | null>(null);
const [formData, setFormData] = useState<PresetFormData>(EMPTY_PRESET_FORM);
const [baseline, setBaseline] = useState<PresetFormData>(EMPTY_PRESET_FORM);
const [isSaving, setIsSaving] = useState(false);
const [deletingId, setDeletingId] = useState<string | null>(null);
const [cloningId, setCloningId] = useState<string | null>(null);
const [archivingId, setArchivingId] = useState<string | null>(null);
const [archiveOpen, setArchiveOpen] = useState(false);
const [deleteTarget, setDeleteTarget] = useState<ShiftPreset | null>(null);
const [pendingDiscard, setPendingDiscard] = useState<(() => void) | null>(null);
// Holds the dropped order until the reorder request settles, so rows don't snap back.
const [pendingOrder, setPendingOrder] = useState<string[] | null>(null);
// The create-form is shown for a new preset; the same form area is reused
// for editing, reachable only via an already-gated Edit button.
const formVisible = editingPreset ? canEditPreset(editingPreset) : canCreate;
const isDirty = formVisible && !samePresetForm(formData, baseline);
const canSave =
!isSaving &&
formData.title.trim() !== "" &&
(!editingPreset || isDirty) &&
(formData.isAllDay || !validateTimeRanges(toTimeRanges(formData)));
useReportDirty(isDirty, onDirtyChange);
const ordered = useMemo(() => {
if (!pendingOrder) return presets;
const rank = new Map(pendingOrder.map((id, index) => [id, index]));
const last = pendingOrder.length;
return [...presets].sort((a, b) => (rank.get(a.id) ?? last) - (rank.get(b.id) ?? last));
}, [presets, pendingOrder]);
const active = activePresets(ordered);
const archived = ordered.filter((p) => p.archivedAt);
const primary = active.filter((p) => !p.isSecondary);
const secondary = active.filter((p) => p.isSecondary);
const existingGroupNames = useMemo(
() => groupPresetsByName(activePresets(presets)).groups.map((g) => g.name),
[presets]
);
const resetForm = () => {
setEditingPreset(null);
setFormData(EMPTY_PRESET_FORM);
setBaseline(EMPTY_PRESET_FORM);
};
// Runs the action directly, or after the user agreed to drop unsaved input.
const guardDiscard = (action: () => void) => {
if (isDirty) setPendingDiscard(() => action);
else action();
};
const saveOrder = async (next: ShiftPreset[]) => {
if (!canReorder) return;
const ids = next.map((p) => p.id);
setPendingOrder(ids);
const success = await reorderPresets(ids.map((id, order) => ({ id, order })));
setPendingOrder((current) => (current === ids ? null : current));
if (success) onPresetsChange?.();
};
const handleSave = async () => {
if (!canSave) return;
setIsSaving(true);
try {
const success = editingPreset
? await updatePreset(editingPreset.id, formData)
: await createPreset(formData);
if (success) {
resetForm();
onPresetsChange?.();
}
} finally {
setIsSaving(false);
}
};
const clonePreset = async (preset: ShiftPreset) => {
if (!canCreate || isSaving || cloningId) return;
setCloningId(preset.id);
try {
const success = await createPreset({
...presetToFormData(preset),
title: `${preset.title} (${t("preset.copiedSuffix")})`,
});
if (success) onPresetsChange?.();
} finally {
setCloningId(null);
}
};
const handleDeleteConfirm = async () => {
const target = deleteTarget;
if (!target) return;
setDeleteTarget(null);
setDeletingId(target.id);
try {
const success = await deletePreset(target.id);
if (success) {
if (editingPreset?.id === target.id) resetForm();
onPresetsChange?.();
}
} finally {
setDeletingId(null);
}
};
const startEdit = (preset: ShiftPreset) => {
if (editingPreset?.id === preset.id) return;
guardDiscard(() => {
const data = presetToFormData(preset);
setEditingPreset(preset);
setFormData(data);
setBaseline(data);
requestAnimationFrame(() => {
formRef.current?.scrollIntoView({ block: "nearest", behavior: "smooth" });
// Focusing on a phone would pop the keyboard over the form
if (desktop) titleRef.current?.focus({ preventScroll: true });
});
});
};
const handleCancel = () => {
if (editingPreset) resetForm();
else guardDiscard(onClose);
};
const handleArchive = async (preset: ShiftPreset, nextArchived: boolean) => {
if (archivingId) return;
setArchivingId(preset.id);
try {
const success = await archivePreset(preset.id, nextArchived);
if (success) {
if (nextArchived && editingPreset?.id === preset.id) resetForm();
if (nextArchived) setArchiveOpen(true);
onPresetsChange?.();
}
} finally {
setArchivingId(null);
}
};
const rowActions = {
canEdit: canEditPreset,
canClone: canCreate,
canReorder,
editingId: editingPreset?.id ?? null,
deletingId,
cloningId,
archivingId,
onEdit: startEdit,
onClone: clonePreset,
onDelete: setDeleteTarget,
onArchive: handleArchive,
};
return (
<>
<PanelBody className="flex flex-col gap-4">
{isReadOnly && <ReadOnlyBanner message={t("guest.cannotEdit")} />}
{presets.length === 0 &&
(loading ? (
<div className="flex justify-center py-6 text-fg-tertiary">
<Loader2 className="size-5 animate-spin" />
</div>
) : (
<p className="rounded-[11px] border border-dashed border-control px-4 py-5 text-center text-[13px] text-fg-tertiary">
{t("preset.noPresets")}
</p>
))}
{primary.length > 0 && (
<PresetSection
label={t("presetSheet.primary")}
presets={primary}
onReorder={(next) => saveOrder([...next, ...secondary])}
{...rowActions}
/>
)}
{secondary.length > 0 && (
<PresetSection
label={t("presetSheet.secondarySection")}
presets={secondary}
onReorder={(next) => saveOrder([...primary, ...next])}
{...rowActions}
/>
)}
{archived.length > 0 && (
<div>
<button
type="button"
onClick={() => setArchiveOpen((current) => !current)}
aria-expanded={archiveOpen}
className="flex w-full items-center gap-1.5 rounded-[9px] px-1 py-1.5 text-[12px] font-semibold uppercase tracking-[0.06em] text-fg-tertiary transition-colors hover:text-fg-secondary"
>
{t("presetSheet.archivedSection")}
<span className="font-mono normal-case tracking-normal opacity-70">
({archived.length})
</span>
{archiveOpen ? (
<ChevronUp className="size-3.5" />
) : (
<ChevronDown className="size-3.5" />
)}
</button>
{archiveOpen && (
<ArchivedPresetSection
label={t("presetSheet.archivedHint")}
presets={archived}
{...rowActions}
/>
)}
</div>
)}
{formVisible && (
<PresetFormCard
formId={formId}
editing={!!editingPreset}
value={formData}
onChange={(patch) => setFormData((current) => ({ ...current, ...patch }))}
onSubmit={handleSave}
disabled={isSaving}
cardRef={formRef}
titleRef={titleRef}
existingGroupNames={existingGroupNames}
splitShiftsEnabled={splitShiftsEnabled}
calendarId={calendarId}
/>
)}
</PanelBody>
{formVisible && (
<PanelFooter>
<Button
type="button"
variant="outline"
className="h-10 flex-1 font-semibold"
onClick={handleCancel}
disabled={isSaving}
>
{t("common.cancel")}
</Button>
<Button
type="submit"
form={formId}
className="h-10 flex-1 font-semibold"
disabled={!canSave}
>
{isSaving ? t("common.saving") : t("presetSheet.save")}
</Button>
</PanelFooter>
)}
<ConfirmationDialog
open={!!pendingDiscard}
onOpenChange={(open) => !open && setPendingDiscard(null)}
onConfirm={() => {
const action = pendingDiscard;
setPendingDiscard(null);
resetForm();
action?.();
}}
/>
<ConfirmationDialog
open={!!deleteTarget}
onOpenChange={(open) => !open && setDeleteTarget(null)}
onConfirm={handleDeleteConfirm}
title={t("presetSheet.delete")}
description={t("preset.deleteConfirm")}
cancelText={t("common.cancel")}
confirmText={t("common.delete")}
confirmVariant="destructive"
/>
</>
);
}
interface PresetManageSheetProps {
open: boolean;
onOpenChange: (open: boolean) => void;
calendarId: string;
onPresetsChange?: () => void;
readOnly?: boolean;
}
export function PresetManageSheet({
open,
onOpenChange,
calendarId,
onPresetsChange,
readOnly = false,
}: PresetManageSheetProps) {
const t = useTranslations();
const [dirty, setDirty] = useState(false);
const [confirmClose, setConfirmClose] = useState(false);
return (
<>
<PanelDialog
bare
open={open}
onOpenChange={(next) => {
if (next) onOpenChange(true);
else if (dirty) setConfirmClose(true);
else onOpenChange(false);
}}
title={t("presetSheet.title")}
description={t("presetSheet.description")}
>
<PresetsPanel
key={calendarId}
calendarId={calendarId}
onClose={() => onOpenChange(false)}
readOnly={readOnly}
onPresetsChange={onPresetsChange}
onDirtyChange={setDirty}
/>
</PanelDialog>
<ConfirmationDialog
open={confirmClose}
onOpenChange={setConfirmClose}
onConfirm={() => {
setConfirmClose(false);
onOpenChange(false);
}}
/>
</>
);
}