Skip to content

Your favorite shift management application!

17.9 KB
Raw Blame
"use client";

import { useId, useState } from "react";
import { useTranslations } from "next-intl";
import { Info, Loader2, Lock, UserRound } from "lucide-react";
import { Button } from "@/components/ui/button";
import { PanelBody, PanelDialog, PanelFooter } from "@/components/panel-dialog";
import {
  ChoiceChips,
  ColorSwatches,
  Field,
  InfoNote,
  SectionLabel,
  ToggleRow,
} from "@/components/form-kit";
import { useViewSettings } from "@/hooks/useViewSettings";
import { useCalendars } from "@/hooks/useCalendars";
import { useCalendarPermission } from "@/hooks/useCalendarPermission";
import { DESKTOP_QUERY, useMediaQuery } from "@/hooks/useMediaQuery";
import {
  CalendarViewSettings,
  PersonalViewSettings,
  calendarViewSettingsEqual,
  sanitizeCalendarViewSettings,
} from "@/lib/view-settings";
import { cn } from "@/lib/utils";
import { useReportDirty } from "@/hooks/useDirtyState";

export type ViewSettingsState = ReturnType<typeof useViewSettings>;

type LimitValue = "1" | "2" | "3" | "auto";
const LIMITS: LimitValue[] = ["1", "2", "3", "auto"];

// Handoff rule: locked controls keep their contrast and only lose the fill
const LOCKED_FIELDS =
  "[&_button:disabled]:cursor-not-allowed [&_button:disabled]:opacity-100 [&_[role=radio][aria-checked=true]:disabled]:bg-transparent [&_[role=switch]:disabled]:border-control [&_[role=switch]:disabled]:bg-surface-sunken";

function toLimit(value: number | null): LimitValue {
  if (value === null) return "auto";
  return (LIMITS.includes(String(value) as LimitValue) ? String(value) : "auto") as LimitValue;
}

function LimitControl({
  value,
  onChange,
  label,
  disabled,
}: {
  value: number | null;
  onChange: (value: number | null) => void;
  label: string;
  disabled?: boolean;
}) {
  const t = useTranslations();
  return (
    <div
      role="radiogroup"
      aria-label={label}
      className={cn(
        "flex rounded-[9px] p-[3px]",
        disabled ? "ring-1 ring-inset ring-line" : "bg-surface-sunken"
      )}
    >
      {LIMITS.map((limit) => {
        const active = toLimit(value) === limit;
        return (
          <button
            key={limit}
            type="button"
            role="radio"
            aria-checked={active}
            disabled={disabled}
            onClick={() => onChange(limit === "auto" ? null : Number(limit))}
            className={cn(
              "h-7 min-w-8 rounded-md px-2 font-mono text-[12.5px] font-medium",
              active
                ? disabled
                  ? "font-semibold text-fg-strong ring-1 ring-inset ring-control"
                  : "bg-surface-card font-semibold text-fg-strong shadow-segment dark:bg-control"
                : "text-fg-secondary"
            )}
          >
            {limit === "auto" ? t("view.limitAuto") : limit}
          </button>
        );
      })}
    </div>
  );
}

