dify/web/context/amplitude-identity-sync.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

62 lines
1.8 KiB
TypeScript

'use client'
import type { GetAccountProfileResponse } from '@dify/contracts/api/console/account/types.gen'
import type { ICurrentWorkspace } from '@/models/common'
import { atom } from 'jotai'
import { atomEffect } from 'jotai-effect'
import { setUserId, setUserProperties } from '@/app/components/base/amplitude'
import { flushRegistrationSuccess } from '@/app/components/base/amplitude/registration-tracking'
import { userProfileAtom } from './account-state'
import { currentWorkspaceAtom } from './workspace-state'
type AmplitudeProperties = Record<string, string | number | boolean>
const amplitudeIdentityAtom = atom<string | undefined>(undefined)
function buildAmplitudeProperties({
currentWorkspace,
userProfile,
}: {
currentWorkspace: ICurrentWorkspace
userProfile: GetAccountProfileResponse
}) {
const properties: AmplitudeProperties = {
email: userProfile.email,
name: userProfile.name,
has_password: userProfile.is_password_set,
}
if (currentWorkspace.id) {
properties.workspace_id = currentWorkspace.id
properties.workspace_name = currentWorkspace.name
properties.workspace_plan = currentWorkspace.plan
properties.workspace_status = currentWorkspace.status
properties.workspace_role = currentWorkspace.role
}
return properties
}
export const amplitudeIdentitySyncAtom = atomEffect((get, set) => {
const userProfile = get(userProfileAtom)
const currentWorkspace = get(currentWorkspaceAtom)
if (!userProfile.id) return
const properties = buildAmplitudeProperties({
currentWorkspace,
userProfile,
})
const identity = JSON.stringify({
userId: userProfile.email,
properties,
})
if (identity === get.peek(amplitudeIdentityAtom)) return
setUserId(userProfile.email)
setUserProperties(properties)
flushRegistrationSuccess()
set(amplitudeIdentityAtom, identity)
})