@connextar/house 0.26.0 → 0.28.0
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/README.md +114 -0
- package/dist/calendar/index.d.ts +9 -0
- package/dist/calendar/index.d.ts.map +1 -0
- package/dist/calendar/index.js +9 -0
- package/dist/calendar/index.js.map +1 -0
- package/dist/calendar/planner.d.ts +145 -0
- package/dist/calendar/planner.d.ts.map +1 -0
- package/dist/calendar/planner.js +315 -0
- package/dist/calendar/planner.js.map +1 -0
- package/dist/voice/handoff.d.ts +20 -0
- package/dist/voice/handoff.d.ts.map +1 -0
- package/dist/voice/handoff.js +18 -0
- package/dist/voice/handoff.js.map +1 -0
- package/dist/voice/index.d.ts +16 -0
- package/dist/voice/index.d.ts.map +1 -0
- package/dist/voice/index.js +16 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/voice/opening-hours.d.ts +23 -0
- package/dist/voice/opening-hours.d.ts.map +1 -0
- package/dist/voice/opening-hours.js +117 -0
- package/dist/voice/opening-hours.js.map +1 -0
- package/dist/voice/out-of-hours.d.ts +29 -0
- package/dist/voice/out-of-hours.d.ts.map +1 -0
- package/dist/voice/out-of-hours.js +20 -0
- package/dist/voice/out-of-hours.js.map +1 -0
- package/dist/voice/phone.d.ts +33 -0
- package/dist/voice/phone.d.ts.map +1 -0
- package/dist/voice/phone.js +62 -0
- package/dist/voice/phone.js.map +1 -0
- package/dist/voice/relay/agent-client.d.ts +24 -0
- package/dist/voice/relay/agent-client.d.ts.map +1 -0
- package/dist/voice/relay/agent-client.js +45 -0
- package/dist/voice/relay/agent-client.js.map +1 -0
- package/dist/voice/relay/model.d.ts +124 -0
- package/dist/voice/relay/model.d.ts.map +1 -0
- package/dist/voice/relay/model.js +304 -0
- package/dist/voice/relay/model.js.map +1 -0
- package/dist/voice/relay/protocol.d.ts +44 -0
- package/dist/voice/relay/protocol.d.ts.map +1 -0
- package/dist/voice/relay/protocol.js +16 -0
- package/dist/voice/relay/protocol.js.map +1 -0
- package/dist/voice/relay/sentence-gate.d.ts +24 -0
- package/dist/voice/relay/sentence-gate.d.ts.map +1 -0
- package/dist/voice/relay/sentence-gate.js +65 -0
- package/dist/voice/relay/sentence-gate.js.map +1 -0
- package/dist/voice/relay/server.d.ts +71 -0
- package/dist/voice/relay/server.d.ts.map +1 -0
- package/dist/voice/relay/server.js +192 -0
- package/dist/voice/relay/server.js.map +1 -0
- package/dist/voice/relay/transfer-line.d.ts +33 -0
- package/dist/voice/relay/transfer-line.d.ts.map +1 -0
- package/dist/voice/relay/transfer-line.js +116 -0
- package/dist/voice/relay/transfer-line.js.map +1 -0
- package/dist/voice/relay-index.d.ts +13 -0
- package/dist/voice/relay-index.d.ts.map +1 -0
- package/dist/voice/relay-index.js +13 -0
- package/dist/voice/relay-index.js.map +1 -0
- package/dist/voice/session-token.d.ts +58 -0
- package/dist/voice/session-token.d.ts.map +1 -0
- package/dist/voice/session-token.js +152 -0
- package/dist/voice/session-token.js.map +1 -0
- package/dist/voice/transfer.d.ts +123 -0
- package/dist/voice/transfer.d.ts.map +1 -0
- package/dist/voice/transfer.js +95 -0
- package/dist/voice/transfer.js.map +1 -0
- package/dist/voice/twilio-request.d.ts +30 -0
- package/dist/voice/twilio-request.d.ts.map +1 -0
- package/dist/voice/twilio-request.js +46 -0
- package/dist/voice/twilio-request.js.map +1 -0
- package/dist/voice/twilio-signature.d.ts +8 -0
- package/dist/voice/twilio-signature.d.ts.map +1 -0
- package/dist/voice/twilio-signature.js +22 -0
- package/dist/voice/twilio-signature.js.map +1 -0
- package/dist/voice/twiml.d.ts +93 -0
- package/dist/voice/twiml.d.ts.map +1 -0
- package/dist/voice/twiml.js +107 -0
- package/dist/voice/twiml.js.map +1 -0
- package/package.json +19 -1
- package/src/calendar/index.ts +45 -0
- package/src/calendar/planner.ts +387 -0
- package/src/voice/handoff.ts +34 -0
- package/src/voice/index.ts +78 -0
- package/src/voice/opening-hours.ts +125 -0
- package/src/voice/out-of-hours.ts +48 -0
- package/src/voice/phone.ts +68 -0
- package/src/voice/relay/agent-client.ts +64 -0
- package/src/voice/relay/model.ts +412 -0
- package/src/voice/relay/protocol.ts +52 -0
- package/src/voice/relay/sentence-gate.ts +64 -0
- package/src/voice/relay/server.ts +242 -0
- package/src/voice/relay/transfer-line.ts +114 -0
- package/src/voice/relay-index.ts +46 -0
- package/src/voice/session-token.ts +205 -0
- package/src/voice/transfer.ts +171 -0
- package/src/voice/twilio-request.ts +60 -0
- package/src/voice/twilio-signature.ts +27 -0
- package/src/voice/twiml.ts +184 -0
package/README.md
CHANGED
|
@@ -1681,6 +1681,80 @@ export async function GET() {
|
|
|
1681
1681
|
}
|
|
1682
1682
|
```
|
|
1683
1683
|
|
|
1684
|
+
### `@connextar/house/voice`, `/voice/relay`
|
|
1685
|
+
|
|
1686
|
+
A Twilio voice line with an AI on it, in two halves: the app's webhooks (`/voice`)
|
|
1687
|
+
and the long-running relay that holds each call's conversation (`/voice/relay`).
|
|
1688
|
+
From PharmaLineAI's voice layer (0.27.7), extracted as the second app, the CX
|
|
1689
|
+
support switchboard (`cx-support`), was about to write it again. PharmaLine
|
|
1690
|
+
re-exports it from its old paths, and its voice tests pass unchanged.
|
|
1691
|
+
|
|
1692
|
+
**`/voice`**: what a Next route handler needs. Imports only `node:crypto`.
|
|
1693
|
+
|
|
1694
|
+
- **TwiML**: `say(text, voice?)`, `play(url)`, `sayAndHangup`, `thenHangup`,
|
|
1695
|
+
`hangup`, `reject("busy" | "rejected")`, `gather({ actionUrl, input, numDigits,
|
|
1696
|
+
hints, prompt, … })`, `connectConversationRelay({ … preamble? })` and
|
|
1697
|
+
`dial({ number, callerId, actionUrl, timeout = 25, screenUrl?, statusCallback? })`.
|
|
1698
|
+
Every value is escaped. `screenUrl` is call screening: Twilio plays that TwiML
|
|
1699
|
+
to whoever answers, and an empty `<Response/>` bridges the call, which is how a
|
|
1700
|
+
mobile's voicemail is told apart from a person. `reject` as the first verb
|
|
1701
|
+
leaves the call unanswered and unbilled.
|
|
1702
|
+
- **Webhooks**: `createTwilioWebhooks(() => ({ authToken, webhookBaseUrl }))` →
|
|
1703
|
+
`{ readTwilioRequest, webhookUrl }`. The reader checks `X-Twilio-Signature`
|
|
1704
|
+
against the request URL and the public one (behind a proxy they differ), and
|
|
1705
|
+
answers 503 with no token, 403 on a bad signature. `twiml(xml)` is the response.
|
|
1706
|
+
- **Session tokens**: `signVoiceSession(signer, { tenantId, callSid, callerNumber })`
|
|
1707
|
+
and `verifyVoiceSession(verifier, token)`. Ed25519 (`vs2.`): the app holds the
|
|
1708
|
+
private key and mints, the relay holds the public key and only checks; the old
|
|
1709
|
+
HMAC `vst.` format still verifies where its secret is kept. Two hours, one socket
|
|
1710
|
+
per token. `generateVoiceSessionKeys()` prints a fresh pair. The wire format is
|
|
1711
|
+
PharmaLine's, byte for byte.
|
|
1712
|
+
- **Phone rules**: `toE164`, `presentedNumber` (withheld stand-ins are null),
|
|
1713
|
+
`isAllowedTransferNumber` (UK landline, 03, mobile and freephone only; never
|
|
1714
|
+
premium, personal or international), `divertsToAiLine`.
|
|
1715
|
+
- **Hours**: `parseOpeningHours`, `isOpenAt`, `nextOpening` ("at 9am tomorrow"),
|
|
1716
|
+
`describeOpeningHours`, and `lineStatus` / `teamReachable` / `transferTarget`.
|
|
1717
|
+
What a closed line _says_ is the app's: PharmaLine names NHS 111, a switchboard
|
|
1718
|
+
says when it will call back.
|
|
1719
|
+
- **Handoff**: `parseHandoff(HandoffData)`. Anything unexpected, including a failed
|
|
1720
|
+
session, is `unknown`, which the app treats as "put them through".
|
|
1721
|
+
- **Transfers**: `plannedTransfer(line, status)` (pure: where a transfer would go,
|
|
1722
|
+
or `none` / `refused` / `loops`), `dialTarget(line, status, how, { store, rules })`
|
|
1723
|
+
and `isTransferComingBack(line, from, store)`. House holds no database, so the
|
|
1724
|
+
store is the app's: `consume` (a rate-limit bucket), `refused` (its audit log, in
|
|
1725
|
+
its words), `remember` (the number just rung), and `isOwnNumber` /
|
|
1726
|
+
`takeRecentDial` for the loop check. The keys are PharmaLine's:
|
|
1727
|
+
`transfers:<id>:from:<caller>`, `transfers-direct:<id>`, `transfers:<id>`.
|
|
1728
|
+
|
|
1729
|
+
**`/voice/relay`**: Node only. Needs `ws` (an optional peer) and the configured
|
|
1730
|
+
provider's SDK.
|
|
1731
|
+
|
|
1732
|
+
- `startRelay({ port, verifier, createSession, twilio, limits, build })`: `/healthz`
|
|
1733
|
+
(via `/health`) and WebSockets on `/relay`. It checks Twilio's upgrade signature,
|
|
1734
|
+
the session token in `setup` (same call, once per token), and room: `limits.
|
|
1735
|
+
callsPerTenant` (default 20) and `pendingSockets` (50). A refused socket is a
|
|
1736
|
+
failed session to Twilio, so the app's handoff puts the caller through. The
|
|
1737
|
+
session is the app's: `createSession({ session, token, setup, parameters, send,
|
|
1738
|
+
log })` returns `{ onPrompt, onInterrupt, onDtmf?, close }`.
|
|
1739
|
+
- `createDialogModel({ provider, apiKey, model, tools: (team) => DialogTool[],
|
|
1740
|
+
transferTool, transferSilentKeys, cacheKeyPrefix, … })`: streams text from
|
|
1741
|
+
Claude or OpenAI with prompt caching on each block of instructions and the
|
|
1742
|
+
conversation, `store: false` on OpenAI, usage in the `/ai` shape, and the
|
|
1743
|
+
transfer tool's `say` spoken while its arguments stream (`TransferLineStream`).
|
|
1744
|
+
`team` picks which tool list a turn gets, so a closed line can offer a different
|
|
1745
|
+
transfer tool.
|
|
1746
|
+
- `SentenceGate` / `keepSentences`: hold streamed words back a sentence at a time
|
|
1747
|
+
and drop the ones that fail a rule, such as a promise to put someone through
|
|
1748
|
+
when nobody can be.
|
|
1749
|
+
- `createAgentFetch({ baseUrl, sessionToken })`: `get` / `post` to the app's
|
|
1750
|
+
agent API with the token as Bearer, an 8 s timeout and `{ success, data }`.
|
|
1751
|
+
|
|
1752
|
+
The call session itself (the prompt, the tools, the fail-safes) stays in each
|
|
1753
|
+
app: PharmaLine's checks identity and prescriptions, the switchboard's takes a
|
|
1754
|
+
name and a query. Both run the same ceilings (40 caller turns, 120 model
|
|
1755
|
+
requests, 60,000 characters), copied rather than shared until a third session
|
|
1756
|
+
needs them.
|
|
1757
|
+
|
|
1684
1758
|
### `@connextar/house/maintenance`, `/maintenance/react`
|
|
1685
1759
|
|
|
1686
1760
|
Planned maintenance on the database server an app depends on, shown to people
|
|
@@ -2462,6 +2536,46 @@ const parsed = inboxActionSchema.safeParse(await request.json());
|
|
|
2462
2536
|
await db.notification.updateMany({ where: { id: { in: parsed.data.ids }, userId }, data: actionChanges(action) });
|
|
2463
2537
|
```
|
|
2464
2538
|
|
|
2539
|
+
### `@connextar/house/calendar`
|
|
2540
|
+
|
|
2541
|
+
The rules of a day / week / month calendar — which view is showing and which
|
|
2542
|
+
day it is on, what that view covers, how ‹ › and the quick ranges move it, what
|
|
2543
|
+
the date button says, and how a day's timed items are laid out and dragged —
|
|
2544
|
+
with no markup and no database in it. quickie built the planner first; AltEd
|
|
2545
|
+
ported it in 0.190.0 and found that everything except the look and the items was
|
|
2546
|
+
the same code with a different time zone, so the shared part lives here.
|
|
2547
|
+
|
|
2548
|
+
- **Days are keys, times are wall-clock.** A day is a `yyyy-MM-dd` key, and day
|
|
2549
|
+
arithmetic (`addDays`, `addMonths`, `startOfWeek`, `startOfMonth`) works on the
|
|
2550
|
+
key itself, never on a local `Date`, so neither the server's zone nor the
|
|
2551
|
+
browser's can move a day. Times are minutes past midnight in a named zone:
|
|
2552
|
+
`zonedInstant(day, minutes, zone)` and `placeOf(instant, zone)` go between
|
|
2553
|
+
them, checked twice so the day the clocks change lands right. Weeks start on
|
|
2554
|
+
Monday.
|
|
2555
|
+
- **The address.** `parseCalendarQuery` reads `?view=&date=` from a request or a
|
|
2556
|
+
page's `searchParams`, falling back to the default view — the app's, or the
|
|
2557
|
+
reader's remembered one — and today; `calendarQuery` writes them back.
|
|
2558
|
+
- **What a view covers.** `visibleRange` (the day, its week, or the whole weeks
|
|
2559
|
+
around the month), `visibleDays` (optionally weekdays only) and
|
|
2560
|
+
`visibleWindow(view, anchor, zone)`, the `[from, to)` instants to read. Read
|
|
2561
|
+
only that window — and for a repeating item, each time it happens in it.
|
|
2562
|
+
- **Moving.** `stepAnchor` for ‹ ›; `QUICK_RANGES` (Today … Last month), with
|
|
2563
|
+
`matchingQuickRange` and `rangeLabel` so the button says "This week" rather
|
|
2564
|
+
than dates when it is; `rangeHeading` for the title over the grid.
|
|
2565
|
+
- **The grid.** `gridSlots` gives the rows, widened to take in anything outside
|
|
2566
|
+
the hours; `layoutDay` gives each timed item its lane among the ones it
|
|
2567
|
+
overlaps; `movedTimes` is a drop's new start and end, keeping the length.
|
|
2568
|
+
|
|
2569
|
+
The app owns the rest: its items and who may change them, its settings and where
|
|
2570
|
+
they're kept, and its grids' markup.
|
|
2571
|
+
|
|
2572
|
+
```ts
|
|
2573
|
+
const today = todayIn("Europe/London");
|
|
2574
|
+
const { view, anchor } = parseCalendarQuery(await searchParams, today, prefs.lastView);
|
|
2575
|
+
const { from, to } = visibleWindow(view, anchor, "Europe/London");
|
|
2576
|
+
const events = await db.event.findMany({ where: { startTime: { lt: to }, endTime: { gt: from } } });
|
|
2577
|
+
```
|
|
2578
|
+
|
|
2465
2579
|
### `@connextar/house/web-search`
|
|
2466
2580
|
|
|
2467
2581
|
Google's organic results for a query, from whichever search API a deployment
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/calendar` — the rules of a day / week / month calendar.
|
|
3
|
+
*
|
|
4
|
+
* Views and the address they live in, what each view covers, stepping and quick ranges, the date button's words, and
|
|
5
|
+
* the day grid's rows, lanes and drag-to-move arithmetic — in a named time zone, with no markup and no database in it:
|
|
6
|
+
* Zod, and nothing else. The app owns its items, its settings, its storage and its look.
|
|
7
|
+
*/
|
|
8
|
+
export { CALENDAR_VIEWS, CALENDAR_VIEW_NAMES, QUICK_RANGES, addDays, addMonths, calendarQuery, calendarViewSchema, daysBetween, endOfMonth, gridSlots, hhmm, isCalendarView, isDayKey, layoutDay, longDate, matchingQuickRange, monthTitle, movedTimes, parseCalendarQuery, placeOf, rangeHeading, rangeLabel, shortDate, startOfMonth, startOfWeek, stepAnchor, todayIn, visibleDays, visibleRange, visibleWindow, weekdayName, weekdayOf, zonedInstant, type CalendarView, type GridHours, type QuickRange, } from "./planner.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/calendar/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,OAAO,EACP,SAAS,EACT,aAAa,EACb,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,UAAU,EACV,SAAS,EACT,YAAY,EACZ,WAAW,EACX,UAAU,EACV,OAAO,EACP,WAAW,EACX,YAAY,EACZ,aAAa,EACb,WAAW,EACX,SAAS,EACT,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/calendar` — the rules of a day / week / month calendar.
|
|
3
|
+
*
|
|
4
|
+
* Views and the address they live in, what each view covers, stepping and quick ranges, the date button's words, and
|
|
5
|
+
* the day grid's rows, lanes and drag-to-move arithmetic — in a named time zone, with no markup and no database in it:
|
|
6
|
+
* Zod, and nothing else. The app owns its items, its settings, its storage and its look.
|
|
7
|
+
*/
|
|
8
|
+
export { CALENDAR_VIEWS, CALENDAR_VIEW_NAMES, QUICK_RANGES, addDays, addMonths, calendarQuery, calendarViewSchema, daysBetween, endOfMonth, gridSlots, hhmm, isCalendarView, isDayKey, layoutDay, longDate, matchingQuickRange, monthTitle, movedTimes, parseCalendarQuery, placeOf, rangeHeading, rangeLabel, shortDate, startOfMonth, startOfWeek, stepAnchor, todayIn, visibleDays, visibleRange, visibleWindow, weekdayName, weekdayOf, zonedInstant, } from "./planner.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/calendar/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,OAAO,EACP,SAAS,EACT,aAAa,EACb,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,UAAU,EACV,SAAS,EACT,YAAY,EACZ,WAAW,EACX,UAAU,EACV,OAAO,EACP,WAAW,EACX,YAAY,EACZ,aAAa,EACb,WAAW,EACX,SAAS,EACT,YAAY,GAIb,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The rules of a day / week / month calendar, with no markup and no database in them.
|
|
4
|
+
*
|
|
5
|
+
* Which view is showing and which day it is anchored on (`parseCalendarQuery`, `calendarQuery`), what that view covers
|
|
6
|
+
* (`visibleRange`, `visibleDays`, `visibleWindow`), how ‹ › and the quick ranges move it (`stepAnchor`,
|
|
7
|
+
* `QUICK_RANGES`), what the date button and heading say (`rangeLabel`, `rangeHeading`), and how a day's timed items
|
|
8
|
+
* are laid out and moved (`gridSlots`, `layoutDay`, `movedTimes`). Built from quickie's planner and AltEd's port of it.
|
|
9
|
+
*
|
|
10
|
+
* Days are calendar days as `yyyy-MM-dd` keys and times are wall-clock minutes past midnight in a named time zone, so a
|
|
11
|
+
* 10:00 lesson sits at 10:00 all year. Day arithmetic is done on the keys themselves — never on a local `Date` — so the
|
|
12
|
+
* server's zone and the browser's can't move a day. Weeks start on Monday (en-GB).
|
|
13
|
+
*/
|
|
14
|
+
export declare const CALENDAR_VIEW_NAMES: readonly ["day", "week", "month"];
|
|
15
|
+
export type CalendarView = (typeof CALENDAR_VIEW_NAMES)[number];
|
|
16
|
+
export declare const CALENDAR_VIEWS: readonly {
|
|
17
|
+
value: CalendarView;
|
|
18
|
+
label: string;
|
|
19
|
+
}[];
|
|
20
|
+
/** A view's name, for a request body or a stored setting. */
|
|
21
|
+
export declare const calendarViewSchema: z.ZodEnum<{
|
|
22
|
+
day: "day";
|
|
23
|
+
week: "week";
|
|
24
|
+
month: "month";
|
|
25
|
+
}>;
|
|
26
|
+
export declare function isCalendarView(value: unknown): value is CalendarView;
|
|
27
|
+
/** Whether `value` is a real day key between the years 1900 and 2200 — not "2026-02-30". */
|
|
28
|
+
export declare function isDayKey(value: unknown): value is string;
|
|
29
|
+
export declare function addDays(key: string, days: number): string;
|
|
30
|
+
/** The same day `months` on, or the month's last day when it hasn't got one (31 January + 1 → 28 February). */
|
|
31
|
+
export declare function addMonths(key: string, months: number): string;
|
|
32
|
+
/** 0 for Sunday to 6 for Saturday, as `Date.getDay`. */
|
|
33
|
+
export declare function weekdayOf(key: string): number;
|
|
34
|
+
/** The Monday of the day's week. */
|
|
35
|
+
export declare function startOfWeek(key: string): string;
|
|
36
|
+
export declare function startOfMonth(key: string): string;
|
|
37
|
+
export declare function endOfMonth(key: string): string;
|
|
38
|
+
/** The day keys from `start` to `end`, both included. */
|
|
39
|
+
export declare function daysBetween(start: string, end: string): string[];
|
|
40
|
+
/**
|
|
41
|
+
* The instant `day` at `minutes` past midnight happens in `timeZone`. Worked out twice, so a time on the day the clocks
|
|
42
|
+
* change lands right too.
|
|
43
|
+
*/
|
|
44
|
+
export declare function zonedInstant(day: string, minutes: number, timeZone: string): Date;
|
|
45
|
+
/** Where an instant falls on the calendar in `timeZone`: its day and minutes past midnight. */
|
|
46
|
+
export declare function placeOf(instant: string | Date, timeZone: string): {
|
|
47
|
+
day: string;
|
|
48
|
+
minutes: number;
|
|
49
|
+
};
|
|
50
|
+
/** Today in `timeZone`, as a day key. */
|
|
51
|
+
export declare function todayIn(timeZone: string, now?: Date): string;
|
|
52
|
+
type QueryInput = URLSearchParams | Record<string, string | string[] | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* The view and anchor day from `?view=&date=` — a request's search params or a page's `searchParams`. Anything
|
|
55
|
+
* unreadable is the default view (the app's, or the reader's remembered one) and today.
|
|
56
|
+
*/
|
|
57
|
+
export declare function parseCalendarQuery(query: QueryInput, todayKey: string, defaultView?: CalendarView): {
|
|
58
|
+
view: CalendarView;
|
|
59
|
+
anchor: string;
|
|
60
|
+
};
|
|
61
|
+
/** The query string for a view and day, `view=week&date=2026-11-23`, to put after the calendar's path and a `?`. */
|
|
62
|
+
export declare function calendarQuery(view: CalendarView, anchor: string): string;
|
|
63
|
+
/** The first and last day a view shows: the day, its Monday–Sunday week, or the whole weeks around its month. */
|
|
64
|
+
export declare function visibleRange(view: CalendarView, anchor: string): {
|
|
65
|
+
start: string;
|
|
66
|
+
end: string;
|
|
67
|
+
};
|
|
68
|
+
/** Every day a view shows, in order; with `weekdaysOnly`, the week and month views leave out Saturday and Sunday. */
|
|
69
|
+
export declare function visibleDays(view: CalendarView, anchor: string, weekdaysOnly?: boolean): string[];
|
|
70
|
+
/**
|
|
71
|
+
* The instants a view covers, `[from, to)`: midnight of its first day to midnight after its last, in `timeZone`. Read
|
|
72
|
+
* only what falls in it — and for a repeating item, every time it happens in it.
|
|
73
|
+
*/
|
|
74
|
+
export declare function visibleWindow(view: CalendarView, anchor: string, timeZone: string): {
|
|
75
|
+
from: Date;
|
|
76
|
+
to: Date;
|
|
77
|
+
};
|
|
78
|
+
/** The anchor one step back or on: a day, a week, or the first of the month before or after. */
|
|
79
|
+
export declare function stepAnchor(view: CalendarView, anchor: string, by: 1 | -1): string;
|
|
80
|
+
export interface QuickRange {
|
|
81
|
+
key: string;
|
|
82
|
+
label: string;
|
|
83
|
+
view: CalendarView;
|
|
84
|
+
anchor: (todayKey: string) => string;
|
|
85
|
+
}
|
|
86
|
+
/** The ranges a date button offers beside its months. */
|
|
87
|
+
export declare const QUICK_RANGES: readonly QuickRange[];
|
|
88
|
+
/** The quick range a view and anchor are, if any ("This week"), so the button can say so. */
|
|
89
|
+
export declare function matchingQuickRange(view: CalendarView, anchor: string, todayKey: string): QuickRange | null;
|
|
90
|
+
/** "12/11/2026". */
|
|
91
|
+
export declare function shortDate(key: string): string;
|
|
92
|
+
/** "Thursday 12 November 2026". */
|
|
93
|
+
export declare function longDate(key: string): string;
|
|
94
|
+
/** "November 2026". */
|
|
95
|
+
export declare function monthTitle(key: string): string;
|
|
96
|
+
/** "Mon", "Tue"… for a day key; `long` gives "Monday". */
|
|
97
|
+
export declare function weekdayName(key: string, long?: boolean): string;
|
|
98
|
+
/**
|
|
99
|
+
* What the date button says: the quick range's name when it is one ("Last week"), otherwise the dates —
|
|
100
|
+
* "Thu 12/11/2026", "02/11/2026 – 08/11/2026" or "November 2026".
|
|
101
|
+
*/
|
|
102
|
+
export declare function rangeLabel(view: CalendarView, anchor: string, todayKey: string): string;
|
|
103
|
+
/** The heading over the grid: "Thursday 12 November 2026", "2 – 8 Nov 2026", "30 Nov – 6 Dec 2026", "November 2026". */
|
|
104
|
+
export declare function rangeHeading(view: CalendarView, anchor: string): string;
|
|
105
|
+
/** Minutes past midnight as `HH:mm`. */
|
|
106
|
+
export declare function hhmm(minutes: number): string;
|
|
107
|
+
export interface GridHours {
|
|
108
|
+
/** The first hour the grid shows, 0–23. */
|
|
109
|
+
startHour: number;
|
|
110
|
+
/** The hour it ends at, 1–24, after `startHour`. */
|
|
111
|
+
endHour: number;
|
|
112
|
+
slotMinutes: 30 | 60;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The rows of a day or week grid, as minutes past midnight: every slot from the first hour to the last, widened to the
|
|
116
|
+
* hour to take in anything timed outside them.
|
|
117
|
+
*/
|
|
118
|
+
export declare function gridSlots(hours: GridHours, spans: readonly {
|
|
119
|
+
start: number;
|
|
120
|
+
end: number;
|
|
121
|
+
}[]): number[];
|
|
122
|
+
/**
|
|
123
|
+
* Each timed item's place in its day: its lane among the ones it overlaps, and how many lanes that cluster of
|
|
124
|
+
* overlapping items has — so two lessons at 15:00 sit side by side at half width each.
|
|
125
|
+
*/
|
|
126
|
+
export declare function layoutDay<T extends {
|
|
127
|
+
start: number;
|
|
128
|
+
end: number;
|
|
129
|
+
}>(items: readonly T[]): (T & {
|
|
130
|
+
lane: number;
|
|
131
|
+
lanes: number;
|
|
132
|
+
})[];
|
|
133
|
+
/**
|
|
134
|
+
* An item's new times when it's dropped on `day` at `minutes` past midnight in `timeZone` — or, with `minutes` null (a
|
|
135
|
+
* month cell), at its own time of day — keeping its length. ISO strings in, ISO strings out.
|
|
136
|
+
*/
|
|
137
|
+
export declare function movedTimes(item: {
|
|
138
|
+
startTime: string;
|
|
139
|
+
endTime: string;
|
|
140
|
+
}, day: string, minutes: number | null, timeZone: string): {
|
|
141
|
+
startTime: string;
|
|
142
|
+
endTime: string;
|
|
143
|
+
};
|
|
144
|
+
export {};
|
|
145
|
+
//# sourceMappingURL=planner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"planner.d.ts","sourceRoot":"","sources":["../../src/calendar/planner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;;;;;GAWG;AAEH,eAAO,MAAM,mBAAmB,mCAAoC,CAAC;AACrE,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhE,eAAO,MAAM,cAAc,EAAE,SAAS;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,MAAM,C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IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAChE,KAAK,EAAE,SAAS,CAAC,EAAE,GAClB,CAAC,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,EAAE,CAuBzC;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,EAC5C,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,GAAG,IAAI,EACtB,QAAQ,EAAE,MAAM,GACf;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAKxC"}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The rules of a day / week / month calendar, with no markup and no database in them.
|
|
4
|
+
*
|
|
5
|
+
* Which view is showing and which day it is anchored on (`parseCalendarQuery`, `calendarQuery`), what that view covers
|
|
6
|
+
* (`visibleRange`, `visibleDays`, `visibleWindow`), how ‹ › and the quick ranges move it (`stepAnchor`,
|
|
7
|
+
* `QUICK_RANGES`), what the date button and heading say (`rangeLabel`, `rangeHeading`), and how a day's timed items
|
|
8
|
+
* are laid out and moved (`gridSlots`, `layoutDay`, `movedTimes`). Built from quickie's planner and AltEd's port of it.
|
|
9
|
+
*
|
|
10
|
+
* Days are calendar days as `yyyy-MM-dd` keys and times are wall-clock minutes past midnight in a named time zone, so a
|
|
11
|
+
* 10:00 lesson sits at 10:00 all year. Day arithmetic is done on the keys themselves — never on a local `Date` — so the
|
|
12
|
+
* server's zone and the browser's can't move a day. Weeks start on Monday (en-GB).
|
|
13
|
+
*/
|
|
14
|
+
export const CALENDAR_VIEW_NAMES = ["day", "week", "month"];
|
|
15
|
+
export const CALENDAR_VIEWS = [
|
|
16
|
+
{ value: "day", label: "Day" },
|
|
17
|
+
{ value: "week", label: "Week" },
|
|
18
|
+
{ value: "month", label: "Month" },
|
|
19
|
+
];
|
|
20
|
+
/** A view's name, for a request body or a stored setting. */
|
|
21
|
+
export const calendarViewSchema = z.enum(CALENDAR_VIEW_NAMES);
|
|
22
|
+
export function isCalendarView(value) {
|
|
23
|
+
return typeof value === "string" && CALENDAR_VIEW_NAMES.includes(value);
|
|
24
|
+
}
|
|
25
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
26
|
+
// Day keys
|
|
27
|
+
const KEY_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
28
|
+
const DAY_MS = 86_400_000;
|
|
29
|
+
const MONTHS = [
|
|
30
|
+
"January",
|
|
31
|
+
"February",
|
|
32
|
+
"March",
|
|
33
|
+
"April",
|
|
34
|
+
"May",
|
|
35
|
+
"June",
|
|
36
|
+
"July",
|
|
37
|
+
"August",
|
|
38
|
+
"September",
|
|
39
|
+
"October",
|
|
40
|
+
"November",
|
|
41
|
+
"December",
|
|
42
|
+
];
|
|
43
|
+
const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
|
44
|
+
function utcOf(key) {
|
|
45
|
+
const [, y, m, d] = KEY_PATTERN.exec(key) ?? [];
|
|
46
|
+
return Date.UTC(Number(y), Number(m) - 1, Number(d));
|
|
47
|
+
}
|
|
48
|
+
function keyOf(utc) {
|
|
49
|
+
return new Date(utc).toISOString().slice(0, 10);
|
|
50
|
+
}
|
|
51
|
+
/** Whether `value` is a real day key between the years 1900 and 2200 — not "2026-02-30". */
|
|
52
|
+
export function isDayKey(value) {
|
|
53
|
+
if (typeof value !== "string" || !KEY_PATTERN.test(value))
|
|
54
|
+
return false;
|
|
55
|
+
const year = Number(value.slice(0, 4));
|
|
56
|
+
return year > 1900 && year < 2200 && keyOf(utcOf(value)) === value;
|
|
57
|
+
}
|
|
58
|
+
export function addDays(key, days) {
|
|
59
|
+
return keyOf(utcOf(key) + days * DAY_MS);
|
|
60
|
+
}
|
|
61
|
+
/** The same day `months` on, or the month's last day when it hasn't got one (31 January + 1 → 28 February). */
|
|
62
|
+
export function addMonths(key, months) {
|
|
63
|
+
const date = new Date(utcOf(key));
|
|
64
|
+
const target = date.getUTCMonth() + months;
|
|
65
|
+
const last = new Date(Date.UTC(date.getUTCFullYear(), target + 1, 0)).getUTCDate();
|
|
66
|
+
return keyOf(Date.UTC(date.getUTCFullYear(), target, Math.min(date.getUTCDate(), last)));
|
|
67
|
+
}
|
|
68
|
+
/** 0 for Sunday to 6 for Saturday, as `Date.getDay`. */
|
|
69
|
+
export function weekdayOf(key) {
|
|
70
|
+
return new Date(utcOf(key)).getUTCDay();
|
|
71
|
+
}
|
|
72
|
+
/** The Monday of the day's week. */
|
|
73
|
+
export function startOfWeek(key) {
|
|
74
|
+
return addDays(key, -((weekdayOf(key) + 6) % 7));
|
|
75
|
+
}
|
|
76
|
+
export function startOfMonth(key) {
|
|
77
|
+
return `${key.slice(0, 7)}-01`;
|
|
78
|
+
}
|
|
79
|
+
export function endOfMonth(key) {
|
|
80
|
+
return addDays(addMonths(startOfMonth(key), 1), -1);
|
|
81
|
+
}
|
|
82
|
+
/** The day keys from `start` to `end`, both included. */
|
|
83
|
+
export function daysBetween(start, end) {
|
|
84
|
+
const days = [];
|
|
85
|
+
for (let d = start; d <= end; d = addDays(d, 1))
|
|
86
|
+
days.push(d);
|
|
87
|
+
return days;
|
|
88
|
+
}
|
|
89
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
90
|
+
// Time zones
|
|
91
|
+
function zoneParts(instant, timeZone) {
|
|
92
|
+
return Object.fromEntries(new Intl.DateTimeFormat("en-CA", {
|
|
93
|
+
timeZone,
|
|
94
|
+
hourCycle: "h23",
|
|
95
|
+
year: "numeric",
|
|
96
|
+
month: "2-digit",
|
|
97
|
+
day: "2-digit",
|
|
98
|
+
hour: "2-digit",
|
|
99
|
+
minute: "2-digit",
|
|
100
|
+
second: "2-digit",
|
|
101
|
+
})
|
|
102
|
+
.formatToParts(instant)
|
|
103
|
+
.map((part) => [part.type, part.value]));
|
|
104
|
+
}
|
|
105
|
+
function offsetMinutes(instant, timeZone) {
|
|
106
|
+
const p = zoneParts(instant, timeZone);
|
|
107
|
+
const asUtc = Date.UTC(Number(p.year), Number(p.month) - 1, Number(p.day), Number(p.hour), Number(p.minute), Number(p.second));
|
|
108
|
+
return Math.round((asUtc - Math.floor(instant.getTime() / 1000) * 1000) / 60_000);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The instant `day` at `minutes` past midnight happens in `timeZone`. Worked out twice, so a time on the day the clocks
|
|
112
|
+
* change lands right too.
|
|
113
|
+
*/
|
|
114
|
+
export function zonedInstant(day, minutes, timeZone) {
|
|
115
|
+
const wallClockAsUtc = utcOf(day) + minutes * 60_000;
|
|
116
|
+
const first = wallClockAsUtc - offsetMinutes(new Date(wallClockAsUtc), timeZone) * 60_000;
|
|
117
|
+
return new Date(wallClockAsUtc - offsetMinutes(new Date(first), timeZone) * 60_000);
|
|
118
|
+
}
|
|
119
|
+
/** Where an instant falls on the calendar in `timeZone`: its day and minutes past midnight. */
|
|
120
|
+
export function placeOf(instant, timeZone) {
|
|
121
|
+
const p = zoneParts(typeof instant === "string" ? new Date(instant) : instant, timeZone);
|
|
122
|
+
return { day: `${p.year}-${p.month}-${p.day}`, minutes: Number(p.hour) * 60 + Number(p.minute) };
|
|
123
|
+
}
|
|
124
|
+
/** Today in `timeZone`, as a day key. */
|
|
125
|
+
export function todayIn(timeZone, now = new Date()) {
|
|
126
|
+
return placeOf(now, timeZone).day;
|
|
127
|
+
}
|
|
128
|
+
function first(query, name) {
|
|
129
|
+
if (query instanceof URLSearchParams)
|
|
130
|
+
return query.get(name) ?? undefined;
|
|
131
|
+
const value = query[name];
|
|
132
|
+
return Array.isArray(value) ? value[0] : value;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The view and anchor day from `?view=&date=` — a request's search params or a page's `searchParams`. Anything
|
|
136
|
+
* unreadable is the default view (the app's, or the reader's remembered one) and today.
|
|
137
|
+
*/
|
|
138
|
+
export function parseCalendarQuery(query, todayKey, defaultView = "month") {
|
|
139
|
+
const view = first(query, "view");
|
|
140
|
+
const date = first(query, "date");
|
|
141
|
+
return { view: isCalendarView(view) ? view : defaultView, anchor: isDayKey(date) ? date : todayKey };
|
|
142
|
+
}
|
|
143
|
+
/** The query string for a view and day, `view=week&date=2026-11-23`, to put after the calendar's path and a `?`. */
|
|
144
|
+
export function calendarQuery(view, anchor) {
|
|
145
|
+
return new URLSearchParams({ view, date: anchor }).toString();
|
|
146
|
+
}
|
|
147
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
148
|
+
// What a view covers
|
|
149
|
+
/** The first and last day a view shows: the day, its Monday–Sunday week, or the whole weeks around its month. */
|
|
150
|
+
export function visibleRange(view, anchor) {
|
|
151
|
+
if (view === "day")
|
|
152
|
+
return { start: anchor, end: anchor };
|
|
153
|
+
if (view === "week")
|
|
154
|
+
return { start: startOfWeek(anchor), end: addDays(startOfWeek(anchor), 6) };
|
|
155
|
+
return { start: startOfWeek(startOfMonth(anchor)), end: addDays(startOfWeek(endOfMonth(anchor)), 6) };
|
|
156
|
+
}
|
|
157
|
+
/** Every day a view shows, in order; with `weekdaysOnly`, the week and month views leave out Saturday and Sunday. */
|
|
158
|
+
export function visibleDays(view, anchor, weekdaysOnly = false) {
|
|
159
|
+
const { start, end } = visibleRange(view, anchor);
|
|
160
|
+
const days = daysBetween(start, end);
|
|
161
|
+
return view !== "day" && weekdaysOnly ? days.filter((d) => ![0, 6].includes(weekdayOf(d))) : days;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The instants a view covers, `[from, to)`: midnight of its first day to midnight after its last, in `timeZone`. Read
|
|
165
|
+
* only what falls in it — and for a repeating item, every time it happens in it.
|
|
166
|
+
*/
|
|
167
|
+
export function visibleWindow(view, anchor, timeZone) {
|
|
168
|
+
const { start, end } = visibleRange(view, anchor);
|
|
169
|
+
return { from: zonedInstant(start, 0, timeZone), to: zonedInstant(addDays(end, 1), 0, timeZone) };
|
|
170
|
+
}
|
|
171
|
+
/** The anchor one step back or on: a day, a week, or the first of the month before or after. */
|
|
172
|
+
export function stepAnchor(view, anchor, by) {
|
|
173
|
+
if (view === "day")
|
|
174
|
+
return addDays(anchor, by);
|
|
175
|
+
if (view === "week")
|
|
176
|
+
return addDays(anchor, 7 * by);
|
|
177
|
+
return startOfMonth(addMonths(startOfMonth(anchor), by));
|
|
178
|
+
}
|
|
179
|
+
/** The ranges a date button offers beside its months. */
|
|
180
|
+
export const QUICK_RANGES = [
|
|
181
|
+
{ key: "today", label: "Today", view: "day", anchor: (t) => t },
|
|
182
|
+
{ key: "tomorrow", label: "Tomorrow", view: "day", anchor: (t) => addDays(t, 1) },
|
|
183
|
+
{ key: "yesterday", label: "Yesterday", view: "day", anchor: (t) => addDays(t, -1) },
|
|
184
|
+
{ key: "this-week", label: "This week", view: "week", anchor: (t) => startOfWeek(t) },
|
|
185
|
+
{ key: "next-week", label: "Next week", view: "week", anchor: (t) => startOfWeek(addDays(t, 7)) },
|
|
186
|
+
{ key: "last-week", label: "Last week", view: "week", anchor: (t) => startOfWeek(addDays(t, -7)) },
|
|
187
|
+
{ key: "this-month", label: "This month", view: "month", anchor: (t) => startOfMonth(t) },
|
|
188
|
+
{ key: "next-month", label: "Next month", view: "month", anchor: (t) => addMonths(startOfMonth(t), 1) },
|
|
189
|
+
{ key: "last-month", label: "Last month", view: "month", anchor: (t) => addMonths(startOfMonth(t), -1) },
|
|
190
|
+
];
|
|
191
|
+
/** The quick range a view and anchor are, if any ("This week"), so the button can say so. */
|
|
192
|
+
export function matchingQuickRange(view, anchor, todayKey) {
|
|
193
|
+
const shown = visibleRange(view, anchor);
|
|
194
|
+
return (QUICK_RANGES.find((range) => {
|
|
195
|
+
if (range.view !== view)
|
|
196
|
+
return false;
|
|
197
|
+
const theirs = visibleRange(range.view, range.anchor(todayKey));
|
|
198
|
+
return theirs.start === shown.start && theirs.end === shown.end;
|
|
199
|
+
}) ?? null);
|
|
200
|
+
}
|
|
201
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
202
|
+
// Words
|
|
203
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
204
|
+
function parts(key) {
|
|
205
|
+
const date = new Date(utcOf(key));
|
|
206
|
+
return { y: date.getUTCFullYear(), m: date.getUTCMonth(), d: date.getUTCDate(), w: date.getUTCDay() };
|
|
207
|
+
}
|
|
208
|
+
/** "12/11/2026". */
|
|
209
|
+
export function shortDate(key) {
|
|
210
|
+
const { y, m, d } = parts(key);
|
|
211
|
+
return `${pad(d)}/${pad(m + 1)}/${y}`;
|
|
212
|
+
}
|
|
213
|
+
/** "Thursday 12 November 2026". */
|
|
214
|
+
export function longDate(key) {
|
|
215
|
+
const { y, m, d, w } = parts(key);
|
|
216
|
+
return `${WEEKDAYS[w]} ${d} ${MONTHS[m]} ${y}`;
|
|
217
|
+
}
|
|
218
|
+
/** "November 2026". */
|
|
219
|
+
export function monthTitle(key) {
|
|
220
|
+
const { y, m } = parts(key);
|
|
221
|
+
return `${MONTHS[m]} ${y}`;
|
|
222
|
+
}
|
|
223
|
+
/** "Mon", "Tue"… for a day key; `long` gives "Monday". */
|
|
224
|
+
export function weekdayName(key, long = false) {
|
|
225
|
+
const name = WEEKDAYS[weekdayOf(key)] ?? "";
|
|
226
|
+
return long ? name : name.slice(0, 3);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* What the date button says: the quick range's name when it is one ("Last week"), otherwise the dates —
|
|
230
|
+
* "Thu 12/11/2026", "02/11/2026 – 08/11/2026" or "November 2026".
|
|
231
|
+
*/
|
|
232
|
+
export function rangeLabel(view, anchor, todayKey) {
|
|
233
|
+
const quick = matchingQuickRange(view, anchor, todayKey);
|
|
234
|
+
if (quick)
|
|
235
|
+
return quick.label;
|
|
236
|
+
if (view === "day")
|
|
237
|
+
return `${weekdayName(anchor)} ${shortDate(anchor)}`;
|
|
238
|
+
if (view === "month")
|
|
239
|
+
return monthTitle(anchor);
|
|
240
|
+
const { start, end } = visibleRange("week", anchor);
|
|
241
|
+
return `${shortDate(start)} – ${shortDate(end)}`;
|
|
242
|
+
}
|
|
243
|
+
/** The heading over the grid: "Thursday 12 November 2026", "2 – 8 Nov 2026", "30 Nov – 6 Dec 2026", "November 2026". */
|
|
244
|
+
export function rangeHeading(view, anchor) {
|
|
245
|
+
if (view === "day")
|
|
246
|
+
return longDate(anchor);
|
|
247
|
+
if (view === "month")
|
|
248
|
+
return monthTitle(anchor);
|
|
249
|
+
const { start, end } = visibleRange("week", anchor);
|
|
250
|
+
const s = parts(start);
|
|
251
|
+
const e = parts(end);
|
|
252
|
+
const mon = (m) => (MONTHS[m] ?? "").slice(0, 3);
|
|
253
|
+
if (s.y !== e.y)
|
|
254
|
+
return `${s.d} ${mon(s.m)} ${s.y} – ${e.d} ${mon(e.m)} ${e.y}`;
|
|
255
|
+
return s.m === e.m ? `${s.d} – ${e.d} ${mon(e.m)} ${e.y}` : `${s.d} ${mon(s.m)} – ${e.d} ${mon(e.m)} ${e.y}`;
|
|
256
|
+
}
|
|
257
|
+
/** Minutes past midnight as `HH:mm`. */
|
|
258
|
+
export function hhmm(minutes) {
|
|
259
|
+
return `${pad(Math.floor(minutes / 60))}:${pad(minutes % 60)}`;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The rows of a day or week grid, as minutes past midnight: every slot from the first hour to the last, widened to the
|
|
263
|
+
* hour to take in anything timed outside them.
|
|
264
|
+
*/
|
|
265
|
+
export function gridSlots(hours, spans) {
|
|
266
|
+
const earliest = Math.min(hours.startHour * 60, ...spans.map((s) => Math.floor(s.start / 60) * 60));
|
|
267
|
+
const latest = Math.max(hours.endHour * 60, ...spans.map((s) => Math.ceil(s.end / 60) * 60));
|
|
268
|
+
const slots = [];
|
|
269
|
+
for (let m = Math.max(0, earliest); m < Math.min(24 * 60, latest); m += hours.slotMinutes)
|
|
270
|
+
slots.push(m);
|
|
271
|
+
return slots;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Each timed item's place in its day: its lane among the ones it overlaps, and how many lanes that cluster of
|
|
275
|
+
* overlapping items has — so two lessons at 15:00 sit side by side at half width each.
|
|
276
|
+
*/
|
|
277
|
+
export function layoutDay(items) {
|
|
278
|
+
const sorted = [...items].sort((a, b) => a.start - b.start || b.end - a.end);
|
|
279
|
+
const placed = [];
|
|
280
|
+
let cluster = [];
|
|
281
|
+
let clusterEnd = -1;
|
|
282
|
+
let laneEnds = [];
|
|
283
|
+
const close = () => {
|
|
284
|
+
for (const p of cluster)
|
|
285
|
+
p.lanes = laneEnds.length;
|
|
286
|
+
cluster = [];
|
|
287
|
+
laneEnds = [];
|
|
288
|
+
};
|
|
289
|
+
for (const item of sorted) {
|
|
290
|
+
if (item.start >= clusterEnd)
|
|
291
|
+
close();
|
|
292
|
+
let lane = laneEnds.findIndex((end) => end <= item.start);
|
|
293
|
+
if (lane < 0)
|
|
294
|
+
lane = laneEnds.push(item.end) - 1;
|
|
295
|
+
else
|
|
296
|
+
laneEnds[lane] = item.end;
|
|
297
|
+
const p = { ...item, lane, lanes: 1 };
|
|
298
|
+
cluster.push(p);
|
|
299
|
+
placed.push(p);
|
|
300
|
+
clusterEnd = Math.max(clusterEnd, item.end);
|
|
301
|
+
}
|
|
302
|
+
close();
|
|
303
|
+
return placed;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* An item's new times when it's dropped on `day` at `minutes` past midnight in `timeZone` — or, with `minutes` null (a
|
|
307
|
+
* month cell), at its own time of day — keeping its length. ISO strings in, ISO strings out.
|
|
308
|
+
*/
|
|
309
|
+
export function movedTimes(item, day, minutes, timeZone) {
|
|
310
|
+
const start = new Date(item.startTime);
|
|
311
|
+
const length = new Date(item.endTime).getTime() - start.getTime();
|
|
312
|
+
const next = zonedInstant(day, minutes ?? placeOf(start, timeZone).minutes, timeZone);
|
|
313
|
+
return { startTime: next.toISOString(), endTime: new Date(next.getTime() + length).toISOString() };
|
|
314
|
+
}
|
|
315
|
+
//# sourceMappingURL=planner.js.map
|