Line data Source code
1 : import { isCloud } from '@/platform/distribution/types'
2 : import { reportError } from '@comfyorg/telemetry/reportError'
3 :
4 2356 : const REPLAY_KEY = 'Comfy.OnboardingReplay'
5 :
6 : interface ReplayRequest {
7 : ownerId: string
8 : survey: boolean
9 : firstRun: boolean
10 : }
11 :
12 0 : function reportStorageError(
13 : error: unknown,
14 : operation: 'writing' | 'reading' | 'clearing'
15 : ) {
16 0 : reportError(error, {
17 : surface: 'platform',
18 : errorType: `error_${operation}_onboarding_replay_request`
19 : })
20 : }
21 :
22 0 : function isReplayRequest(value: unknown): value is ReplayRequest {
23 0 : if (typeof value !== 'object' || value === null) return false
24 0 : return (
25 : 'survey' in value &&
26 : typeof value.survey === 'boolean' &&
27 : 'firstRun' in value &&
28 : typeof value.firstRun === 'boolean' &&
29 : 'ownerId' in value &&
30 : typeof value.ownerId === 'string'
31 : )
32 : }
33 :
34 0 : function writeReplayRequest(request: ReplayRequest): boolean {
35 0 : try {
36 0 : sessionStorage.setItem(REPLAY_KEY, JSON.stringify(request))
37 0 : return true
38 : } catch (error) {
39 0 : reportStorageError(error, 'writing')
40 0 : return false
41 : }
42 : }
43 :
44 2355 : function readReplayRequest(
45 : ownerId: string | undefined
46 : ): ReplayRequest | undefined {
47 2355 : if (ownerId === undefined) return
48 2355 : try {
49 16 : const stored = sessionStorage.getItem(REPLAY_KEY)
50 16 : if (stored === null) return
51 0 : const request: unknown = JSON.parse(stored)
52 2355 : if (!isReplayRequest(request) || request.ownerId !== ownerId) {
53 0 : sessionStorage.removeItem(REPLAY_KEY)
54 0 : return
55 : }
56 0 : return request
57 : } catch (error) {
58 0 : reportStorageError(error, 'reading')
59 : }
60 : }
61 :
62 0 : function consumeReplayRequest(
63 : gate: 'survey' | 'firstRun',
64 : ownerId: string | undefined
65 : ): void {
66 0 : const request = readReplayRequest(ownerId)
67 0 : if (!request) return
68 0 : const next = { ...request, [gate]: false }
69 0 : try {
70 0 : sessionStorage.setItem(REPLAY_KEY, JSON.stringify(next))
71 : } catch (error) {
72 0 : reportStorageError(error, 'clearing')
73 : }
74 : }
75 :
76 0 : export function isSurveyReplayRequested(ownerId: string | undefined): boolean {
77 0 : return readReplayRequest(ownerId)?.survey === true
78 : }
79 :
80 0 : export function consumeSurveyReplayRequest(ownerId: string | undefined): void {
81 0 : consumeReplayRequest('survey', ownerId)
82 : }
83 :
84 0 : export function restoreSurveyReplayRequest(ownerId: string | undefined): void {
85 0 : const current = readReplayRequest(ownerId)
86 0 : if (!current) return
87 0 : writeReplayRequest({
88 : ownerId: current.ownerId,
89 : survey: true,
90 : firstRun: current.firstRun
91 : })
92 : }
93 :
94 2355 : export function isFirstRunReplayRequested(
95 : ownerId: string | undefined
96 : ): boolean {
97 2355 : return readReplayRequest(ownerId)?.firstRun === true
98 : }
99 :
100 0 : export function consumeFirstRunReplayRequest(
101 : ownerId: string | undefined
102 : ): void {
103 0 : consumeReplayRequest('firstRun', ownerId)
104 : }
105 :
106 0 : export function requestOnboardingReplay(ownerId: string | undefined): boolean {
107 0 : if (!isCloud) return true
108 : if (ownerId === undefined) return false
109 : return writeReplayRequest({
110 : ownerId,
111 : survey: true,
112 : firstRun: true
113 : })
114 : }
115 :
116 1 : export function clearOnboardingReplay(ownerId?: string): void {
117 1 : try {
118 1 : sessionStorage.removeItem(REPLAY_KEY)
119 : } catch (error) {
120 0 : reportStorageError(error, 'clearing')
121 0 : if (ownerId) writeReplayRequest({ ownerId, survey: false, firstRun: false })
122 : }
123 : }
|