/** Field UI shared by the personal view and a calendar's own view. */
function ViewFields({
  value,
  onChange,
  disabled = false,
  stampBar,
  onlyMyShifts,
  wrapTitles,
}: {
  value: CalendarViewSettings;
  onChange: (patch: Partial<CalendarViewSettings>) => void;
  disabled?: boolean;
  /** Only the personal view carries the stamp-bar toggle */
  stampBar?: { checked: boolean; onChange: (checked: boolean) => void };
  /** Only the personal view carries this, and only for signed-in accounts */
  onlyMyShifts?: {
    checked: boolean;
    onChange: (checked: boolean) => void;
    /** The selected calendar has signups turned off, so the filter would show nothing */
    signupsDisabled?: boolean;
  };
  /** Only the personal view carries the wrapped-title toggle */
  wrapTitles?: { checked: boolean; onChange: (checked: boolean) => void };
}) {
  const t = useTranslations();
  const id = useId();
  const desktop = useMediaQuery(DESKTOP_QUERY, true);
  const weekdays = [
    { day: 1, label: t("view.monday") },
    { day: 2, label: t("view.tuesday") },
    { day: 3, label: t("view.wednesday") },
    { day: 4, label: t("view.thursday") },
    { day: 5, label: t("view.friday") },
    { day: 6, label: t("view.saturday") },
    { day: 0, label: t("view.sunday") },
  ];
  const days = value.highlightedWeekdays;
  // None of these is part of CalendarViewSettings, so a calendar's own view can never take them over
  const hasPersonalSection = !!stampBar || !!onlyMyShifts || !!wrapTitles;

  return (
    <div className="flex flex-col gap-6">
      {hasPersonalSection && (
        <section className="flex flex-col gap-3">
          <SectionLabel className="mb-0">{t("view.personalSection")}</SectionLabel>
          {stampBar && (
            <ToggleRow
              id={`${id}-dock`}
              title={t("view.showPresetBar")}
              description={t("view.showPresetBarHint")}
              checked={stampBar.checked}
              onCheckedChange={stampBar.onChange}
            />
          )}
          {onlyMyShifts && (
            <>
              <ToggleRow
                id={`${id}-only-mine`}
                title={t("view.onlyMyShifts")}
                description={t("view.onlyMyShiftsHint")}
                checked={onlyMyShifts.checked}
                onCheckedChange={onlyMyShifts.onChange}
              />
              {onlyMyShifts.checked && onlyMyShifts.signupsDisabled && (
                <InfoNote icon={Info}>{t("view.onlyMyShiftsSignupsDisabledHint")}</InfoNote>
              )}
            </>
          )}
          {wrapTitles && (
            <ToggleRow
              id={`${id}-wrap-titles`}
              title={t("view.wrapTitles")}
              description={t("view.wrapTitlesHint")}
              checked={wrapTitles.checked}
              onCheckedChange={wrapTitles.onChange}
            />
          )}
        </section>
      )}

      <div className={cn("flex flex-col gap-6", disabled && LOCKED_FIELDS)}>
        <section
          className={cn(
            "flex flex-col gap-3",
            hasPersonalSection && "border-t border-line pt-5"
          )}
        >
          <SectionLabel className="mb-0">{t("view.density")}</SectionLabel>
          <div className="flex items-center justify-between gap-4">
            <div className="min-w-0">
              <div className="text-[14px] font-semibold text-fg-strong">{t("view.shiftsPerDay")}</div>
              <div className="text-[12.5px] text-fg-secondary">{t("view.shiftsPerDayHint")}</div>
            </div>
            <LimitControl
              label={t("view.shiftsPerDay")}
              value={value.shiftsPerDay}
              onChange={(shiftsPerDay) => onChange({ shiftsPerDay })}
              disabled={disabled}
            />
          </div>
          <div className="flex items-center justify-between gap-4">
            <div className="min-w-0">
              <div className="text-[14px] font-semibold text-fg-strong">
                {t("view.externalShiftsPerDay")}
              </div>
              <div className="text-[12.5px] text-fg-secondary">
                {t("view.externalShiftsPerDayHint")}
              </div>
            </div>
            <LimitControl
              label={t("view.externalShiftsPerDay")}
              value={value.externalShiftsPerDay}
              onChange={(externalShiftsPerDay) => onChange({ externalShiftsPerDay })}
              disabled={disabled}
            />
          </div>
          {/* Phone cells have no room for a second line, so the toggle would do nothing there */}
          {desktop && (
            <ToggleRow
              id={`${id}-notes`}
              title={t("view.showNotes")}
              description={t("view.showNotesHint")}
              checked={value.showShiftNotes}
              onCheckedChange={(showShiftNotes) => onChange({ showShiftNotes })}
              disabled={disabled}
            />
          )}
        </section>

        <section className="flex flex-col gap-3 border-t border-line pt-5">
          <SectionLabel className="mb-0">{t("view.sortOptions")}</SectionLabel>
          <Field label={t("view.sortBy")}>
            <ChoiceChips
              value={value.sortType}
              onChange={(sortType) => onChange({ sortType })}
              disabled={disabled}
              options={[
                { value: "startTime", label: t("view.sortByStartTime") },
                { value: "createdAt", label: t("view.sortByCreatedAt") },
                { value: "title", label: t("view.sortByTitle") },
              ]}
            />
          </Field>
          <Field label={t("view.sortOrder")}>
            <ChoiceChips
              value={value.sortOrder}
              onChange={(sortOrder) => onChange({ sortOrder })}
              disabled={disabled}
              options={[
                { value: "asc", label: t("view.sortOrderAsc") },
                { value: "desc", label: t("view.sortOrderDesc") },
              ]}
            />
          </Field>
          <ToggleRow
            id={`${id}-combined`}
            title={t("view.combinedSort")}
            description={t("view.combinedSortHint")}
            checked={value.combinedSort}
            onCheckedChange={(combinedSort) => onChange({ combinedSort })}
            disabled={disabled}
          />
        </section>

        <section className="flex flex-col gap-3 border-t border-line pt-5">
          <SectionLabel className="mb-0">{t("view.dayHighlighting")}</SectionLabel>
          <ToggleRow
            id={`${id}-weekends`}
            title={t("view.highlightWeekends")}
            description={t("view.highlightWeekendsHint")}
            checked={days.includes(0) && days.includes(6)}
            onCheckedChange={(checked) =>
              onChange({
                highlightedWeekdays: checked
                  ? Array.from(new Set([...days, 0, 6]))
                  : days.filter((d) => d !== 0 && d !== 6),
              })
            }
            disabled={disabled}
          />
          <Field label={t("view.customWeekdays")} hint={t("view.customWeekdaysHint")}>
            <div className="grid grid-cols-7 gap-1.5">
              {weekdays.map(({ day, label }) => {
                const selected = days.includes(day);
                return (
                  <button
                    key={day}
                    type="button"
                    aria-pressed={selected}
                    title={label}
                    disabled={disabled}
                    onClick={() =>
                      onChange({
                        highlightedWeekdays: selected
                          ? days.filter((d) => d !== day)
                          : [...days, day],
                      })
                    }
                    className={cn(
                      "h-9 rounded-lg border text-[12.5px] font-semibold",
                      selected ? "border-brand text-brand-ink" : "border-line text-fg-secondary",
                      !disabled && (selected ? "bg-brand-soft" : "bg-surface-card")
                    )}
                  >
                    {label.slice(0, 2)}
                  </button>
                );
              })}
            </div>
          </Field>
          <Field label={t("view.highlightColor")}>
            <ColorSwatches
              size="sm"
              value={value.highlightColor}
              onChange={(highlightColor) => onChange({ highlightColor })}
              disabled={disabled}
            />
          </Field>
        </section>
      </div>
    </div>
  );
}

