Line data Source code
1 : import type {
2 : SsoFlow,
3 : SsoRequiredPresentation,
4 : SsoRequiredTrigger,
5 : SsoSignInFailureReason,
6 : SsoSurface
7 : } from '@comfyorg/account-core/telemetry'
8 : import {
9 : createSsoFlow,
10 : SSO_TELEMETRY_EVENT,
11 : ssoFlowStore
12 : } from '@comfyorg/account-core/telemetry'
13 :
14 : import { useTelemetry } from '@comfyorg/telemetry'
15 :
16 : /** The attempt that just signed in, until its first workspace opens. */
17 2356 : let signedInFlow: SsoFlow | undefined
18 :
19 0 : function flowProperties({ flowId, surface }: SsoFlow) {
20 0 : return { surface, flow_id: flowId }
21 : }
22 :
23 : /**
24 : * Starts a new attempt each time SSO is required. A `redirect` leaves for SSO
25 : * without a screen, so its attempt is already continued.
26 : */
27 0 : export function trackSsoRequiredShown(
28 : surface: SsoSurface,
29 : trigger: SsoRequiredTrigger,
30 : presentation: SsoRequiredPresentation
31 : ): void {
32 0 : const telemetry = useTelemetry()
33 0 : if (!telemetry) return
34 0 : const flow = ssoFlowStore.start(
35 : trigger === 'customer_create' ? 'cloud_customer_create' : surface,
36 : presentation === 'redirect'
37 : )
38 0 : telemetry.trackSsoEvent({
39 : name: SSO_TELEMETRY_EVENT.requiredShown,
40 : properties: { ...flowProperties(flow), trigger, presentation }
41 : })
42 : }
43 :
44 0 : export function trackSsoContinueClicked(surface: SsoSurface): void {
45 0 : const telemetry = useTelemetry()
46 0 : if (!telemetry) return
47 0 : const flow = ssoFlowStore.markContinued(surface)
48 0 : telemetry.trackSsoEvent({
49 : name: SSO_TELEMETRY_EVENT.continueClicked,
50 : properties: flowProperties(flow)
51 : })
52 : }
53 :
54 0 : export function trackSsoSignInFailed(
55 : reason: SsoSignInFailureReason,
56 : surface: SsoSurface
57 : ): void {
58 0 : const telemetry = useTelemetry()
59 0 : if (!telemetry) return
60 0 : const flow = ssoFlowStore.finish() ?? createSsoFlow(surface)
61 0 : telemetry.trackSsoEvent({
62 : name: SSO_TELEMETRY_EVENT.signInFailed,
63 : properties: { ...flowProperties(flow), reason }
64 : })
65 : }
66 :
67 : /** An SSO session after an attempt this tab continued is that attempt's sign-in. */
68 0 : export function trackSsoSignInCompleted(): void {
69 0 : const flow = ssoFlowStore.finish()
70 0 : if (!flow?.continued) return
71 0 : signedInFlow = flow
72 0 : useTelemetry()?.trackSsoEvent({
73 : name: SSO_TELEMETRY_EVENT.signInCompleted,
74 : properties: flowProperties(flow)
75 : })
76 : }
77 :
78 : /** The SSO-required screen closed without the person continuing. */
79 0 : export function abandonSsoFlow(): void {
80 0 : ssoFlowStore.abandon()
81 : }
82 :
83 : /** Another sign-in method completed, so no SSO attempt is in flight. */
84 1 : export function endSsoFlow(): void {
85 1 : ssoFlowStore.finish()
86 : }
87 :
88 4 : export function trackSsoWorkspaceLanded(
89 : landedInDefaultWorkspace: boolean
90 : ): void {
91 4 : const flow = signedInFlow
92 4 : signedInFlow = undefined
93 4 : if (!flow) return
94 4 : useTelemetry()?.trackSsoEvent({
95 : name: SSO_TELEMETRY_EVENT.workspaceLanded,
96 : properties: {
97 : ...flowProperties(flow),
98 : landed_in_default_workspace: landedInDefaultWorkspace
99 : }
100 : })
101 : }
|