'use client' import { useCallback, useEffect, useId, useRef, useState } from 'react' import type { ConstructionMode } from './types.js' import { isPathAllowed } from './logic.js' import { constructionCloseLabel } from './texts.js' import { CONSTRUCTION_NOTICE_PARAM, CONSTRUCTION_NOTICE_VALUE } from './types.js' export type ConstructionNoticeClassNames = { button?: string dialog?: string message?: string overlay?: string title?: string } /** * The maintenance mode notice (Site settings → Maintenance). * Opens after a redirect from a blocked address (?notice=construction, then * removed from the URL) and when a visitor clicks a link to a page that isn't * available — the click is cancelled. Closes with its button or Escape. * Renders nothing while the mode is off. * * Unstyled (classNames, like CookieBanner); texts come from the panel. With no * button text set, the button shows "×" and gets a screen-reader name in the * page language ("Close" / "Zamknij"): `locale`, else `mode.locale`. * * // [locale]/layout.tsx * */ export function ConstructionNotice({ classNames: cn = {}, closeLabel, locale, mode, }: { classNames?: ConstructionNoticeClassNames /** Screen-reader name of the "×" button. Default: "Close" / "Zamknij" by page language. */ closeLabel?: string /** Page language. Default: `mode.locale` (set by getConstructionMode). */ locale?: string mode: ConstructionMode }) { const [open, setOpen] = useState(false) const buttonRef = useRef(null) const titleId = useId() const close = useCallback(() => setOpen(false), []) const { allowedPaths, enabled } = mode useEffect(() => { if (!enabled) {return} // After a redirect: show the notice once and clean the address bar const url = new URL(window.location.href) if (url.searchParams.get(CONSTRUCTION_NOTICE_PARAM) === CONSTRUCTION_NOTICE_VALUE) { setOpen(true) url.searchParams.delete(CONSTRUCTION_NOTICE_PARAM) window.history.replaceState(window.history.state, '', `${url.pathname}${url.search}${url.hash}`) } // Capture phase, before Next's — a cancelled click doesn't navigate const onClick = (event: MouseEvent) => { if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {return} const anchor = event.target instanceof Element ? event.target.closest('a[href]') : null if (!(anchor instanceof HTMLAnchorElement) || anchor.hasAttribute('download')) {return} if (anchor.target && anchor.target !== '_self') {return} const target = new URL(anchor.href, window.location.href) if (target.origin !== window.location.origin || isPathAllowed(target.pathname, allowedPaths)) {return} event.preventDefault() event.stopPropagation() setOpen(true) } document.addEventListener('click', onClick, true) return () => document.removeEventListener('click', onClick, true) }, [enabled, allowedPaths]) useEffect(() => { if (!open) {return} buttonRef.current?.focus() const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape') {close()} } document.addEventListener('keydown', onKey) return () => document.removeEventListener('keydown', onKey) }, [open, close]) if (!enabled || !open) {return null} return ( {mode.texts.title ? {mode.texts.title} : null} {mode.texts.message ? {mode.texts.message} : null} {mode.texts.button ?? '×'} ) }
{mode.texts.title}
{mode.texts.message}