@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.
- package/dist/_chunks-es/index.js +6 -1
- package/dist/_chunks-es/index.js.map +1 -1
- package/dist/_chunks-es/studio.js +798 -0
- package/dist/_chunks-es/studio.js.map +1 -0
- package/dist/core.d.ts +132 -34
- package/dist/core.js +3 -729
- package/dist/core.js.map +1 -1
- package/dist/system.d.ts +681 -0
- package/dist/system.js +528 -0
- package/dist/system.js.map +1 -0
- package/package.json +12 -1
- package/src/_exports/system.ts +1 -0
- package/src/core/log/index.ts +33 -0
- package/src/core/user-applications/core-app.ts +19 -6
- package/src/core/user-applications/studios/schemas.ts +12 -1
- package/src/core/user-applications/studios/studio.ts +92 -77
- package/src/core/user-applications/studios/workspace.ts +5 -0
- package/src/core/user-applications/user-application.ts +17 -1
- package/src/system/auth.machine.ts +223 -0
- package/src/system/index.ts +11 -0
- package/src/system/inspect.ts +40 -0
- package/src/system/root.machine.ts +190 -0
- package/src/system/system-preferences.machine.ts +215 -0
- package/src/system/telemetry.machine.ts +179 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { createSanityInstance, type SanityInstance } from "@sanity/sdk";
|
|
2
|
+
import { raise, sendTo, setup, type ActorOptions } from "xstate";
|
|
3
|
+
|
|
4
|
+
import { authLogic } from "./auth.machine";
|
|
5
|
+
import { inspect } from "./inspect";
|
|
6
|
+
import {
|
|
7
|
+
type SystemPreferencesAdapter,
|
|
8
|
+
systemPreferencesLogic,
|
|
9
|
+
} from "./system-preferences.machine";
|
|
10
|
+
import {
|
|
11
|
+
telemetryLogic,
|
|
12
|
+
type WorkbenchUserProperties,
|
|
13
|
+
} from "./telemetry.machine";
|
|
14
|
+
|
|
15
|
+
type OSInput = WorkbenchUserProperties & {
|
|
16
|
+
systemPreferencesAdapter?: SystemPreferencesAdapter;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type OSContext = {
|
|
20
|
+
instance: SanityInstance;
|
|
21
|
+
userProperties: WorkbenchUserProperties;
|
|
22
|
+
systemPreferencesAdapter: SystemPreferencesAdapter | undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The base inputs for the OS machine.
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export interface OSBaseInput extends Pick<OSContext, "instance"> {}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The sanity OS machine, responsible for managing the global state of the OS.
|
|
33
|
+
* @public
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { os, createOSOptions } from "@sanity/workbench/system";
|
|
37
|
+
* import { useActor } from "@xstate/react";
|
|
38
|
+
*
|
|
39
|
+
* const [state, send] = useActor(os, createOSOptions({
|
|
40
|
+
* version: "1.0.0",
|
|
41
|
+
* organizationId: "...",
|
|
42
|
+
* environment: "production",
|
|
43
|
+
* userAgent: navigator.userAgent,
|
|
44
|
+
* }));
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export const os = setup({
|
|
48
|
+
types: {
|
|
49
|
+
input: {} as OSInput,
|
|
50
|
+
context: {} as OSContext,
|
|
51
|
+
events: {} as
|
|
52
|
+
| { type: "boot.auth.ready" }
|
|
53
|
+
| { type: "boot.auth.failed" }
|
|
54
|
+
| { type: "boot.telemetry.ready" },
|
|
55
|
+
// https://github.com/statelyai/xstate/issues/5515
|
|
56
|
+
children: {} as {
|
|
57
|
+
auth: "auth";
|
|
58
|
+
telemetry: "telemetry";
|
|
59
|
+
"system-preferences": "systemPreferences";
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
actors: {
|
|
63
|
+
auth: authLogic,
|
|
64
|
+
telemetry: telemetryLogic,
|
|
65
|
+
systemPreferences: systemPreferencesLogic,
|
|
66
|
+
},
|
|
67
|
+
guards: {
|
|
68
|
+
hasTag: (_, params: { hasTag: boolean }) => params.hasTag,
|
|
69
|
+
},
|
|
70
|
+
actions: {
|
|
71
|
+
raiseAuthReady: raise({ type: "boot.auth.ready" }),
|
|
72
|
+
raiseAuthFailed: raise({ type: "boot.auth.failed" }),
|
|
73
|
+
raiseTelemetryReady: raise({
|
|
74
|
+
type: "boot.telemetry.ready",
|
|
75
|
+
}),
|
|
76
|
+
startTelemetry: sendTo("telemetry", {
|
|
77
|
+
type: "telemetry.start",
|
|
78
|
+
}),
|
|
79
|
+
},
|
|
80
|
+
}).createMachine({
|
|
81
|
+
id: "os",
|
|
82
|
+
context: ({ input }) => ({
|
|
83
|
+
instance: createSanityInstance(),
|
|
84
|
+
userProperties: {
|
|
85
|
+
version: input.version,
|
|
86
|
+
organizationId: input.organizationId,
|
|
87
|
+
environment: input.environment,
|
|
88
|
+
userAgent: input.userAgent,
|
|
89
|
+
},
|
|
90
|
+
systemPreferencesAdapter: input.systemPreferencesAdapter,
|
|
91
|
+
}),
|
|
92
|
+
initial: "booting",
|
|
93
|
+
invoke: [
|
|
94
|
+
{
|
|
95
|
+
id: "auth",
|
|
96
|
+
systemId: "auth",
|
|
97
|
+
src: "auth",
|
|
98
|
+
input: ({ context }) => ({ instance: context.instance }),
|
|
99
|
+
onSnapshot: [
|
|
100
|
+
{
|
|
101
|
+
guard: {
|
|
102
|
+
type: "hasTag",
|
|
103
|
+
params: ({ event }) => ({
|
|
104
|
+
hasTag: event.snapshot.hasTag("authenticated"),
|
|
105
|
+
}),
|
|
106
|
+
},
|
|
107
|
+
actions: [{ type: "raiseAuthReady" }],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
guard: {
|
|
111
|
+
type: "hasTag",
|
|
112
|
+
params: ({ event }) => ({
|
|
113
|
+
hasTag: event.snapshot.hasTag("error"),
|
|
114
|
+
}),
|
|
115
|
+
},
|
|
116
|
+
actions: [{ type: "raiseAuthFailed" }],
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: "telemetry",
|
|
122
|
+
systemId: "telemetry",
|
|
123
|
+
src: "telemetry",
|
|
124
|
+
input: ({ context }) => ({
|
|
125
|
+
instance: context.instance,
|
|
126
|
+
...context.userProperties,
|
|
127
|
+
}),
|
|
128
|
+
onSnapshot: [
|
|
129
|
+
{
|
|
130
|
+
guard: {
|
|
131
|
+
type: "hasTag",
|
|
132
|
+
params: ({ event }) => ({
|
|
133
|
+
hasTag: event.snapshot.hasTag("telemetry-resolved"),
|
|
134
|
+
}),
|
|
135
|
+
},
|
|
136
|
+
actions: [{ type: "raiseTelemetryReady" }],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: "system-preferences",
|
|
142
|
+
systemId: "system-preferences",
|
|
143
|
+
src: "systemPreferences",
|
|
144
|
+
input: ({ context }) => ({ adapter: context.systemPreferencesAdapter }),
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
states: {
|
|
148
|
+
booting: {
|
|
149
|
+
initial: "auth",
|
|
150
|
+
states: {
|
|
151
|
+
auth: {
|
|
152
|
+
on: {
|
|
153
|
+
"boot.auth.ready": {
|
|
154
|
+
target: "telemetry",
|
|
155
|
+
actions: [{ type: "startTelemetry" }],
|
|
156
|
+
},
|
|
157
|
+
"boot.auth.failed": { target: "error" },
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
telemetry: {
|
|
161
|
+
on: {
|
|
162
|
+
"boot.telemetry.ready": { target: "done" },
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
error: {},
|
|
166
|
+
done: { type: "final" },
|
|
167
|
+
},
|
|
168
|
+
onDone: { target: "running" },
|
|
169
|
+
},
|
|
170
|
+
running: {
|
|
171
|
+
type: "parallel",
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Creates a set of default options for the OS machine. Forwards the workbench
|
|
178
|
+
* user properties to the machine's input and wires the structured-logging
|
|
179
|
+
* `inspect` callback. Browser-specific concerns (color-scheme seeding,
|
|
180
|
+
* persistence) live in the {@link SystemPreferencesAdapter} the host passes
|
|
181
|
+
* via `systemPreferencesAdapter`.
|
|
182
|
+
* @public
|
|
183
|
+
*/
|
|
184
|
+
export function createOSOptions(input: OSInput) {
|
|
185
|
+
return {
|
|
186
|
+
id: "os",
|
|
187
|
+
input,
|
|
188
|
+
inspect,
|
|
189
|
+
} satisfies ActorOptions<typeof os>;
|
|
190
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { assign, fromCallback, sendTo, setup } from "xstate";
|
|
2
|
+
|
|
3
|
+
type ColorScheme = "light" | "dark";
|
|
4
|
+
|
|
5
|
+
type ColorSchemePreference = "system" | ColorScheme;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The system-preferences machine's context. Consumers read fields directly
|
|
9
|
+
* (e.g. via `useSelector`) rather than going through a resolver utility —
|
|
10
|
+
* the action handlers keep `colorScheme` in sync with `preferredColorScheme`
|
|
11
|
+
* and `osColorScheme`.
|
|
12
|
+
*
|
|
13
|
+
* Note: `colorScheme` is intentionally stored as derived state in context so
|
|
14
|
+
* consumers can read the resolved value directly without a utility. The
|
|
15
|
+
* action handlers below are the single source of truth for the resolution.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export type SystemPreferencesContext = {
|
|
19
|
+
/** User's color scheme choice; `system` defers to the OS preference. */
|
|
20
|
+
preferredColorScheme: ColorSchemePreference;
|
|
21
|
+
/** Last reported OS color scheme. Used to resolve `colorScheme` when the
|
|
22
|
+
* preference is `system`. */
|
|
23
|
+
osColorScheme: ColorScheme;
|
|
24
|
+
/** Resolved color scheme — what the UI should render. Recomputed by the
|
|
25
|
+
* action handlers whenever an input changes. */
|
|
26
|
+
colorScheme: ColorScheme;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const DEFAULT_PREFERRED_COLOR_SCHEME: ColorSchemePreference = "system";
|
|
30
|
+
const DEFAULT_OS_COLOR_SCHEME: ColorScheme = "light";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Events the system-preferences machine accepts. Both originate from the
|
|
34
|
+
* host's adapter — `preferredColorScheme.set` is also forwarded back to it
|
|
35
|
+
* so it can persist the user's choice.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export type SystemPreferencesEvent =
|
|
39
|
+
| {
|
|
40
|
+
type: "preferredColorScheme.set";
|
|
41
|
+
preferredColorScheme: ColorSchemePreference;
|
|
42
|
+
}
|
|
43
|
+
| {
|
|
44
|
+
type: "osColorScheme.set";
|
|
45
|
+
osColorScheme: ColorScheme;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Adapter interface the host implements to give the system-preferences
|
|
50
|
+
* machine access to the user's environment (OS color scheme, persistent
|
|
51
|
+
* storage, cross-context change notifications).
|
|
52
|
+
*
|
|
53
|
+
* The package owns all orchestration — synchronous seeding, idempotent
|
|
54
|
+
* persistence, mapping cleared storage to `"system"`. The host's
|
|
55
|
+
* implementation is pure DOM/storage glue: read, write, subscribe.
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export type SystemPreferencesAdapter = {
|
|
59
|
+
/** Read the user's stored preference. `null` means "no preference set"
|
|
60
|
+
* (the machine treats this as `"system"`). */
|
|
61
|
+
getPreferredColorScheme: () => ColorSchemePreference | null;
|
|
62
|
+
/** Write the user's preference to persistent storage. */
|
|
63
|
+
persistPreferredColorScheme: (value: ColorSchemePreference) => void;
|
|
64
|
+
/** Subscribe to cross-context preference changes (e.g. another tab
|
|
65
|
+
* writing to the shared storage). The callback receives the new stored
|
|
66
|
+
* value (or `null` if it was cleared). Returns an unsubscribe function. */
|
|
67
|
+
subscribePreferredColorScheme: (
|
|
68
|
+
callback: (next: ColorSchemePreference | null) => void,
|
|
69
|
+
) => () => void;
|
|
70
|
+
/** Read the current OS-detected color scheme. */
|
|
71
|
+
getOsColorScheme: () => ColorScheme;
|
|
72
|
+
/** Subscribe to OS color-scheme changes (e.g. user flipping dark mode).
|
|
73
|
+
* Returns an unsubscribe function. */
|
|
74
|
+
subscribeOsColorScheme: (callback: (next: ColorScheme) => void) => () => void;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const resolveColorScheme = (
|
|
78
|
+
preferred: ColorSchemePreference,
|
|
79
|
+
osColorScheme: ColorScheme,
|
|
80
|
+
): ColorScheme => (preferred === "system" ? osColorScheme : preferred);
|
|
81
|
+
|
|
82
|
+
// Internal bridge actor — subscribes to the host's adapter for runtime
|
|
83
|
+
// changes and persists user-driven preference changes when the parent
|
|
84
|
+
// forwards them. No-op if the host didn't pass an adapter (SSR, tests).
|
|
85
|
+
type AdapterBridgeReceiveEvent = Extract<
|
|
86
|
+
SystemPreferencesEvent,
|
|
87
|
+
{ type: "preferredColorScheme.set" }
|
|
88
|
+
>;
|
|
89
|
+
|
|
90
|
+
const adapterBridgeLogic = fromCallback<
|
|
91
|
+
AdapterBridgeReceiveEvent,
|
|
92
|
+
SystemPreferencesAdapter | undefined,
|
|
93
|
+
SystemPreferencesEvent
|
|
94
|
+
>(({ input: adapter, sendBack, receive }) => {
|
|
95
|
+
if (!adapter) return;
|
|
96
|
+
|
|
97
|
+
const unsubscribePreferredColorScheme = adapter.subscribePreferredColorScheme(
|
|
98
|
+
(next) => {
|
|
99
|
+
// Falls back to `"system"` so an external clear (another tab deleting
|
|
100
|
+
// the key) resets the preference rather than silently keeping the
|
|
101
|
+
// previous value.
|
|
102
|
+
sendBack({
|
|
103
|
+
type: "preferredColorScheme.set",
|
|
104
|
+
preferredColorScheme: next ?? "system",
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const unsubscribeOs = adapter.subscribeOsColorScheme((next) => {
|
|
110
|
+
sendBack({ type: "osColorScheme.set", osColorScheme: next });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
receive((event) => {
|
|
114
|
+
// Idempotent: skip writes that match the current stored value so a
|
|
115
|
+
// `preferredColorScheme.set` originating from cross-context change
|
|
116
|
+
// doesn't loop back into another write.
|
|
117
|
+
if (adapter.getPreferredColorScheme() === event.preferredColorScheme)
|
|
118
|
+
return;
|
|
119
|
+
|
|
120
|
+
adapter.persistPreferredColorScheme(event.preferredColorScheme);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
unsubscribePreferredColorScheme();
|
|
125
|
+
unsubscribeOs();
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
export const systemPreferencesLogic = setup({
|
|
133
|
+
types: {
|
|
134
|
+
input: {} as { adapter?: SystemPreferencesAdapter },
|
|
135
|
+
context: {} as SystemPreferencesContext & {
|
|
136
|
+
adapter: SystemPreferencesAdapter | undefined;
|
|
137
|
+
},
|
|
138
|
+
events: {} as SystemPreferencesEvent,
|
|
139
|
+
},
|
|
140
|
+
actors: {
|
|
141
|
+
adapterBridge: adapterBridgeLogic,
|
|
142
|
+
},
|
|
143
|
+
actions: {
|
|
144
|
+
setPreferredColorScheme: assign({
|
|
145
|
+
preferredColorScheme: (
|
|
146
|
+
_,
|
|
147
|
+
params: { preferredColorScheme: ColorSchemePreference },
|
|
148
|
+
) => params.preferredColorScheme,
|
|
149
|
+
colorScheme: (
|
|
150
|
+
{ context },
|
|
151
|
+
params: { preferredColorScheme: ColorSchemePreference },
|
|
152
|
+
) =>
|
|
153
|
+
resolveColorScheme(params.preferredColorScheme, context.osColorScheme),
|
|
154
|
+
}),
|
|
155
|
+
setOsColorScheme: assign({
|
|
156
|
+
osColorScheme: (_, params: { osColorScheme: ColorScheme }) =>
|
|
157
|
+
params.osColorScheme,
|
|
158
|
+
colorScheme: ({ context }, params: { osColorScheme: ColorScheme }) =>
|
|
159
|
+
resolveColorScheme(context.preferredColorScheme, params.osColorScheme),
|
|
160
|
+
}),
|
|
161
|
+
},
|
|
162
|
+
}).createMachine({
|
|
163
|
+
id: "system-preferences",
|
|
164
|
+
initial: "ready",
|
|
165
|
+
context: ({ input }) => {
|
|
166
|
+
const adapter = input.adapter;
|
|
167
|
+
const preferredColorScheme =
|
|
168
|
+
adapter?.getPreferredColorScheme() ?? DEFAULT_PREFERRED_COLOR_SCHEME;
|
|
169
|
+
const osColorScheme =
|
|
170
|
+
adapter?.getOsColorScheme() ?? DEFAULT_OS_COLOR_SCHEME;
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
adapter,
|
|
174
|
+
preferredColorScheme,
|
|
175
|
+
osColorScheme,
|
|
176
|
+
colorScheme: resolveColorScheme(preferredColorScheme, osColorScheme),
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
invoke: {
|
|
180
|
+
id: "adapter",
|
|
181
|
+
src: "adapterBridge",
|
|
182
|
+
input: ({ context }) => context.adapter,
|
|
183
|
+
},
|
|
184
|
+
states: {
|
|
185
|
+
ready: {
|
|
186
|
+
on: {
|
|
187
|
+
"preferredColorScheme.set": {
|
|
188
|
+
actions: [
|
|
189
|
+
{
|
|
190
|
+
type: "setPreferredColorScheme",
|
|
191
|
+
params: ({ event }) => ({
|
|
192
|
+
preferredColorScheme: event.preferredColorScheme,
|
|
193
|
+
}),
|
|
194
|
+
},
|
|
195
|
+
// Forward to the adapter bridge so it can persist the user's
|
|
196
|
+
// choice. The bridge guards against redundant writes, so
|
|
197
|
+
// round-tripping a `preferredColorScheme.set` it sourced itself
|
|
198
|
+
// (e.g. from a `storage` event in another tab) is a no-op.
|
|
199
|
+
sendTo("adapter", ({ event }) => event),
|
|
200
|
+
],
|
|
201
|
+
},
|
|
202
|
+
"osColorScheme.set": {
|
|
203
|
+
actions: [
|
|
204
|
+
{
|
|
205
|
+
type: "setOsColorScheme",
|
|
206
|
+
params: ({ event }) => ({
|
|
207
|
+
osColorScheme: event.osColorScheme,
|
|
208
|
+
}),
|
|
209
|
+
},
|
|
210
|
+
],
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { getClient, type SanityInstance } from "@sanity/sdk";
|
|
2
|
+
import {
|
|
3
|
+
type ConsentStatus,
|
|
4
|
+
createBatchedStore,
|
|
5
|
+
createSessionId,
|
|
6
|
+
type TelemetryEvent,
|
|
7
|
+
type TelemetryStore,
|
|
8
|
+
} from "@sanity/telemetry";
|
|
9
|
+
import { assign, fromPromise, setup } from "xstate";
|
|
10
|
+
|
|
11
|
+
import type { OSBaseInput } from "./root.machine";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export type WorkbenchUserProperties = {
|
|
17
|
+
version: string;
|
|
18
|
+
organizationId: string;
|
|
19
|
+
environment: string;
|
|
20
|
+
userAgent: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export interface TelemetryInput extends OSBaseInput, WorkbenchUserProperties {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* TODO: this shouldn't be unique to the telemetry machine,
|
|
30
|
+
* remove this and set it globally with a single client actor.
|
|
31
|
+
*/
|
|
32
|
+
const TELEMETRY_API_VERSION = "2024-11-12";
|
|
33
|
+
/**
|
|
34
|
+
* 30 seconds, in milliseconds, is the default flush interval
|
|
35
|
+
* for the telemetry store.
|
|
36
|
+
*/
|
|
37
|
+
const FLUSH_INTERVAL_MS = 30_000;
|
|
38
|
+
|
|
39
|
+
type ConsentResult = { status: ConsentStatus };
|
|
40
|
+
|
|
41
|
+
const checkConsentLogic = fromPromise<
|
|
42
|
+
ConsentResult,
|
|
43
|
+
{ instance: SanityInstance }
|
|
44
|
+
>(async ({ input, signal }) => {
|
|
45
|
+
try {
|
|
46
|
+
const client = getClient(input.instance, {
|
|
47
|
+
apiVersion: TELEMETRY_API_VERSION,
|
|
48
|
+
});
|
|
49
|
+
return await client.request<ConsentResult>({
|
|
50
|
+
uri: "/intake/telemetry-status",
|
|
51
|
+
tag: "telemetry-consent",
|
|
52
|
+
signal,
|
|
53
|
+
});
|
|
54
|
+
} catch {
|
|
55
|
+
return { status: "undetermined" } satisfies ConsentResult;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
type TelemetryContext = {
|
|
60
|
+
instance: SanityInstance;
|
|
61
|
+
store: TelemetryStore<WorkbenchUserProperties> | null;
|
|
62
|
+
userProperties: WorkbenchUserProperties;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
type TelemetryMachineEvent =
|
|
66
|
+
| { type: "telemetry.start" }
|
|
67
|
+
| { type: "telemetry.stop" };
|
|
68
|
+
|
|
69
|
+
export const telemetryLogic = setup({
|
|
70
|
+
types: {
|
|
71
|
+
input: {} as TelemetryInput,
|
|
72
|
+
context: {} as TelemetryContext,
|
|
73
|
+
events: {} as TelemetryMachineEvent,
|
|
74
|
+
},
|
|
75
|
+
actors: {
|
|
76
|
+
checkConsent: checkConsentLogic,
|
|
77
|
+
},
|
|
78
|
+
guards: {
|
|
79
|
+
isConsentGranted: (_, params: { status: ConsentStatus }) =>
|
|
80
|
+
params.status === "granted",
|
|
81
|
+
},
|
|
82
|
+
actions: {
|
|
83
|
+
createStore: assign({
|
|
84
|
+
store: ({ context }) => {
|
|
85
|
+
const sessionId = createSessionId();
|
|
86
|
+
const client = getClient(context.instance, {
|
|
87
|
+
apiVersion: TELEMETRY_API_VERSION,
|
|
88
|
+
});
|
|
89
|
+
const store = createBatchedStore<WorkbenchUserProperties>(sessionId, {
|
|
90
|
+
flushInterval: FLUSH_INTERVAL_MS,
|
|
91
|
+
resolveConsent: () =>
|
|
92
|
+
client.request<{ status: ConsentStatus }>({
|
|
93
|
+
uri: "/intake/telemetry-status",
|
|
94
|
+
tag: "telemetry-consent",
|
|
95
|
+
}),
|
|
96
|
+
sendEvents: (batch: TelemetryEvent[]) =>
|
|
97
|
+
client.request({
|
|
98
|
+
uri: "/intake/batch",
|
|
99
|
+
method: "POST",
|
|
100
|
+
body: { batch },
|
|
101
|
+
tag: "telemetry.batch",
|
|
102
|
+
}),
|
|
103
|
+
sendBeacon: (batch: TelemetryEvent[]) => {
|
|
104
|
+
if (typeof navigator === "undefined") {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return navigator.sendBeacon(
|
|
108
|
+
client.getUrl("/intake/batch"),
|
|
109
|
+
JSON.stringify({ batch }),
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
store.logger.updateUserProperties(context.userProperties);
|
|
114
|
+
return store;
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
teardownStore: ({ context }) => {
|
|
118
|
+
context.store?.end();
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
}).createMachine({
|
|
122
|
+
id: "telemetry",
|
|
123
|
+
initial: "idle",
|
|
124
|
+
context: ({ input }) => ({
|
|
125
|
+
instance: input.instance,
|
|
126
|
+
store: null,
|
|
127
|
+
userProperties: {
|
|
128
|
+
version: input.version,
|
|
129
|
+
organizationId: input.organizationId,
|
|
130
|
+
environment: input.environment,
|
|
131
|
+
userAgent: input.userAgent,
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
states: {
|
|
135
|
+
idle: {
|
|
136
|
+
on: {
|
|
137
|
+
"telemetry.start": {
|
|
138
|
+
target: "checkingConsent",
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
checkingConsent: {
|
|
143
|
+
invoke: {
|
|
144
|
+
src: "checkConsent",
|
|
145
|
+
input: ({ context }) => ({
|
|
146
|
+
instance: context.instance,
|
|
147
|
+
}),
|
|
148
|
+
onDone: [
|
|
149
|
+
{
|
|
150
|
+
guard: {
|
|
151
|
+
type: "isConsentGranted",
|
|
152
|
+
params: ({ event }) => ({
|
|
153
|
+
status: event.output.status,
|
|
154
|
+
}),
|
|
155
|
+
},
|
|
156
|
+
actions: [{ type: "createStore" }],
|
|
157
|
+
target: "active",
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
target: "denied",
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
active: {
|
|
166
|
+
tags: ["telemetry-resolved"],
|
|
167
|
+
exit: [{ type: "teardownStore" }],
|
|
168
|
+
on: {
|
|
169
|
+
"telemetry.stop": { target: "stopped" },
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
stopped: {
|
|
173
|
+
type: "final",
|
|
174
|
+
},
|
|
175
|
+
denied: {
|
|
176
|
+
tags: ["telemetry-resolved"],
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
});
|