Line data Source code
1 : import {
2 : breakpointsTailwind,
3 : createSharedComposable,
4 : until,
5 : useBreakpoints
6 : } from '@vueuse/core'
7 : import { computed, ref, watch } from 'vue'
8 :
9 : import { useSubscription } from '@/platform/billing/subscription/composables/useSubscription'
10 : import { isCloud } from '@/platform/distribution/types'
11 : import {
12 : consumeFirstRunReplayRequest,
13 : isFirstRunReplayRequested
14 : } from '@/platform/onboarding/onboardingReplay'
15 : import { useOnboardingTourStore } from '@/platform/onboarding/onboardingTourStore'
16 : import { useSettingStore } from '@/platform/settings/settingStore'
17 : import { reportError } from '@comfyorg/telemetry/reportError'
18 : import type { StartupOutcome } from '@/platform/workflow/persistence/base/draftTypes'
19 : import type { SharedWorkflowUrlLoadStatus } from '@/platform/workflow/sharing/composables/useSharedWorkflowUrlLoader'
20 : import { useNewUserService } from '@/services/useNewUserService'
21 : import { useAuthStore } from '@/stores/authStore'
22 : import { useCommandStore } from '@/platform/keybindings/commandStore'
23 :
24 : import { useFirstRunTourController } from '@/renderer/extensions/firstRunTour/tour/useFirstRunTourController'
25 :
26 2355 : const STARTUP_DECISION_TIMEOUT_MS = 60_000
27 :
28 : type FirstRunScreenState =
29 : | { phase: 'released' }
30 : | { phase: 'visible' }
31 : | { phase: 'handoff'; handoffs: ReadonlySet<symbol> }
32 :
33 : type FirstRunScreenEvent =
34 : | { type: 'shown' }
35 : | { type: 'dismissed' }
36 : | { type: 'released' }
37 : | { type: 'handoffStarted'; ownership: symbol }
38 : | { type: 'handoffFinished'; ownership: symbol }
39 :
40 0 : function finishFirstRunHandoff(
41 : state: FirstRunScreenState,
42 : ownership: symbol
43 : ): FirstRunScreenState {
44 0 : if (state.phase !== 'handoff' || !state.handoffs.has(ownership)) return state
45 0 : const handoffs = new Set(state.handoffs)
46 0 : handoffs.delete(ownership)
47 0 : return handoffs.size === 0
48 : ? { phase: 'released' }
49 : : { phase: 'handoff', handoffs }
50 : }
51 :
52 1 : function transitionFirstRunScreen(
53 : state: FirstRunScreenState,
54 : event: FirstRunScreenEvent
55 : ): FirstRunScreenState {
56 1 : switch (event.type) {
57 : case 'shown':
58 0 : return { phase: 'visible' }
59 : case 'dismissed':
60 0 : return state.phase === 'visible' ? { phase: 'released' } : state
61 : case 'released':
62 1 : return { phase: 'released' }
63 : case 'handoffStarted':
64 0 : return {
65 : phase: 'handoff',
66 : handoffs: new Set([
67 : ...(state.phase === 'handoff' ? state.handoffs : []),
68 : event.ownership
69 : ])
70 : }
71 : case 'handoffFinished':
72 0 : return finishFirstRunHandoff(state, event.ownership)
73 : }
74 : }
75 :
76 2355 : export const useFirstRunEntry = createSharedComposable(() => {
77 2355 : const authStore = useAuthStore()
78 2355 : const settingStore = useSettingStore()
79 2355 : const firstRunScreen = ref<FirstRunScreenState>({ phase: 'released' })
80 2355 : const startupDecided = ref(false)
81 2355 : let authGeneration = 0
82 : const isDesktopWidth =
83 2355 : useBreakpoints(breakpointsTailwind).greaterOrEqual('md')
84 :
85 2355 : const gettingStartedVisible = computed(
86 : () => firstRunScreen.value.phase === 'visible'
87 : )
88 2355 : const firstRunHoldsScreen = computed(
89 : () => firstRunScreen.value.phase !== 'released'
90 : )
91 :
92 1 : function dispatchFirstRunScreen(event: FirstRunScreenEvent): void {
93 1 : firstRunScreen.value = transitionFirstRunScreen(firstRunScreen.value, event)
94 : }
95 :
96 2355 : watch(
97 : () => authStore.userId,
98 : (userId, previousUserId) => {
99 2 : if (previousUserId === undefined || userId === previousUserId) return
100 1 : authGeneration++
101 1 : dispatchFirstRunScreen({ type: 'released' })
102 1 : void useFirstRunTourController()
103 : .cancelPendingStart()
104 : .catch((error) =>
105 : reportError(error, {
106 : surface: 'platform',
107 : errorType: 'failure_restoring_first_run_renderer_setting',
108 : level: 'warning'
109 : })
110 : )
111 1 : const tourStore = useOnboardingTourStore()
112 1 : if (tourStore.activeTour === 'firstRun') tourStore.postpone()
113 : },
114 : { flush: 'sync' }
115 : )
116 :
117 : type FirstRunDecision = 'getting-started' | 'defer' | 'complete'
118 :
119 2356 : function decideFirstRun(): FirstRunDecision {
120 2356 : if (!isCloud) return 'complete'
121 :
122 : const isNewUser =
123 : isFirstRunReplayRequested(authStore.userId) ||
124 : useNewUserService().isNewUser()
125 : if (isNewUser === false) return 'complete'
126 :
127 : if (!isDesktopWidth.value) return 'defer'
128 : if (!useSubscription().isSubscriptionEnabled()) return 'defer'
129 : if (isNewUser === null) return 'defer'
130 :
131 : return 'getting-started'
132 : }
133 :
134 1 : function isFirstRunCandidate(): boolean {
135 1 : return decideFirstRun() === 'getting-started'
136 : }
137 :
138 2355 : async function handleStartupOutcome(outcome: StartupOutcome) {
139 2355 : try {
140 2355 : await showFirstRunScreen(outcome)
141 : } finally {
142 2355 : if (outcome !== 'url-intent') startupDecided.value = true
143 : }
144 : }
145 :
146 2355 : async function showFirstRunScreen(outcome: StartupOutcome) {
147 2355 : const decision = decideFirstRun()
148 :
149 : const isReplay =
150 : isFirstRunReplayRequested(authStore.userId) &&
151 : decision === 'getting-started'
152 2355 : if (!isReplay) {
153 2355 : if (outcome === 'restored') return
154 3 : if (settingStore.get('Comfy.TutorialCompleted')) return
155 : }
156 :
157 3 : if (outcome === 'url-intent') {
158 2 : if (decision === 'complete') await markTutorialCompleted()
159 2 : return
160 : }
161 :
162 1 : if (decision === 'getting-started') {
163 0 : dispatchFirstRunScreen({ type: 'shown' })
164 0 : consumeFirstRunReplayRequest(authStore.userId)
165 0 : return
166 : }
167 :
168 1 : if (decision === 'complete') await markTutorialCompleted()
169 1 : await useCommandStore().execute('Comfy.BrowseTemplates')
170 : }
171 :
172 2335 : async function handleUrlWorkflow(
173 : outcome: StartupOutcome | undefined,
174 : templateId?: string,
175 : sharedStatus?: SharedWorkflowUrlLoadStatus
176 : ) {
177 2335 : try {
178 2335 : if (!isTourableUrlWorkflow(outcome, templateId, sharedStatus)) return
179 0 : const shareLoaded = isSharedWorkflowLoaded(sharedStatus)
180 0 : const ownerId = authStore.userId
181 : const started = await useFirstRunTourController().beginTour(
182 : shareLoaded ? undefined : templateId,
183 : () => authStore.userId !== ownerId
184 : )
185 2335 : if (!started) return
186 0 : consumeFirstRunReplayRequest(ownerId)
187 0 : await markTutorialCompleted()
188 : } finally {
189 2335 : startupDecided.value = true
190 : }
191 : }
192 :
193 2335 : function isTourableUrlWorkflow(
194 : outcome: StartupOutcome | undefined,
195 : templateId?: string,
196 : sharedStatus?: SharedWorkflowUrlLoadStatus
197 : ): boolean {
198 2335 : if (outcome !== 'url-intent' || !isFirstRunCandidate()) return false
199 2335 : return templateId !== undefined || isSharedWorkflowLoaded(sharedStatus)
200 : }
201 :
202 0 : function isSharedWorkflowLoaded(
203 : status: SharedWorkflowUrlLoadStatus | undefined
204 : ): boolean {
205 0 : return status === 'loaded' || status === 'loaded-without-assets'
206 : }
207 :
208 2355 : let startupDecision: Promise<boolean> | undefined
209 4 : function whenStartupDecided(): Promise<boolean> {
210 4 : if (startupDecided.value) return Promise.resolve(true)
211 2 : startupDecision ??= until(startupDecided).toBe(true, {
212 : timeout: STARTUP_DECISION_TIMEOUT_MS,
213 : throwOnTimeout: false
214 : })
215 2 : return startupDecision
216 : }
217 :
218 3 : async function markTutorialCompleted() {
219 3 : try {
220 3 : await settingStore.set('Comfy.TutorialCompleted', true)
221 : } catch (error) {
222 0 : reportError(error, {
223 : surface: 'platform',
224 : errorType: 'failure_writing_tutorial_completed_setting',
225 : level: 'warning'
226 : })
227 : }
228 : }
229 :
230 0 : async function dismissGettingStarted() {
231 0 : dispatchFirstRunScreen({ type: 'dismissed' })
232 0 : await markTutorialCompleted()
233 : }
234 :
235 0 : async function dismissIntoFirstRunTour(templateId: string): Promise<void> {
236 0 : const ownerId = authStore.userId
237 0 : const ownerGeneration = authGeneration
238 0 : const ownership = Symbol('first-run-tour-handoff')
239 0 : dispatchFirstRunScreen({ type: 'handoffStarted', ownership })
240 0 : try {
241 0 : await dismissGettingStarted()
242 0 : if (authStore.userId !== ownerId || authGeneration !== ownerGeneration)
243 0 : return
244 0 : await useFirstRunTourController().beginTour(
245 : templateId,
246 : () => authStore.userId !== ownerId || authGeneration !== ownerGeneration
247 : )
248 : } finally {
249 0 : dispatchFirstRunScreen({ type: 'handoffFinished', ownership })
250 : }
251 : }
252 :
253 2355 : return {
254 : gettingStartedVisible,
255 : firstRunHoldsScreen,
256 : whenStartupDecided,
257 : handleStartupOutcome,
258 : handleUrlWorkflow,
259 : dismissGettingStarted,
260 : dismissIntoFirstRunTour
261 : }
262 : })
|