interface CalendarViewPanelProps {
  calendarId: string;
  personal: PersonalViewSettings;
  onClose: () => void;
  onCancel: () => void;
  onDirtyChange: (dirty: boolean) => void;
}

/**
 * Settings section for a calendar's own view. Off by default; while off,
 * everyone with access sees their personal view instead.
 */
export function CalendarViewPanel(props: CalendarViewPanelProps) {
  const { loading } = useCalendars();

  // The body snapshots the saved view once, so it must not mount before the
  // calendar is cached: it would read "off" and let a Save clear the pinned
  // view for everyone with access.
  if (loading) {
    return (
      <PanelBody>
        <div className="flex justify-center py-6 text-fg-tertiary">
          <Loader2 className="size-5 animate-spin" />
        </div>
      </PanelBody>
    );
  }

  return <CalendarViewPanelBody {...props} />;
}

function CalendarViewPanelBody({
  calendarId,
  personal,
  onClose,
  onCancel,
  onDirtyChange,
}: CalendarViewPanelProps) {
  const t = useTranslations();
  const id = useId();
  const { calendars, updateCalendar } = useCalendars();
  // Calendar-wide view settings are calendar-settings territory, not an editAnyShift
  // approximation — narrower than the old canEdit/canManage coarse checks (see Paket 5b report).
  const { can } = useCalendarPermission(calendarId);
  const canManage = can("manageCalendarSettings");
  const calendar = calendars.find((c) => c.id === calendarId);
  const saved = calendar?.viewSettings ? sanitizeCalendarViewSettings(calendar.viewSettings) : null;
  const personalFields = sanitizeCalendarViewSettings(personal);

  const [enabled, setEnabled] = useState(!!saved);
  const [draft, setDraft] = useState<CalendarViewSettings>(() => saved ?? personalFields);
  const [saving, setSaving] = useState(false);

  const dirty =
    !!calendar &&
    canManage &&
    (enabled !== !!saved || (!!saved && enabled && !calendarViewSettingsEqual(draft, saved)));

  useReportDirty(dirty, onDirtyChange);

  const toggle = (on: boolean) => {
    // Turning it on starts from the saved view, or from the personal one the first time
    if (on) setDraft(saved ?? personalFields);
    setEnabled(on);
  };

  const save = async () => {
    setSaving(true);
    try {
      await updateCalendar(calendarId, { viewSettings: enabled ? draft : null });
      onClose();
    } catch {
      // updateCalendar already reported the error
    } finally {
      setSaving(false);
    }
  };

  const editable = canManage && enabled;
  const shown = canManage ? (enabled ? draft : personalFields) : (saved ?? personalFields);
  const ownView = canManage ? enabled : !!saved;

  return (
    <>
      <PanelBody>
        <div className="flex flex-col gap-5">
          <div className={cn("flex flex-col gap-3", !canManage && LOCKED_FIELDS)}>
            <ToggleRow
              id={`${id}-own`}
              title={t("view.calendarOwn")}
              description={t("view.calendarOwnHint")}
              checked={ownView}
              onCheckedChange={toggle}
              disabled={!canManage}
            />
            {ownView && !canManage && (
              <InfoNote icon={Lock}>{t("view.calendarLocked")}</InfoNote>
            )}
            {!ownView && <InfoNote icon={UserRound}>{t("view.calendarOffNote")}</InfoNote>}
          </div>
          <div className="border-t border-line pt-5">
            <ViewFields
              value={shown}
              onChange={(patch) => setDraft((prev) => ({ ...prev, ...patch }))}
              disabled={!editable}
            />
          </div>
        </div>
      </PanelBody>
      {canManage && (
        <PanelFooter>
          <p className="hidden min-w-0 flex-1 text-[13px] text-fg-secondary lg:block">
            {t("settings.appliesToAll")}
          </p>
          <Button
            variant="outline"
            className="h-10 flex-1 font-semibold lg:flex-none lg:px-4"
            onClick={onCancel}
          >
            {t("common.cancel")}
          </Button>
          <Button
            className="h-10 flex-1 font-semibold lg:flex-none lg:px-4"
            disabled={!dirty || saving}
            onClick={save}
          >
            {saving ? t("common.saving") : t("common.save")}
          </Button>
        </PanelFooter>
      )}
    </>
  );
}

