@profitstudio/booking-engine 0.0.0-stage → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/CHANGELOG.md +562 -0
  2. package/LICENSE +21 -0
  3. package/README.md +18 -2
  4. package/THIRD_PARTY_NOTICES.md +41 -0
  5. package/bin/booking-engine.mjs +575 -0
  6. package/bin/connect.mjs +408 -0
  7. package/bin/photos.mjs +82 -0
  8. package/dist/admin/BookingAdmin.d.ts +22 -0
  9. package/dist/admin/BookingAdmin.js +337 -0
  10. package/dist/admin/admin.css +1423 -0
  11. package/dist/admin/alerts.d.ts +10 -0
  12. package/dist/admin/alerts.js +11 -0
  13. package/dist/admin/booking-detail.d.ts +37 -0
  14. package/dist/admin/booking-detail.js +149 -0
  15. package/dist/admin/content-inputs.d.ts +15 -0
  16. package/dist/admin/content-inputs.js +105 -0
  17. package/dist/admin/context.d.ts +37 -0
  18. package/dist/admin/context.js +8 -0
  19. package/dist/admin/drawer.d.ts +14 -0
  20. package/dist/admin/drawer.js +56 -0
  21. package/dist/admin/feedback.d.ts +32 -0
  22. package/dist/admin/feedback.js +70 -0
  23. package/dist/admin/fields.d.ts +27 -0
  24. package/dist/admin/fields.js +146 -0
  25. package/dist/admin/fonts/OFL-Geist-Mono.txt +93 -0
  26. package/dist/admin/fonts/OFL-Geist.txt +93 -0
  27. package/dist/admin/fonts/geist-latin-wght-normal.woff2 +0 -0
  28. package/dist/admin/fonts/geist-mono-latin-wght-normal.woff2 +0 -0
  29. package/dist/admin/icons.d.ts +66 -0
  30. package/dist/admin/icons.js +72 -0
  31. package/dist/admin/image-upload.d.ts +58 -0
  32. package/dist/admin/image-upload.js +111 -0
  33. package/dist/admin/index.d.ts +6 -0
  34. package/dist/admin/index.js +6 -0
  35. package/dist/admin/lucide.generated.d.ts +521 -0
  36. package/dist/admin/lucide.generated.js +76 -0
  37. package/dist/admin/sections/BookingsSection.d.ts +1 -0
  38. package/dist/admin/sections/BookingsSection.js +137 -0
  39. package/dist/admin/sections/EmailSection.d.ts +8 -0
  40. package/dist/admin/sections/EmailSection.js +182 -0
  41. package/dist/admin/sections/HoursSection.d.ts +1 -0
  42. package/dist/admin/sections/HoursSection.js +110 -0
  43. package/dist/admin/sections/OverviewSection.d.ts +1 -0
  44. package/dist/admin/sections/OverviewSection.js +427 -0
  45. package/dist/admin/sections/ServicesSection.d.ts +1 -0
  46. package/dist/admin/sections/ServicesSection.js +236 -0
  47. package/dist/admin/sections/SettingsSection.d.ts +10 -0
  48. package/dist/admin/sections/SettingsSection.js +96 -0
  49. package/dist/admin/sections/WebsiteSection.d.ts +1 -0
  50. package/dist/admin/sections/WebsiteSection.js +228 -0
  51. package/dist/admin/sections.d.ts +22 -0
  52. package/dist/admin/sections.js +29 -0
  53. package/dist/admin/setup.d.ts +1 -0
  54. package/dist/admin/setup.js +45 -0
  55. package/dist/admin/today.d.ts +43 -0
  56. package/dist/admin/today.js +115 -0
  57. package/dist/admin/ui.d.ts +134 -0
  58. package/dist/admin/ui.js +220 -0
  59. package/dist/calendar.d.ts +64 -0
  60. package/dist/calendar.js +139 -0
  61. package/dist/client.d.ts +147 -0
  62. package/dist/client.js +507 -0
  63. package/dist/config.d.ts +25 -0
  64. package/dist/config.js +120 -0
  65. package/dist/content/image.d.ts +20 -0
  66. package/dist/content/image.js +90 -0
  67. package/dist/content/resolve.d.ts +12 -0
  68. package/dist/content/resolve.js +73 -0
  69. package/dist/content/schema.d.ts +119 -0
  70. package/dist/content/schema.js +53 -0
  71. package/dist/content/store.d.ts +48 -0
  72. package/dist/content/store.js +123 -0
  73. package/dist/content/validate.d.ts +33 -0
  74. package/dist/content/validate.js +177 -0
  75. package/dist/errors.d.ts +19 -0
  76. package/dist/errors.js +94 -0
  77. package/dist/generated/manifest.d.ts +6 -0
  78. package/dist/generated/manifest.js +7 -0
  79. package/dist/index.d.ts +20 -0
  80. package/dist/index.js +20 -0
  81. package/dist/presets/hair-salon.d.ts +8 -0
  82. package/dist/presets/hair-salon.js +94 -0
  83. package/dist/presets/index.d.ts +5 -0
  84. package/dist/presets/index.js +6 -0
  85. package/dist/preview.d.ts +18 -0
  86. package/dist/preview.js +134 -0
  87. package/dist/semver.d.ts +16 -0
  88. package/dist/semver.js +55 -0
  89. package/dist/time.d.ts +31 -0
  90. package/dist/time.js +138 -0
  91. package/dist/types.d.ts +477 -0
  92. package/dist/types.js +3 -0
  93. package/dist/vite.d.ts +52 -0
  94. package/dist/vite.js +150 -0
  95. package/docs/BUILD-A-SITE.md +203 -0
  96. package/docs/EMAIL-SETUP.md +224 -0
  97. package/docs/INTEGRATION.md +1109 -0
  98. package/engine.manifest.json +90 -0
  99. package/functions/booking-mailer/deno.json +6 -0
  100. package/functions/booking-mailer/index.ts +44 -0
  101. package/functions/booking-mailer/mailer.js +156 -0
  102. package/functions/booking-mailer/providers/index.js +42 -0
  103. package/functions/booking-mailer/providers/resend.js +75 -0
  104. package/functions/booking-mailer/templates.js +345 -0
  105. package/migrations/0001_core.sql +1673 -0
  106. package/migrations/0002_security_hardening.sql +711 -0
  107. package/migrations/0003_private_definers_and_holds.sql +808 -0
  108. package/migrations/0004_verified_requests.sql +1702 -0
  109. package/migrations/0005_scoped_mailer_credential.sql +347 -0
  110. package/migrations/0006_trusted_client_ip.sql +56 -0
  111. package/migrations/0007_site_content.sql +380 -0
  112. package/migrations/0008_service_images_opening_times.sql +328 -0
  113. package/package.json +85 -4
  114. package/templates/BOOKING_ENGINE.md +61 -0
