Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/app/(frontend)/_components/CalendarButton.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
"use client";

import Link from "next/link";
import { useSyncExternalStore } from "react";
import { useEffect, useState, useSyncExternalStore } from "react";
import { useLanguage } from "@/context/LanguageContext";
import { defaultYear } from "@/lib/calendar/defaults";
import { getCalendarDay } from "@/lib/calendar/schedule";

const subscribe = () => () => {};

const isCalendarSeason = () => {
const getSeason = () => {
const { today, ended } = getCalendarDay(new Date(), {
year: defaultYear,
opensAtHour: 0,
});
return today > 0 && !ended;
if (today === 0) return "before";
return ended ? "after" : "during";
};

/** Shown from 1 to 24 December, Paris time. Checked in the browser so the home page stays static. */
/** Shown from 1 to 24 December, Paris time, and earlier to signed-in members when the admin allows it. Checked in the browser so the home page stays static. */
export default function CalendarButton() {
const { isFrench } = useLanguage();
const inSeason = useSyncExternalStore(subscribe, isCalendarSeason, () => false);
const season = useSyncExternalStore(subscribe, getSeason, () => null);

if (!inSeason) return null;
const [memberPreview, setMemberPreview] = useState(false);

useEffect(() => {
if (season !== "before") return;
fetch("/api/calendar/button", { cache: "no-store" })
.then((response) => (response.ok ? response.json() : null))
.then((body: { show: boolean } | null) => setMemberPreview(body?.show === true))
.catch(() => {});
}, [season]);

if (season !== "during" && !memberPreview) return null;

return (
<Link
Expand Down
11 changes: 11 additions & 0 deletions src/app/(frontend)/api/calendar/button/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NextResponse, type NextRequest } from "next/server";
import { isMember } from "@/lib/calendar/member";
import { getCalendarSettings } from "@/lib/calendar/settings";

/** Whether the home page shows the calendar button before 1 December: signed-in members only, when the admin allows it. */
export async function GET(request: NextRequest) {
const show =
(await isMember(request.headers)) &&
(await getCalendarSettings()).previewButton;
return NextResponse.json({ show }, { headers: { "Cache-Control": "no-store" } });
}
9 changes: 9 additions & 0 deletions src/globals/CalendarSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export const CalendarSettings: GlobalConfig = {
"The calendar opens to visitors by itself on 1 December. Untick to take it down (visitors get a holding page); tick again to bring it back. Signed-in members always see the preview.",
},
},
{
name: "previewButton",
type: "checkbox",
defaultValue: false,
admin: {
description:
"Show the home page calendar button before 1 December to people signed in to this admin. Visitors see nothing different until 1 December.",
},
},
{
name: "year",
type: "number",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/calendar/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { defaultFonts } from "./fontOptions";

export interface CalendarSettings {
visible: boolean;
previewButton: boolean;
year: number;
opensAtHour: number;
afterEnd: "open" | "holding";
Expand Down Expand Up @@ -88,6 +89,7 @@ export async function getCalendarSettings(): Promise<CalendarSettings> {

return {
visible: en.behaviour?.visible ?? true,
previewButton: en.behaviour?.previewButton ?? false,
year: en.behaviour?.year ?? defaultYear,
opensAtHour: en.behaviour?.opensAtHour ?? defaultHour,
afterEnd: en.behaviour?.afterEnd ?? "open",
Expand Down
Loading
Loading