@sanity/workbench 0.1.0-alpha.12 → 0.1.0-alpha.14

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.
@@ -0,0 +1,681 @@
1
+ import { ActorRefFromLogic } from "xstate";
2
+ import { AuthState } from "@sanity/sdk";
3
+ import { AuthStateType } from "@sanity/sdk";
4
+ import { CallbackActorLogic } from "xstate";
5
+ import { ConsentStatus } from "@sanity/telemetry";
6
+ import { CurrentUser } from "@sanity/sdk";
7
+ import { EventObject } from "xstate";
8
+ import { InspectionEvent } from "xstate";
9
+ import { MetaObject } from "xstate";
10
+ import { NonReducibleUnknown } from "xstate";
11
+ import { ObservableActorLogic } from "xstate";
12
+ import { PromiseActorLogic } from "xstate";
13
+ import { SanityInstance } from "@sanity/sdk";
14
+ import { StateMachine } from "xstate";
15
+ import { TelemetryStore } from "@sanity/telemetry";
16
+
17
+ /**
18
+ * @internal
19
+ */
20
+ export declare interface AuthInput extends OSBaseInput {}
21
+
22
+ declare type ColorScheme = "light" | "dark";
23
+
24
+ declare type ColorSchemePreference = "system" | ColorScheme;
25
+
26
+ /**
27
+ * Creates a set of default options for the OS machine. Forwards the workbench
28
+ * user properties to the machine's input and wires the structured-logging
29
+ * `inspect` callback. Browser-specific concerns (color-scheme seeding,
30
+ * persistence) live in the {@link SystemPreferencesAdapter} the host passes
31
+ * via `systemPreferencesAdapter`.
32
+ * @public
33
+ */
34
+ export declare function createOSOptions(input: OSInput): {
35
+ id: string;
36
+ input: OSInput;
37
+ inspect: (event: InspectionEvent) => void;
38
+ };
39
+
40
+ /**
41
+ * @internal
42
+ */
43
+ export declare interface LogoutInput extends OSBaseInput {}
44
+
45
+ /**
46
+ * The sanity OS machine, responsible for managing the global state of the OS.
47
+ * @public
48
+ * @example
49
+ * ```ts
50
+ * import { os, createOSOptions } from "@sanity/workbench/system";
51
+ * import { useActor } from "@xstate/react";
52
+ *
53
+ * const [state, send] = useActor(os, createOSOptions({
54
+ * version: "1.0.0",
55
+ * organizationId: "...",
56
+ * environment: "production",
57
+ * userAgent: navigator.userAgent,
58
+ * }));
59
+ * ```
60
+ */
61
+ export declare const os: StateMachine<
62
+ OSContext,
63
+ | {
64
+ type: "boot.auth.ready";
65
+ }
66
+ | {
67
+ type: "boot.auth.failed";
68
+ }
69
+ | {
70
+ type: "boot.telemetry.ready";
71
+ },
72
+ {
73
+ telemetry?:
74
+ | ActorRefFromLogic<
75
+ StateMachine<
76
+ {
77
+ instance: SanityInstance;
78
+ store: TelemetryStore<WorkbenchUserProperties> | null;
79
+ userProperties: WorkbenchUserProperties;
80
+ },
81
+ | {
82
+ type: "telemetry.start";
83
+ }
84
+ | {
85
+ type: "telemetry.stop";
86
+ },
87
+ {
88
+ [x: string]:
89
+ | ActorRefFromLogic<
90
+ PromiseActorLogic<
91
+ {
92
+ status: ConsentStatus;
93
+ },
94
+ {
95
+ instance: SanityInstance;
96
+ },
97
+ EventObject
98
+ >
99
+ >
100
+ | undefined;
101
+ },
102
+ {
103
+ src: "checkConsent";
104
+ logic: PromiseActorLogic<
105
+ {
106
+ status: ConsentStatus;
107
+ },
108
+ {
109
+ instance: SanityInstance;
110
+ },
111
+ EventObject
112
+ >;
113
+ id: string | undefined;
114
+ },
115
+ | {
116
+ type: "createStore";
117
+ params: NonReducibleUnknown;
118
+ }
119
+ | {
120
+ type: "teardownStore";
121
+ params: unknown;
122
+ },
123
+ {
124
+ type: "isConsentGranted";
125
+ params: {
126
+ status: ConsentStatus;
127
+ };
128
+ },
129
+ never,
130
+ "active" | "stopped" | "denied" | "idle" | "checkingConsent",
131
+ string,
132
+ TelemetryInput,
133
+ NonReducibleUnknown,
134
+ EventObject,
135
+ MetaObject,
136
+ {
137
+ id: "telemetry";
138
+ states: {
139
+ readonly idle: {};
140
+ readonly checkingConsent: {};
141
+ readonly active: {};
142
+ readonly stopped: {};
143
+ readonly denied: {};
144
+ };
145
+ }
146
+ >
147
+ >
148
+ | undefined;
149
+ auth?:
150
+ | ActorRefFromLogic<
151
+ StateMachine<
152
+ {
153
+ instance: SanityInstance;
154
+ token: string | null;
155
+ currentUser: CurrentUser | null;
156
+ error: unknown;
157
+ },
158
+ {
159
+ type: "auth.logout";
160
+ },
161
+ {
162
+ [x: string]:
163
+ | ActorRefFromLogic<
164
+ ObservableActorLogic<AuthState, AuthInput, EventObject>
165
+ >
166
+ | ActorRefFromLogic<
167
+ PromiseActorLogic<void, LogoutInput, EventObject>
168
+ >
169
+ | undefined;
170
+ },
171
+ | {
172
+ src: "authState";
173
+ logic: ObservableActorLogic<AuthState, AuthInput, EventObject>;
174
+ id: string | undefined;
175
+ }
176
+ | {
177
+ src: "logoutActor";
178
+ logic: PromiseActorLogic<void, LogoutInput, EventObject>;
179
+ id: string | undefined;
180
+ },
181
+ | {
182
+ type: "setLoggedIn";
183
+ params: {
184
+ token: string;
185
+ currentUser: CurrentUser;
186
+ };
187
+ }
188
+ | {
189
+ type: "clearAuth";
190
+ params: NonReducibleUnknown;
191
+ }
192
+ | {
193
+ type: "setError";
194
+ params: {
195
+ error: unknown;
196
+ };
197
+ },
198
+ | {
199
+ type: "isLoggedInComplete";
200
+ params: {
201
+ state: AuthState | undefined;
202
+ };
203
+ }
204
+ | {
205
+ type: "isAuthState";
206
+ params: {
207
+ state: AuthState | undefined;
208
+ type: AuthStateType;
209
+ };
210
+ },
211
+ "authTimeout",
212
+ "init" | AuthStateType | "logging-out",
213
+ "error" | "authenticating" | "authenticated",
214
+ AuthInput,
215
+ NonReducibleUnknown,
216
+ EventObject,
217
+ MetaObject,
218
+ {
219
+ id: "auth";
220
+ states: {
221
+ readonly init: {};
222
+ readonly "logging-in": {};
223
+ readonly "logged-in": {};
224
+ readonly "logging-out": {};
225
+ readonly "logged-out": {};
226
+ readonly error: {};
227
+ };
228
+ }
229
+ >
230
+ >
231
+ | undefined;
232
+ "system-preferences"?:
233
+ | ActorRefFromLogic<
234
+ StateMachine<
235
+ SystemPreferencesContext & {
236
+ adapter: SystemPreferencesAdapter | undefined;
237
+ },
238
+ | {
239
+ type: "preferredColorScheme.set";
240
+ preferredColorScheme: ("light" | "dark") | "system";
241
+ }
242
+ | {
243
+ type: "osColorScheme.set";
244
+ osColorScheme: "light" | "dark";
245
+ },
246
+ {
247
+ [x: string]:
248
+ | ActorRefFromLogic<
249
+ CallbackActorLogic<
250
+ {
251
+ type: "preferredColorScheme.set";
252
+ preferredColorScheme: ("light" | "dark") | "system";
253
+ },
254
+ SystemPreferencesAdapter | undefined,
255
+ SystemPreferencesEvent
256
+ >
257
+ >
258
+ | undefined;
259
+ },
260
+ {
261
+ src: "adapterBridge";
262
+ logic: CallbackActorLogic<
263
+ {
264
+ type: "preferredColorScheme.set";
265
+ preferredColorScheme: ("light" | "dark") | "system";
266
+ },
267
+ SystemPreferencesAdapter | undefined,
268
+ SystemPreferencesEvent
269
+ >;
270
+ id: string | undefined;
271
+ },
272
+ | {
273
+ type: "setPreferredColorScheme";
274
+ params: {
275
+ preferredColorScheme: ("light" | "dark") | "system";
276
+ };
277
+ }
278
+ | {
279
+ type: "setOsColorScheme";
280
+ params: {
281
+ osColorScheme: "light" | "dark";
282
+ };
283
+ },
284
+ never,
285
+ never,
286
+ "ready",
287
+ string,
288
+ {
289
+ adapter?: SystemPreferencesAdapter;
290
+ },
291
+ NonReducibleUnknown,
292
+ EventObject,
293
+ MetaObject,
294
+ {
295
+ id: "system-preferences";
296
+ states: {
297
+ readonly ready: {};
298
+ };
299
+ }
300
+ >
301
+ >
302
+ | undefined;
303
+ },
304
+ | {
305
+ src: "telemetry";
306
+ logic: StateMachine<
307
+ {
308
+ instance: SanityInstance;
309
+ store: TelemetryStore<WorkbenchUserProperties> | null;
310
+ userProperties: WorkbenchUserProperties;
311
+ },
312
+ | {
313
+ type: "telemetry.start";
314
+ }
315
+ | {
316
+ type: "telemetry.stop";
317
+ },
318
+ {
319
+ [x: string]:
320
+ | ActorRefFromLogic<
321
+ PromiseActorLogic<
322
+ {
323
+ status: ConsentStatus;
324
+ },
325
+ {
326
+ instance: SanityInstance;
327
+ },
328
+ EventObject
329
+ >
330
+ >
331
+ | undefined;
332
+ },
333
+ {
334
+ src: "checkConsent";
335
+ logic: PromiseActorLogic<
336
+ {
337
+ status: ConsentStatus;
338
+ },
339
+ {
340
+ instance: SanityInstance;
341
+ },
342
+ EventObject
343
+ >;
344
+ id: string | undefined;
345
+ },
346
+ | {
347
+ type: "createStore";
348
+ params: NonReducibleUnknown;
349
+ }
350
+ | {
351
+ type: "teardownStore";
352
+ params: unknown;
353
+ },
354
+ {
355
+ type: "isConsentGranted";
356
+ params: {
357
+ status: ConsentStatus;
358
+ };
359
+ },
360
+ never,
361
+ "active" | "stopped" | "denied" | "idle" | "checkingConsent",
362
+ string,
363
+ TelemetryInput,
364
+ NonReducibleUnknown,
365
+ EventObject,
366
+ MetaObject,
367
+ {
368
+ id: "telemetry";
369
+ states: {
370
+ readonly idle: {};
371
+ readonly checkingConsent: {};
372
+ readonly active: {};
373
+ readonly stopped: {};
374
+ readonly denied: {};
375
+ };
376
+ }
377
+ >;
378
+ id: "telemetry";
379
+ }
380
+ | {
381
+ src: "auth";
382
+ logic: StateMachine<
383
+ {
384
+ instance: SanityInstance;
385
+ token: string | null;
386
+ currentUser: CurrentUser | null;
387
+ error: unknown;
388
+ },
389
+ {
390
+ type: "auth.logout";
391
+ },
392
+ {
393
+ [x: string]:
394
+ | ActorRefFromLogic<
395
+ ObservableActorLogic<AuthState, AuthInput, EventObject>
396
+ >
397
+ | ActorRefFromLogic<
398
+ PromiseActorLogic<void, LogoutInput, EventObject>
399
+ >
400
+ | undefined;
401
+ },
402
+ | {
403
+ src: "authState";
404
+ logic: ObservableActorLogic<AuthState, AuthInput, EventObject>;
405
+ id: string | undefined;
406
+ }
407
+ | {
408
+ src: "logoutActor";
409
+ logic: PromiseActorLogic<void, LogoutInput, EventObject>;
410
+ id: string | undefined;
411
+ },
412
+ | {
413
+ type: "setLoggedIn";
414
+ params: {
415
+ token: string;
416
+ currentUser: CurrentUser;
417
+ };
418
+ }
419
+ | {
420
+ type: "clearAuth";
421
+ params: NonReducibleUnknown;
422
+ }
423
+ | {
424
+ type: "setError";
425
+ params: {
426
+ error: unknown;
427
+ };
428
+ },
429
+ | {
430
+ type: "isLoggedInComplete";
431
+ params: {
432
+ state: AuthState | undefined;
433
+ };
434
+ }
435
+ | {
436
+ type: "isAuthState";
437
+ params: {
438
+ state: AuthState | undefined;
439
+ type: AuthStateType;
440
+ };
441
+ },
442
+ "authTimeout",
443
+ "init" | AuthStateType | "logging-out",
444
+ "error" | "authenticating" | "authenticated",
445
+ AuthInput,
446
+ NonReducibleUnknown,
447
+ EventObject,
448
+ MetaObject,
449
+ {
450
+ id: "auth";
451
+ states: {
452
+ readonly init: {};
453
+ readonly "logging-in": {};
454
+ readonly "logged-in": {};
455
+ readonly "logging-out": {};
456
+ readonly "logged-out": {};
457
+ readonly error: {};
458
+ };
459
+ }
460
+ >;
461
+ id: "auth";
462
+ }
463
+ | {
464
+ src: "systemPreferences";
465
+ logic: StateMachine<
466
+ SystemPreferencesContext & {
467
+ adapter: SystemPreferencesAdapter | undefined;
468
+ },
469
+ | {
470
+ type: "preferredColorScheme.set";
471
+ preferredColorScheme: ("light" | "dark") | "system";
472
+ }
473
+ | {
474
+ type: "osColorScheme.set";
475
+ osColorScheme: "light" | "dark";
476
+ },
477
+ {
478
+ [x: string]:
479
+ | ActorRefFromLogic<
480
+ CallbackActorLogic<
481
+ {
482
+ type: "preferredColorScheme.set";
483
+ preferredColorScheme: ("light" | "dark") | "system";
484
+ },
485
+ SystemPreferencesAdapter | undefined,
486
+ SystemPreferencesEvent
487
+ >
488
+ >
489
+ | undefined;
490
+ },
491
+ {
492
+ src: "adapterBridge";
493
+ logic: CallbackActorLogic<
494
+ {
495
+ type: "preferredColorScheme.set";
496
+ preferredColorScheme: ("light" | "dark") | "system";
497
+ },
498
+ SystemPreferencesAdapter | undefined,
499
+ SystemPreferencesEvent
500
+ >;
501
+ id: string | undefined;
502
+ },
503
+ | {
504
+ type: "setPreferredColorScheme";
505
+ params: {
506
+ preferredColorScheme: ("light" | "dark") | "system";
507
+ };
508
+ }
509
+ | {
510
+ type: "setOsColorScheme";
511
+ params: {
512
+ osColorScheme: "light" | "dark";
513
+ };
514
+ },
515
+ never,
516
+ never,
517
+ "ready",
518
+ string,
519
+ {
520
+ adapter?: SystemPreferencesAdapter;
521
+ },
522
+ NonReducibleUnknown,
523
+ EventObject,
524
+ MetaObject,
525
+ {
526
+ id: "system-preferences";
527
+ states: {
528
+ readonly ready: {};
529
+ };
530
+ }
531
+ >;
532
+ id: "system-preferences";
533
+ },
534
+ | {
535
+ type: "raiseAuthReady";
536
+ params: NonReducibleUnknown;
537
+ }
538
+ | {
539
+ type: "raiseAuthFailed";
540
+ params: NonReducibleUnknown;
541
+ }
542
+ | {
543
+ type: "raiseTelemetryReady";
544
+ params: NonReducibleUnknown;
545
+ }
546
+ | {
547
+ type: "startTelemetry";
548
+ params: NonReducibleUnknown;
549
+ },
550
+ {
551
+ type: "hasTag";
552
+ params: {
553
+ hasTag: boolean;
554
+ };
555
+ },
556
+ never,
557
+ | "running"
558
+ | {
559
+ booting: "error" | "done" | "telemetry" | "auth";
560
+ },
561
+ string,
562
+ OSInput,
563
+ NonReducibleUnknown,
564
+ EventObject,
565
+ MetaObject,
566
+ {
567
+ id: "os";
568
+ states: {
569
+ readonly booting: {
570
+ states: {
571
+ readonly auth: {};
572
+ readonly telemetry: {};
573
+ readonly error: {};
574
+ readonly done: {};
575
+ };
576
+ };
577
+ readonly running: {};
578
+ };
579
+ }
580
+ >;
581
+
582
+ /**
583
+ * The base inputs for the OS machine.
584
+ * @internal
585
+ */
586
+ declare interface OSBaseInput extends Pick<OSContext, "instance"> {}
587
+
588
+ declare type OSContext = {
589
+ instance: SanityInstance;
590
+ userProperties: WorkbenchUserProperties;
591
+ systemPreferencesAdapter: SystemPreferencesAdapter | undefined;
592
+ };
593
+
594
+ declare type OSInput = WorkbenchUserProperties & {
595
+ systemPreferencesAdapter?: SystemPreferencesAdapter;
596
+ };
597
+
598
+ /**
599
+ * Adapter interface the host implements to give the system-preferences
600
+ * machine access to the user's environment (OS color scheme, persistent
601
+ * storage, cross-context change notifications).
602
+ *
603
+ * The package owns all orchestration — synchronous seeding, idempotent
604
+ * persistence, mapping cleared storage to `"system"`. The host's
605
+ * implementation is pure DOM/storage glue: read, write, subscribe.
606
+ * @public
607
+ */
608
+ export declare type SystemPreferencesAdapter = {
609
+ /** Read the user's stored preference. `null` means "no preference set"
610
+ * (the machine treats this as `"system"`). */
611
+ getPreferredColorScheme: () => ColorSchemePreference | null;
612
+ /** Write the user's preference to persistent storage. */
613
+ persistPreferredColorScheme: (value: ColorSchemePreference) => void;
614
+ /** Subscribe to cross-context preference changes (e.g. another tab
615
+ * writing to the shared storage). The callback receives the new stored
616
+ * value (or `null` if it was cleared). Returns an unsubscribe function. */
617
+ subscribePreferredColorScheme: (
618
+ callback: (next: ColorSchemePreference | null) => void,
619
+ ) => () => void;
620
+ /** Read the current OS-detected color scheme. */
621
+ getOsColorScheme: () => ColorScheme;
622
+ /** Subscribe to OS color-scheme changes (e.g. user flipping dark mode).
623
+ * Returns an unsubscribe function. */
624
+ subscribeOsColorScheme: (callback: (next: ColorScheme) => void) => () => void;
625
+ };
626
+
627
+ /**
628
+ * The system-preferences machine's context. Consumers read fields directly
629
+ * (e.g. via `useSelector`) rather than going through a resolver utility —
630
+ * the action handlers keep `colorScheme` in sync with `preferredColorScheme`
631
+ * and `osColorScheme`.
632
+ *
633
+ * Note: `colorScheme` is intentionally stored as derived state in context so
634
+ * consumers can read the resolved value directly without a utility. The
635
+ * action handlers below are the single source of truth for the resolution.
636
+ * @public
637
+ */
638
+ export declare type SystemPreferencesContext = {
639
+ /** User's color scheme choice; `system` defers to the OS preference. */
640
+ preferredColorScheme: ColorSchemePreference;
641
+ /** Last reported OS color scheme. Used to resolve `colorScheme` when the
642
+ * preference is `system`. */
643
+ osColorScheme: ColorScheme;
644
+ /** Resolved color scheme — what the UI should render. Recomputed by the
645
+ * action handlers whenever an input changes. */
646
+ colorScheme: ColorScheme;
647
+ };
648
+
649
+ /**
650
+ * Events the system-preferences machine accepts. Both originate from the
651
+ * host's adapter — `preferredColorScheme.set` is also forwarded back to it
652
+ * so it can persist the user's choice.
653
+ * @public
654
+ */
655
+ export declare type SystemPreferencesEvent =
656
+ | {
657
+ type: "preferredColorScheme.set";
658
+ preferredColorScheme: ColorSchemePreference;
659
+ }
660
+ | {
661
+ type: "osColorScheme.set";
662
+ osColorScheme: ColorScheme;
663
+ };
664
+
665
+ /**
666
+ * @internal
667
+ */
668
+ export declare interface TelemetryInput
669
+ extends OSBaseInput, WorkbenchUserProperties {}
670
+
671
+ /**
672
+ * @public
673
+ */
674
+ export declare type WorkbenchUserProperties = {
675
+ version: string;
676
+ organizationId: string;
677
+ environment: string;
678
+ userAgent: string;
679
+ };
680
+
681
+ export {};