@@ -0,0 +1,337 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
3
+ import { presets as builtInPresets } from '../presets/index.js';
4
+ import { formatTime, timeZoneLabel } from '../time.js';
5
+ import { ALERTS_VIEW, alertsAddress } from './alerts.js';
6
+ import { AdminContext, useAdmin } from './context.js';
7
+ import { ToastRegion, useConfirm, useToasts } from './feedback.js';
8
+ import { Icon } from './icons.js';
9
+ import { ADMIN_SECTIONS, NAV_GROUPS } from './sections.js';
10
+ import { Button, Field, IconButton, Kbd, Notice, monogram, useNow } from './ui.js';
11
+ const APPEARANCE_KEY = 'pbe-admin-appearance';
12
+ /** The owner's Light / Dark / System choice (kept in this browser) and the mode it resolves to. */
13
+ function useAppearance() {
14
+ const [appearance, setChoice] = useState(() => {
15
+ try {
16
+ const v = window.localStorage.getItem(APPEARANCE_KEY);
17
+ return v === 'light' || v === 'dark' ? v : 'system';
18
+ }
19
+ catch {
20
+ return 'system';
21
+ }
22
+ });
23
+ const query = useMemo(() => (typeof window !== 'undefined' && window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null), []);
24
+ const [systemDark, setSystemDark] = useState(() => query?.matches ?? false);
25
+ useEffect(() => {
26
+ if (!query)
27
+ return;
28
+ const onChange = (e) => setSystemDark(e.matches);
29
+ query.addEventListener('change', onChange);
30
+ return () => query.removeEventListener('change', onChange);
31
+ }, [query]);
32
+ const setAppearance = useCallback((a) => {
33
+ setChoice(a);
34
+ try {
35
+ window.localStorage.setItem(APPEARANCE_KEY, a);
36
+ }
37
+ catch {
38
+ // Private mode: the choice lasts for this page only.
39
+ }
40
+ }, []);
41
+ return { appearance, setAppearance, mode: appearance === 'system' ? (systemDark ? 'dark' : 'light') : appearance };
42
+ }
43
+ function AppearanceSwitch({ value, onChange }) {
44
+ const options = [
45
+ ['light', 'Light', 'sun'],
46
+ ['dark', 'Dark', 'moon'],
47
+ ['system', 'System', 'system'],
48
+ ];
49
+ return (_jsx("div", { className: "pbe-appearance", role: "group", "aria-label": "Appearance", children: options.map(([id, label, icon]) => (_jsxs("button", { type: "button", "aria-pressed": value === id, "aria-label": label, title: label, onClick: () => onChange(id), children: [_jsx(Icon, { name: icon }), _jsx("span", { className: "pbe-appearance-label", "aria-hidden": "true", children: label })] }, id))) }));
50
+ }
51
+ const isMac = () => typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);
52
+ /** "#bookings/pending" → section "bookings", view "pending". */
53
+ function routeFromHash() {
54
+ const [id = '', view] = (typeof window === 'undefined' ? '' : window.location.hash.replace(/^#/, '')).split('/');
55
+ return ADMIN_SECTIONS.some((s) => s.id === id) ? { section: id, view: view ?? null } : { section: 'overview', view: null };
56
+ }
57
+ /**
58
+ * The packaged admin dashboard. Mount it on its own page (e.g. /admin/).
59
+ * It handles sign-in, admin checks and every management screen.
60
+ */
61
+ export function BookingAdmin({ engine, presets, siteUrl = '/', content }) {
62
+ const [phase, setPhase] = useState({ kind: 'checking' });
63
+ const { appearance, setAppearance, mode } = useAppearance();
64
+ const rootProps = { 'data-appearance': appearance, 'data-mode': mode };
65
+ const [route, setRoute] = useState(routeFromHash);
66
+ const section = route.section;
67
+ const [moreOpen, setMoreOpen] = useState(false);
68
+ const [pendingCount, setPendingCount] = useState(null);
69
+ const [website, setWebsite] = useState(null);
70
+ const presetList = useMemo(() => presets ?? Object.values(builtInPresets), [presets]);
71
+ const { toasts, toast, dismiss } = useToasts();
72
+ const { confirm, dialog } = useConfirm();
73
+ const moreButton = useRef(null);
74
+ const sheet = useRef(null);
75
+ const evaluate = useCallback(async () => {
76
+ if (engine.mode !== 'live') {
77
+ setPhase({ kind: 'unconfigured' });
78
+ return;
79
+ }
80
+ const info = await engine.getInfo();
81
+ if (!info.ok) {
82
+ setPhase({ kind: 'database', error: info.error });
83
+ return;
84
+ }
85
+ if (!info.data.compatible) {
86
+ setPhase({
87
+ kind: 'database',
88
+ error: {
89
+ code: 'SCHEMA_INCOMPATIBLE',
90
+ message: `The database is at Booking Engine schema ${info.data.schemaVersion}; this dashboard needs ${info.data.requiredSchemaVersion} (same major version or newer).`,
91
+ },
92
+ });
93
+ return;
94
+ }
95
+ const email = await engine.auth.getUserEmail();
96
+ if (!email) {
97
+ setPhase({ kind: 'signed-out' });
98
+ return;
99
+ }
100
+ const who = await engine.admin.whoami();
101
+ if (!who.ok) {
102
+ setPhase(who.error.code === 'NOT_AUTHORIZED' ? { kind: 'signed-out' } : { kind: 'database', error: who.error });
103
+ return;
104
+ }
105
+ if (!who.data.isAdmin) {
106
+ setPhase({ kind: 'not-admin', email: who.data.email ?? email });
107
+ return;
108
+ }
109
+ const settings = await engine.admin.getSettings();
110
+ if (!settings.ok) {
111
+ setPhase({ kind: 'database', error: settings.error });
112
+ return;
113
+ }
114
+ setPhase({ kind: 'ready', email, settings: settings.data });
115
+ }, [engine]);
116
+ useEffect(() => {
117
+ void evaluate();
118
+ return engine.auth.onChange(() => void evaluate());
119
+ }, [engine, evaluate]);
120
+ useEffect(() => {
121
+ const onHash = () => setRoute(routeFromHash());
122
+ window.addEventListener('hashchange', onHash);
123
+ return () => window.removeEventListener('hashchange', onHash);
124
+ }, []);
125
+ const ready = phase.kind === 'ready';
126
+ const timezone = phase.kind === 'ready' ? phase.settings.timezone : null;
127
+ const refreshCounts = useCallback(() => {
128
+ if (!timezone)
129
+ return;
130
+ const today = new Intl.DateTimeFormat('en-CA', { timeZone: timezone }).format(new Date());
131
+ const until = new Date(Date.parse(`${today}T00:00:00Z`) + 365 * 86_400_000).toISOString().slice(0, 10);
132
+ void engine.admin.listBookings({ from: today, to: until, statuses: ['pending', 'expired'] }).then((r) => {
133
+ if (r.ok)
134
+ setPendingCount(r.data.length);
135
+ });
136
+ }, [engine, timezone]);
137
+ useEffect(() => {
138
+ if (ready)
139
+ refreshCounts();
140
+ }, [ready, refreshCounts]);
141
+ const refreshWebsite = useCallback(() => {
142
+ if (!content)
143
+ return;
144
+ void engine.admin.getSiteContent().then((r) => {
145
+ if (r.ok)
146
+ setWebsite({ version: r.data.version, publishedAt: r.data.publishedAt, hasUnpublishedChanges: r.data.hasUnpublishedChanges });
147
+ });
148
+ }, [engine, content]);
149
+ useEffect(() => {
150
+ if (ready)
151
+ refreshWebsite();
152
+ }, [ready, refreshWebsite]);
153
+ const refreshSettings = useCallback(async () => {
154
+ const r = await engine.admin.getSettings();
155
+ if (r.ok)
156
+ setPhase((p) => (p.kind === 'ready' ? { ...p, settings: r.data } : p));
157
+ }, [engine]);
158
+ const go = useCallback((id, view) => {
159
+ setRoute({ section: id, view: view ?? null });
160
+ setMoreOpen(false);
161
+ if (typeof window !== 'undefined') {
162
+ window.history.replaceState(null, '', `#${id}${view && view !== 'search' ? `/${view}` : ''}`);
163
+ window.scrollTo({ top: 0 });
164
+ // Tell assistive technology the page changed (the search view focuses its own field).
165
+ if (view !== 'search')
166
+ window.requestAnimationFrame(() => document.getElementById('pbe-main')?.focus({ preventScroll: true }));
167
+ }
168
+ }, []);
169
+ // ⌘K / Ctrl+K: search bookings from anywhere.
170
+ useEffect(() => {
171
+ if (!ready)
172
+ return;
173
+ const onKey = (e) => {
174
+ if ((e.metaKey || e.ctrlKey) && !e.altKey && !e.shiftKey && e.key.toLowerCase() === 'k') {
175
+ e.preventDefault();
176
+ go('bookings', 'search');
177
+ }
178
+ };
179
+ window.addEventListener('keydown', onKey);
180
+ return () => window.removeEventListener('keydown', onKey);
181
+ }, [ready, go]);
182
+ // The phone "More" sheet: focus moves in, Esc closes it and gives focus back.
183
+ useEffect(() => {
184
+ if (!moreOpen)
185
+ return;
186
+ sheet.current?.querySelector('a, button')?.focus();
187
+ const onKey = (e) => {
188
+ if (e.key === 'Escape') {
189
+ setMoreOpen(false);
190
+ moreButton.current?.focus();
191
+ }
192
+ };
193
+ document.addEventListener('keydown', onKey);
194
+ return () => document.removeEventListener('keydown', onKey);
195
+ }, [moreOpen]);
196
+ if (phase.kind === 'checking') {
197
+ return (_jsx("div", { className: "pbe-admin pbe-center", ...rootProps, children: _jsxs("div", { className: "pbe-loading-screen", role: "status", children: [_jsx("span", { className: "pbe-spinner", "aria-hidden": "true" }), _jsx("span", { children: "Opening your dashboard\u2026" })] }) }));
198
+ }
199
+ if (phase.kind === 'unconfigured')
200
+ return _jsx(Unconfigured, { mode: engine.mode, rootProps: rootProps });
201
+ if (phase.kind === 'database')
202
+ return _jsx(DatabaseProblem, { error: phase.error, onRetry: evaluate, rootProps: rootProps });
203
+ if (phase.kind === 'signed-out')
204
+ return _jsx(SignIn, { engine: engine, siteUrl: siteUrl, rootProps: rootProps });
205
+ if (phase.kind === 'not-admin')
206
+ return _jsx(NotAdmin, { engine: engine, email: phase.email, rootProps: rootProps });
207
+ const context = {
208
+ engine,
209
+ settings: phase.settings,
210
+ refreshSettings,
211
+ presets: presetList,
212
+ go,
213
+ view: route.view,
214
+ pendingCount,
215
+ refreshCounts,
216
+ contentSchema: content ?? null,
217
+ siteUrl,
218
+ website,
219
+ refreshWebsite,
220
+ toast,
221
+ confirm,
222
+ };
223
+ const current = ADMIN_SECTIONS.find((s) => s.id === section) ?? ADMIN_SECTIONS[0];
224
+ const Current = current.component;
225
+ const business = phase.settings.businessName;
226
+ const mark = monogram(business);
227
+ const signOut = () => void engine.auth.signOut();
228
+ const openSearch = () => go('bookings', 'search');
229
+ const primary = ADMIN_SECTIONS.filter((s) => s.primary);
230
+ const secondary = ADMIN_SECTIONS.filter((s) => !s.primary);
231
+ const websiteWord = website ? (website.hasUnpublishedChanges ? 'Draft' : website.version > 0 ? 'Live' : 'Default') : null;
232
+ const navLink = (s, variant) => {
233
+ const active = s.id === section;
234
+ const describedBy = s.id === 'bookings' && pendingCount ? 'pbe-desc-pending' : s.id === 'website' && websiteWord ? 'pbe-desc-website' : undefined;
235
+ const open = (e) => {
236
+ e.preventDefault();
237
+ go(s.id);
238
+ };
239
+ if (variant === 'tab') {
240
+ return (_jsxs("a", { href: `#${s.id}`, className: "pbe-tab-link", "aria-current": active ? 'page' : undefined, "aria-describedby": describedBy, onClick: open, children: [_jsx("span", { className: "pbe-tab-icon", children: _jsx(Icon, { name: s.icon, size: 20 }) }), _jsx("span", { children: s.label }), s.id === 'bookings' && pendingCount ? (_jsx("span", { className: "pbe-tab-badge", "aria-hidden": "true", children: pendingCount })) : null] }));
241
+ }
242
+ return (_jsxs("a", { href: `#${s.id}`, className: "pbe-nav-item", "aria-current": active ? 'page' : undefined, "aria-describedby": describedBy, onClick: open, children: [_jsx(Icon, { name: s.icon }), _jsx("span", { className: "pbe-nav-text", children: s.label }), variant === 'sidebar' && s.id === 'bookings' && pendingCount ? (_jsx("span", { className: "pbe-count", "aria-hidden": "true", children: pendingCount })) : null, variant === 'sidebar' && s.id === 'website' && websiteWord ? (_jsxs("span", { className: "pbe-nav-meta", "aria-hidden": "true", children: [_jsx("span", { className: `pbe-dot${websiteWord === 'Live' ? ' pbe-dot--ok' : websiteWord === 'Draft' ? ' pbe-dot--warn' : ' pbe-dot--off'}` }), websiteWord] })) : null] }));
243
+ };
244
+ return (_jsx(AdminContext.Provider, { value: context, children: _jsxs("div", { className: "pbe-admin pbe-app", ...rootProps, children: [_jsx("a", { className: "pbe-skip", href: "#pbe-main", children: "Skip to content" }), _jsx("span", { id: "pbe-desc-pending", hidden: true, children: pendingCount === 1 ? '1 request needs your answer' : `${pendingCount ?? 0} requests need your answer` }), _jsx("span", { id: "pbe-desc-website", hidden: true, children: websiteWord === 'Live' ? `Live, version ${website?.version}` : websiteWord === 'Draft' ? 'Draft changes not published' : 'Showing its built-in text' }), _jsxs("div", { className: "pbe-sidebar", children: [_jsxs("div", { className: "pbe-brand", children: [_jsx("span", { className: "pbe-monogram", "aria-hidden": "true", children: mark }), _jsx("span", { className: "pbe-business", title: business, children: business })] }), _jsxs("button", { type: "button", className: "pbe-search-launch", onClick: openSearch, children: [_jsx(Icon, { name: "search" }), _jsx("span", { children: "Search bookings" }), _jsx(Kbd, { children: isMac() ? '⌘K' : 'Ctrl K' })] }), _jsx("nav", { className: "pbe-nav", "aria-label": "Dashboard sections", children: NAV_GROUPS.map((g) => (_jsxs("div", { className: "pbe-nav-group", children: [g.label ? (_jsx("p", { className: "pbe-nav-label", id: `pbe-nav-${g.id}`, children: g.label })) : null, _jsx("ul", { className: "pbe-nav-list", "aria-labelledby": g.label ? `pbe-nav-${g.id}` : undefined, children: ADMIN_SECTIONS.filter((s) => s.group === g.id).map((s) => (_jsx("li", { children: navLink(s, 'sidebar') }, s.id))) })] }, g.id))) }), _jsxs("div", { className: "pbe-sidebar-foot", children: [_jsxs("a", { className: "pbe-site-link", href: siteUrl, target: "_blank", rel: "noopener", children: [_jsx(Icon, { name: "external" }), "View website"] }), _jsx(AppearanceSwitch, { value: appearance, onChange: setAppearance }), _jsxs("div", { className: "pbe-account", children: [_jsx(Icon, { name: "account", size: 20 }), _jsx("span", { className: "pbe-account-email", title: phase.email ?? undefined, children: phase.email }), _jsx(IconButton, { icon: "signout", label: "Sign out", onClick: signOut })] })] })] }), _jsxs("header", { className: "pbe-mobilebar", inert: moreOpen || undefined, children: [_jsxs("div", { className: "pbe-brand", children: [_jsx("span", { className: "pbe-monogram", "aria-hidden": "true", children: mark }), _jsx("span", { className: "pbe-business", children: business })] }), _jsx(IconButton, { icon: "search", label: "Search bookings", onClick: openSearch }), _jsx("a", { className: "pbe-icon-button", href: siteUrl, target: "_blank", rel: "noopener", "aria-label": "View website", title: "View website", children: _jsx(Icon, { name: "external", size: 20 }) })] }), _jsxs("div", { className: "pbe-canvas", inert: moreOpen || undefined, children: [_jsxs("header", { className: "pbe-topbar", children: [_jsxs("div", { className: "pbe-crumb", children: [_jsx(Icon, { name: current.icon }), _jsx("span", { children: current.label })] }), _jsxs("div", { className: "pbe-topbar-actions", children: [_jsx(BusinessClock, { timezone: phase.settings.timezone, locale: phase.settings.locale }), _jsxs("a", { className: "pbe-button pbe-button--secondary pbe-button--sm", href: siteUrl, target: "_blank", rel: "noopener", children: [_jsx(Icon, { name: "external" }), "View website"] })] })] }), _jsxs("main", { className: "pbe-main", id: "pbe-main", tabIndex: -1, children: [_jsx(EmailHealthBanner, {}, `email-${section}`), section !== 'email' ? _jsx(AlertsBanner, {}) : null, _jsx(Current, {}, `${section}/${route.view ?? ''}`)] }), _jsxs("footer", { className: "pbe-footer", children: ["Booking Engine ", engine.version] })] }), moreOpen ? _jsx("button", { type: "button", className: "pbe-scrim", tabIndex: -1, "aria-label": "Close menu", onClick: () => setMoreOpen(false) }) : null, _jsxs("nav", { className: "pbe-tabbar", "aria-label": "Dashboard sections", children: [_jsxs("div", { className: "pbe-sheet", id: "pbe-more-sheet", ref: sheet, hidden: !moreOpen, children: [_jsx("div", { className: "pbe-sheet-grip", "aria-hidden": "true" }), _jsx("ul", { className: "pbe-nav-list", children: secondary.map((s) => (_jsx("li", { children: navLink(s, 'sheet') }, s.id))) }), _jsxs("div", { className: "pbe-sheet-section", children: [_jsx(AppearanceSwitch, { value: appearance, onChange: setAppearance }), _jsxs("div", { className: "pbe-account", children: [_jsx(Icon, { name: "account", size: 20 }), _jsx("span", { className: "pbe-account-email", children: phase.email }), _jsx(IconButton, { icon: "signout", label: "Sign out", onClick: signOut })] })] })] }), _jsxs("ul", { className: "pbe-tabs", children: [primary.map((s) => (_jsx("li", { children: navLink(s, 'tab') }, s.id))), _jsx("li", { children: _jsxs("button", { ref: moreButton, type: "button", className: `pbe-tab-link${!current.primary ? ' pbe-tab-link--within' : ''}`, "aria-expanded": moreOpen, "aria-controls": "pbe-more-sheet", onClick: () => setMoreOpen((o) => !o), children: [_jsx("span", { className: "pbe-tab-icon", children: _jsx(Icon, { name: "more", size: 20 }) }), _jsx("span", { children: "More" })] }) })] })] }), _jsx(ToastRegion, { toasts: toasts, dismiss: dismiss }), dialog] }) }));
245
+ }
246
+ /** The business's own time, which every booking time on the dashboard uses. */
247
+ function BusinessClock({ timezone, locale }) {
248
+ const now = useNow(15_000);
249
+ const label = timeZoneLabel(timezone, locale);
250
+ return (_jsxs("span", { className: "pbe-chip", title: `Every time on this dashboard is ${label}`, children: [_jsx(Icon, { name: "website" }), label, " \u00B7 ", formatTime(new Date(now).toISOString(), timezone, locale)] }));
251
+ }
252
+ /** On every page: say so when booking emails are not getting out (customers then cannot receive codes). */
253
+ function EmailHealthBanner() {
254
+ const { engine, settings, go } = useAdmin();
255
+ const [status, setStatus] = useState(null);
256
+ useEffect(() => {
257
+ let alive = true;
258
+ engine.admin.emailStatus().then((r) => {
259
+ if (alive && r.ok)
260
+ setStatus(r.data);
261
+ });
262
+ return () => {
263
+ alive = false;
264
+ };
265
+ }, [engine]);
266
+ if (!status)
267
+ return null;
268
+ const { lastSuccessAt, lastFailureAt, lastError } = status.mailer;
269
+ const failing = Boolean(lastFailureAt && (!lastSuccessAt || Date.parse(lastFailureAt) > Date.parse(lastSuccessAt)));
270
+ const stuck = Boolean(status.counts.oldestQueuedAt && Date.now() - Date.parse(status.counts.oldestQueuedAt) > 5 * 60_000);
271
+ // The same count and 24-hour window the database uses to refuse new codes (RATE_LIMITED).
272
+ const capReached = settings.onlineBookingEnabled && status.counts.codesLast24h >= settings.maxVerificationEmailsPerDay;
273
+ if (!failing && !stuck && !capReached)
274
+ return null;
275
+ if (!failing && !stuck) {
276
+ return (_jsxs("div", { className: "pbe-notice pbe-notice--warning pbe-banner", role: "alert", children: [_jsx(Icon, { name: "mail-problem" }), _jsxs("div", { className: "pbe-notice-body", children: [_jsx("strong", { children: "New customers cannot get a booking code right now." }), " The daily limit of", ' ', settings.maxVerificationEmailsPerDay, " code ", settings.maxVerificationEmailsPerDay === 1 ? 'email' : 'emails', " is used up (", status.counts.codesLast24h, " in the last 24 hours), so the website asks visitors to call or email instead. It frees up as those emails turn a day old. Raise the limit if this is real demand; if the requests look unfamiliar, keep it \u2014 it is protecting your email quota.", ' ', _jsx("button", { type: "button", className: "pbe-link-button", onClick: () => go('email'), children: "Open Email & online booking" })] })] }));
277
+ }
278
+ return (_jsxs("div", { className: "pbe-notice pbe-notice--warning pbe-banner", role: "alert", children: [_jsx(Icon, { name: "mail-problem" }), _jsxs("div", { className: "pbe-notice-body", children: [_jsx("strong", { children: "Booking emails are not getting out." }), ' ', failing ? `Last error: ${lastError ?? 'unknown'}. ` : 'Emails have been waiting for more than 5 minutes. ', settings.onlineBookingEnabled ? 'Customers cannot receive their booking codes until this is fixed. ' : '', _jsx("button", { type: "button", className: "pbe-link-button", onClick: () => go('email'), children: "Open Email & online booking" })] })] }));
279
+ }
280
+ /** On every page but Email (which says it in place): online booking is on, yet nobody is told about new requests. */
281
+ function AlertsBanner() {
282
+ const { settings, go } = useAdmin();
283
+ if (!settings.onlineBookingEnabled || alertsAddress(settings))
284
+ return null;
285
+ return (_jsxs("div", { className: "pbe-notice pbe-notice--warning pbe-banner", role: "alert", children: [_jsx(Icon, { name: "mail-problem" }), _jsxs("div", { className: "pbe-notice-body", children: [_jsx("strong", { children: "New requests are not emailed to you." }), " Online booking is on, but no address is set for new-request alerts, so a request could wait unanswered until its hold ends.", ' ', _jsx("button", { type: "button", className: "pbe-link-button", onClick: () => go('email', ALERTS_VIEW), children: "Choose an address" })] })] }));
286
+ }
287
+ function Shell({ title, icon, brand, children, rootProps, wide, }) {
288
+ return (_jsx("div", { className: "pbe-admin pbe-center", ...rootProps, children: _jsxs("div", { className: `pbe-center-card${wide ? ' pbe-center-card--wide' : ''}`, children: [brand ? (_jsxs("div", { className: "pbe-center-brand", children: [_jsx("span", { className: "pbe-monogram", "aria-hidden": "true", children: monogram(brand) }), _jsx("span", { className: "pbe-business", children: brand })] })) : null, icon ? (_jsx("span", { className: "pbe-center-icon", "aria-hidden": "true", children: _jsx(Icon, { name: icon, size: 20 }) })) : null, _jsx("h1", { className: "pbe-center-title", children: title }), children] }) }));
289
+ }
290
+ function SignIn({ engine, siteUrl, rootProps }) {
291
+ const [email, setEmail] = useState('');
292
+ const [password, setPassword] = useState('');
293
+ const [error, setError] = useState(null);
294
+ const [busy, setBusy] = useState(false);
295
+ const [business, setBusiness] = useState(null);
296
+ useEffect(() => {
297
+ let alive = true;
298
+ engine.getBusiness().then((r) => {
299
+ if (alive && r.ok)
300
+ setBusiness(r.data.name);
301
+ });
302
+ return () => {
303
+ alive = false;
304
+ };
305
+ }, [engine]);
306
+ async function submit(event) {
307
+ event.preventDefault();
308
+ if (busy)
309
+ return;
310
+ setBusy(true);
311
+ setError(null);
312
+ const r = await engine.auth.signIn(email, password);
313
+ setBusy(false);
314
+ if (!r.ok)
315
+ setError(r.error.message);
316
+ }
317
+ return (_jsxs(Shell, { title: "Sign in to your dashboard", brand: business ?? undefined, rootProps: rootProps, children: [_jsx("p", { className: "pbe-center-text", children: "Bookings, opening hours and your website, in one place." }), _jsxs("form", { className: "pbe-form pbe-center-body", onSubmit: submit, noValidate: false, children: [error ? _jsx(Notice, { kind: "error", children: error }) : null, _jsx(Field, { id: "pbe-email", label: "Email", children: _jsx("input", { id: "pbe-email", type: "email", autoComplete: "username", required: true, value: email, onChange: (e) => setEmail(e.target.value) }) }), _jsx(Field, { id: "pbe-password", label: "Password", children: _jsx("input", { id: "pbe-password", type: "password", autoComplete: "current-password", required: true, value: password, onChange: (e) => setPassword(e.target.value) }) }), _jsx(Button, { type: "submit", variant: "primary", size: "lg", block: true, loading: busy, children: "Sign in" }), _jsx("a", { className: "pbe-link-button pbe-link-button--quiet", href: siteUrl, style: { alignSelf: 'center' }, children: "Back to website" })] })] }));
318
+ }
319
+ function NotAdmin({ engine, email, rootProps }) {
320
+ const sql = `select booking.add_admin('${(email ?? 'you@example.com').replace(/'/g, "''")}');`;
321
+ const [copied, setCopied] = useState(false);
322
+ return (_jsxs(Shell, { title: "This account is not an admin", icon: "denied", rootProps: rootProps, wide: true, children: [_jsxs("p", { className: "pbe-center-text", children: ["You are signed in as ", _jsx("strong", { children: email }), ", but this account has no access to the dashboard."] }), _jsx("p", { className: "pbe-center-text", children: "If this is your business, give the account admin access once in the Supabase SQL Editor:" }), _jsxs("div", { className: "pbe-center-body", children: [_jsx("pre", { className: "pbe-code", children: sql }), _jsxs("div", { className: "pbe-form-actions", children: [_jsx(Button, { icon: copied ? 'check' : 'copy', onClick: () => {
323
+ void navigator.clipboard?.writeText(sql).then(() => setCopied(true));
324
+ }, children: copied ? 'Copied' : 'Copy SQL' }), _jsx(Button, { variant: "quiet", icon: "signout", onClick: () => void engine.auth.signOut(), children: "Sign out" })] })] })] }));
325
+ }
326
+ function Unconfigured({ mode, rootProps }) {
327
+ const vars = (_jsxs(_Fragment, { children: [_jsx("code", { className: "pbe-inline-code", children: "VITE_SUPABASE_URL" }), " and ", _jsx("code", { className: "pbe-inline-code", children: "VITE_SUPABASE_PUBLISHABLE_KEY" })] }));
328
+ return (_jsxs(Shell, { title: "Connect your backend", icon: "connect", rootProps: rootProps, wide: true, children: [mode === 'preview' ? (_jsxs("p", { className: "pbe-center-text", children: ["The dashboard needs your Supabase project. Add ", vars, " to ", _jsx("code", { className: "pbe-inline-code", children: ".env.local" }), ", apply the Booking Engine migrations, then restart the development server."] })) : (_jsxs("p", { className: "pbe-center-text", children: ["This build of the website was made without its Supabase connection. Set ", vars, " in your host's environment variables (on Hostinger: the website's environment variables), then build and deploy again. The values are compiled into the website when it is built."] })), mode === 'preview' ? (_jsx("div", { className: "pbe-center-body", children: _jsx(Notice, { kind: "info", children: "The website is running in preview mode with sample data. Nothing is saved." }) })) : null] }));
329
+ }
330
+ function DatabaseProblem({ error, onRetry, rootProps }) {
331
+ const [busy, setBusy] = useState(false);
332
+ return (_jsx(Shell, { title: "The booking database needs attention", icon: "database", rootProps: rootProps, wide: true, children: _jsxs("div", { className: "pbe-center-body", children: [_jsx(Notice, { kind: error.code === 'NETWORK' ? 'warning' : 'error', children: error.message }), error.code === 'SCHEMA_MISSING' || error.code === 'SCHEMA_INCOMPATIBLE' ? (_jsxs("p", { className: "pbe-center-text", children: ["From the project folder run ", _jsx("code", { className: "pbe-inline-code", children: "npx supabase@2 db push" }), " to apply the Booking Engine migrations, then try again."] })) : null, _jsx("div", { className: "pbe-form-actions", children: _jsx(Button, { variant: "primary", icon: "retry", loading: busy, onClick: async () => {
333
+ setBusy(true);
334
+ await onRetry();
335
+ setBusy(false);
336
+ }, children: "Try again" }) })] }) }));
337
+ }