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
+4 -3
View File
@@ -21,10 +21,11 @@ export const MESSAGE_MAX_CHARS = 120;
export type ScreenStatus = 'OPEN' | 'CLOSED' | 'CLOSING_SOON' | 'OPENING_SOON';
/**
* Where the banner text came from: something a person wrote, or the automatic
* notice raised when the week departs from the reference schedule.
* Where the banner text came from: the reason written on today's exception,
* a free message somebody composed, or the automatic notice raised when the
* week departs from the reference schedule.
*/
export type ScreenBannerKind = 'MESSAGE' | 'SCHEDULE_TODAY' | 'SCHEDULE_WEEK';
export type ScreenBannerKind = 'EXCEPTION' | 'MESSAGE' | 'SCHEDULE_TODAY' | 'SCHEDULE_WEEK';
export type ScreenToday = {
date_fr: string;
+47
View File
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest';
import { parsePreviewTime, PREVIEW_RANGE_DAYS } from './preview-time';
const NOW = new Date('2026-09-23T08:00:00Z');
describe('parsePreviewTime', () => {
it('defaults to now when nothing is asked for', () => {
expect(parsePreviewTime(null, NOW)).toEqual({ at: NOW, isNow: true });
});
it('accepts a moment in the future', () => {
const result = parsePreviewTime('2026-09-24T08:00:00Z', NOW);
expect(result.at.toISOString()).toBe('2026-09-24T08:00:00.000Z');
expect(result.isNow).toBe(false);
});
it('accepts a local datetime as a browser sends it', () => {
// <input type="datetime-local"> has no timezone; it is read as local time.
expect(parsePreviewTime('2026-12-24T10:00', NOW).at.getFullYear()).toBe(2026);
});
it('accepts a moment in the past', () => {
expect(parsePreviewTime('2026-09-20T08:00:00Z', NOW).isNow).toBe(false);
});
it('falls back to now on an unparseable value', () => {
// Better a correct screen than a confident rendering of nonsense.
expect(parsePreviewTime('demain matin', NOW)).toEqual({ at: NOW, isNow: true });
expect(parsePreviewTime('', NOW)).toEqual({ at: NOW, isNow: true });
});
it('refuses a moment beyond the planning horizon', () => {
const far = new Date(NOW.getTime() + (PREVIEW_RANGE_DAYS + 1) * 86_400_000);
expect(parsePreviewTime(far.toISOString(), NOW).isNow).toBe(true);
});
it('accepts a moment just inside the horizon', () => {
const edge = new Date(NOW.getTime() + (PREVIEW_RANGE_DAYS - 1) * 86_400_000);
expect(parsePreviewTime(edge.toISOString(), NOW).isNow).toBe(false);
});
it('treats a moment within the minute as now', () => {
const almost = new Date(NOW.getTime() + 30_000);
expect(parsePreviewTime(almost.toISOString(), NOW).isNow).toBe(true);
});
});
+32
View File
@@ -0,0 +1,32 @@
/**
* Reading the moment a preview is asked for.
*
* The whole display pipeline is already a function of "now", so showing the
* panel as it will be next Tuesday costs nothing but passing a different
* instant. What it does need is a bound: an unparseable or absurd value
* should not render a screen for the year 3000 and leave someone believing it.
*/
/** As far ahead or behind as a shop plausibly plans. */
export const PREVIEW_RANGE_DAYS = 400;
export type PreviewTime = { at: Date; isNow: boolean };
export function parsePreviewTime(raw: string | null, now: Date): PreviewTime {
if (!raw) {
return { at: now, isNow: true };
}
const parsed = new Date(raw);
if (Number.isNaN(parsed.getTime())) {
return { at: now, isNow: true };
}
const limit = PREVIEW_RANGE_DAYS * 86_400_000;
const distance = parsed.getTime() - now.getTime();
if (Math.abs(distance) > limit) {
return { at: now, isNow: true };
}
return { at: parsed, isNow: Math.abs(distance) < 60_000 };
}
+131 -2
View File
@@ -212,11 +212,140 @@ describe('buildScreenPayload', () => {
expect(payload.week[3]?.is_closed).toBe(true);
});
it('shows why today’s hours changed, while the door is shut', () => {
// Monday 2026-09-21, closed all day, with a reason written on it.
const payload = buildScreenPayload(
input({
now: new Date('2026-09-21T10:00:00Z'),
ctx: context({
exceptions: [
{
date: '2026-09-21',
isClosed: true,
slots: null,
reason: 'TEMPORARY',
noteFr: 'Fermé pour inventaire, retour mardi',
noteEn: 'Closed for stocktaking, back on Tuesday',
source: 'MANUAL',
},
],
}),
}),
);
expect(payload.banner).toEqual({
kind: 'EXCEPTION',
text_fr: 'Fermé pour inventaire, retour mardi',
text_en: 'Closed for stocktaking, back on Tuesday',
});
});
it('drops the reason the moment the shop opens', () => {
// A late opening, seen from after it: the notice explaining it is worse
// than useless once the door is open.
const lateOpening = context({
exceptions: [
{
date: '2026-09-22',
isClosed: false,
slots: [{ open: '14:00', close: '18:30' }],
reason: 'TEMPORARY',
noteFr: 'Ouverture à 14:00',
noteEn: 'Opening at 2:00 pm',
source: 'MANUAL',
},
],
});
// 12:00 local, still shut: the reason is shown.
expect(
buildScreenPayload(input({ now: new Date('2026-09-22T10:00:00Z'), ctx: lateOpening }))
.banner?.kind,
).toBe('EXCEPTION');
// 15:00 local, open: it is gone, and the automatic notice takes over.
const afterOpening = buildScreenPayload(
input({ now: new Date('2026-09-22T13:00:00Z'), ctx: lateOpening }),
);
expect(afterOpening.banner?.kind).not.toBe('EXCEPTION');
expect(afterOpening.banner?.text_fr).not.toContain('Ouverture à 14:00');
});
it('puts the reason ahead of a free message while the shop is shut', () => {
// "Closed this afternoon" is what someone at the door needs; "new
// collection in store" can wait.
const payload = buildScreenPayload(
input({
now: new Date('2026-09-21T10:00:00Z'),
ctx: context({
exceptions: [
{
date: '2026-09-21',
isClosed: true,
slots: null,
reason: 'TEMPORARY',
noteFr: 'Fermeture exceptionnelle',
noteEn: 'Exceptionally closed',
source: 'MANUAL',
},
],
}),
messages: [
{
textFr: 'Nouvelle collection en boutique',
textEn: 'New collection in store',
startsAt: null,
endsAt: null,
priority: 9,
isActive: true,
},
],
}),
);
expect(payload.banner?.text_fr).toBe('Fermeture exceptionnelle');
});
it('falls back to the free message when the exception has no reason', () => {
const payload = buildScreenPayload(
input({
now: new Date('2026-09-21T10:00:00Z'),
ctx: context({
exceptions: [
{
date: '2026-09-21',
isClosed: true,
slots: null,
reason: 'TEMPORARY',
noteFr: null,
noteEn: null,
source: 'MANUAL',
},
],
}),
messages: [
{
textFr: 'Nouvelle collection',
textEn: 'New collection',
startsAt: null,
endsAt: null,
priority: 0,
isActive: true,
},
],
}),
);
expect(payload.banner).toMatchObject({ kind: 'MESSAGE', text_fr: 'Nouvelle collection' });
});
it('carries no banner when the shop has nothing to say', () => {
expect(buildScreenPayload(input()).banner).toBeNull();
});
it('raises the automatic banner when only today departs from the reference week', () => {
// No reason written on the exception: that is what leaves the automatic
// notice to speak. With one, the reason would take the banner instead.
const payload = buildScreenPayload(
input({
ctx: context({
@@ -226,8 +355,8 @@ describe('buildScreenPayload', () => {
isClosed: false,
slots: [{ open: '14:00', close: '18:30' }],
reason: 'TEMPORARY',
noteFr: 'Ouverture retardée',
noteEn: 'Late opening',
noteFr: null,
noteEn: null,
source: 'MANUAL',
},
],
+24 -3
View File
@@ -26,7 +26,7 @@ import {
type NextChange,
} from '@/lib/schedule/format';
import { detectDivergence, getCurrentStatus, resolveCivilDay } from '@/lib/schedule/resolver';
import type { ScheduleContext } from '@/lib/schedule/types';
import type { ScheduleContext, ShopStatus } from '@/lib/schedule/types';
import {
AUTO_BANNERS,
@@ -112,7 +112,7 @@ export function buildScreenPayload({
is_exception: status.today.isException,
},
week: buildWeek(today, ctx),
banner: buildBanner(now, ctx, messages),
banner: buildBanner(now, ctx, messages, status, isOpenNow),
};
}
@@ -138,8 +138,29 @@ function buildBanner(
now: Date,
ctx: ScheduleContext,
messages: MessageCandidate[],
status: ShopStatus,
isOpenNow: boolean,
): ScreenBanner | null {
// A message someone took the trouble to write always wins the space.
// The reason today's hours changed, while the door is shut.
//
// 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. And it disappears the moment the shop opens — a notice
// explaining a late opening is worse than useless once the door is open.
//
// `isOpenNow` is the state at this minute, not the day's: a day that opens
// at 14:00 has opening hours all morning while its door is shut, and that
// morning is exactly when the reason needs to be on screen.
const exceptionNote = status.today.isException && !isOpenNow ? status.today : null;
if (exceptionNote?.noteFr) {
return {
kind: 'EXCEPTION',
text_fr: clamp(exceptionNote.noteFr),
text_en: clamp(exceptionNote.noteEn ?? ''),
};
}
// Otherwise a message someone took the trouble to write wins the space.
const message = selectActiveMessage(messages, now);
if (message) {
return {
+16
View File
@@ -0,0 +1,16 @@
import type { Template } from '@/app/admin/components/TemplatePicker';
import { prisma } from '@/lib/db';
/**
* The saved phrases, in the order the picker should offer them.
*
* Loaded by every page that composes a message, which is the point: a phrase
* written once should be reachable everywhere without being retyped.
*/
export async function loadTemplates(): Promise<Template[]> {
const rows = await prisma.messageTemplate.findMany({
orderBy: [{ sortOrder: 'asc' }, { createdAt: 'asc' }],
select: { id: true, textFr: true },
});
return rows;
}