@profitstudio/booking-engine 0.0.0-stage → 2.0.0-beta.2
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.
- package/CHANGELOG.md +578 -0
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/THIRD_PARTY_NOTICES.md +41 -0
- package/bin/booking-engine.mjs +575 -0
- package/bin/connect.mjs +408 -0
- package/bin/photos.mjs +82 -0
- package/dist/admin/BookingAdmin.d.ts +29 -0
- package/dist/admin/BookingAdmin.js +338 -0
- package/dist/admin/admin.css +1423 -0
- package/dist/admin/alerts.d.ts +10 -0
- package/dist/admin/alerts.js +11 -0
- package/dist/admin/booking-detail.d.ts +47 -0
- package/dist/admin/booking-detail.js +159 -0
- package/dist/admin/content-inputs.d.ts +15 -0
- package/dist/admin/content-inputs.js +105 -0
- package/dist/admin/context.d.ts +41 -0
- package/dist/admin/context.js +8 -0
- package/dist/admin/drawer.d.ts +14 -0
- package/dist/admin/drawer.js +56 -0
- package/dist/admin/feedback.d.ts +32 -0
- package/dist/admin/feedback.js +70 -0
- package/dist/admin/fields.d.ts +27 -0
- package/dist/admin/fields.js +146 -0
- package/dist/admin/fonts/OFL-Geist-Mono.txt +93 -0
- package/dist/admin/fonts/OFL-Geist.txt +93 -0
- package/dist/admin/fonts/geist-latin-wght-normal.woff2 +0 -0
- package/dist/admin/fonts/geist-mono-latin-wght-normal.woff2 +0 -0
- package/dist/admin/icons.d.ts +66 -0
- package/dist/admin/icons.js +72 -0
- package/dist/admin/image-upload.d.ts +58 -0
- package/dist/admin/image-upload.js +111 -0
- package/dist/admin/index.d.ts +7 -0
- package/dist/admin/index.js +6 -0
- package/dist/admin/lucide.generated.d.ts +521 -0
- package/dist/admin/lucide.generated.js +76 -0
- package/dist/admin/sections/BookingsSection.d.ts +1 -0
- package/dist/admin/sections/BookingsSection.js +137 -0
- package/dist/admin/sections/EmailSection.d.ts +8 -0
- package/dist/admin/sections/EmailSection.js +184 -0
- package/dist/admin/sections/HoursSection.d.ts +1 -0
- package/dist/admin/sections/HoursSection.js +110 -0
- package/dist/admin/sections/OverviewSection.d.ts +1 -0
- package/dist/admin/sections/OverviewSection.js +427 -0
- package/dist/admin/sections/ServicesSection.d.ts +1 -0
- package/dist/admin/sections/ServicesSection.js +236 -0
- package/dist/admin/sections/SettingsSection.d.ts +10 -0
- package/dist/admin/sections/SettingsSection.js +96 -0
- package/dist/admin/sections/WebsiteSection.d.ts +1 -0
- package/dist/admin/sections/WebsiteSection.js +228 -0
- package/dist/admin/sections.d.ts +22 -0
- package/dist/admin/sections.js +29 -0
- package/dist/admin/setup.d.ts +1 -0
- package/dist/admin/setup.js +45 -0
- package/dist/admin/today.d.ts +43 -0
- package/dist/admin/today.js +115 -0
- package/dist/admin/ui.d.ts +134 -0
- package/dist/admin/ui.js +220 -0
- package/dist/calendar.d.ts +64 -0
- package/dist/calendar.js +139 -0
- package/dist/client.d.ts +147 -0
- package/dist/client.js +507 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +120 -0
- package/dist/content/image.d.ts +20 -0
- package/dist/content/image.js +90 -0
- package/dist/content/resolve.d.ts +12 -0
- package/dist/content/resolve.js +73 -0
- package/dist/content/schema.d.ts +119 -0
- package/dist/content/schema.js +53 -0
- package/dist/content/store.d.ts +48 -0
- package/dist/content/store.js +123 -0
- package/dist/content/validate.d.ts +33 -0
- package/dist/content/validate.js +177 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +94 -0
- package/dist/generated/manifest.d.ts +6 -0
- package/dist/generated/manifest.js +7 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +20 -0
- package/dist/presets/hair-salon.d.ts +8 -0
- package/dist/presets/hair-salon.js +94 -0
- package/dist/presets/index.d.ts +5 -0
- package/dist/presets/index.js +6 -0
- package/dist/preview.d.ts +18 -0
- package/dist/preview.js +134 -0
- package/dist/semver.d.ts +16 -0
- package/dist/semver.js +55 -0
- package/dist/time.d.ts +31 -0
- package/dist/time.js +138 -0
- package/dist/types.d.ts +477 -0
- package/dist/types.js +3 -0
- package/dist/vite.d.ts +52 -0
- package/dist/vite.js +150 -0
- package/docs/BUILD-A-SITE.md +203 -0
- package/docs/EMAIL-SETUP.md +224 -0
- package/docs/INTEGRATION.md +1112 -0
- package/engine.manifest.json +90 -0
- package/functions/booking-mailer/deno.json +6 -0
- package/functions/booking-mailer/index.ts +44 -0
- package/functions/booking-mailer/mailer.js +156 -0
- package/functions/booking-mailer/providers/index.js +42 -0
- package/functions/booking-mailer/providers/resend.js +75 -0
- package/functions/booking-mailer/templates.js +345 -0
- package/migrations/0001_core.sql +1673 -0
- package/migrations/0002_security_hardening.sql +711 -0
- package/migrations/0003_private_definers_and_holds.sql +808 -0
- package/migrations/0004_verified_requests.sql +1702 -0
- package/migrations/0005_scoped_mailer_credential.sql +347 -0
- package/migrations/0006_trusted_client_ip.sql +56 -0
- package/migrations/0007_site_content.sql +380 -0
- package/migrations/0008_service_images_opening_times.sql +328 -0
- package/package.json +85 -4
- package/templates/BOOKING_ENGINE.md +61 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { BUSINESS_GROUP, POLICY_GROUP } from './fields.js';
|
|
3
|
+
import { BookingsSection } from './sections/BookingsSection.js';
|
|
4
|
+
import { EmailSection } from './sections/EmailSection.js';
|
|
5
|
+
import { HoursSection } from './sections/HoursSection.js';
|
|
6
|
+
import { OverviewSection } from './sections/OverviewSection.js';
|
|
7
|
+
import { ServicesSection } from './sections/ServicesSection.js';
|
|
8
|
+
import { SettingsSection } from './sections/SettingsSection.js';
|
|
9
|
+
import { WebsiteSection } from './sections/WebsiteSection.js';
|
|
10
|
+
/**
|
|
11
|
+
* Dashboard sections, in navigation order. "Today" is the landing page.
|
|
12
|
+
* "Website" edits the content the website declared (its SiteContentSchema,
|
|
13
|
+
* passed to <BookingAdmin content>).
|
|
14
|
+
*/
|
|
15
|
+
export const ADMIN_SECTIONS = [
|
|
16
|
+
{ id: 'overview', label: 'Today', icon: 'today', primary: true, group: 'run', component: OverviewSection },
|
|
17
|
+
{ id: 'bookings', label: 'Bookings', icon: 'bookings', primary: true, group: 'run', component: BookingsSection },
|
|
18
|
+
{ id: 'website', label: 'Website', icon: 'website', primary: true, group: 'online', component: WebsiteSection },
|
|
19
|
+
{ id: 'services', label: 'Services', icon: 'services', primary: true, group: 'setup', component: ServicesSection },
|
|
20
|
+
{ id: 'hours', label: 'Hours & closures', icon: 'hours', group: 'setup', component: HoursSection },
|
|
21
|
+
{ id: 'email', label: 'Email & online booking', icon: 'email', group: 'setup', component: EmailSection },
|
|
22
|
+
{ id: 'policies', label: 'Booking policies', icon: 'policies', group: 'setup', component: () => _jsx(SettingsSection, { group: POLICY_GROUP }) },
|
|
23
|
+
{ id: 'business', label: 'Business', icon: 'business', group: 'setup', component: () => _jsx(SettingsSection, { group: BUSINESS_GROUP }) },
|
|
24
|
+
];
|
|
25
|
+
export const NAV_GROUPS = [
|
|
26
|
+
{ id: 'run', label: null },
|
|
27
|
+
{ id: 'online', label: 'Online' },
|
|
28
|
+
{ id: 'setup', label: 'Setup' },
|
|
29
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function SetupChecklist(): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// "Finish setting up": what still stands between the owner and a booking page
|
|
3
|
+
// they can share. Shown on the Today page until everything is done.
|
|
4
|
+
import { useEffect, useState } from 'react';
|
|
5
|
+
import { ALERTS_VIEW, TEST_EMAIL_VIEW, alertsAddress } from './alerts.js';
|
|
6
|
+
import { useAdmin } from './context.js';
|
|
7
|
+
import { Icon } from './icons.js';
|
|
8
|
+
import { Meter } from './ui.js';
|
|
9
|
+
export function SetupChecklist() {
|
|
10
|
+
const { engine, settings, go } = useAdmin();
|
|
11
|
+
const [counts, setCounts] = useState(null);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let alive = true;
|
|
14
|
+
Promise.all([engine.admin.listServices(), engine.admin.getWorkingHours(), engine.admin.emailStatus()]).then(([s, h, e]) => {
|
|
15
|
+
if (alive && s.ok && h.ok) {
|
|
16
|
+
setCounts({ services: s.data.length, hours: h.data.length, emailWorks: e.ok && Boolean(e.data.mailer.lastSuccessAt) });
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return () => {
|
|
20
|
+
alive = false;
|
|
21
|
+
};
|
|
22
|
+
}, [engine, settings.onlineBookingEnabled]);
|
|
23
|
+
if (!counts)
|
|
24
|
+
return null;
|
|
25
|
+
// Every new request is emailed to this address: without one, nobody hears about it.
|
|
26
|
+
const alerts = alertsAddress(settings);
|
|
27
|
+
const items = [
|
|
28
|
+
{ id: 'business', done: settings.businessName !== 'My Business' && settings.timezone !== 'UTC', text: 'Set your business name and time zone', section: 'business' },
|
|
29
|
+
{ id: 'services', done: counts.services > 0, text: 'Add your services and prices', section: 'services' },
|
|
30
|
+
{ id: 'hours', done: counts.hours > 0, text: 'Set your opening hours', section: 'hours' },
|
|
31
|
+
{ id: 'alerts', done: Boolean(alerts), text: 'Choose where new-request alerts go', section: 'email', view: ALERTS_VIEW },
|
|
32
|
+
{
|
|
33
|
+
id: 'test',
|
|
34
|
+
done: counts.emailWorks || settings.onlineBookingEnabled,
|
|
35
|
+
text: alerts ? `Send a test email to ${alerts}` : 'Send a test email to your alerts address',
|
|
36
|
+
section: 'email',
|
|
37
|
+
view: TEST_EMAIL_VIEW,
|
|
38
|
+
},
|
|
39
|
+
{ id: 'online', done: settings.onlineBookingEnabled, text: 'Turn on online booking', section: 'email' },
|
|
40
|
+
];
|
|
41
|
+
const done = items.filter((i) => i.done).length;
|
|
42
|
+
if (done === items.length)
|
|
43
|
+
return null;
|
|
44
|
+
return (_jsxs("section", { className: "pbe-card pbe-setup", "aria-labelledby": "pbe-setup-title", children: [_jsxs("div", { className: "pbe-setup-head", children: [_jsxs("div", { children: [_jsx("h2", { id: "pbe-setup-title", className: "pbe-card-title", children: "Finish setting up" }), _jsxs("p", { className: "pbe-card-sub", children: ["Before you share your booking page \u00B7 ", done, " of ", items.length, " done"] })] }), _jsx("span", { className: "pbe-push", children: _jsx(Meter, { value: done / items.length, label: `Setup progress: ${done} of ${items.length} steps done` }) })] }), _jsx("ul", { className: "pbe-setup-list", children: items.map((i) => i.done ? (_jsx("li", { children: _jsxs("span", { className: "pbe-setup-item pbe-setup-item--done", children: [_jsx("span", { className: "pbe-step-mark", "aria-hidden": "true", children: _jsx(Icon, { name: "check" }) }), i.text, _jsx("span", { className: "pbe-sr-only", children: " (done)" })] }) }, i.id)) : (_jsx("li", { children: _jsxs("button", { type: "button", className: "pbe-setup-item", onClick: () => go(i.section, i.view), children: [_jsx("span", { className: "pbe-step-mark", "aria-hidden": "true" }), i.text, _jsx("span", { className: "pbe-arrow", children: _jsx(Icon, { name: "arrow" }) })] }) }, i.id))) })] }));
|
|
45
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { AdminBooking, AvailabilityException, BookingStatus, OpeningHoursInterval } from '../types.js';
|
|
2
|
+
export type Span = [start: number, end: number];
|
|
3
|
+
/** "09:30" → 570 minutes after midnight ("24:00" → 1440). */
|
|
4
|
+
export declare function toMinutes(hhmm: string): number;
|
|
5
|
+
/** A booking's business-local minutes on its day. */
|
|
6
|
+
export declare function bookingSpan(b: AdminBooking): Span;
|
|
7
|
+
/** Statuses that occupy time on the calendar (an expired hold no longer does). */
|
|
8
|
+
export declare const OCCUPYING: ReadonlySet<BookingStatus>;
|
|
9
|
+
/** Merge overlapping spans. */
|
|
10
|
+
export declare function union(spans: Span[]): Span[];
|
|
11
|
+
export declare const total: (spans: Span[]) => number;
|
|
12
|
+
/** The closures that touch a date, and whether one closes the whole day. */
|
|
13
|
+
export declare function closuresOn(date: string, closures: AvailabilityException[]): {
|
|
14
|
+
allDay: AvailabilityException | null;
|
|
15
|
+
partial: Array<{
|
|
16
|
+
span: Span;
|
|
17
|
+
reason: string | null;
|
|
18
|
+
}>;
|
|
19
|
+
};
|
|
20
|
+
/** When the business is open on a date: its weekly hours minus that day's closures. */
|
|
21
|
+
export declare function openSpans(date: string, hours: OpeningHoursInterval[], closures: AvailabilityException[]): Span[];
|
|
22
|
+
/** Minutes of `spans` covered by `busy` (each minute counted once). */
|
|
23
|
+
export declare function overlapMinutes(busy: Span[], spans: Span[]): number;
|
|
24
|
+
/** Free time inside the open spans, after `from` minutes, not taken by `busy`. */
|
|
25
|
+
export declare function freeSpans(open: Span[], busy: Span[], from: number): Span[];
|
|
26
|
+
/** Monday of the week that contains `date` (weeks run Monday to Sunday). */
|
|
27
|
+
export declare function weekStart(date: string): string;
|
|
28
|
+
/** How much of a request's hold is left. The hold length is today's policy, so the fraction is a guide. */
|
|
29
|
+
export declare function holdState(b: AdminBooking, holdMinutes: number, now: number): {
|
|
30
|
+
left: number;
|
|
31
|
+
ended: boolean;
|
|
32
|
+
fraction: number;
|
|
33
|
+
soon: boolean;
|
|
34
|
+
} | null;
|
|
35
|
+
/** A request whose hold still runs at `now` (an ended hold can still be confirmed if its time is free). */
|
|
36
|
+
export declare function holdRunning(b: AdminBooking, now: number): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Requests in the order the owner should answer them, at `now`: running holds
|
|
39
|
+
* by when they end (soonest first), then holds that have ended — still
|
|
40
|
+
* answerable — by appointment time. A hold that ends while the page is open
|
|
41
|
+
* moves with it, without waiting for a reload.
|
|
42
|
+
*/
|
|
43
|
+
export declare function byUrgency(now: number): (a: AdminBooking, b: AdminBooking) => number;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Today's numbers, computed from what the engine returns (bookings, weekly
|
|
2
|
+
// hours, closures). Nothing here estimates or invents: every value can be
|
|
3
|
+
// traced to a booking, an opening hour or a closure.
|
|
4
|
+
import { addDays, weekdayOf } from '../time.js';
|
|
5
|
+
/** "09:30" → 570 minutes after midnight ("24:00" → 1440). */
|
|
6
|
+
export function toMinutes(hhmm) {
|
|
7
|
+
const [h, m] = hhmm.split(':').map(Number);
|
|
8
|
+
return (h ?? 0) * 60 + (m ?? 0);
|
|
9
|
+
}
|
|
10
|
+
/** A booking's business-local minutes on its day. */
|
|
11
|
+
export function bookingSpan(b) {
|
|
12
|
+
const start = toMinutes(b.time);
|
|
13
|
+
const end = toMinutes(b.endTime);
|
|
14
|
+
return [start, end <= start ? 1440 : end];
|
|
15
|
+
}
|
|
16
|
+
/** Statuses that occupy time on the calendar (an expired hold no longer does). */
|
|
17
|
+
export const OCCUPYING = new Set(['pending', 'confirmed', 'completed', 'no_show']);
|
|
18
|
+
function subtract(spans, [cs, ce]) {
|
|
19
|
+
return spans.flatMap(([a, b]) => {
|
|
20
|
+
if (ce <= a || cs >= b)
|
|
21
|
+
return [[a, b]];
|
|
22
|
+
const out = [];
|
|
23
|
+
if (cs > a)
|
|
24
|
+
out.push([a, cs]);
|
|
25
|
+
if (ce < b)
|
|
26
|
+
out.push([ce, b]);
|
|
27
|
+
return out;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** Merge overlapping spans. */
|
|
31
|
+
export function union(spans) {
|
|
32
|
+
const sorted = [...spans].sort((x, y) => x[0] - y[0]);
|
|
33
|
+
const out = [];
|
|
34
|
+
for (const [a, b] of sorted) {
|
|
35
|
+
const last = out.at(-1);
|
|
36
|
+
if (last && a <= last[1])
|
|
37
|
+
last[1] = Math.max(last[1], b);
|
|
38
|
+
else
|
|
39
|
+
out.push([a, b]);
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
export const total = (spans) => spans.reduce((sum, [a, b]) => sum + (b - a), 0);
|
|
44
|
+
/** The closures that touch a date, and whether one closes the whole day. */
|
|
45
|
+
export function closuresOn(date, closures) {
|
|
46
|
+
const touching = closures.filter((c) => c.startsOn <= date && date <= c.endsOn);
|
|
47
|
+
return {
|
|
48
|
+
allDay: touching.find((c) => !c.startTime) ?? null,
|
|
49
|
+
partial: touching
|
|
50
|
+
.filter((c) => c.startTime && c.endTime)
|
|
51
|
+
.map((c) => ({ span: [toMinutes(c.startTime), c.endTime === '24:00' ? 1440 : toMinutes(c.endTime)], reason: c.reason })),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** When the business is open on a date: its weekly hours minus that day's closures. */
|
|
55
|
+
export function openSpans(date, hours, closures) {
|
|
56
|
+
const weekday = weekdayOf(date);
|
|
57
|
+
let spans = union(hours
|
|
58
|
+
.filter((h) => h.weekday === weekday)
|
|
59
|
+
.map((h) => [toMinutes(h.start), h.end === '24:00' ? 1440 : toMinutes(h.end)])
|
|
60
|
+
.filter(([a, b]) => b > a));
|
|
61
|
+
const { allDay, partial } = closuresOn(date, closures);
|
|
62
|
+
if (allDay)
|
|
63
|
+
return [];
|
|
64
|
+
for (const p of partial)
|
|
65
|
+
spans = subtract(spans, p.span);
|
|
66
|
+
return spans;
|
|
67
|
+
}
|
|
68
|
+
/** Minutes of `spans` covered by `busy` (each minute counted once). */
|
|
69
|
+
export function overlapMinutes(busy, spans) {
|
|
70
|
+
let sum = 0;
|
|
71
|
+
for (const [a, b] of union(busy))
|
|
72
|
+
for (const [c, d] of spans)
|
|
73
|
+
sum += Math.max(0, Math.min(b, d) - Math.max(a, c));
|
|
74
|
+
return sum;
|
|
75
|
+
}
|
|
76
|
+
/** Free time inside the open spans, after `from` minutes, not taken by `busy`. */
|
|
77
|
+
export function freeSpans(open, busy, from) {
|
|
78
|
+
let free = open.map(([a, b]) => [Math.max(a, from), b]).filter(([a, b]) => b > a);
|
|
79
|
+
for (const s of union(busy))
|
|
80
|
+
free = subtract(free, s);
|
|
81
|
+
return free;
|
|
82
|
+
}
|
|
83
|
+
/** Monday of the week that contains `date` (weeks run Monday to Sunday). */
|
|
84
|
+
export function weekStart(date) {
|
|
85
|
+
return addDays(date, 1 - weekdayOf(date));
|
|
86
|
+
}
|
|
87
|
+
/** How much of a request's hold is left. The hold length is today's policy, so the fraction is a guide. */
|
|
88
|
+
export function holdState(b, holdMinutes, now) {
|
|
89
|
+
if (!b.holdExpiresAt)
|
|
90
|
+
return null;
|
|
91
|
+
const left = Date.parse(b.holdExpiresAt) - now;
|
|
92
|
+
const fraction = holdMinutes > 0 ? left / (holdMinutes * 60_000) : 0;
|
|
93
|
+
return { left, ended: left <= 0 || b.status === 'expired', fraction: Math.max(0, Math.min(1, fraction)), soon: left > 0 && left < 30 * 60_000 };
|
|
94
|
+
}
|
|
95
|
+
/** A request whose hold still runs at `now` (an ended hold can still be confirmed if its time is free). */
|
|
96
|
+
export function holdRunning(b, now) {
|
|
97
|
+
return b.status === 'pending' && Boolean(b.holdExpiresAt) && Date.parse(b.holdExpiresAt) > now;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Requests in the order the owner should answer them, at `now`: running holds
|
|
101
|
+
* by when they end (soonest first), then holds that have ended — still
|
|
102
|
+
* answerable — by appointment time. A hold that ends while the page is open
|
|
103
|
+
* moves with it, without waiting for a reload.
|
|
104
|
+
*/
|
|
105
|
+
export function byUrgency(now) {
|
|
106
|
+
return (a, b) => {
|
|
107
|
+
const ra = holdRunning(a, now) ? 0 : 1;
|
|
108
|
+
const rb = holdRunning(b, now) ? 0 : 1;
|
|
109
|
+
if (ra !== rb)
|
|
110
|
+
return ra - rb;
|
|
111
|
+
if (ra === 0)
|
|
112
|
+
return Date.parse(a.holdExpiresAt) - Date.parse(b.holdExpiresAt);
|
|
113
|
+
return Date.parse(a.startsAt) - Date.parse(b.startsAt);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { type ButtonHTMLAttributes, type ReactNode } from 'react';
|
|
2
|
+
import type { BookingStatus, EngineError, Result } from '../types.js';
|
|
3
|
+
import { type IconName } from './icons.js';
|
|
4
|
+
export type Load<T> = {
|
|
5
|
+
state: 'loading';
|
|
6
|
+
} | {
|
|
7
|
+
state: 'ready';
|
|
8
|
+
data: T;
|
|
9
|
+
} | {
|
|
10
|
+
state: 'error';
|
|
11
|
+
error: EngineError;
|
|
12
|
+
};
|
|
13
|
+
/** Load a Result-returning call; `reload` runs it again (keeping the old data on screen meanwhile). */
|
|
14
|
+
export declare function useResult<T>(loader: () => Promise<Result<T>>, deps: unknown[]): [Load<T>, () => void];
|
|
15
|
+
/** The current time, refreshed every `intervalMs` (countdowns, "now" lines). */
|
|
16
|
+
export declare function useNow(intervalMs?: number): number;
|
|
17
|
+
/** True while a media query matches (layout choices that CSS alone cannot make, e.g. docked vs. modal). */
|
|
18
|
+
export declare function useMediaQuery(query: string): boolean;
|
|
19
|
+
export type ButtonVariant = 'primary' | 'secondary' | 'quiet' | 'danger' | 'danger-quiet';
|
|
20
|
+
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
21
|
+
variant?: ButtonVariant;
|
|
22
|
+
size?: 'sm' | 'md' | 'lg';
|
|
23
|
+
/** Shows a spinner in place of the label (the button keeps its size and focus) and ignores clicks. */
|
|
24
|
+
loading?: boolean;
|
|
25
|
+
icon?: IconName;
|
|
26
|
+
iconAfter?: IconName;
|
|
27
|
+
block?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare function Button({ variant, size, loading, icon, iconAfter, block, className, children, onClick, type, ...rest }: ButtonProps): import("react").JSX.Element;
|
|
30
|
+
export declare function IconButton({ icon, label, size, bordered, className, ...rest }: Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'children'> & {
|
|
31
|
+
icon: IconName;
|
|
32
|
+
label: string;
|
|
33
|
+
size?: 'sm' | 'md';
|
|
34
|
+
bordered?: boolean;
|
|
35
|
+
}): import("react").JSX.Element;
|
|
36
|
+
export declare function Notice({ kind, children, className, }: {
|
|
37
|
+
kind?: 'info' | 'success' | 'error' | 'warning';
|
|
38
|
+
children: ReactNode;
|
|
39
|
+
className?: string;
|
|
40
|
+
}): import("react").JSX.Element;
|
|
41
|
+
export declare function ErrorNotice({ error, onRetry }: {
|
|
42
|
+
error: EngineError;
|
|
43
|
+
onRetry?: () => void;
|
|
44
|
+
}): import("react").JSX.Element;
|
|
45
|
+
export declare function Field({ id, label, help, error, children, }: {
|
|
46
|
+
id: string;
|
|
47
|
+
label: string;
|
|
48
|
+
help?: string;
|
|
49
|
+
error?: string | null;
|
|
50
|
+
children: ReactNode;
|
|
51
|
+
}): import("react").JSX.Element;
|
|
52
|
+
/** An on/off setting: a real checkbox announced as a switch. */
|
|
53
|
+
export declare function Switch({ id, label, help, checked, onChange, disabled, }: {
|
|
54
|
+
id: string;
|
|
55
|
+
label: ReactNode;
|
|
56
|
+
help?: ReactNode;
|
|
57
|
+
checked: boolean;
|
|
58
|
+
onChange: (checked: boolean) => void;
|
|
59
|
+
disabled?: boolean;
|
|
60
|
+
}): import("react").JSX.Element;
|
|
61
|
+
export interface SegmentOption {
|
|
62
|
+
id: string;
|
|
63
|
+
label: string;
|
|
64
|
+
count?: number | null;
|
|
65
|
+
}
|
|
66
|
+
/** Views of one list (role="tablist"); arrow keys move between them. */
|
|
67
|
+
export declare function Segmented({ label, options, value, onChange, }: {
|
|
68
|
+
label: string;
|
|
69
|
+
options: SegmentOption[];
|
|
70
|
+
value: string;
|
|
71
|
+
onChange: (id: string) => void;
|
|
72
|
+
}): import("react").JSX.Element;
|
|
73
|
+
export declare const STATUS_LABELS: Record<BookingStatus, string>;
|
|
74
|
+
/** A booking's status: a word and a shape (never colour alone). */
|
|
75
|
+
export declare function StatusBadge({ status }: {
|
|
76
|
+
status: BookingStatus;
|
|
77
|
+
}): import("react").JSX.Element;
|
|
78
|
+
export declare function Badge({ tone, children }: {
|
|
79
|
+
tone?: 'ok' | 'warn' | 'danger' | 'neutral';
|
|
80
|
+
children: ReactNode;
|
|
81
|
+
}): import("react").JSX.Element;
|
|
82
|
+
export declare function Kbd({ children }: {
|
|
83
|
+
children: ReactNode;
|
|
84
|
+
}): import("react").JSX.Element;
|
|
85
|
+
export declare function PageHeader({ id, title, eyebrow, sub, actions, }: {
|
|
86
|
+
id: string;
|
|
87
|
+
title: ReactNode;
|
|
88
|
+
eyebrow?: ReactNode;
|
|
89
|
+
sub?: ReactNode;
|
|
90
|
+
actions?: ReactNode;
|
|
91
|
+
}): import("react").JSX.Element;
|
|
92
|
+
export declare function EmptyState({ icon, title, children, action, variant, }: {
|
|
93
|
+
icon?: IconName;
|
|
94
|
+
title: string;
|
|
95
|
+
children?: ReactNode;
|
|
96
|
+
action?: ReactNode;
|
|
97
|
+
variant?: 'plain' | 'card' | 'compact';
|
|
98
|
+
}): import("react").JSX.Element;
|
|
99
|
+
/** A generic loading line (prefer a skeleton shaped like the content). */
|
|
100
|
+
export declare function Loading({ label }: {
|
|
101
|
+
label?: string;
|
|
102
|
+
}): import("react").JSX.Element;
|
|
103
|
+
/** Rows of a table or list that is still loading. */
|
|
104
|
+
export declare function SkeletonRows({ rows, label }: {
|
|
105
|
+
rows?: number;
|
|
106
|
+
label?: string;
|
|
107
|
+
}): import("react").JSX.Element;
|
|
108
|
+
/** A block of text lines that is still loading (cards, forms). */
|
|
109
|
+
export declare function SkeletonBlock({ lines, label }: {
|
|
110
|
+
lines?: number;
|
|
111
|
+
label?: string;
|
|
112
|
+
}): import("react").JSX.Element;
|
|
113
|
+
export declare function Meter({ value, label }: {
|
|
114
|
+
value: number;
|
|
115
|
+
label: string;
|
|
116
|
+
}): import("react").JSX.Element;
|
|
117
|
+
/** A small countdown ring: how much of a hold (or an appointment) is left. */
|
|
118
|
+
export declare function Ring({ fraction, tone, size }: {
|
|
119
|
+
fraction: number;
|
|
120
|
+
tone?: 'pending' | 'soon' | 'ended' | 'accent';
|
|
121
|
+
size?: 24 | 32 | 36;
|
|
122
|
+
}): import("react").JSX.Element;
|
|
123
|
+
export declare function formatMinutes(total: number): string;
|
|
124
|
+
/** A span of time in words: "45 min", "1 h 50 min", "2 d 3 h". */
|
|
125
|
+
export declare function formatSpan(ms: number): string;
|
|
126
|
+
/** "just now", "4 min ago", "3 h ago", "2 days ago". */
|
|
127
|
+
export declare function formatAgo(iso: string | null, now?: number): string;
|
|
128
|
+
export declare function centsToInput(cents: number | null): string;
|
|
129
|
+
/** "65" / "65.5" / "65,50" → 6550; "" → null; invalid → undefined. */
|
|
130
|
+
export declare function inputToCents(value: string): number | null | undefined;
|
|
131
|
+
/** Up to two letters for the business mark, e.g. "Copper & Comb" → "CC". */
|
|
132
|
+
export declare function monogram(name: string): string;
|
|
133
|
+
/** The customer's first name, for friendly copy ("Leo gets an email"). */
|
|
134
|
+
export declare function firstName(name: string): string;
|
package/dist/admin/ui.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
// The dashboard's component family: one set of buttons, fields, badges,
|
|
3
|
+
// switches, segmented views, empty states, skeletons and meters, styled by
|
|
4
|
+
// admin.css. Sections compose these; they never style their own controls.
|
|
5
|
+
import { useCallback, useEffect, useRef, useState, } from 'react';
|
|
6
|
+
import { Icon } from './icons.js';
|
|
7
|
+
/** Load a Result-returning call; `reload` runs it again (keeping the old data on screen meanwhile). */
|
|
8
|
+
export function useResult(loader, deps) {
|
|
9
|
+
const [load, setLoad] = useState({ state: 'loading' });
|
|
10
|
+
const [tick, setTick] = useState(0);
|
|
11
|
+
const loaderRef = useRef(loader);
|
|
12
|
+
loaderRef.current = loader;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
let alive = true;
|
|
15
|
+
setLoad((prev) => (prev.state === 'ready' ? prev : { state: 'loading' }));
|
|
16
|
+
loaderRef.current().then((r) => {
|
|
17
|
+
if (!alive)
|
|
18
|
+
return;
|
|
19
|
+
setLoad(r.ok ? { state: 'ready', data: r.data } : { state: 'error', error: r.error });
|
|
20
|
+
});
|
|
21
|
+
return () => {
|
|
22
|
+
alive = false;
|
|
23
|
+
};
|
|
24
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
25
|
+
}, [tick, ...deps]);
|
|
26
|
+
const reload = useCallback(() => setTick((t) => t + 1), []);
|
|
27
|
+
return [load, reload];
|
|
28
|
+
}
|
|
29
|
+
/** The current time, refreshed every `intervalMs` (countdowns, "now" lines). */
|
|
30
|
+
export function useNow(intervalMs = 30_000) {
|
|
31
|
+
const [now, setNow] = useState(() => Date.now());
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const id = window.setInterval(() => setNow(Date.now()), intervalMs);
|
|
34
|
+
return () => window.clearInterval(id);
|
|
35
|
+
}, [intervalMs]);
|
|
36
|
+
return now;
|
|
37
|
+
}
|
|
38
|
+
/** True while a media query matches (layout choices that CSS alone cannot make, e.g. docked vs. modal). */
|
|
39
|
+
export function useMediaQuery(query) {
|
|
40
|
+
const get = () => (typeof window !== 'undefined' && window.matchMedia ? window.matchMedia(query).matches : false);
|
|
41
|
+
const [matches, setMatches] = useState(get);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (!window.matchMedia)
|
|
44
|
+
return;
|
|
45
|
+
const list = window.matchMedia(query);
|
|
46
|
+
const onChange = () => setMatches(list.matches);
|
|
47
|
+
onChange();
|
|
48
|
+
list.addEventListener('change', onChange);
|
|
49
|
+
return () => list.removeEventListener('change', onChange);
|
|
50
|
+
}, [query]);
|
|
51
|
+
return matches;
|
|
52
|
+
}
|
|
53
|
+
export function Button({ variant = 'secondary', size = 'md', loading = false, icon, iconAfter, block, className, children, onClick, type = 'button', ...rest }) {
|
|
54
|
+
const classes = ['pbe-button', `pbe-button--${variant}`];
|
|
55
|
+
if (size !== 'md')
|
|
56
|
+
classes.push(`pbe-button--${size}`);
|
|
57
|
+
if (block)
|
|
58
|
+
classes.push('pbe-button--block');
|
|
59
|
+
if (className)
|
|
60
|
+
classes.push(className);
|
|
61
|
+
return (_jsxs("button", { ...rest, type: type, className: classes.join(' '), "aria-busy": loading || undefined, "aria-disabled": loading || undefined, onClick: (e) => {
|
|
62
|
+
if (loading) {
|
|
63
|
+
e.preventDefault();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
onClick?.(e);
|
|
67
|
+
}, children: [icon ? _jsx(Icon, { name: icon }) : null, children, iconAfter ? _jsx(Icon, { name: iconAfter }) : null, loading ? _jsx("span", { className: "pbe-spinner", "aria-hidden": "true" }) : null] }));
|
|
68
|
+
}
|
|
69
|
+
export function IconButton({ icon, label, size = 'md', bordered, className, ...rest }) {
|
|
70
|
+
const classes = ['pbe-icon-button'];
|
|
71
|
+
if (size === 'sm')
|
|
72
|
+
classes.push('pbe-icon-button--sm');
|
|
73
|
+
if (bordered)
|
|
74
|
+
classes.push('pbe-icon-button--bordered');
|
|
75
|
+
if (className)
|
|
76
|
+
classes.push(className);
|
|
77
|
+
return (_jsx("button", { type: "button", ...rest, className: classes.join(' '), "aria-label": label, title: label, children: _jsx(Icon, { name: icon }) }));
|
|
78
|
+
}
|
|
79
|
+
// --------------------------------------------------------------- notices ---
|
|
80
|
+
const NOTICE_ICON = {
|
|
81
|
+
info: 'info',
|
|
82
|
+
success: 'success',
|
|
83
|
+
warning: 'warning',
|
|
84
|
+
error: 'error',
|
|
85
|
+
};
|
|
86
|
+
export function Notice({ kind = 'info', children, className, }) {
|
|
87
|
+
return (_jsxs("div", { className: `pbe-notice pbe-notice--${kind}${className ? ` ${className}` : ''}`, role: kind === 'error' ? 'alert' : 'status', children: [_jsx(Icon, { name: NOTICE_ICON[kind] }), _jsx("div", { className: "pbe-notice-body", children: children })] }));
|
|
88
|
+
}
|
|
89
|
+
export function ErrorNotice({ error, onRetry }) {
|
|
90
|
+
return (_jsxs(Notice, { kind: "error", children: [error.message, onRetry ? (_jsxs(_Fragment, { children: [' ', _jsx("button", { type: "button", className: "pbe-link-button", onClick: onRetry, children: "Try again" })] })) : null] }));
|
|
91
|
+
}
|
|
92
|
+
// ---------------------------------------------------------------- fields ---
|
|
93
|
+
export function Field({ id, label, help, error, children, }) {
|
|
94
|
+
return (_jsxs("div", { className: `pbe-field${error ? ' pbe-field--error' : ''}`, children: [_jsx("label", { htmlFor: id, children: label }), children, help ? (_jsx("p", { className: "pbe-help", id: `${id}-help`, children: help })) : null, error ? (_jsxs("p", { className: "pbe-field-error", id: `${id}-error`, children: [_jsx(Icon, { name: "error" }), error] })) : null] }));
|
|
95
|
+
}
|
|
96
|
+
/** An on/off setting: a real checkbox announced as a switch. */
|
|
97
|
+
export function Switch({ id, label, help, checked, onChange, disabled, }) {
|
|
98
|
+
return (_jsxs("div", { className: "pbe-switch-row", children: [_jsx("input", { id: id, type: "checkbox", role: "switch", className: "pbe-switch", checked: checked, disabled: disabled, "aria-describedby": help ? `${id}-help` : undefined, onChange: (e) => onChange(e.target.checked) }), _jsxs("span", { className: "pbe-switch-text", children: [_jsx("label", { htmlFor: id, className: "pbe-switch-label", children: label }), help ? (_jsx("span", { className: "pbe-help", id: `${id}-help`, children: help })) : null] })] }));
|
|
99
|
+
}
|
|
100
|
+
/** Views of one list (role="tablist"); arrow keys move between them. */
|
|
101
|
+
export function Segmented({ label, options, value, onChange, }) {
|
|
102
|
+
const refs = useRef([]);
|
|
103
|
+
function onKey(e, index) {
|
|
104
|
+
const last = options.length - 1;
|
|
105
|
+
const next = e.key === 'ArrowRight' ? (index === last ? 0 : index + 1) : e.key === 'ArrowLeft' ? (index === 0 ? last : index - 1) : e.key === 'Home' ? 0 : e.key === 'End' ? last : null;
|
|
106
|
+
if (next === null)
|
|
107
|
+
return;
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
refs.current[next]?.focus();
|
|
110
|
+
onChange(options[next].id);
|
|
111
|
+
}
|
|
112
|
+
return (_jsx("div", { className: "pbe-segmented", role: "tablist", "aria-label": label, children: options.map((o, i) => (_jsxs("button", { ref: (el) => {
|
|
113
|
+
refs.current[i] = el;
|
|
114
|
+
}, type: "button", role: "tab", className: "pbe-tab", "aria-selected": o.id === value, tabIndex: o.id === value || (i === 0 && !options.some((x) => x.id === value)) ? 0 : -1, onClick: () => onChange(o.id), onKeyDown: (e) => onKey(e, i), children: [o.label, o.count ? _jsx("span", { className: "pbe-count", children: o.count }) : null] }, o.id))) }));
|
|
115
|
+
}
|
|
116
|
+
export const STATUS_LABELS = {
|
|
117
|
+
pending: 'Request',
|
|
118
|
+
confirmed: 'Confirmed',
|
|
119
|
+
completed: 'Done',
|
|
120
|
+
cancelled: 'Cancelled',
|
|
121
|
+
no_show: 'No-show',
|
|
122
|
+
expired: 'Hold expired',
|
|
123
|
+
};
|
|
124
|
+
/** A booking's status: a word and a shape (never colour alone). */
|
|
125
|
+
export function StatusBadge({ status }) {
|
|
126
|
+
return (_jsxs("span", { className: `pbe-badge pbe-badge--${status}`, children: [_jsx("span", { className: "pbe-badge-dot", "aria-hidden": "true" }), STATUS_LABELS[status]] }));
|
|
127
|
+
}
|
|
128
|
+
export function Badge({ tone = 'neutral', children }) {
|
|
129
|
+
return (_jsxs("span", { className: `pbe-badge pbe-badge--${tone}`, children: [tone !== 'neutral' ? _jsx("span", { className: "pbe-badge-dot", "aria-hidden": "true" }) : null, children] }));
|
|
130
|
+
}
|
|
131
|
+
export function Kbd({ children }) {
|
|
132
|
+
return _jsx("kbd", { className: "pbe-kbd", children: children });
|
|
133
|
+
}
|
|
134
|
+
// ------------------------------------------------- page, empty, loading ---
|
|
135
|
+
export function PageHeader({ id, title, eyebrow, sub, actions, }) {
|
|
136
|
+
return (_jsxs("header", { className: "pbe-page-head", children: [_jsxs("div", { children: [eyebrow ? _jsx("p", { className: "pbe-eyebrow", children: eyebrow }) : null, _jsx("h1", { id: id, className: "pbe-page-title", children: title }), sub ? _jsx("p", { className: "pbe-page-sub", children: sub }) : null] }), actions ? _jsx("div", { className: "pbe-page-actions", children: actions }) : null] }));
|
|
137
|
+
}
|
|
138
|
+
export function EmptyState({ icon = 'inbox', title, children, action, variant = 'plain', }) {
|
|
139
|
+
return (_jsxs("div", { className: `pbe-empty${variant === 'card' ? ' pbe-empty--card' : variant === 'compact' ? ' pbe-empty--compact' : ''}`, children: [_jsx("span", { className: "pbe-empty-icon", "aria-hidden": "true", children: _jsx(Icon, { name: icon, size: 20 }) }), _jsx("p", { className: "pbe-empty-title", children: title }), children ? _jsx("p", { className: "pbe-empty-text", children: children }) : null, action ? _jsx("div", { className: "pbe-empty-action", children: action }) : null] }));
|
|
140
|
+
}
|
|
141
|
+
/** A generic loading line (prefer a skeleton shaped like the content). */
|
|
142
|
+
export function Loading({ label = 'Loading…' }) {
|
|
143
|
+
return (_jsxs("div", { className: "pbe-loading-screen", role: "status", children: [_jsx("span", { className: "pbe-spinner", "aria-hidden": "true" }), _jsx("span", { children: label })] }));
|
|
144
|
+
}
|
|
145
|
+
/** Rows of a table or list that is still loading. */
|
|
146
|
+
export function SkeletonRows({ rows = 4, label = 'Loading…' }) {
|
|
147
|
+
return (_jsxs("div", { role: "status", "aria-live": "polite", children: [_jsx("span", { className: "pbe-sr-only", children: label }), Array.from({ length: rows }, (_, i) => (_jsxs("div", { className: "pbe-sk-row", "aria-hidden": "true", children: [_jsx("span", { className: "pbe-sk" }), _jsx("span", { className: "pbe-sk" }), _jsx("span", { className: "pbe-sk" }), _jsx("span", { className: "pbe-sk" }), _jsx("span", { className: "pbe-sk" })] }, i)))] }));
|
|
148
|
+
}
|
|
149
|
+
/** A block of text lines that is still loading (cards, forms). */
|
|
150
|
+
export function SkeletonBlock({ lines = 3, label = 'Loading…' }) {
|
|
151
|
+
return (_jsxs("div", { className: "pbe-skeleton", role: "status", children: [_jsx("span", { className: "pbe-sr-only", children: label }), _jsx("span", { className: "pbe-sk pbe-sk--title", "aria-hidden": "true" }), Array.from({ length: lines }, (_, i) => (_jsx("span", { className: "pbe-sk", style: { width: `${88 - i * 14}%` }, "aria-hidden": "true" }, i)))] }));
|
|
152
|
+
}
|
|
153
|
+
// ----------------------------------------------------- meters and rings ---
|
|
154
|
+
export function Meter({ value, label }) {
|
|
155
|
+
const pct = Math.round(Math.max(0, Math.min(1, value)) * 100);
|
|
156
|
+
return (_jsx("div", { className: "pbe-meter", role: "meter", "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": pct, "aria-label": label, children: _jsx("span", { style: { width: `${pct}%` } }) }));
|
|
157
|
+
}
|
|
158
|
+
/** A small countdown ring: how much of a hold (or an appointment) is left. */
|
|
159
|
+
export function Ring({ fraction, tone = 'pending', size = 32 }) {
|
|
160
|
+
const r = size / 2 - (size === 24 ? 2 : 2.5);
|
|
161
|
+
const length = 2 * Math.PI * r;
|
|
162
|
+
const f = tone === 'ended' ? 0 : Math.max(0, Math.min(1, fraction));
|
|
163
|
+
return (_jsxs("svg", { className: `pbe-ring${tone !== 'pending' ? ` pbe-ring--${tone}` : ''}${size === 24 ? ' pbe-ring--sm' : ''}`, width: size, height: size, viewBox: `0 0 ${size} ${size}`, "aria-hidden": "true", children: [_jsx("circle", { className: "pbe-ring-track", cx: size / 2, cy: size / 2, r: r }), f > 0 ? (_jsx("circle", { className: "pbe-ring-value", cx: size / 2, cy: size / 2, r: r, strokeDasharray: length, strokeDashoffset: length * (1 - f) })) : null] }));
|
|
164
|
+
}
|
|
165
|
+
// ------------------------------------------------------------ formatting ---
|
|
166
|
+
export function formatMinutes(total) {
|
|
167
|
+
const h = Math.floor(total / 60);
|
|
168
|
+
const m = Math.round(total % 60);
|
|
169
|
+
if (!h)
|
|
170
|
+
return `${m} min`;
|
|
171
|
+
return m ? `${h} h ${m} min` : `${h} h`;
|
|
172
|
+
}
|
|
173
|
+
/** A span of time in words: "45 min", "1 h 50 min", "2 d 3 h". */
|
|
174
|
+
export function formatSpan(ms) {
|
|
175
|
+
const minutes = Math.max(0, Math.round(ms / 60_000));
|
|
176
|
+
if (minutes < 1)
|
|
177
|
+
return 'under a minute';
|
|
178
|
+
if (minutes < 60)
|
|
179
|
+
return `${minutes} min`;
|
|
180
|
+
const hours = Math.floor(minutes / 60);
|
|
181
|
+
if (hours < 24)
|
|
182
|
+
return minutes % 60 ? `${hours} h ${minutes % 60} min` : `${hours} h`;
|
|
183
|
+
const days = Math.floor(hours / 24);
|
|
184
|
+
return hours % 24 ? `${days} d ${hours % 24} h` : `${days} d`;
|
|
185
|
+
}
|
|
186
|
+
/** "just now", "4 min ago", "3 h ago", "2 days ago". */
|
|
187
|
+
export function formatAgo(iso, now = Date.now()) {
|
|
188
|
+
if (!iso)
|
|
189
|
+
return '';
|
|
190
|
+
const minutes = Math.round((now - Date.parse(iso)) / 60_000);
|
|
191
|
+
if (minutes < 1)
|
|
192
|
+
return 'just now';
|
|
193
|
+
if (minutes < 60)
|
|
194
|
+
return `${minutes} min ago`;
|
|
195
|
+
const hours = Math.round(minutes / 60);
|
|
196
|
+
if (hours < 48)
|
|
197
|
+
return `${hours} h ago`;
|
|
198
|
+
return `${Math.round(hours / 24)} days ago`;
|
|
199
|
+
}
|
|
200
|
+
export function centsToInput(cents) {
|
|
201
|
+
return cents === null ? '' : (cents / 100).toFixed(cents % 100 === 0 ? 0 : 2);
|
|
202
|
+
}
|
|
203
|
+
/** "65" / "65.5" / "65,50" → 6550; "" → null; invalid → undefined. */
|
|
204
|
+
export function inputToCents(value) {
|
|
205
|
+
const v = value.trim().replace(',', '.');
|
|
206
|
+
if (!v)
|
|
207
|
+
return null;
|
|
208
|
+
if (!/^\d+(\.\d{1,2})?$/.test(v))
|
|
209
|
+
return undefined;
|
|
210
|
+
return Math.round(Number(v) * 100);
|
|
211
|
+
}
|
|
212
|
+
/** Up to two letters for the business mark, e.g. "Copper & Comb" → "CC". */
|
|
213
|
+
export function monogram(name) {
|
|
214
|
+
const words = name.split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
|
215
|
+
return ((words[0]?.[0] ?? '') + (words.length > 1 ? (words[words.length - 1]?.[0] ?? '') : (words[0]?.[1] ?? ''))).toUpperCase() || 'B';
|
|
216
|
+
}
|
|
217
|
+
/** The customer's first name, for friendly copy ("Leo gets an email"). */
|
|
218
|
+
export function firstName(name) {
|
|
219
|
+
return name.trim().split(/\s+/)[0] || name;
|
|
220
|
+
}
|