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
142 lines
4.5 KiB
TypeScript
142 lines
4.5 KiB
TypeScript
'use client';
|
||
|
||
import { useState } from 'react';
|
||
|
||
/**
|
||
* The panel, at a readable size.
|
||
*
|
||
* Rendered by the same code path that serves the device, so this is not an
|
||
* impression of the screen: it is the screen.
|
||
*
|
||
* With `timeTravel`, it also answers the question the shop actually asks
|
||
* before a holiday — "what will the window say while I'm away?" — without
|
||
* waiting until someone is standing in front of a locked door to find out.
|
||
*/
|
||
export default function ScreenPreview({
|
||
className = '',
|
||
timeTravel = false,
|
||
}: {
|
||
className?: string;
|
||
timeTravel?: boolean;
|
||
}) {
|
||
const [at, setAt] = useState('');
|
||
const [nonce, setNonce] = useState(() => Date.now());
|
||
|
||
const source = at
|
||
? `/api/admin/preview?at=${encodeURIComponent(at)}&t=${nonce}`
|
||
: `/api/admin/preview?t=${nonce}`;
|
||
|
||
function jumpTo(date: Date) {
|
||
setAt(toLocalInput(date));
|
||
setNonce(Date.now());
|
||
}
|
||
|
||
return (
|
||
<figure className={className}>
|
||
<div className="overflow-hidden rounded-[var(--radius-md)] border border-[var(--line-strong)] bg-white">
|
||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||
<img
|
||
key={source}
|
||
src={source}
|
||
alt={
|
||
at
|
||
? `Aperçu de l’écran tel qu’il sera le ${at.replace('T', ' à ')}`
|
||
: 'Aperçu de l’écran de la vitrine'
|
||
}
|
||
width={800}
|
||
height={480}
|
||
className="block h-auto w-full"
|
||
/>
|
||
</div>
|
||
|
||
{timeTravel ? (
|
||
<div className="mt-3 flex flex-wrap items-end gap-x-3 gap-y-2">
|
||
<label className="text-sm">
|
||
<span className="block text-[var(--ink-muted)]">Afficher l’écran au</span>
|
||
<input
|
||
type="datetime-local"
|
||
value={at}
|
||
onChange={(event) => {
|
||
setAt(event.target.value);
|
||
setNonce(Date.now());
|
||
}}
|
||
className="mt-1 rounded-[var(--radius-sm)] border border-[var(--line-strong)] bg-[var(--surface)] px-2 py-1.5 text-base"
|
||
/>
|
||
</label>
|
||
|
||
<div className="flex flex-wrap gap-2 text-sm">
|
||
<Jump label="Demain 10:00" onClick={() => jumpTo(tomorrowAt(10))} />
|
||
<Jump label="Samedi 10:00" onClick={() => jumpTo(nextWeekdayAt(6, 10))} />
|
||
<Jump label="Dans 1 mois" onClick={() => jumpTo(inDays(30, 10))} />
|
||
{at ? (
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
setAt('');
|
||
setNonce(Date.now());
|
||
}}
|
||
className="rounded-[var(--radius-sm)] border border-[var(--accent)] px-3 py-1.5 text-[var(--accent-ink)] hover:bg-[var(--accent-tint)]"
|
||
>
|
||
Revenir à maintenant
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
) : null}
|
||
|
||
<figcaption className="mt-2 flex flex-wrap items-center gap-3 text-xs text-[var(--ink-muted)]">
|
||
<span>
|
||
{at
|
||
? 'Projection : l’écran tel qu’il sera à cette date, avec les horaires et fermetures enregistrés.'
|
||
: '800 × 480, noir et blanc — rendu réellement envoyé à l’écran.'}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
onClick={() => setNonce(Date.now())}
|
||
className="underline-offset-2 hover:underline"
|
||
>
|
||
Rafraîchir
|
||
</button>
|
||
</figcaption>
|
||
</figure>
|
||
);
|
||
}
|
||
|
||
function Jump({ label, onClick }: { label: string; onClick: () => void }) {
|
||
return (
|
||
<button
|
||
type="button"
|
||
onClick={onClick}
|
||
className="rounded-[var(--radius-sm)] border border-[var(--line-strong)] px-3 py-1.5 hover:bg-[var(--surface-muted)]"
|
||
>
|
||
{label}
|
||
</button>
|
||
);
|
||
}
|
||
|
||
/** `datetime-local` speaks local wall-clock time with no zone, so build one. */
|
||
function toLocalInput(date: Date): string {
|
||
const pad = (value: number) => value.toString().padStart(2, '0');
|
||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(
|
||
date.getHours(),
|
||
)}:${pad(date.getMinutes())}`;
|
||
}
|
||
|
||
function tomorrowAt(hour: number): Date {
|
||
return inDays(1, hour);
|
||
}
|
||
|
||
function inDays(days: number, hour: number): Date {
|
||
const date = new Date();
|
||
date.setDate(date.getDate() + days);
|
||
date.setHours(hour, 0, 0, 0);
|
||
return date;
|
||
}
|
||
|
||
/** The next occurrence of a weekday, 0 = Sunday. */
|
||
function nextWeekdayAt(weekday: number, hour: number): Date {
|
||
const date = new Date();
|
||
const ahead = (weekday - date.getDay() + 7) % 7 || 7;
|
||
return inDays(ahead, hour);
|
||
}
|