@justanarthur/modo-ui-notch 0.2.0 → 0.3.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 +11 -11
- package/blocks/activities/agenda/examples.mdx +21 -5
- package/blocks/activities/agenda/index.tsx +291 -182
- package/blocks/activities/clock/examples.mdx +73 -0
- package/blocks/activities/clock/index.tsx +619 -0
- package/blocks/activities/now-playing/examples.mdx +32 -13
- package/blocks/activities/now-playing/index.tsx +319 -126
- package/blocks/activities/weather/examples.mdx +115 -0
- package/blocks/activities/weather/index.tsx +634 -0
- package/blocks/app/expanded-island/examples.mdx +8 -7
- package/blocks/app/expanded-island/index.tsx +19 -3
- package/blocks/app/settings-window/examples.mdx +6 -2
- package/blocks/app/settings-window/index.tsx +25 -7
- package/blocks/system/clipboard/examples.mdx +83 -0
- package/blocks/system/clipboard/index.tsx +563 -0
- package/blocks/system/recent-files/examples.mdx +66 -0
- package/blocks/system/recent-files/index.tsx +542 -0
- package/blocks/system/system-stats/examples.mdx +38 -9
- package/blocks/system/system-stats/index.tsx +241 -107
- package/components/indicators/media-scrubber/examples.mdx +8 -0
- package/components/indicators/media-scrubber/index.tsx +37 -13
- package/components/island/island/examples.mdx +4 -4
- package/components/island/island/index.tsx +2 -2
- package/components/island/island-flash/examples.mdx +15 -18
- package/components/island/island-flash/index.tsx +4 -4
- package/components/island/island-tabs/examples.mdx +5 -5
- package/components/island/widget/examples.mdx +76 -0
- package/components/island/widget/index.tsx +431 -0
- package/dist/blocks/activities/agenda/index.d.ts +1 -1
- package/dist/blocks/activities/agenda/index.js +3 -3
- package/dist/blocks/activities/clock/index.d.ts +2 -0
- package/dist/blocks/activities/clock/index.js +25 -0
- package/dist/blocks/activities/now-playing/index.d.ts +3 -2
- package/dist/blocks/activities/now-playing/index.js +5 -3
- package/dist/blocks/activities/weather/index.d.ts +2 -0
- package/dist/blocks/activities/weather/index.js +21 -0
- package/dist/blocks/app/expanded-island/index.d.ts +1 -1
- package/dist/blocks/app/expanded-island/index.js +4 -3
- package/dist/blocks/app/settings-window/index.d.ts +1 -1
- package/dist/blocks/app/settings-window/index.js +1 -1
- package/dist/blocks/system/clipboard/index.d.ts +2 -0
- package/dist/blocks/system/clipboard/index.js +17 -0
- package/dist/blocks/system/recent-files/index.d.ts +2 -0
- package/dist/blocks/system/recent-files/index.js +19 -0
- package/dist/blocks/system/system-stats/index.d.ts +2 -2
- package/dist/blocks/system/system-stats/index.js +4 -1
- package/dist/components/indicators/media-scrubber/index.d.ts +1 -1
- package/dist/components/indicators/media-scrubber/index.js +1 -1
- package/dist/components/island/island/index.d.ts +1 -1
- package/dist/components/island/island/index.js +2 -2
- package/dist/components/island/island-flash/index.d.ts +1 -1
- package/dist/components/island/island-flash/index.js +1 -1
- package/dist/components/island/island-tabs/index.js +2 -2
- package/dist/components/island/widget/index.d.ts +2 -0
- package/dist/components/island/widget/index.js +30 -0
- package/dist/index.d.ts +15 -15
- package/dist/index.js +102 -58
- package/dist/lib/artwork.d.ts +2 -0
- package/dist/lib/artwork.js +8 -0
- package/dist/lib/geometry.js +1 -1
- package/dist/shared/chunk-2818esrq.d.ts +140 -0
- package/dist/shared/{chunk-yngkkxz2.d.ts → chunk-2m5dz8nf.d.ts} +4 -4
- package/dist/shared/chunk-3thgcfcq.d.ts +151 -0
- package/dist/shared/chunk-416tsgbc.d.ts +175 -0
- package/dist/shared/{chunk-zvh0d3qb.js → chunk-61t2dfte.js} +1 -1
- package/dist/shared/chunk-7fvnrtz4.d.ts +105 -0
- package/dist/shared/{chunk-hpsabfsd.d.ts → chunk-81fpptwp.d.ts} +1 -1
- package/dist/shared/chunk-9c7ewvkk.js +381 -0
- package/dist/shared/{chunk-mp1nx0ez.js → chunk-9m0xbg5v.js} +14 -3
- package/dist/shared/{chunk-wwjxs75m.js → chunk-9xtcv3sy.js} +26 -12
- package/dist/shared/chunk-bvtyjcce.d.ts +123 -0
- package/dist/shared/{chunk-s33y1pcq.js → chunk-cc9nwbtf.js} +20 -10
- package/dist/shared/chunk-da177e8r.d.ts +114 -0
- package/dist/shared/{chunk-6c1jbefs.d.ts → chunk-fexwzycv.d.ts} +3 -3
- package/dist/shared/chunk-gejenfap.d.ts +25 -0
- package/dist/shared/{chunk-rr5e0g15.d.ts → chunk-j755a1a5.d.ts} +7 -1
- package/dist/shared/chunk-j77cx8dp.js +498 -0
- package/dist/shared/chunk-mt0k6v8x.js +474 -0
- package/dist/shared/{chunk-78w7qwp0.js → chunk-ndf79pv5.js} +1 -1
- package/dist/shared/{chunk-9nhpg9t8.d.ts → chunk-pfhy5e4q.d.ts} +7 -1
- package/dist/shared/chunk-qdzemt2v.js +477 -0
- package/dist/shared/{chunk-rpfwv3pz.js → chunk-r471sknr.js} +1 -1
- package/dist/shared/chunk-s2jeqqan.js +243 -0
- package/dist/shared/chunk-t0hcxh89.d.ts +124 -0
- package/dist/shared/chunk-w5h6ay93.js +72 -0
- package/dist/shared/chunk-wkg17nxs.d.ts +101 -0
- package/dist/shared/chunk-x7f3g82r.js +285 -0
- package/dist/shared/chunk-xq5hc5gv.js +333 -0
- package/dist/shared/{chunk-sbnrhy5m.js → chunk-y0188bbn.js} +2 -2
- package/dist/shared/chunk-ym0xbses.js +518 -0
- package/dist/styles.css +97 -55
- package/index.ts +7 -6
- package/lib/artwork.ts +112 -0
- package/lib/geometry.ts +1 -1
- package/mock/activities.tsx +9 -32
- package/mock/artwork.tsx +13 -1
- package/mock/clipboard.tsx +79 -0
- package/mock/clock.tsx +77 -0
- package/mock/data.ts +30 -44
- package/mock/files.tsx +70 -0
- package/mock/island-content.tsx +43 -118
- package/mock/screen.tsx +76 -0
- package/mock/settings.tsx +370 -91
- package/mock/system.ts +46 -0
- package/mock/weather.tsx +105 -0
- package/package.json +7 -2
- package/blocks/activities/shelf/examples.mdx +0 -33
- package/blocks/activities/shelf/index.tsx +0 -289
- package/blocks/activities/timer/examples.mdx +0 -52
- package/blocks/activities/timer/index.tsx +0 -292
- package/blocks/system/controls/examples.mdx +0 -34
- package/blocks/system/controls/index.tsx +0 -167
- package/blocks/system/devices/examples.mdx +0 -25
- package/blocks/system/devices/index.tsx +0 -172
- package/blocks/system/power-alert/examples.mdx +0 -32
- package/blocks/system/power-alert/index.tsx +0 -89
- package/components/indicators/battery-glyph/examples.mdx +0 -22
- package/components/indicators/battery-glyph/index.tsx +0 -91
- package/dist/blocks/activities/shelf/index.d.ts +0 -2
- package/dist/blocks/activities/shelf/index.js +0 -14
- package/dist/blocks/activities/timer/index.d.ts +0 -2
- package/dist/blocks/activities/timer/index.js +0 -19
- package/dist/blocks/system/controls/index.d.ts +0 -2
- package/dist/blocks/system/controls/index.js +0 -10
- package/dist/blocks/system/devices/index.d.ts +0 -2
- package/dist/blocks/system/devices/index.js +0 -12
- package/dist/blocks/system/power-alert/index.d.ts +0 -2
- package/dist/blocks/system/power-alert/index.js +0 -9
- package/dist/components/indicators/battery-glyph/index.d.ts +0 -2
- package/dist/components/indicators/battery-glyph/index.js +0 -9
- package/dist/shared/chunk-2qpcsv5h.d.ts +0 -45
- package/dist/shared/chunk-45gytfr1.d.ts +0 -84
- package/dist/shared/chunk-50ffk4r7.d.ts +0 -69
- package/dist/shared/chunk-5w0yck07.d.ts +0 -32
- package/dist/shared/chunk-7hr53mby.js +0 -116
- package/dist/shared/chunk-b8jb1w72.js +0 -145
- package/dist/shared/chunk-cpewy6yg.js +0 -178
- package/dist/shared/chunk-e3e4s4ds.d.ts +0 -77
- package/dist/shared/chunk-enm6td0p.js +0 -280
- package/dist/shared/chunk-fvedaas2.d.ts +0 -33
- package/dist/shared/chunk-gex38j8x.d.ts +0 -82
- package/dist/shared/chunk-hwtxpzg0.js +0 -69
- package/dist/shared/chunk-phb53x1f.js +0 -252
- package/dist/shared/chunk-r8esmjfs.d.ts +0 -42
- package/dist/shared/chunk-rcp1g9ca.js +0 -70
- package/dist/shared/chunk-rx9sphz2.js +0 -266
- package/dist/shared/chunk-xdeek16r.d.ts +0 -58
- package/dist/shared/chunk-xkzjk1tq.js +0 -206
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Local addition: a notch app's clock, the time, the date and a few world
|
|
3
|
+
* clocks, with a Pomodoro cycle that takes the widget over while it runs.
|
|
4
|
+
* The run is a value the host may own and persist.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use client'
|
|
8
|
+
|
|
9
|
+
import Button from '@justanarthur/modo-ui/components/actions/button'
|
|
10
|
+
import Meter from '@justanarthur/modo-ui/components/feedback/meter'
|
|
11
|
+
import { useControllableState } from '@justanarthur/modo-ui/lib/use-controllable-state'
|
|
12
|
+
import { cn } from '@justanarthur/modo-ui/lib/utils'
|
|
13
|
+
import {
|
|
14
|
+
CalendarDays,
|
|
15
|
+
Clock3,
|
|
16
|
+
Coffee,
|
|
17
|
+
Moon,
|
|
18
|
+
Pause,
|
|
19
|
+
Play,
|
|
20
|
+
Plus,
|
|
21
|
+
Repeat,
|
|
22
|
+
RotateCcw,
|
|
23
|
+
SkipForward,
|
|
24
|
+
Sun,
|
|
25
|
+
Target,
|
|
26
|
+
Timer,
|
|
27
|
+
} from 'lucide-react'
|
|
28
|
+
import { useEffect } from 'react'
|
|
29
|
+
import IslandFlash from '../../../components/island/island-flash'
|
|
30
|
+
import Widget from '../../../components/island/widget'
|
|
31
|
+
import { formatClock, MINUTE, SECOND, useEdgeClock } from '../../../lib/clock'
|
|
32
|
+
|
|
33
|
+
type PomodoroPhase = 'focus' | 'short-break' | 'long-break'
|
|
34
|
+
|
|
35
|
+
/** A Pomodoro cycle's lengths, in minutes, and how many focus rounds come before the long break. */
|
|
36
|
+
interface PomodoroSettings {
|
|
37
|
+
focus: number
|
|
38
|
+
shortBreak: number
|
|
39
|
+
longBreak: number
|
|
40
|
+
rounds: number
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A Pomodoro phase: running while it has `endsAt`, paused while it has `remaining`. */
|
|
44
|
+
interface PomodoroRun {
|
|
45
|
+
phase: PomodoroPhase
|
|
46
|
+
/** The focus round it belongs to, from 1: a break follows its round. */
|
|
47
|
+
round: number
|
|
48
|
+
/** The phase's whole length, in ms. */
|
|
49
|
+
total: number
|
|
50
|
+
/** When it ends, in epoch ms, while it runs. */
|
|
51
|
+
endsAt?: number
|
|
52
|
+
/** The time left, in ms, while it is paused. */
|
|
53
|
+
remaining?: number
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A world clock: a city and its IANA time zone. */
|
|
57
|
+
interface ClockZone {
|
|
58
|
+
city: string
|
|
59
|
+
/** An IANA time zone: `America/Chicago`. */
|
|
60
|
+
timeZone: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ClockProps {
|
|
64
|
+
/** The world clocks beside the time, two or three. */
|
|
65
|
+
zones?: ClockZone[]
|
|
66
|
+
/** Shows the seconds, re-rendering on each second's edge instead of each minute's. Defaults to `false`. */
|
|
67
|
+
seconds?: boolean
|
|
68
|
+
/** A 12-hour clock (`true`) or a 24-hour one (`false`). Defaults to the locale's. */
|
|
69
|
+
hour12?: boolean
|
|
70
|
+
/** The time zone of the main clock. Defaults to the system's. */
|
|
71
|
+
timeZone?: string
|
|
72
|
+
/** Formats the time and the date. Defaults to the browser's locale. */
|
|
73
|
+
locale?: Intl.LocalesArgument
|
|
74
|
+
/** The moment the clock shows, in epoch ms. Defaults to now, re-read on each minute (or second). */
|
|
75
|
+
now?: number
|
|
76
|
+
/** The Pomodoro cycle's lengths, merged over 25 / 5 / 15 minutes and 4 rounds; `false` leaves the Pomodoro out. */
|
|
77
|
+
pomodoro?: Partial<PomodoroSettings> | false
|
|
78
|
+
/** The Pomodoro run (controlled); `null` shows the clock. */
|
|
79
|
+
value?: PomodoroRun | null
|
|
80
|
+
/** The run at first (uncontrolled). Defaults to `null`. */
|
|
81
|
+
defaultValue?: PomodoroRun | null
|
|
82
|
+
/** Called with the run a start, pause, resume, skip, +1 min or reset leaves (`null` after a reset). */
|
|
83
|
+
onValueChange?: (run: PomodoroRun | null) => void
|
|
84
|
+
/** Called when a phase reaches zero, with the next one, which waits paused to be started: show `Clock.PhaseDone`. */
|
|
85
|
+
onPhaseEnd?: (ended: PomodoroRun, next: PomodoroRun) => void
|
|
86
|
+
/** Classes for the root, which fills its container. */
|
|
87
|
+
className?: string
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const DEFAULTS: PomodoroSettings = { focus: 25, shortBreak: 5, longBreak: 15, rounds: 4 }
|
|
91
|
+
|
|
92
|
+
// Literal tables: UnoCSS only generates the classes it can read whole. Focus is warm, a break green.
|
|
93
|
+
const RING: Record<PomodoroPhase, string> = {
|
|
94
|
+
focus: '[&_circle:last-child]:text-status-warning',
|
|
95
|
+
'short-break': '[&_circle:last-child]:text-status-success',
|
|
96
|
+
'long-break': '[&_circle:last-child]:text-status-success',
|
|
97
|
+
}
|
|
98
|
+
const DOT: Record<PomodoroPhase, string> = {
|
|
99
|
+
focus: 'bg-status-warning',
|
|
100
|
+
'short-break': 'bg-status-success',
|
|
101
|
+
'long-break': 'bg-status-success',
|
|
102
|
+
}
|
|
103
|
+
const TILE: Record<PomodoroPhase, string> = {
|
|
104
|
+
focus: 'bg-status-warning/16 text-status-warning',
|
|
105
|
+
'short-break': 'bg-status-success/16 text-status-success',
|
|
106
|
+
'long-break': 'bg-status-success/16 text-status-success',
|
|
107
|
+
}
|
|
108
|
+
const MARK: Record<PomodoroPhase, string> = {
|
|
109
|
+
focus: 'var(--status-warning)',
|
|
110
|
+
'short-break': 'var(--status-success)',
|
|
111
|
+
'long-break': 'var(--status-success)',
|
|
112
|
+
}
|
|
113
|
+
const GLYPH = { focus: Target, 'short-break': Coffee, 'long-break': Coffee }
|
|
114
|
+
|
|
115
|
+
const settingsOf = (pomodoro?: Partial<PomodoroSettings> | false): PomodoroSettings => ({
|
|
116
|
+
...DEFAULTS,
|
|
117
|
+
...(pomodoro || {}),
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
const lengthOf = (phase: PomodoroPhase, settings: PomodoroSettings) =>
|
|
121
|
+
(phase === 'focus' ? settings.focus : phase === 'short-break' ? settings.shortBreak : settings.longBreak) * MINUTE
|
|
122
|
+
|
|
123
|
+
/** The phase after `run`'s and its round: a break after focus (the long one after the last round), then the next round. */
|
|
124
|
+
function following(run: Pick<PomodoroRun, 'phase' | 'round'>, settings: PomodoroSettings) {
|
|
125
|
+
if (run.phase === 'focus')
|
|
126
|
+
return { phase: run.round >= settings.rounds ? 'long-break' : 'short-break', round: run.round } as const
|
|
127
|
+
return { phase: 'focus', round: run.phase === 'long-break' ? 1 : run.round + 1 } as const
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The first focus round, running from now. */
|
|
131
|
+
function startPomodoro(pomodoro?: Partial<PomodoroSettings>): PomodoroRun {
|
|
132
|
+
const total = lengthOf('focus', settingsOf(pomodoro))
|
|
133
|
+
return { phase: 'focus', round: 1, total, endsAt: Date.now() + total }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The phase after `run`, paused at its whole length until it is started: what a phase reaching zero leaves. */
|
|
137
|
+
function advancePomodoro(run: PomodoroRun, pomodoro?: Partial<PomodoroSettings>): PomodoroRun {
|
|
138
|
+
const settings = settingsOf(pomodoro)
|
|
139
|
+
const next = following(run, settings)
|
|
140
|
+
const total = lengthOf(next.phase, settings)
|
|
141
|
+
return { ...next, total, remaining: total }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** `run` running from now, from the time it had left. */
|
|
145
|
+
function resumePomodoro(run: PomodoroRun): PomodoroRun {
|
|
146
|
+
return { phase: run.phase, round: run.round, total: run.total, endsAt: Date.now() + (run.remaining ?? run.total) }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const pause = (run: PomodoroRun, left: number): PomodoroRun => ({
|
|
150
|
+
phase: run.phase,
|
|
151
|
+
round: run.round,
|
|
152
|
+
total: run.total,
|
|
153
|
+
remaining: left,
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
const addMinute = (run: PomodoroRun): PomodoroRun =>
|
|
157
|
+
run.endsAt === undefined
|
|
158
|
+
? { ...run, total: run.total + MINUTE, remaining: (run.remaining ?? run.total) + MINUTE }
|
|
159
|
+
: { ...run, total: run.total + MINUTE, endsAt: run.endsAt + MINUTE }
|
|
160
|
+
|
|
161
|
+
/** The time `run` has left, re-read on each second's edge while it runs, never in between. */
|
|
162
|
+
function useLeft(run: PomodoroRun) {
|
|
163
|
+
const end = run.endsAt ?? 0
|
|
164
|
+
const now = useEdgeClock(end, run.endsAt !== undefined && Date.now() < end + SECOND)
|
|
165
|
+
return run.endsAt === undefined ? (run.remaining ?? run.total) : Math.max(0, end - now)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function phaseName(run: Pick<PomodoroRun, 'phase' | 'round'>, rounds: number) {
|
|
169
|
+
if (run.phase === 'focus') return `Focus ${run.round} of ${rounds}`
|
|
170
|
+
return run.phase === 'long-break' ? 'Long break' : 'Short break'
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A time split into its figure and its day period: `4:05` and `PM`, or `16:05` alone. */
|
|
174
|
+
function timeParts(
|
|
175
|
+
ms: number,
|
|
176
|
+
{ locale, hour12, timeZone, seconds }: Pick<ClockProps, 'locale' | 'hour12' | 'timeZone' | 'seconds'>,
|
|
177
|
+
) {
|
|
178
|
+
const parts = new Intl.DateTimeFormat(locale, {
|
|
179
|
+
hour: 'numeric',
|
|
180
|
+
minute: '2-digit',
|
|
181
|
+
second: seconds ? '2-digit' : undefined,
|
|
182
|
+
hour12,
|
|
183
|
+
timeZone,
|
|
184
|
+
}).formatToParts(ms)
|
|
185
|
+
const period = parts.find(part => part.type === 'dayPeriod')?.value
|
|
186
|
+
const text = parts
|
|
187
|
+
.filter(part => part.type !== 'dayPeriod')
|
|
188
|
+
.map(part => part.value)
|
|
189
|
+
.join('')
|
|
190
|
+
.trim()
|
|
191
|
+
return { text, period }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const shortTime = (ms: number, options: Pick<ClockProps, 'locale' | 'hour12' | 'timeZone'>) =>
|
|
195
|
+
new Date(ms).toLocaleTimeString(options.locale, {
|
|
196
|
+
hour: 'numeric',
|
|
197
|
+
minute: '2-digit',
|
|
198
|
+
hour12: options.hour12,
|
|
199
|
+
timeZone: options.timeZone,
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
/** A zone's offset from UTC at `ms`, in minutes; the system's without a zone. */
|
|
203
|
+
function offsetOf(ms: number, timeZone?: string) {
|
|
204
|
+
if (!timeZone) return -new Date(ms).getTimezoneOffset()
|
|
205
|
+
const name = new Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'longOffset' })
|
|
206
|
+
.formatToParts(ms)
|
|
207
|
+
.find(part => part.type === 'timeZoneName')?.value
|
|
208
|
+
const match = name?.match(/([+-])(\d{2}):(\d{2})/)
|
|
209
|
+
if (!match) return 0
|
|
210
|
+
const minutes = Number(match[2]) * 60 + Number(match[3])
|
|
211
|
+
return match[1] === '-' ? -minutes : minutes
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** An offset difference as `+9h`, `−6h`, `+5:30`. */
|
|
215
|
+
function offsetLabel(minutes: number) {
|
|
216
|
+
if (minutes === 0) return 'Same time'
|
|
217
|
+
const sign = minutes > 0 ? '+' : '−'
|
|
218
|
+
const h = Math.floor(Math.abs(minutes) / 60)
|
|
219
|
+
const m = Math.abs(minutes) % 60
|
|
220
|
+
return m ? `${sign}${h}:${String(m).padStart(2, '0')}` : `${sign}${h}h`
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const dayKey = (ms: number, timeZone?: string) =>
|
|
224
|
+
new Intl.DateTimeFormat('en-CA', { timeZone, year: 'numeric', month: '2-digit', day: '2-digit' }).format(ms)
|
|
225
|
+
|
|
226
|
+
const hourIn = (ms: number, timeZone: string) =>
|
|
227
|
+
Number(new Intl.DateTimeFormat('en-US', { timeZone, hour: 'numeric', hourCycle: 'h23' }).format(ms))
|
|
228
|
+
|
|
229
|
+
interface ZoneRowProps extends Pick<ClockProps, 'locale' | 'hour12' | 'timeZone'> {
|
|
230
|
+
zone: ClockZone
|
|
231
|
+
now: number
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function ZoneRow({ zone, now, locale, hour12, timeZone }: ZoneRowProps) {
|
|
235
|
+
const there = dayKey(now, zone.timeZone)
|
|
236
|
+
const here = dayKey(now, timeZone)
|
|
237
|
+
const day = there > here ? 'Tomorrow' : there < here ? 'Yesterday' : undefined
|
|
238
|
+
const hour = hourIn(now, zone.timeZone)
|
|
239
|
+
const DayGlyph = hour >= 6 && hour < 18 ? Sun : Moon
|
|
240
|
+
return (
|
|
241
|
+
<Widget.Row
|
|
242
|
+
leading={shortTime(now, { locale, hour12, timeZone: zone.timeZone })}
|
|
243
|
+
trailing={
|
|
244
|
+
<>
|
|
245
|
+
{day && <span>{day} ·</span>}
|
|
246
|
+
<span className="tabular-nums">{offsetLabel(offsetOf(now, zone.timeZone) - offsetOf(now, timeZone))}</span>
|
|
247
|
+
<DayGlyph size={12} strokeWidth={1.5} aria-label={DayGlyph === Sun ? 'Day' : 'Night'} className="ml-1" />
|
|
248
|
+
</>
|
|
249
|
+
}
|
|
250
|
+
>
|
|
251
|
+
{zone.city}
|
|
252
|
+
</Widget.Row>
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
interface RingProps {
|
|
257
|
+
run: PomodoroRun
|
|
258
|
+
rounds?: number
|
|
259
|
+
className: string
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The time left as a Meter ring in the phase's hue. */
|
|
263
|
+
function PhaseRing({ run, rounds = DEFAULTS.rounds, className }: RingProps) {
|
|
264
|
+
const left = useLeft(run)
|
|
265
|
+
return (
|
|
266
|
+
<Meter
|
|
267
|
+
variant="ring"
|
|
268
|
+
label={`${phaseName(run, rounds)}, ${formatClock(Math.ceil(left / SECOND))} left`}
|
|
269
|
+
value={run.total ? left / run.total : 0}
|
|
270
|
+
tone="default"
|
|
271
|
+
animate={false}
|
|
272
|
+
className={cn(RING[run.phase], className)}
|
|
273
|
+
/>
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
interface PartProps {
|
|
278
|
+
/** The Pomodoro run; without one the part shows nothing. */
|
|
279
|
+
run?: PomodoroRun | null
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The compact island's left wing: the ring of the phase's time left. */
|
|
283
|
+
function ClockLeading({ run }: PartProps) {
|
|
284
|
+
if (!run) return null
|
|
285
|
+
return <PhaseRing run={run} className="size-4" />
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function TimeLeft({ run }: { run: PomodoroRun }) {
|
|
289
|
+
const left = useLeft(run)
|
|
290
|
+
return (
|
|
291
|
+
<span
|
|
292
|
+
className={cn(
|
|
293
|
+
'text-caption weight-semibold tabular-nums',
|
|
294
|
+
run.endsAt === undefined ? 'text-muted-foreground' : 'text-foreground',
|
|
295
|
+
)}
|
|
296
|
+
>
|
|
297
|
+
{formatClock(Math.ceil(left / SECOND))}
|
|
298
|
+
</span>
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** The compact island's right wing: the phase's time left as `mm:ss`, muted while paused. */
|
|
303
|
+
function ClockTrailing({ run }: PartProps) {
|
|
304
|
+
if (!run) return null
|
|
305
|
+
return <TimeLeft run={run} />
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The split island's bubble: the ring of the phase's time left. */
|
|
309
|
+
function ClockMinimal({ run }: PartProps) {
|
|
310
|
+
if (!run) return null
|
|
311
|
+
return <PhaseRing run={run} className="size-5.5" />
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
interface ClockPhaseDoneProps {
|
|
315
|
+
/** The phase that reached zero. */
|
|
316
|
+
ended: Pick<PomodoroRun, 'phase' | 'round'>
|
|
317
|
+
/** The cycle's lengths, as given to the Clock. */
|
|
318
|
+
pomodoro?: Partial<PomodoroSettings>
|
|
319
|
+
/** Called by the button that starts the next phase; without it the button hides. */
|
|
320
|
+
onStart?: () => void
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** The island's flash when a phase reaches zero: "Focus done — break time" or "Break over", and a button to start the next. */
|
|
324
|
+
function ClockPhaseDone({ ended, pomodoro, onStart }: ClockPhaseDoneProps) {
|
|
325
|
+
const settings = settingsOf(pomodoro)
|
|
326
|
+
const next = following(ended, settings)
|
|
327
|
+
const minutes = lengthOf(next.phase, settings) / MINUTE
|
|
328
|
+
const Glyph = GLYPH[next.phase]
|
|
329
|
+
const breakNext = next.phase !== 'focus'
|
|
330
|
+
const title = !breakNext
|
|
331
|
+
? 'Break over'
|
|
332
|
+
: next.phase === 'long-break'
|
|
333
|
+
? 'Focus done — long break'
|
|
334
|
+
: 'Focus done — break time'
|
|
335
|
+
const caption = !breakNext
|
|
336
|
+
? `${phaseName(next, settings.rounds)} · ${minutes} min`
|
|
337
|
+
: next.phase === 'long-break'
|
|
338
|
+
? `${minutes} min break · all ${settings.rounds} rounds done`
|
|
339
|
+
: `${minutes} min break · ${ended.round} of ${settings.rounds} rounds done`
|
|
340
|
+
return (
|
|
341
|
+
<IslandFlash
|
|
342
|
+
icon={
|
|
343
|
+
<span className={cn('flex size-9 items-center justify-center rounded-lg', TILE[next.phase])}>
|
|
344
|
+
<Glyph size={16} strokeWidth={1.5} />
|
|
345
|
+
</span>
|
|
346
|
+
}
|
|
347
|
+
title={title}
|
|
348
|
+
caption={caption}
|
|
349
|
+
action={
|
|
350
|
+
onStart && (
|
|
351
|
+
<Button variant="secondary" size="compact" onClick={onStart}>
|
|
352
|
+
{breakNext ? 'Start break' : 'Start focus'}
|
|
353
|
+
</Button>
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
/>
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
interface RunningProps extends Pick<ClockProps, 'locale' | 'hour12' | 'timeZone' | 'onPhaseEnd' | 'className'> {
|
|
361
|
+
run: PomodoroRun
|
|
362
|
+
settings: PomodoroSettings
|
|
363
|
+
onChange: (run: PomodoroRun | null) => void
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** The coming phases from `run`'s end, each with when it starts. */
|
|
367
|
+
function schedule(run: PomodoroRun, start: number, settings: PomodoroSettings, count: number) {
|
|
368
|
+
const phases: { phase: PomodoroPhase; round: number; start: number; length: number }[] = []
|
|
369
|
+
let at: Pick<PomodoroRun, 'phase' | 'round'> = run
|
|
370
|
+
let when = start
|
|
371
|
+
for (let i = 0; i < count; i++) {
|
|
372
|
+
at = following(at, settings)
|
|
373
|
+
const length = lengthOf(at.phase, settings)
|
|
374
|
+
phases.push({ ...at, start: when, length })
|
|
375
|
+
when += length
|
|
376
|
+
}
|
|
377
|
+
return phases
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function Running({ run, settings, onChange, onPhaseEnd, locale, hour12, timeZone, className }: RunningProps) {
|
|
381
|
+
const left = useLeft(run)
|
|
382
|
+
const running = run.endsAt !== undefined
|
|
383
|
+
const now = running ? (run.endsAt ?? 0) - left : Date.now()
|
|
384
|
+
|
|
385
|
+
useEffect(() => {
|
|
386
|
+
if (!running || left > 0) return
|
|
387
|
+
const next = advancePomodoro(run, settings)
|
|
388
|
+
onPhaseEnd?.(run, next)
|
|
389
|
+
onChange(next)
|
|
390
|
+
}, [running, left, run, settings, onChange, onPhaseEnd])
|
|
391
|
+
|
|
392
|
+
const ends = now + left
|
|
393
|
+
const ready = !running && left === run.total
|
|
394
|
+
const next = following(run, settings)
|
|
395
|
+
const options = { locale, hour12, timeZone }
|
|
396
|
+
const status = running
|
|
397
|
+
? `Until ${shortTime(ends, options)}`
|
|
398
|
+
: ready
|
|
399
|
+
? `Ready · ${Math.round(run.total / MINUTE)} min`
|
|
400
|
+
: 'Paused'
|
|
401
|
+
const nextLabel =
|
|
402
|
+
next.phase === 'focus'
|
|
403
|
+
? `${phaseName(next, settings.rounds)} next`
|
|
404
|
+
: `${lengthOf(next.phase, settings) / MINUTE} min ${next.phase === 'long-break' ? 'long break' : 'break'} next`
|
|
405
|
+
|
|
406
|
+
return (
|
|
407
|
+
<Widget
|
|
408
|
+
className={className}
|
|
409
|
+
media={<PhaseRing run={run} rounds={settings.rounds} className="size-24" />}
|
|
410
|
+
aside={
|
|
411
|
+
<Widget.Section
|
|
412
|
+
label="Up next"
|
|
413
|
+
trailing={<span className="tabular-nums">{shortTime(Date.now(), options)}</span>}
|
|
414
|
+
>
|
|
415
|
+
{schedule(run, ends, settings, 3).map(phase => (
|
|
416
|
+
<Widget.Row
|
|
417
|
+
key={`${phase.phase}-${phase.round}-${phase.start}`}
|
|
418
|
+
leading={shortTime(phase.start, options)}
|
|
419
|
+
color={MARK[phase.phase]}
|
|
420
|
+
trailing={`${phase.length / MINUTE} min`}
|
|
421
|
+
>
|
|
422
|
+
{phaseName(phase, settings.rounds)}
|
|
423
|
+
</Widget.Row>
|
|
424
|
+
))}
|
|
425
|
+
</Widget.Section>
|
|
426
|
+
}
|
|
427
|
+
>
|
|
428
|
+
<Widget.Header
|
|
429
|
+
icon={Timer}
|
|
430
|
+
trailing={<Widget.Action icon={RotateCcw} label="Reset" onClick={() => onChange(null)} />}
|
|
431
|
+
>
|
|
432
|
+
Pomodoro
|
|
433
|
+
</Widget.Header>
|
|
434
|
+
<div className="flex min-w-0 items-center justify-between gap-3">
|
|
435
|
+
<Widget.Figure value={formatClock(Math.ceil(left / SECOND))} />
|
|
436
|
+
<div className="-mr-1 flex shrink-0 items-center gap-1">
|
|
437
|
+
<Widget.Action icon={Plus} label="Add a minute" onClick={() => onChange(addMinute(run))} />
|
|
438
|
+
<Widget.Action
|
|
439
|
+
icon={running ? Pause : Play}
|
|
440
|
+
label={running ? 'Pause' : ready ? 'Start' : 'Resume'}
|
|
441
|
+
emphasis="primary"
|
|
442
|
+
filled
|
|
443
|
+
onClick={() => onChange(running ? pause(run, left) : resumePomodoro(run))}
|
|
444
|
+
/>
|
|
445
|
+
<Widget.Action
|
|
446
|
+
icon={SkipForward}
|
|
447
|
+
label={next.phase === 'focus' ? 'Skip to focus' : 'Skip to break'}
|
|
448
|
+
filled
|
|
449
|
+
onClick={() => onChange(resumePomodoro(advancePomodoro(run, settings)))}
|
|
450
|
+
/>
|
|
451
|
+
</div>
|
|
452
|
+
</div>
|
|
453
|
+
<div className="flex min-w-0 flex-col gap-0.5">
|
|
454
|
+
<Widget.Title leading={<span aria-hidden className={cn('size-2.5 shrink-0 rounded-full', DOT[run.phase])} />}>
|
|
455
|
+
{phaseName(run, settings.rounds)}
|
|
456
|
+
</Widget.Title>
|
|
457
|
+
<Widget.Meta className="pl-4.5">
|
|
458
|
+
<Widget.MetaItem icon={running ? Clock3 : Pause}>{status}</Widget.MetaItem>
|
|
459
|
+
<Widget.MetaItem icon={GLYPH[next.phase]}>{nextLabel}</Widget.MetaItem>
|
|
460
|
+
</Widget.Meta>
|
|
461
|
+
</div>
|
|
462
|
+
</Widget>
|
|
463
|
+
)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* A notch app's clock: the time as the hero, the date above it, the world
|
|
468
|
+
* clocks beside it, and a Pomodoro cycle that takes over while it runs.
|
|
469
|
+
*
|
|
470
|
+
* The time is set large in hours and minutes (`seconds` adds them, muted,
|
|
471
|
+
* and a tick on each second), on the locale's clock or the one `hour12`
|
|
472
|
+
* picks, with the time zone's name under it and the date on the line above.
|
|
473
|
+
* Beside it, past a hairline, the world clocks (`zones`): each city's time,
|
|
474
|
+
* how far it is from here (`−6h`), whether it's day or night there, and
|
|
475
|
+
* `Tomorrow` or `Yesterday` when its date differs. Nothing ticks between
|
|
476
|
+
* minutes; `now` pins the moment.
|
|
477
|
+
*
|
|
478
|
+
* Under the time the Pomodoro waits with its lengths (`pomodoro`: focus,
|
|
479
|
+
* short and long break in minutes, and the rounds before the long one) and
|
|
480
|
+
* a Start focus button. A run takes the widget over: the phase's ring in
|
|
481
|
+
* its hue (focus warm, a break green) as the picture, the time left as the
|
|
482
|
+
* hero, its round (`Focus 2 of 4`), when it ends and what comes next, and
|
|
483
|
+
* the next phases with their start times beside it. Pause / resume is the
|
|
484
|
+
* main action, with +1 min and skip beside it and reset at the top.
|
|
485
|
+
*
|
|
486
|
+
* The run is a value (`value` / `onValueChange`, or kept here from
|
|
487
|
+
* `defaultValue`): a running phase carries when it ends, a paused one the
|
|
488
|
+
* time it had left, so a host can persist it and pick it up after a
|
|
489
|
+
* reload. When a phase reaches zero the next one waits paused at its whole
|
|
490
|
+
* length and `onPhaseEnd` is called; the helpers `startPomodoro`,
|
|
491
|
+
* `advancePomodoro` and `resumePomodoro` build the same runs for a host
|
|
492
|
+
* that keeps time itself.
|
|
493
|
+
*
|
|
494
|
+
* The other forms are parts: `Clock.Leading` and `Clock.Trailing` for the
|
|
495
|
+
* compact island's wings (the ring and `mm:ss`, nothing without a run),
|
|
496
|
+
* `Clock.Minimal`, the ring, for the split island's bubble, and
|
|
497
|
+
* `Clock.PhaseDone`, the flash at a phase's end with a button to start the
|
|
498
|
+
* next.
|
|
499
|
+
*
|
|
500
|
+
* @example {@include ./examples.mdx}
|
|
501
|
+
*/
|
|
502
|
+
const Clock = Object.assign(
|
|
503
|
+
function Clock({
|
|
504
|
+
zones = [],
|
|
505
|
+
seconds = false,
|
|
506
|
+
hour12,
|
|
507
|
+
timeZone,
|
|
508
|
+
locale,
|
|
509
|
+
now: nowProp,
|
|
510
|
+
pomodoro,
|
|
511
|
+
value,
|
|
512
|
+
defaultValue = null,
|
|
513
|
+
onValueChange,
|
|
514
|
+
onPhaseEnd,
|
|
515
|
+
className,
|
|
516
|
+
}: ClockProps) {
|
|
517
|
+
const [run, setRun] = useControllableState(value, defaultValue, onValueChange)
|
|
518
|
+
const ticking = useEdgeClock(0, nowProp === undefined && !run, seconds ? SECOND : MINUTE)
|
|
519
|
+
const settings = settingsOf(pomodoro)
|
|
520
|
+
|
|
521
|
+
if (run)
|
|
522
|
+
return (
|
|
523
|
+
<Running
|
|
524
|
+
run={run}
|
|
525
|
+
settings={settings}
|
|
526
|
+
onChange={setRun}
|
|
527
|
+
onPhaseEnd={onPhaseEnd}
|
|
528
|
+
locale={locale}
|
|
529
|
+
hour12={hour12}
|
|
530
|
+
timeZone={timeZone}
|
|
531
|
+
className={className}
|
|
532
|
+
/>
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
const now = nowProp ?? ticking
|
|
536
|
+
const time = timeParts(now, { locale, hour12, timeZone })
|
|
537
|
+
const second = seconds && new Intl.DateTimeFormat('en-US', { second: '2-digit', timeZone }).format(now)
|
|
538
|
+
const date = new Date(now).toLocaleDateString(locale, { weekday: 'long', month: 'long', day: 'numeric', timeZone })
|
|
539
|
+
const zoneName = new Intl.DateTimeFormat(locale, { timeZone, timeZoneName: 'long' })
|
|
540
|
+
.formatToParts(now)
|
|
541
|
+
.find(part => part.type === 'timeZoneName')?.value
|
|
542
|
+
|
|
543
|
+
return (
|
|
544
|
+
<Widget
|
|
545
|
+
className={className}
|
|
546
|
+
aside={
|
|
547
|
+
zones.length > 0 && (
|
|
548
|
+
<Widget.Section label="World clock">
|
|
549
|
+
{zones.slice(0, 3).map(zone => (
|
|
550
|
+
<ZoneRow
|
|
551
|
+
key={zone.timeZone + zone.city}
|
|
552
|
+
zone={zone}
|
|
553
|
+
now={now}
|
|
554
|
+
locale={locale}
|
|
555
|
+
hour12={hour12}
|
|
556
|
+
timeZone={timeZone}
|
|
557
|
+
/>
|
|
558
|
+
))}
|
|
559
|
+
</Widget.Section>
|
|
560
|
+
)
|
|
561
|
+
}
|
|
562
|
+
>
|
|
563
|
+
<Widget.Header icon={CalendarDays}>{date}</Widget.Header>
|
|
564
|
+
<div className="flex min-w-0 items-end justify-between gap-3">
|
|
565
|
+
<Widget.Figure
|
|
566
|
+
value={
|
|
567
|
+
<>
|
|
568
|
+
{time.text}
|
|
569
|
+
{second && <span className="text-muted-foreground">:{second.padStart(2, '0')}</span>}
|
|
570
|
+
</>
|
|
571
|
+
}
|
|
572
|
+
unit={time.period}
|
|
573
|
+
/>
|
|
574
|
+
{pomodoro !== false && (
|
|
575
|
+
<Button
|
|
576
|
+
variant="secondary"
|
|
577
|
+
size="compact"
|
|
578
|
+
leadingIcon={Play}
|
|
579
|
+
className="mb-1"
|
|
580
|
+
onClick={() => setRun(startPomodoro(settings))}
|
|
581
|
+
>
|
|
582
|
+
Start focus
|
|
583
|
+
</Button>
|
|
584
|
+
)}
|
|
585
|
+
</div>
|
|
586
|
+
{zoneName && <Widget.Subtitle>{zoneName}</Widget.Subtitle>}
|
|
587
|
+
{pomodoro !== false && (
|
|
588
|
+
<Widget.Meta className="mt-1">
|
|
589
|
+
<Widget.MetaItem icon={Target}>{settings.focus} min focus</Widget.MetaItem>
|
|
590
|
+
<Widget.MetaItem icon={Coffee}>
|
|
591
|
+
{settings.shortBreak} / {settings.longBreak} min breaks
|
|
592
|
+
</Widget.MetaItem>
|
|
593
|
+
<Widget.MetaItem icon={Repeat}>{settings.rounds} rounds</Widget.MetaItem>
|
|
594
|
+
</Widget.Meta>
|
|
595
|
+
)}
|
|
596
|
+
</Widget>
|
|
597
|
+
)
|
|
598
|
+
},
|
|
599
|
+
{
|
|
600
|
+
Leading: ClockLeading,
|
|
601
|
+
Trailing: ClockTrailing,
|
|
602
|
+
Minimal: ClockMinimal,
|
|
603
|
+
PhaseDone: ClockPhaseDone,
|
|
604
|
+
},
|
|
605
|
+
)
|
|
606
|
+
|
|
607
|
+
export default Clock
|
|
608
|
+
|
|
609
|
+
export type { ClockPhaseDoneProps, ClockProps, ClockZone, PomodoroPhase, PomodoroRun, PomodoroSettings }
|
|
610
|
+
export {
|
|
611
|
+
advancePomodoro,
|
|
612
|
+
Clock,
|
|
613
|
+
ClockLeading,
|
|
614
|
+
ClockMinimal,
|
|
615
|
+
ClockPhaseDone,
|
|
616
|
+
ClockTrailing,
|
|
617
|
+
resumePomodoro,
|
|
618
|
+
startPomodoro,
|
|
619
|
+
}
|