@zappie/foundations 0.19.0 → 0.20.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/CHANGELOG.md +8 -0
- package/README.md +66 -0
- package/dist/hours/index.d.ts +237 -0
- package/dist/hours/index.js +2 -0
- package/dist/hours/index.js.map +6 -0
- package/dist/version.js +1 -1
- package/package.json +5 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @zappie/foundations
|
|
2
2
|
|
|
3
|
+
## 0.20.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 4102e33: `@zappie/foundations/hours` puts a branch's hours into words, in the branch's own time zone, then the store's, then Africa/Lagos. `openStatus` and `useOpenStatus` say whether it is open, closing soon or when it opens next. `opensLabel` and `nextOpening` read the API's next opening and fall back to the hours. `closedForOrders` and `takesOrdersAhead` say whether it takes an order now or for later. `openingHours` groups a week from Sunday, Monday or today, and `branchHours` gives a branch card its status, today's hours and its week. Every label takes your clock style, weekday names and words. `useNow` is the shopper's clock once the page is in the browser.
|
|
8
|
+
|
|
9
|
+
The starter's Our branches page shows each branch's status, whether it takes orders while closed and its week from today.
|
|
10
|
+
|
|
3
11
|
## 0.19.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ Want ready-made, styled components instead? Copy them into your project from the
|
|
|
38
38
|
| `@zappie/foundations/branch-switch` | Choosing a branch in a React Router site without waiting for the server |
|
|
39
39
|
| `@zappie/foundations/branch-finder` | Opening the site's branch picker or locator from any control |
|
|
40
40
|
| `@zappie/foundations/branch-list` | Nearest-first branch lists and what each branch sells |
|
|
41
|
+
| `@zappie/foundations/hours` | Open now, opening labels, closed for orders and a week of hours |
|
|
41
42
|
| `@zappie/foundations/checkout` | Checkout behaviour for a bag or a cart page, with no markup |
|
|
42
43
|
| `@zappie/foundations/loyalty` | The rewards sheet's parts, the bag's rewards row and the claim toast |
|
|
43
44
|
| `@zappie/foundations/order` | A customer's order details page, as parts or one default layout |
|
|
@@ -650,6 +651,71 @@ const offers = useBranchOffers(store.branches, { active: open });
|
|
|
650
651
|
- `menusLabel(names)` joins the names with " · ", and reads "Breakfast only"
|
|
651
652
|
for one.
|
|
652
653
|
|
|
654
|
+
## Hours
|
|
655
|
+
|
|
656
|
+
`@zappie/foundations/hours` puts a branch's hours into words: open now and
|
|
657
|
+
closing soon, when it opens next, whether it takes orders, a week of hours and
|
|
658
|
+
a branch card's hours. It reads each branch in its own time zone, then the
|
|
659
|
+
store's (`storeTimezone`), then Africa/Lagos. It loads only on the pages that
|
|
660
|
+
use it.
|
|
661
|
+
|
|
662
|
+
```tsx
|
|
663
|
+
import {
|
|
664
|
+
closedForOrders,
|
|
665
|
+
opensLabel,
|
|
666
|
+
useBranchHours,
|
|
667
|
+
useOpenStatus,
|
|
668
|
+
} from "@zappie/foundations/hours";
|
|
669
|
+
|
|
670
|
+
const status = useOpenStatus(branch, { storeTimezone: store.timezone });
|
|
671
|
+
// undefined on the server, then { isOpen, closingSoon, closedForOrders, label: "Open until 10 pm", ... }
|
|
672
|
+
const hours = useBranchHours(branch, { storeTimezone: store.timezone });
|
|
673
|
+
{
|
|
674
|
+
hours?.week.map((row) => (
|
|
675
|
+
<div key={row.dayLabel} aria-current={row.today && "date"}>
|
|
676
|
+
<dt>{row.dayLabel}</dt>
|
|
677
|
+
<dd>{row.hoursLabel}</dd>
|
|
678
|
+
</div>
|
|
679
|
+
));
|
|
680
|
+
}
|
|
681
|
+
opensLabel(branch, { clock: { hour12: false } }); // "Opens tomorrow at 7:00"
|
|
682
|
+
closedForOrders(branch); // closed, and not taking orders for later
|
|
683
|
+
```
|
|
684
|
+
|
|
685
|
+
- `openStatus(branch, options)` works out the state at `now` from the weekly
|
|
686
|
+
hours, special hours, overnight hours and `closedUntil`. Its label reads
|
|
687
|
+
"Open until 10 pm", "Closes at 10 pm" within `closingSoonMinutes` (30 by
|
|
688
|
+
default), "Opens at 8 am", "Opens tomorrow at 8 am", "Opens Mon at 8 am" or
|
|
689
|
+
"Closed". `closedForOrders` is true when it is closed and takes no orders
|
|
690
|
+
ahead.
|
|
691
|
+
- `opensLabel(branch, options)` and `nextOpening(branch, options)` give when a
|
|
692
|
+
closed branch opens next. `from: "api"`, the default, reads `isOpen`,
|
|
693
|
+
`closedUntil` and `nextChangeAt`, and falls back to the hours when no
|
|
694
|
+
opening is still ahead; `from: "hours"` reads only the hours. Today and
|
|
695
|
+
tomorrow are the branch's local dates.
|
|
696
|
+
- Every label takes `clock` (`hour12`, `padHour`, `minutes`, `period`,
|
|
697
|
+
`space`, `roundLastMinute`), `weekday` (`short` or `long`) and `phrases`,
|
|
698
|
+
whose `{time}` and `{weekday}` fill in: `{ today: "Opens today {time}" }`.
|
|
699
|
+
`formatClock(time, clock)` formats one wall-clock time.
|
|
700
|
+
- `closedForOrders(branch, { lastOrderMinutes? })` is the API's closed and no
|
|
701
|
+
orders ahead. With `lastOrderMinutes`, an open branch that closes within
|
|
702
|
+
them takes no order either, unless it takes orders ahead.
|
|
703
|
+
`takesOrdersAhead(branch, menu, menuIds)` says whether a bag can be timed
|
|
704
|
+
for later, by the branch or a pre-order menu.
|
|
705
|
+
- `openingHours(times, options)` groups a week into runs of days that share
|
|
706
|
+
their hours, from `start` (`sunday`, `monday` or `today`). `closed` names
|
|
707
|
+
the closed days, `everyDay` collapses a week of the same hours, `allDay`
|
|
708
|
+
names midnight to midnight, `today` marks its row, and `dayNames`, `range`
|
|
709
|
+
and `through` set the words.
|
|
710
|
+
- `branchHours(branch, options)` is a branch card's hours: its `status`,
|
|
711
|
+
`today`'s hours with special hours, and its `week` from today.
|
|
712
|
+
- `useNow()` is the time once the page is idle in the browser, then each
|
|
713
|
+
minute and on focus; undefined on the server. `useOpenStatus` and
|
|
714
|
+
`useBranchHours` run on it, so the server's HTML never disagrees with the
|
|
715
|
+
first render.
|
|
716
|
+
|
|
717
|
+
`src/hours/ADOPTING.md` maps each site's own copies onto these.
|
|
718
|
+
|
|
653
719
|
## Item form focus
|
|
654
720
|
|
|
655
721
|
When an add fails validation, `ItemForm.Root` focuses the first invalid group
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { OpeningTime, Branch, Menu } from '@zappie/sdk';
|
|
2
|
+
|
|
3
|
+
/** A day of the week, Sunday 0 to Saturday 6, as `OpeningTime.day` counts it. */
|
|
4
|
+
type Weekday = OpeningTime["day"];
|
|
5
|
+
/** The time zone for a branch and a store that name none. */
|
|
6
|
+
declare const DEFAULT_TIMEZONE = "Africa/Lagos";
|
|
7
|
+
/**
|
|
8
|
+
* The time zone a branch's hours are kept in: its own, then the store's, then Africa/Lagos.
|
|
9
|
+
*
|
|
10
|
+
* @param storeTimezone - The store's time zone, for a branch that names none.
|
|
11
|
+
*/
|
|
12
|
+
declare function branchTimezone(branch: Pick<Branch, "timezone">, storeTimezone?: string): string;
|
|
13
|
+
/** The weekday it is at `now` in `timezone`. */
|
|
14
|
+
declare function todayIn(timezone: string, now?: Date): Weekday;
|
|
15
|
+
/**
|
|
16
|
+
* How a time reads. The default is a 12-hour clock in lower case with a space, minutes only when they aren't
|
|
17
|
+
* zero: "9 am", "7:30 pm".
|
|
18
|
+
*/
|
|
19
|
+
type ClockStyle = {
|
|
20
|
+
/** A 12-hour clock with am and pm. Defaults to true. A 24-hour clock always shows its minutes: "7:00", "19:30". */
|
|
21
|
+
hour12?: boolean | undefined;
|
|
22
|
+
/** Pad the hour to two digits: "09:00 AM". Defaults to false. */
|
|
23
|
+
padHour?: boolean | undefined;
|
|
24
|
+
/** `auto` drops zero minutes ("9 am"), `always` keeps them ("9:00 am"). Defaults to `auto`. */
|
|
25
|
+
minutes?: "auto" | "always" | undefined;
|
|
26
|
+
/** `lower` reads "am", `upper` reads "AM". Defaults to `lower`. */
|
|
27
|
+
period?: "lower" | "upper" | undefined;
|
|
28
|
+
/** A space between the time and am or pm. Defaults to true. */
|
|
29
|
+
space?: boolean | undefined;
|
|
30
|
+
/** Read a minute to the hour as the hour, so the API's 16:59 close reads "5 pm". Defaults to false. */
|
|
31
|
+
roundLastMinute?: boolean | undefined;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Format an HH:mm or HH:mm:ss wall-clock time. Returns invalid input unchanged.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* formatClock("19:30"); // "7:30 pm"
|
|
39
|
+
* formatClock("09:00", { padHour: true, minutes: "always", period: "upper", space: false }); // "09:00AM"
|
|
40
|
+
* formatClock("07:00", { hour12: false }); // "7:00"
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function formatClock(time: string, { hour12, padHour, minutes, period, space, roundLastMinute, }?: ClockStyle): string;
|
|
44
|
+
/** When a closed branch opens next, in its own time zone. */
|
|
45
|
+
type NextOpening = {
|
|
46
|
+
at: Date;
|
|
47
|
+
/** Whether that is today or tomorrow in the branch's time zone, or a later day. */
|
|
48
|
+
day: "today" | "tomorrow" | "later";
|
|
49
|
+
weekday: Weekday;
|
|
50
|
+
/** The local wall-clock time, HH:mm. */
|
|
51
|
+
time: string;
|
|
52
|
+
};
|
|
53
|
+
/** The instant to measure from and the store's time zone, for a branch that names none. */
|
|
54
|
+
type HoursOptions = {
|
|
55
|
+
/** Defaults to the current time. */
|
|
56
|
+
now?: Date | undefined;
|
|
57
|
+
/** The store's time zone, for a branch that names none. The branch's own always wins. */
|
|
58
|
+
storeTimezone?: string | undefined;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* When a closed branch opens next, or undefined while it is open or when nothing is ahead.
|
|
62
|
+
*
|
|
63
|
+
* `api` (the default) reads the API's answer: open is `branch.isOpen`, and the opening is `closedUntil`, then
|
|
64
|
+
* `nextChangeAt`. It falls back to the hours when the API sends no opening still ahead. `hours` works both out from
|
|
65
|
+
* the weekly hours, special hours and `closedUntil` at `now`, so it keeps up with the clock after the store was read.
|
|
66
|
+
*/
|
|
67
|
+
declare function nextOpening(branch: Branch, { from, now, storeTimezone, }?: HoursOptions & {
|
|
68
|
+
from?: "api" | "hours" | undefined;
|
|
69
|
+
}): NextOpening | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* The words around an opening. `{time}` is the formatted time and `{weekday}` the day's name.
|
|
72
|
+
* The defaults read "Opens at 7 am", "Opens tomorrow at 7 am" and "Opens Thu at 7 am".
|
|
73
|
+
*/
|
|
74
|
+
type OpeningPhrases = {
|
|
75
|
+
today: string;
|
|
76
|
+
tomorrow: string;
|
|
77
|
+
later: string;
|
|
78
|
+
};
|
|
79
|
+
/** How an opening or a status reads. */
|
|
80
|
+
type LabelOptions = {
|
|
81
|
+
clock?: ClockStyle | undefined;
|
|
82
|
+
/** `short` reads "Thu", `long` reads "Thursday". Defaults to `short`. */
|
|
83
|
+
weekday?: "short" | "long" | undefined;
|
|
84
|
+
phrases?: Partial<OpeningPhrases> | undefined;
|
|
85
|
+
};
|
|
86
|
+
/** Put an opening into words, as `opensLabel` does. */
|
|
87
|
+
declare function describeOpening(next: NextOpening, { clock, weekday, phrases }?: LabelOptions): string;
|
|
88
|
+
/**
|
|
89
|
+
* When a closed branch opens next, in words and in its own time zone: "Opens at 7 am", "Opens tomorrow at 7 am",
|
|
90
|
+
* "Opens Thu at 7 am". Undefined while it is open or when nothing is ahead.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* opensLabel(branch, { clock: { hour12: false } }); // "Opens tomorrow at 7:00"
|
|
95
|
+
* opensLabel(branch, { from: "hours", weekday: "long", phrases: { today: "Opens today {time}" } });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function opensLabel(branch: Branch, options?: HoursOptions & LabelOptions & {
|
|
99
|
+
from?: "api" | "hours" | undefined;
|
|
100
|
+
}): string | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Whether the branch takes no order at all now: closed, and not taking orders for later either.
|
|
103
|
+
*
|
|
104
|
+
* With `lastOrderMinutes`, an open branch that closes within that many minutes takes no order either, unless it takes
|
|
105
|
+
* orders for later. Open is the API's `branch.isOpen`, and the closing comes from its hours at `now`.
|
|
106
|
+
*/
|
|
107
|
+
declare function closedForOrders(branch: Branch, { lastOrderMinutes, now, storeTimezone, }?: HoursOptions & {
|
|
108
|
+
lastOrderMinutes?: number | undefined;
|
|
109
|
+
}): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Whether a bag can be timed for later: the branch takes orders ahead, or the bag holds an item from a pre-order
|
|
112
|
+
* menu, which is scheduled on that menu's own terms even at a branch that takes no orders ahead. Either way the
|
|
113
|
+
* branch must take scheduled orders.
|
|
114
|
+
*
|
|
115
|
+
* @param menuIds - The source menus of the bag's lines.
|
|
116
|
+
*/
|
|
117
|
+
declare function takesOrdersAhead(branch: Branch, menu: Menu | undefined, menuIds: readonly string[]): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* The words of a status. `{time}` is the closing time in `open` and `closingSoon`.
|
|
120
|
+
* The defaults read "Open until 10 pm", "Closes at 10 pm" and "Closed".
|
|
121
|
+
*/
|
|
122
|
+
type StatusPhrases = OpeningPhrases & {
|
|
123
|
+
open: string;
|
|
124
|
+
closingSoon: string;
|
|
125
|
+
closed: string;
|
|
126
|
+
};
|
|
127
|
+
/** A branch's state at an instant, worked out from its hours in its own time zone. */
|
|
128
|
+
type HoursStatus = {
|
|
129
|
+
isOpen: boolean;
|
|
130
|
+
/** Whether the branch takes orders for later, so a closed branch can still take one. */
|
|
131
|
+
canOrderAhead: boolean;
|
|
132
|
+
/** Closed now and not taking orders for later. */
|
|
133
|
+
closedForOrders: boolean;
|
|
134
|
+
/** Open, and closing within `closingSoonMinutes`. */
|
|
135
|
+
closingSoon: boolean;
|
|
136
|
+
label: string;
|
|
137
|
+
/** When it opens next, while it is closed. */
|
|
138
|
+
next?: NextOpening;
|
|
139
|
+
/** The next opening or closing found in the hours, for a timer. */
|
|
140
|
+
nextChange?: Date;
|
|
141
|
+
timezone: string;
|
|
142
|
+
};
|
|
143
|
+
/** Options for `openStatus`. */
|
|
144
|
+
type OpenStatusOptions = HoursOptions & Omit<LabelOptions, "phrases"> & {
|
|
145
|
+
phrases?: Partial<StatusPhrases> | undefined;
|
|
146
|
+
/** How near the closing time counts as closing soon. Defaults to 30 minutes. */
|
|
147
|
+
closingSoonMinutes?: number | undefined;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Whether a branch is open at `now` and its next change in words, from its weekly hours, special hours, overnight
|
|
151
|
+
* hours and `closedUntil`, in the branch's own time zone.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* openStatus(branch, { now, storeTimezone: store.timezone });
|
|
156
|
+
* // { isOpen: true, closingSoon: false, label: "Open until 10 pm", ... }
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
declare function openStatus(branch: Branch, { now, storeTimezone, closingSoonMinutes, phrases, ...label }?: OpenStatusOptions): HoursStatus;
|
|
160
|
+
/** One row of a week's hours: a run of days that share their hours. */
|
|
161
|
+
type HoursRow = {
|
|
162
|
+
days: Weekday[];
|
|
163
|
+
dayLabel: string;
|
|
164
|
+
hoursLabel: string;
|
|
165
|
+
/** Set on a run of closed days, when `closed` names them. */
|
|
166
|
+
closed?: true;
|
|
167
|
+
/** Set on the row that holds `today`. */
|
|
168
|
+
today?: true;
|
|
169
|
+
};
|
|
170
|
+
/** How a week's hours are grouped and read. */
|
|
171
|
+
type OpeningHoursOptions = {
|
|
172
|
+
/** The first day: `sunday`, `monday` or `today`. Defaults to `sunday`. `today` needs `today`. */
|
|
173
|
+
start?: "sunday" | "monday" | "today" | undefined;
|
|
174
|
+
/** Today's weekday, from `todayIn`, to start the week on or to mark its row. */
|
|
175
|
+
today?: Weekday | undefined;
|
|
176
|
+
/** Name the closed days with this label ("Closed") instead of leaving them out. */
|
|
177
|
+
closed?: string | undefined;
|
|
178
|
+
/** One row with this label ("Every day") when all seven days share their hours. */
|
|
179
|
+
everyDay?: string | undefined;
|
|
180
|
+
/** Read hours from midnight to 23:5x with this label ("Open all day"). */
|
|
181
|
+
allDay?: string | undefined;
|
|
182
|
+
clock?: ClockStyle | undefined;
|
|
183
|
+
/** Seven day names from Sunday. Defaults to the full English names. */
|
|
184
|
+
dayNames?: readonly string[] | undefined;
|
|
185
|
+
/** Between an opening and a closing time. Defaults to " – ". */
|
|
186
|
+
range?: string | undefined;
|
|
187
|
+
/** Between the first and last day of a run. Defaults to " – ". */
|
|
188
|
+
through?: string | undefined;
|
|
189
|
+
/** Between two sets of hours on one day. Defaults to ", ". */
|
|
190
|
+
separator?: string | undefined;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* A week's opening hours grouped into runs of consecutive days that share their hours, from Sunday, Monday or
|
|
194
|
+
* today. Closed days are left out unless `closed` names them, and a week with no open day has no rows.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* openingHours(branch.openingTimes, { start: "monday", closed: "Closed", everyDay: "Every day" });
|
|
199
|
+
* // [{ days: [1, 2, 3, 4, 5], dayLabel: "Monday – Friday", hoursLabel: "8 am – 10 pm" },
|
|
200
|
+
* // { days: [6, 0], dayLabel: "Saturday – Sunday", hoursLabel: "Closed", closed: true }]
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
declare function openingHours(times: readonly OpeningTime[], options?: OpeningHoursOptions): HoursRow[];
|
|
204
|
+
/** A branch's hours for a list of branches: its status, today's hours and its week from today. */
|
|
205
|
+
type BranchHours = {
|
|
206
|
+
status: HoursStatus;
|
|
207
|
+
/** Today's hours in the branch's time zone, with today's special hours. Empty on a closed day. */
|
|
208
|
+
today: string;
|
|
209
|
+
week: HoursRow[];
|
|
210
|
+
};
|
|
211
|
+
/** Options for `branchHours`: `hours` reads today's hours and the week, the rest read the status. */
|
|
212
|
+
type BranchHoursOptions = OpenStatusOptions & {
|
|
213
|
+
hours?: Omit<OpeningHoursOptions, "today"> | undefined;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Everything a branch card shows about hours, in the branch's own time zone: whether it is open at `now` and its next
|
|
217
|
+
* change, today's hours (special hours included), and the week starting today with today's row marked.
|
|
218
|
+
*/
|
|
219
|
+
declare function branchHours(branch: Branch, { hours, ...options }?: BranchHoursOptions): BranchHours;
|
|
220
|
+
/**
|
|
221
|
+
* The time now, once the page is in the browser and idle, then each `every` milliseconds and on window focus.
|
|
222
|
+
* Undefined while the server renders and until then, so hours are worked out on the shopper's clock and the server's
|
|
223
|
+
* HTML never disagrees with the first render.
|
|
224
|
+
*
|
|
225
|
+
* @param every - How often to refresh. Defaults to a minute.
|
|
226
|
+
*/
|
|
227
|
+
declare function useNow(every?: number): Date | undefined;
|
|
228
|
+
/**
|
|
229
|
+
* `openStatus` on the shopper's clock (`useNow`): undefined while the server renders and until the page is idle,
|
|
230
|
+
* then refreshed each minute and on window focus.
|
|
231
|
+
*/
|
|
232
|
+
declare function useOpenStatus(branch: Branch | undefined, options?: Omit<OpenStatusOptions, "now">): HoursStatus | undefined;
|
|
233
|
+
/** `branchHours` on the shopper's clock (`useNow`), undefined until the page is idle in the browser. */
|
|
234
|
+
declare function useBranchHours(branch: Branch, options?: Omit<BranchHoursOptions, "now">): BranchHours | undefined;
|
|
235
|
+
|
|
236
|
+
export { DEFAULT_TIMEZONE, branchHours, branchTimezone, closedForOrders, describeOpening, formatClock, nextOpening, openStatus, openingHours, opensLabel, takesOrdersAhead, todayIn, useBranchHours, useNow, useOpenStatus };
|
|
237
|
+
export type { BranchHours, BranchHoursOptions, ClockStyle, HoursOptions, HoursRow, HoursStatus, LabelOptions, NextOpening, OpenStatusOptions, OpeningHoursOptions, OpeningPhrases, StatusPhrases, Weekday };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{useEffect,useState}from"react";import{canOrderAhead,dateOffset,getOpenStatus,localParts,parseWallClock}from"../status/hours.js";var DEFAULT_TIMEZONE="Africa/Lagos";var LONG_DAYS=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];var SHORT_DAYS=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];var MINUTE=6e4;function branchTimezone(branch,storeTimezone){return branch.timezone??storeTimezone??DEFAULT_TIMEZONE}function todayIn(timezone,now=new Date){return new Date(`${localParts(now,timezone).date}T00:00:00Z`).getUTCDay()}function formatClock(time,{hour12=true,padHour=false,minutes="auto",period="lower",space=true,roundLastMinute=false}={}){const clock=parseWallClock(time);if(!clock){return time}let total=clock.hour24*60+clock.minute;if(roundLastMinute&&clock.minute===59){total=(total+1)%1440}const hour24=Math.floor(total/60);const minute=String(total%60).padStart(2,"0");const hour=String(hour12?hour24%12||12:hour24).padStart(padHour?2:1,"0");if(!hour12){return`${hour}:${minute}`}const half=hour24>=12?"PM":"AM";return`${hour}${minutes==="always"||minute!=="00"?`:${minute}`:""}${space?" ":""}${period==="upper"?half:half.toLowerCase()}`}var pad=value=>String(value).padStart(2,"0");function opening(at,timezone,now){const local=localParts(at,timezone);const today=localParts(now,timezone).date;return{at,day:local.date===today?"today":local.date===dateOffset(today,1)?"tomorrow":"later",weekday:new Date(`${local.date}T00:00:00Z`).getUTCDay(),time:`${pad(local.hour)}:${pad(local.minute)}`}}function nextOpening(branch,{from="api",now=new Date,storeTimezone}={}){const timezone=branchTimezone(branch,storeTimezone);if(from==="api"){if(branch.isOpen){return void 0}const value=branch.closedUntil??branch.nextChangeAt;const at=value?new Date(value):void 0;if(at&&at.getTime()>now.getTime()){return opening(at,timezone,now)}}const state=getOpenStatus(branch,timezone,now);return state.isOpen||!state.nextChange?void 0:opening(state.nextChange,timezone,now)}var OPENING={today:"Opens at {time}",tomorrow:"Opens tomorrow at {time}",later:"Opens {weekday} at {time}"};function describeOpening(next,{clock,weekday="short",phrases}={}){return{...OPENING,...phrases}[next.day].replace("{time}",formatClock(next.time,clock)).replace("{weekday}",(weekday==="long"?LONG_DAYS:SHORT_DAYS)[next.weekday])}function opensLabel(branch,options={}){const next=nextOpening(branch,options);return next&&describeOpening(next,options)}function closedForOrders(branch,{lastOrderMinutes,now=new Date,storeTimezone}={}){const ahead=canOrderAhead(branch);if(!branch.isOpen||ahead||!lastOrderMinutes){return!branch.isOpen&&!ahead}const state=getOpenStatus(branch,branchTimezone(branch,storeTimezone),now);return Boolean(state.isOpen&&state.nextChange&&state.nextChange.getTime()-now.getTime()<=lastOrderMinutes*MINUTE)}function takesOrdersAhead(branch,menu,menuIds){const preorder=menuIds.some(id=>menu?.menus?.find(section=>section.id===id)?.schedule?.preorder);return branch.fulfilment.scheduling&&(preorder||canOrderAhead(branch))}var STATUS={...OPENING,open:"Open until {time}",closingSoon:"Closes at {time}",closed:"Closed"};function openStatus(branch,{now=new Date,storeTimezone,closingSoonMinutes=30,phrases,...label}={}){const timezone=branchTimezone(branch,storeTimezone);const state=getOpenStatus(branch,timezone,now);const words={...STATUS,...phrases};const closingSoon=Boolean(state.isOpen&&state.nextChange&&state.nextChange.getTime()-now.getTime()<=closingSoonMinutes*MINUTE);const next=!state.isOpen&&state.nextChange?opening(state.nextChange,timezone,now):void 0;return{isOpen:state.isOpen,canOrderAhead:state.canOrderAhead,closedForOrders:!state.isOpen&&!state.canOrderAhead,closingSoon,label:state.isOpen?(closingSoon?words.closingSoon:words.open).replace("{time}",state.nextChange?formatClock(opening(state.nextChange,timezone,now).time,label.clock):""):next?describeOpening(next,{...label,phrases:words}):words.closed,...next?{next}:{},...state.nextChange?{nextChange:state.nextChange}:{},timezone}}function dayHours(times,day,{allDay,clock,range=" \u2013 ",separator=", "}){return times.filter(time=>!time.disabled&&time.day===day).sort((a,b)=>a.opens.localeCompare(b.opens)).map(time=>allDay!==void 0&&time.opens.startsWith("00:00")&&/^23:5\d/.test(time.closes)?allDay:`${formatClock(time.opens,clock)}${range}${formatClock(time.closes,clock)}`).join(separator)}function openingHours(times,options={}){const{start="sunday",today,closed,everyDay,dayNames=LONG_DAYS,through=" \u2013 "}=options;const first=start==="monday"?1:start==="today"?today??0:0;const week=Array.from({length:7},(_,index)=>(first+index)%7);const hours=week.map(day=>dayHours(times,day,options));if(!hours.some(Boolean)){return[]}const mark=days=>today!==void 0&&days.includes(today)?{today:true}:{};if(everyDay!==void 0&&hours.every(value=>value===hours[0])){return[{days:week,dayLabel:everyDay,hoursLabel:hours[0],...mark(week)}]}const runs=[];week.forEach((day,index)=>{const value=hours[index]||closed;if(value===void 0){return}const run=runs.at(-1);if(run&&run.last===index-1&&run.hours===value){run.days.push(day);run.last=index}else{runs.push({days:[day],hours:value,last:index})}});return runs.map(({days,hours:value})=>({days,dayLabel:days.length===1?dayNames[days[0]]:`${dayNames[days[0]]}${through}${dayNames[days.at(-1)]}`,hoursLabel:value,...value===closed&&!dayHours(times,days[0],options)?{closed:true}:{},...mark(days)}))}function branchHours(branch,{hours={},...options}={}){const status=openStatus(branch,options);const now=options.now??new Date;const day=todayIn(status.timezone,now);const date=localParts(now,status.timezone).date;const special=branch.specialHours?.find(entry=>entry.date===date);const clock=hours.clock??options.clock;const style={...hours,clock};return{status,today:special?special.closed||!special.opens||!special.closes?"":dayHours([{day,opens:special.opens,closes:special.closes}],day,style):dayHours(branch.openingTimes,day,style),week:openingHours(branch.openingTimes,{start:"today",...style,today:day})}}function useNow(every=MINUTE){const[now,setNow]=useState();useEffect(()=>{const tick=()=>setNow(new Date);const idle=typeof requestIdleCallback==="function"?requestIdleCallback(tick,{timeout:1500}):void 0;const soon=idle===void 0?setTimeout(tick,0):void 0;const timer=setInterval(tick,every);window.addEventListener("focus",tick);return()=>{if(idle!==void 0){cancelIdleCallback(idle)}clearTimeout(soon);clearInterval(timer);window.removeEventListener("focus",tick)}},[every]);return now}function useOpenStatus(branch,options={}){const now=useNow();return branch&&now?openStatus(branch,{...options,now}):void 0}function useBranchHours(branch,options={}){const now=useNow();return now?branchHours(branch,{...options,now}):void 0}export{DEFAULT_TIMEZONE,branchHours,branchTimezone,closedForOrders,describeOpening,formatClock,nextOpening,openStatus,openingHours,opensLabel,takesOrdersAhead,todayIn,useBranchHours,useNow,useOpenStatus};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hours/index.ts"],
|
|
4
|
+
"mappings": "AACA,OAAS,UAAW,aAAgB,QACpC,OACE,cACA,WACA,cACA,WACA,mBACK,qBAMA,IAAM,iBAAmB,eAEhC,IAAM,UAAY,CAChB,SACA,SACA,UACA,YACA,WACA,SACA,UACF,EACA,IAAM,WAAa,CAAC,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,KAAK,EACnE,IAAM,OAAS,IAOR,SAAS,eACd,OACA,cACQ,CACR,OAAO,OAAO,UAAY,eAAiB,gBAC7C,CAGO,SAAS,QAAQ,SAAkB,IAAM,IAAI,KAAiB,CACnE,OAAO,IAAI,KACT,GAAG,WAAW,IAAK,QAAQ,EAAE,IAAI,YACnC,EAAE,UAAU,CACd,CA+BO,SAAS,YACd,KACA,CACE,OAAS,KACT,QAAU,MACV,QAAU,OACV,OAAS,QACT,MAAQ,KACR,gBAAkB,KACpB,EAAgB,CAAC,EACT,CACR,MAAM,MAAQ,eAAe,IAAI,EACjC,GAAI,CAAC,MAAO,CACV,OAAO,IACT,CACA,IAAI,MAAQ,MAAM,OAAS,GAAK,MAAM,OACtC,GAAI,iBAAmB,MAAM,SAAW,GAAI,CAC1C,OAAS,MAAQ,GAAK,IACxB,CACA,MAAM,OAAS,KAAK,MAAM,MAAQ,EAAE,EACpC,MAAM,OAAS,OAAO,MAAQ,EAAE,EAAE,SAAS,EAAG,GAAG,EACjD,MAAM,KAAO,OAAO,OAAS,OAAS,IAAM,GAAK,MAAM,EAAE,SACvD,QAAU,EAAI,EACd,GACF,EACA,GAAI,CAAC,OAAQ,CACX,MAAO,GAAG,IAAI,IAAI,MAAM,EAC1B,CACA,MAAM,KAAO,QAAU,GAAK,KAAO,KACnC,MAAO,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E,CAGO,SAAS,eACd,OACA,QAA2C,CAAC,EACnB,CACzB,MAAM,IAAM,OAAO,EACnB,OAAO,IAAM,YAAY,OAAQ,CAAE,GAAG,QAAS,GAAI,CAAC,EAAI,MAC1D",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var PACKAGE_VERSION=true?"0.
|
|
1
|
+
var PACKAGE_VERSION=true?"0.20.0":"0.0.0-development";export{PACKAGE_VERSION};
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zappie/foundations",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "UI-less React primitives for Zappie storefronts: behaviour, state and accessibility without styles.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"types": "./dist/branch-list/index.d.ts",
|
|
26
26
|
"import": "./dist/branch-list/index.js"
|
|
27
27
|
},
|
|
28
|
+
"./hours": {
|
|
29
|
+
"types": "./dist/hours/index.d.ts",
|
|
30
|
+
"import": "./dist/hours/index.js"
|
|
31
|
+
},
|
|
28
32
|
"./schedule": {
|
|
29
33
|
"types": "./dist/schedule/index.d.ts",
|
|
30
34
|
"import": "./dist/schedule/index.js"
|