/** Body of "Meine Ansicht"; changes apply immediately. */
export function PersonalViewPanel({
  settings,
  calendarId,
}: {
  settings: ViewSettingsState;
  /** Selected calendar, to point out when its own view takes precedence */
  calendarId?: string | null;
}) {
  const t = useTranslations();
  const { calendars } = useCalendars();
  const calendar = calendarId ? calendars.find((c) => c.id === calendarId) : undefined;
  const { personal, updatePersonal, storedInAccount } = settings;
  // The calendar's own view replaces these fields as a whole; the stamp bar and
  // the own-shifts filter never come from it (see resolveViewSettings), so they stay editable
  const overridden = !!calendar?.viewSettings;

  return (
    <PanelBody>
      <div className="flex flex-col gap-5">
        {overridden && (
          <InfoNote icon={Info}>
            {t("view.overriddenByCalendar", { calendar: calendar!.name })}
          </InfoNote>
        )}
        <ViewFields
          value={personal}
          onChange={updatePersonal}
          disabled={overridden}
          stampBar={{
            checked: personal.showStampBar,
            onChange: (showStampBar) => updatePersonal({ showStampBar }),
          }}
          onlyMyShifts={
            storedInAccount
              ? {
                  checked: personal.onlyMyShifts,
                  onChange: (onlyMyShifts) => updatePersonal({ onlyMyShifts }),
                  signupsDisabled: calendar ? calendar.signupsEnabled === false : false,
                }
              : undefined
          }
          wrapTitles={{
            checked: personal.wrapShiftTitles,
            onChange: (wrapShiftTitles) => updatePersonal({ wrapShiftTitles }),
          }}
        />
      </div>
    </PanelBody>
  );
}

/** "Meine Ansicht": the personal view, applied to every calendar without its own view. */
export function ViewSettingsSheet({
  open,
  onOpenChange,
  settings,
  calendarId,
}: {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  settings: ViewSettingsState;
  calendarId?: string | null;
}) {
  const t = useTranslations();
  return (
    <PanelDialog
      bare
      open={open}
      onOpenChange={onOpenChange}
      title={t("view.settingsTitle")}
      description={
        settings.storedInAccount
          ? t("view.settingsDescription")
          : t("view.settingsDescriptionLocal")
      }
    >
      <PersonalViewPanel settings={settings} calendarId={calendarId} />
    </PanelDialog>
  );
}