feat: explain a schedule change on the panel, and preview it in advance

Two things the shop asked for, and one it will notice.

**A message on an exception.** A closure or a late opening can now carry
a reason, and it reaches the panel. It shows while the door is shut and
disappears the moment the shop opens — a notice explaining a late
opening is worse than useless once the door is open.

It outranks a free message on purpose: "closed this afternoon, back
tomorrow" is what someone standing outside needs, and "new collection in
store" can wait. Say so if you would rather it were the other way.

The first version keyed this off the day's `isOpen`, which means "this
day has opening hours" — so a day opening at 14:00 counted as open all
morning, exactly when the reason is needed. A test caught it; the rule
now reads the state at this minute.

**Reusable phrases.** The same handful of notices get written over and
over. /admin/modeles keeps them, translated once, and offers them
wherever a message is composed — exceptions, closure periods, the
banner. Picking one costs no translation at all: the saved English is
reused directly, where the service takes the better part of ten seconds.

Exception notes and holiday labels are translated too, which they were
not before. After the save rather than during it, so a slow service
never costs the shop its dates.

**Previewing the future.** The dashboard can render the panel at any
moment within about a year: "what will the window say while I'm away?"
is worth answering before someone is standing in front of a locked door.
The whole pipeline was already a function of "now", so this costs
passing a different instant. An unparseable or absurd value falls back
to the present rather than confidently rendering nonsense.

Checked against a three-week holiday, which surfaced something worth
knowing: the automatic "opens on…" line stays empty, because the
resolver's search is bounded to fourteen days. During a long closure the
message is the only thing that tells customers when the shop is back —
which is a good reason for this feature to exist.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
2026-09-23 09:34:14 +02:00
co-authored by Claude Opus 5
parent 743a3778cc
commit 5f7aa951d3
29 changed files with 1141 additions and 59 deletions
+14 -5
View File
@@ -2,9 +2,10 @@
import { useState, useTransition } from 'react';
import TemplatePicker, { type Template } from '@/app/admin/components/TemplatePicker';
import { countDays } from '@/lib/schedule/vacations';
import { createVacation, deleteVacation } from './actions';
import { createVacation, deleteVacation, translateVacationLabel } from './actions';
export type VacationRow = {
id: string;
@@ -18,9 +19,11 @@ export type VacationRow = {
export default function VacationManager({
canEdit,
periods,
templates,
}: {
canEdit: boolean;
periods: VacationRow[];
templates: Template[];
}) {
const [startDate, setStartDate] = useState('');
const [endDate, setEndDate] = useState('');
@@ -97,6 +100,7 @@ export default function VacationManager({
className="mt-1 w-full rounded-[var(--radius-sm)] border border-[var(--line-strong)] bg-[var(--surface)] px-2 py-2 text-base"
/>
</label>
<TemplatePicker templates={templates} onPick={setLabelFr} disabled={pending} />
</div>
<div className="mt-4 flex flex-wrap items-center gap-3">
@@ -104,10 +108,15 @@ export default function VacationManager({
type="button"
disabled={pending || !startDate || !endDate || labelFr.trim().length === 0}
onClick={() =>
run(
() => createVacation({ startDate, endDate, labelFr }),
'Période de fermeture enregistrée.',
)
run(async () => {
const created = await createVacation({ startDate, endDate, labelFr });
if (created.ok) {
// After the fact: the dates matter more than the English,
// and the translation service is measured in seconds.
await translateVacationLabel(created.id);
}
return created;
}, 'Période de fermeture enregistrée.')
}
className="rounded-[var(--radius-md)] bg-[var(--accent)] px-4 py-2.5 font-medium text-[var(--on-accent)] transition-colors hover:bg-[var(--accent-hover)] disabled:opacity-40"
>
+38 -2
View File
@@ -7,6 +7,7 @@ import { requireAdmin } from '@/lib/auth/actions';
import { prisma } from '@/lib/db';
import type { CivilDate } from '@/lib/schedule/civil';
import { countDays, findOverlap, validatePeriod } from '@/lib/schedule/vacations';
import { translateToEnglish } from '@/lib/translation/service';
export type ActionResult = { ok: true } | { ok: false; error: string };
@@ -14,7 +15,9 @@ export async function createVacation(input: {
startDate: CivilDate;
endDate: CivilDate;
labelFr: string;
}): Promise<ActionResult> {
// Returns the new id so the caller can translate it without having to ask
// which row was "the last one", which would be a race.
}): Promise<{ ok: true; id: string } | { ok: false; error: string }> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
@@ -45,11 +48,17 @@ export async function createVacation(input: {
};
}
// A phrase saved as a template is already translated; reusing it is instant
// where the service takes seconds. The label reaches the panel during the
// closure, so it needs its English.
const fromTemplate = await prisma.messageTemplate.findUnique({ where: { textFr: labelFr } });
const created = await prisma.vacationPeriod.create({
data: {
startDate: asDate(input.startDate),
endDate: asDate(input.endDate),
labelFr,
labelEn: fromTemplate?.textEn ?? null,
createdBy: gate.value.email,
},
});
@@ -68,7 +77,7 @@ export async function createVacation(input: {
});
revalidateAll();
return { ok: true };
return { ok: true, id: created.id };
}
export async function deleteVacation(id: string): Promise<ActionResult> {
@@ -116,3 +125,30 @@ function revalidateAll(): void {
revalidatePath('/admin/vacances');
revalidatePath('/admin/exceptions');
}
/**
* Translates a period's label, if it still needs one.
*
* Separate from the creation, so a slow translation service never costs the
* shop its holiday dates. A failure leaves the French alone on screen.
*/
export async function translateVacationLabel(id: string): Promise<ActionResult> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const existing = await prisma.vacationPeriod.findUnique({ where: { id } });
if (!existing || existing.labelEn) {
return { ok: true };
}
const outcome = await translateToEnglish(existing.labelFr);
if (!outcome.ok) {
return { ok: false, error: outcome.error };
}
await prisma.vacationPeriod.update({ where: { id }, data: { labelEn: outcome.text } });
revalidateAll();
return { ok: true };
}
+7 -1
View File
@@ -3,6 +3,7 @@ import { prisma } from '@/lib/db';
import { toCivilDate } from '@/lib/schedule/civil';
import { loadSettings } from '@/lib/schedule/context';
import { periodsOverlap, upcomingPeriods } from '@/lib/schedule/vacations';
import { loadTemplates } from '@/lib/templates';
import VacationManager, { type VacationRow } from './VacationManager';
import YearCalendar from './YearCalendar';
@@ -10,7 +11,11 @@ import YearCalendar from './YearCalendar';
export const metadata = { title: 'Vacances — ITA ITO' };
export default async function VacationsPage() {
const [session, settings] = await Promise.all([auth(), loadSettings()]);
const [session, settings, templates] = await Promise.all([
auth(),
loadSettings(),
loadTemplates(),
]);
const today = toCivilDate(new Date(), settings.timezone);
const rows = await prisma.vacationPeriod.findMany({ orderBy: { startDate: 'asc' } });
@@ -39,6 +44,7 @@ export default async function VacationsPage() {
<VacationManager
canEdit={session?.user?.role === 'admin'}
periods={upcomingPeriods(periods, today)}
templates={templates}
/>
<section className="mt-10">