@opencode/ui 0.0.0-dev-750 → 0.0.0-dev-784
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/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/scroll-view-machine.d.ts +66 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/theme/context.d.ts +20 -9
- package/dist/theme/machine.d.ts +174 -0
- package/package.json +2 -2
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +113 -83
- package/src/components/scroll-view-machine.ts +231 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/data-display/line-comment/line-comment.tsx +154 -178
- package/src/data-display/line-comment/machine.ts +392 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.tsx +49 -27
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +118 -20
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/theme/context.tsx +134 -240
- package/src/theme/machine.ts +508 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import type { DesktopTheme } from "./types"
|
|
2
|
+
|
|
3
|
+
export type ColorScheme = "light" | "dark" | "system"
|
|
4
|
+
|
|
5
|
+
export type ResolvedMode = "light" | "dark"
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_THEME_ID = "oc-2"
|
|
8
|
+
|
|
9
|
+
/** A bundled theme import that has not settled by then shows the default theme until it arrives. */
|
|
10
|
+
export const THEME_LOAD_TIMEOUT_MS = 5_000
|
|
11
|
+
|
|
12
|
+
export type ThemeSelection = { readonly themeId: string; readonly scheme: ColorScheme }
|
|
13
|
+
|
|
14
|
+
/** Overrides one or both axes of the committed selection; a preview with neither is `committed`. */
|
|
15
|
+
export type ThemePreview = { readonly themeId?: string; readonly scheme?: ColorScheme }
|
|
16
|
+
|
|
17
|
+
/** What the window shows. `preload` is a registered theme that may still arrive: the cached colors stay on screen. */
|
|
18
|
+
export type AppliedTheme =
|
|
19
|
+
| {
|
|
20
|
+
readonly kind: "theme"
|
|
21
|
+
readonly id: string
|
|
22
|
+
readonly theme: DesktopTheme
|
|
23
|
+
readonly mode: ResolvedMode
|
|
24
|
+
readonly scheme: ColorScheme
|
|
25
|
+
}
|
|
26
|
+
| { readonly kind: "preload"; readonly id: string; readonly mode: ResolvedMode; readonly scheme: ColorScheme }
|
|
27
|
+
|
|
28
|
+
/** The colors in localStorage that the next start paints before the app loads. `unknown` keeps the last run's. */
|
|
29
|
+
export type CachedTheme =
|
|
30
|
+
| { readonly status: "unknown" }
|
|
31
|
+
| { readonly status: "cleared" }
|
|
32
|
+
| { readonly status: "cached"; readonly id: string; readonly theme: DesktopTheme }
|
|
33
|
+
|
|
34
|
+
type ThemeData = {
|
|
35
|
+
readonly committed: ThemeSelection
|
|
36
|
+
readonly systemMode: ResolvedMode
|
|
37
|
+
/** `settled` once every theme source has registered what it will: a missing registered theme is not coming. */
|
|
38
|
+
readonly registration: "pending" | "settled"
|
|
39
|
+
/** Loaded bundled themes and registered themes, by id. */
|
|
40
|
+
readonly themes: Readonly<Record<string, DesktopTheme>>
|
|
41
|
+
/** The attempt of each bundled theme import in flight, including one that timed out and may still arrive. */
|
|
42
|
+
readonly loads: Readonly<Record<string, number>>
|
|
43
|
+
/**
|
|
44
|
+
* Bundled themes the window shows the default theme for: the import failed, or timed out and may still arrive.
|
|
45
|
+
* Selecting, previewing or preloading one tries again.
|
|
46
|
+
*/
|
|
47
|
+
readonly failed: Readonly<Record<string, true>>
|
|
48
|
+
readonly issued: number
|
|
49
|
+
readonly applied: AppliedTheme | undefined
|
|
50
|
+
readonly cached: CachedTheme
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type ThemeMachineState =
|
|
54
|
+
| ({ readonly status: "committed" } & ThemeData)
|
|
55
|
+
| ({ readonly status: "previewing"; readonly preview: ThemePreview } & ThemeData)
|
|
56
|
+
| ({ readonly status: "disposed" } & ThemeData)
|
|
57
|
+
|
|
58
|
+
type ActiveState = Exclude<ThemeMachineState, { status: "disposed" }>
|
|
59
|
+
|
|
60
|
+
export type ThemeEvent =
|
|
61
|
+
| { readonly type: "theme.selected"; readonly id: string }
|
|
62
|
+
| { readonly type: "scheme.selected"; readonly scheme: ColorScheme }
|
|
63
|
+
| { readonly type: "preview.theme"; readonly id: string }
|
|
64
|
+
| { readonly type: "preview.scheme"; readonly scheme: ColorScheme }
|
|
65
|
+
| { readonly type: "preview.committed" }
|
|
66
|
+
| { readonly type: "preview.cancelled" }
|
|
67
|
+
| { readonly type: "theme.registered"; readonly theme: DesktopTheme }
|
|
68
|
+
| { readonly type: "theme.unregistered"; readonly theme: DesktopTheme }
|
|
69
|
+
| { readonly type: "settled" }
|
|
70
|
+
| { readonly type: "themes.preload" }
|
|
71
|
+
| { readonly type: "theme.loaded"; readonly id: string; readonly attempt: number; readonly theme: DesktopTheme }
|
|
72
|
+
| { readonly type: "theme.load.failed"; readonly id: string; readonly attempt: number }
|
|
73
|
+
| { readonly type: "theme.load.timed-out"; readonly id: string; readonly attempt: number }
|
|
74
|
+
| { readonly type: "system.mode.changed"; readonly mode: ResolvedMode }
|
|
75
|
+
| { readonly type: "storage.theme.changed"; readonly id: string }
|
|
76
|
+
| { readonly type: "storage.scheme.changed"; readonly scheme: ColorScheme }
|
|
77
|
+
| { readonly type: "disposed" }
|
|
78
|
+
|
|
79
|
+
export type ThemeEffect =
|
|
80
|
+
| { readonly type: "load-theme"; readonly id: string; readonly attempt: number; readonly timeoutMs: number }
|
|
81
|
+
| { readonly type: "cancel-load-timer"; readonly attempt: number }
|
|
82
|
+
| { readonly type: "cancel-all-loads" }
|
|
83
|
+
| { readonly type: "persist-theme"; readonly id: string }
|
|
84
|
+
| { readonly type: "persist-scheme"; readonly scheme: ColorScheme }
|
|
85
|
+
| { readonly type: "cache-theme"; readonly id: string; readonly theme: DesktopTheme }
|
|
86
|
+
| { readonly type: "clear-cache" }
|
|
87
|
+
| {
|
|
88
|
+
readonly type: "apply-theme"
|
|
89
|
+
readonly id: string
|
|
90
|
+
readonly theme: DesktopTheme
|
|
91
|
+
readonly mode: ResolvedMode
|
|
92
|
+
readonly scheme: ColorScheme
|
|
93
|
+
}
|
|
94
|
+
| { readonly type: "notify-applied"; readonly mode: ResolvedMode; readonly scheme: ColorScheme }
|
|
95
|
+
| { readonly type: "warn-missing-theme"; readonly id: string }
|
|
96
|
+
|
|
97
|
+
export type ThemeTransition = { readonly state: ThemeMachineState; readonly effects: readonly ThemeEffect[] }
|
|
98
|
+
|
|
99
|
+
type Step = { readonly state: ActiveState; readonly effects: readonly ThemeEffect[] }
|
|
100
|
+
|
|
101
|
+
const NO_EFFECTS: readonly ThemeEffect[] = []
|
|
102
|
+
|
|
103
|
+
export function parseColorScheme(value: string | null | undefined): ColorScheme {
|
|
104
|
+
return value === "light" || value === "dark" ? value : "system"
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createThemeState(input: {
|
|
108
|
+
readonly themeId: string
|
|
109
|
+
readonly scheme: ColorScheme
|
|
110
|
+
readonly systemMode: ResolvedMode
|
|
111
|
+
readonly defaultTheme: DesktopTheme
|
|
112
|
+
readonly bundled: ReadonlySet<string>
|
|
113
|
+
}): ThemeTransition {
|
|
114
|
+
return reconcile(
|
|
115
|
+
{
|
|
116
|
+
status: "committed",
|
|
117
|
+
committed: { themeId: input.themeId, scheme: input.scheme },
|
|
118
|
+
systemMode: input.systemMode,
|
|
119
|
+
registration: "pending",
|
|
120
|
+
themes: { [DEFAULT_THEME_ID]: input.defaultTheme },
|
|
121
|
+
loads: {},
|
|
122
|
+
failed: {},
|
|
123
|
+
issued: 0,
|
|
124
|
+
applied: undefined,
|
|
125
|
+
cached: { status: "unknown" },
|
|
126
|
+
},
|
|
127
|
+
input.bundled,
|
|
128
|
+
NO_EFFECTS,
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function transitionTheme(
|
|
133
|
+
state: ThemeMachineState,
|
|
134
|
+
event: ThemeEvent,
|
|
135
|
+
bundled: ReadonlySet<string>,
|
|
136
|
+
): ThemeTransition {
|
|
137
|
+
if (state.status === "disposed") return { state, effects: NO_EFFECTS }
|
|
138
|
+
|
|
139
|
+
switch (event.type) {
|
|
140
|
+
case "theme.selected": {
|
|
141
|
+
if (!known(state, event.id, bundled)) return { state, effects: [{ type: "warn-missing-theme", id: event.id }] }
|
|
142
|
+
|
|
143
|
+
if (state.committed.themeId === event.id && !state.failed[event.id] && preview(state).themeId === undefined)
|
|
144
|
+
return { state, effects: NO_EFFECTS }
|
|
145
|
+
|
|
146
|
+
return reconcile(
|
|
147
|
+
{
|
|
148
|
+
...withPreview(state, { ...preview(state), themeId: undefined }),
|
|
149
|
+
...retry(state, [event.id]),
|
|
150
|
+
committed: { ...state.committed, themeId: event.id },
|
|
151
|
+
},
|
|
152
|
+
bundled,
|
|
153
|
+
[{ type: "persist-theme", id: event.id }],
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
case "scheme.selected": {
|
|
158
|
+
if (state.committed.scheme === event.scheme && preview(state).scheme === undefined)
|
|
159
|
+
return { state, effects: NO_EFFECTS }
|
|
160
|
+
|
|
161
|
+
return reconcile(
|
|
162
|
+
{
|
|
163
|
+
...withPreview(state, { ...preview(state), scheme: undefined }),
|
|
164
|
+
committed: { ...state.committed, scheme: event.scheme },
|
|
165
|
+
},
|
|
166
|
+
bundled,
|
|
167
|
+
[{ type: "persist-scheme", scheme: event.scheme }],
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
case "preview.theme": {
|
|
172
|
+
if (!event.id || !known(state, event.id, bundled)) return { state, effects: NO_EFFECTS }
|
|
173
|
+
|
|
174
|
+
if (preview(state).themeId === event.id && !state.failed[event.id]) return { state, effects: NO_EFFECTS }
|
|
175
|
+
|
|
176
|
+
return reconcile(
|
|
177
|
+
{ ...withPreview(state, { ...preview(state), themeId: event.id }), ...retry(state, [event.id]) },
|
|
178
|
+
bundled,
|
|
179
|
+
NO_EFFECTS,
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
case "preview.scheme": {
|
|
184
|
+
if (preview(state).scheme === event.scheme) return { state, effects: NO_EFFECTS }
|
|
185
|
+
|
|
186
|
+
return reconcile(withPreview(state, { ...preview(state), scheme: event.scheme }), bundled, NO_EFFECTS)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
case "preview.committed": {
|
|
190
|
+
if (state.status !== "previewing") return { state, effects: NO_EFFECTS }
|
|
191
|
+
|
|
192
|
+
return reconcile(
|
|
193
|
+
{
|
|
194
|
+
...withPreview(state, {}),
|
|
195
|
+
committed: {
|
|
196
|
+
themeId: state.preview.themeId ?? state.committed.themeId,
|
|
197
|
+
scheme: state.preview.scheme ?? state.committed.scheme,
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
bundled,
|
|
201
|
+
[
|
|
202
|
+
...(state.preview.themeId === undefined
|
|
203
|
+
? []
|
|
204
|
+
: [{ type: "persist-theme", id: state.preview.themeId } as const]),
|
|
205
|
+
...(state.preview.scheme === undefined
|
|
206
|
+
? []
|
|
207
|
+
: [{ type: "persist-scheme", scheme: state.preview.scheme } as const]),
|
|
208
|
+
],
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
case "preview.cancelled": {
|
|
213
|
+
if (state.status !== "previewing") return { state, effects: NO_EFFECTS }
|
|
214
|
+
|
|
215
|
+
return reconcile(withPreview(state, {}), bundled, NO_EFFECTS)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
case "theme.registered": {
|
|
219
|
+
if (state.themes[event.theme.id] === event.theme) return { state, effects: NO_EFFECTS }
|
|
220
|
+
|
|
221
|
+
return reconcile(
|
|
222
|
+
{
|
|
223
|
+
...state,
|
|
224
|
+
themes: { ...state.themes, [event.theme.id]: event.theme },
|
|
225
|
+
failed: omit(state.failed, [event.theme.id]),
|
|
226
|
+
},
|
|
227
|
+
bundled,
|
|
228
|
+
NO_EFFECTS,
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
case "theme.unregistered": {
|
|
233
|
+
// A theme registered again under the same id replaced this one; leave it.
|
|
234
|
+
if (event.theme.id === DEFAULT_THEME_ID || state.themes[event.theme.id] !== event.theme)
|
|
235
|
+
return { state, effects: NO_EFFECTS }
|
|
236
|
+
|
|
237
|
+
return reconcile({ ...state, themes: omit(state.themes, [event.theme.id]) }, bundled, NO_EFFECTS)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
case "settled": {
|
|
241
|
+
if (state.registration === "settled") return { state, effects: NO_EFFECTS }
|
|
242
|
+
|
|
243
|
+
return reconcile({ ...state, registration: "settled" }, bundled, NO_EFFECTS)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
case "themes.preload": {
|
|
247
|
+
const queued = [...bundled].filter(
|
|
248
|
+
(id) => id !== DEFAULT_THEME_ID && !state.themes[id] && (state.loads[id] === undefined || state.failed[id]),
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
if (queued.length === 0) return { state, effects: NO_EFFECTS }
|
|
252
|
+
|
|
253
|
+
const requested = request({ ...state, ...retry(state, queued) }, queued, bundled)
|
|
254
|
+
|
|
255
|
+
return reconcile(requested.state, bundled, requested.effects)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
case "theme.loaded": {
|
|
259
|
+
if (state.loads[event.id] !== event.attempt) return { state, effects: NO_EFFECTS }
|
|
260
|
+
|
|
261
|
+
return reconcile(
|
|
262
|
+
{
|
|
263
|
+
...state,
|
|
264
|
+
loads: omit(state.loads, [event.id]),
|
|
265
|
+
failed: omit(state.failed, [event.id]),
|
|
266
|
+
themes: { ...state.themes, [event.id]: event.theme },
|
|
267
|
+
},
|
|
268
|
+
bundled,
|
|
269
|
+
[{ type: "cancel-load-timer", attempt: event.attempt }],
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
case "theme.load.failed": {
|
|
274
|
+
if (state.loads[event.id] !== event.attempt) return { state, effects: NO_EFFECTS }
|
|
275
|
+
|
|
276
|
+
return reconcile(
|
|
277
|
+
{ ...state, loads: omit(state.loads, [event.id]), failed: { ...state.failed, [event.id]: true } },
|
|
278
|
+
bundled,
|
|
279
|
+
[{ type: "cancel-load-timer", attempt: event.attempt }],
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// The window shows the default theme meanwhile, but the attempt stays current: a late import still applies.
|
|
284
|
+
case "theme.load.timed-out": {
|
|
285
|
+
if (state.loads[event.id] !== event.attempt || state.failed[event.id]) return { state, effects: NO_EFFECTS }
|
|
286
|
+
|
|
287
|
+
return reconcile({ ...state, failed: { ...state.failed, [event.id]: true } }, bundled, NO_EFFECTS)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
case "system.mode.changed": {
|
|
291
|
+
if (state.systemMode === event.mode) return { state, effects: NO_EFFECTS }
|
|
292
|
+
|
|
293
|
+
return reconcile({ ...state, systemMode: event.mode }, bundled, NO_EFFECTS)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
case "storage.theme.changed": {
|
|
297
|
+
if (!event.id || (state.committed.themeId === event.id && !state.failed[event.id]))
|
|
298
|
+
return { state, effects: NO_EFFECTS }
|
|
299
|
+
|
|
300
|
+
return reconcile(
|
|
301
|
+
{
|
|
302
|
+
...state,
|
|
303
|
+
...retry(state, [event.id]),
|
|
304
|
+
committed: { ...state.committed, themeId: event.id },
|
|
305
|
+
},
|
|
306
|
+
bundled,
|
|
307
|
+
NO_EFFECTS,
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
case "storage.scheme.changed": {
|
|
312
|
+
if (state.committed.scheme === event.scheme) return { state, effects: NO_EFFECTS }
|
|
313
|
+
|
|
314
|
+
return reconcile({ ...state, committed: { ...state.committed, scheme: event.scheme } }, bundled, NO_EFFECTS)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
case "disposed": {
|
|
318
|
+
const { preview: _, ...data } = { preview: undefined, ...state }
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
state: { ...data, status: "disposed", loads: {} },
|
|
322
|
+
effects: Object.keys(state.loads).length > 0 ? [{ type: "cancel-all-loads" }] : NO_EFFECTS,
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** The theme the window should show when nothing is previewed. */
|
|
329
|
+
export function selectCommittedThemeId(state: ThemeMachineState, bundled: ReadonlySet<string>) {
|
|
330
|
+
const id = state.committed.themeId
|
|
331
|
+
|
|
332
|
+
if (id === DEFAULT_THEME_ID || state.themes[id]) return id
|
|
333
|
+
|
|
334
|
+
if (bundled.has(id)) return state.failed[id] ? DEFAULT_THEME_ID : id
|
|
335
|
+
|
|
336
|
+
return state.registration === "pending" ? id : DEFAULT_THEME_ID
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function selectThemeId(state: ThemeMachineState, bundled: ReadonlySet<string>) {
|
|
340
|
+
return (state.status === "previewing" ? state.preview.themeId : undefined) ?? selectCommittedThemeId(state, bundled)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The scheme the user chose; a preview does not change it. */
|
|
344
|
+
export function selectColorScheme(state: ThemeMachineState) {
|
|
345
|
+
return state.committed.scheme
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The light or dark mode the window shows, preview included. */
|
|
349
|
+
export function selectMode(state: ThemeMachineState): ResolvedMode {
|
|
350
|
+
const scheme = previewedScheme(state)
|
|
351
|
+
|
|
352
|
+
return scheme === "system" ? state.systemMode : scheme
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** An import is still within its timeout. */
|
|
356
|
+
export function selectThemesLoading(state: ThemeMachineState) {
|
|
357
|
+
return Object.keys(state.loads).some((id) => !state.failed[id])
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function reconcile(draft: ActiveState, bundled: ReadonlySet<string>, prefix: readonly ThemeEffect[]): ThemeTransition {
|
|
361
|
+
const valid = validatePreview(draft, bundled)
|
|
362
|
+
|
|
363
|
+
const requested = request(
|
|
364
|
+
valid,
|
|
365
|
+
[...new Set([valid.committed.themeId, preview(valid).themeId].filter((id) => id !== undefined))],
|
|
366
|
+
bundled,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
const cached = reconcileCache(requested.state, bundled)
|
|
370
|
+
const applied = reconcileApplied(cached.state, bundled)
|
|
371
|
+
|
|
372
|
+
return { state: applied.state, effects: [...prefix, ...requested.effects, ...cached.effects, ...applied.effects] }
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// A previewed theme that was withdrawn or failed to load leaves the preview; its scheme, if any, stays previewed.
|
|
376
|
+
function validatePreview(state: ActiveState, bundled: ReadonlySet<string>): ActiveState {
|
|
377
|
+
const id = preview(state).themeId
|
|
378
|
+
|
|
379
|
+
if (id === undefined || id === DEFAULT_THEME_ID || state.themes[id] || (bundled.has(id) && !state.failed[id]))
|
|
380
|
+
return state
|
|
381
|
+
|
|
382
|
+
return withPreview(state, { ...preview(state), themeId: undefined })
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function request(state: ActiveState, ids: readonly string[], bundled: ReadonlySet<string>) {
|
|
386
|
+
return ids.reduce<Step>(
|
|
387
|
+
(result, id) => {
|
|
388
|
+
if (
|
|
389
|
+
id === DEFAULT_THEME_ID ||
|
|
390
|
+
!bundled.has(id) ||
|
|
391
|
+
result.state.themes[id] ||
|
|
392
|
+
result.state.failed[id] ||
|
|
393
|
+
result.state.loads[id] !== undefined
|
|
394
|
+
)
|
|
395
|
+
return result
|
|
396
|
+
|
|
397
|
+
const attempt = result.state.issued + 1
|
|
398
|
+
|
|
399
|
+
return {
|
|
400
|
+
state: { ...result.state, issued: attempt, loads: { ...result.state.loads, [id]: attempt } },
|
|
401
|
+
effects: [...result.effects, { type: "load-theme", id, attempt, timeoutMs: THEME_LOAD_TIMEOUT_MS }],
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
{ state, effects: NO_EFFECTS },
|
|
405
|
+
)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// The cache follows the committed theme only: a preview never reaches the next start.
|
|
409
|
+
function reconcileCache(state: ActiveState, bundled: ReadonlySet<string>): Step {
|
|
410
|
+
const id = state.committed.themeId
|
|
411
|
+
const theme = state.themes[id]
|
|
412
|
+
|
|
413
|
+
if (theme && id !== DEFAULT_THEME_ID) {
|
|
414
|
+
if (state.cached.status === "cached" && state.cached.id === id && state.cached.theme === theme)
|
|
415
|
+
return { state, effects: NO_EFFECTS }
|
|
416
|
+
|
|
417
|
+
return {
|
|
418
|
+
state: { ...state, cached: { status: "cached", id, theme } },
|
|
419
|
+
effects: [{ type: "cache-theme", id, theme }],
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// A bundled theme that has not loaded (loading, slow or failed) or a registered theme that may still arrive: the last
|
|
424
|
+
// run's colors for it stay, unless they are another theme's.
|
|
425
|
+
const waiting =
|
|
426
|
+
id !== DEFAULT_THEME_ID &&
|
|
427
|
+
(bundled.has(id) || state.registration === "pending") &&
|
|
428
|
+
!(state.cached.status === "cached" && state.cached.id !== id)
|
|
429
|
+
|
|
430
|
+
if (waiting || state.cached.status === "cleared") return { state, effects: NO_EFFECTS }
|
|
431
|
+
|
|
432
|
+
return {
|
|
433
|
+
state: { ...state, cached: { status: "cleared" } },
|
|
434
|
+
effects: [{ type: "clear-cache" }],
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function reconcileApplied(state: ActiveState, bundled: ReadonlySet<string>): Step {
|
|
439
|
+
const id = selectThemeId(state, bundled)
|
|
440
|
+
const scheme = previewedScheme(state)
|
|
441
|
+
const mode = selectMode(state)
|
|
442
|
+
const theme = state.themes[id]
|
|
443
|
+
const applied = state.applied
|
|
444
|
+
|
|
445
|
+
if (theme) {
|
|
446
|
+
if (
|
|
447
|
+
applied?.kind === "theme" &&
|
|
448
|
+
applied.id === id &&
|
|
449
|
+
applied.theme === theme &&
|
|
450
|
+
applied.mode === mode &&
|
|
451
|
+
applied.scheme === scheme
|
|
452
|
+
)
|
|
453
|
+
return { state, effects: NO_EFFECTS }
|
|
454
|
+
|
|
455
|
+
return {
|
|
456
|
+
state: { ...state, applied: { kind: "theme", id, theme, mode, scheme } },
|
|
457
|
+
effects: [{ type: "apply-theme", id, theme, mode, scheme }],
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// A bundled theme is loading: keep what is on screen until it arrives.
|
|
462
|
+
if (bundled.has(id)) return { state, effects: NO_EFFECTS }
|
|
463
|
+
|
|
464
|
+
if (applied?.kind === "preload" && applied.id === id && applied.mode === mode && applied.scheme === scheme)
|
|
465
|
+
return { state, effects: NO_EFFECTS }
|
|
466
|
+
|
|
467
|
+
return {
|
|
468
|
+
state: { ...state, applied: { kind: "preload", id, mode, scheme } },
|
|
469
|
+
effects: [{ type: "notify-applied", mode, scheme }],
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function known(state: ThemeMachineState, id: string, bundled: ReadonlySet<string>) {
|
|
474
|
+
return id === DEFAULT_THEME_ID || bundled.has(id) || !!state.themes[id]
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function preview(state: ThemeMachineState): ThemePreview {
|
|
478
|
+
return state.status === "previewing" ? state.preview : {}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function previewedScheme(state: ThemeMachineState) {
|
|
482
|
+
return preview(state).scheme ?? state.committed.scheme
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function withPreview(state: ActiveState, next: ThemePreview): ActiveState {
|
|
486
|
+
const { preview: _, ...data } = { preview: undefined, ...state }
|
|
487
|
+
|
|
488
|
+
if (next.themeId === undefined && next.scheme === undefined) return { ...data, status: "committed" }
|
|
489
|
+
|
|
490
|
+
return { ...data, status: "previewing", preview: next }
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Clears a failure and drops an import that timed out, so the next request starts a fresh attempt with its own timeout.
|
|
494
|
+
function retry(state: ActiveState, ids: readonly string[]) {
|
|
495
|
+
return {
|
|
496
|
+
failed: omit(state.failed, ids),
|
|
497
|
+
loads: omit(
|
|
498
|
+
state.loads,
|
|
499
|
+
ids.filter((id) => state.failed[id]),
|
|
500
|
+
),
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function omit<T>(record: Readonly<Record<string, T>>, ids: readonly string[]) {
|
|
505
|
+
if (!ids.some((id) => id in record)) return record
|
|
506
|
+
|
|
507
|
+
return Object.fromEntries(Object.entries(record).filter(([id]) => !ids.includes(id)))
|
|
508
|
+
}
|