Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.7 KB
Raw Blame
import {
    MakeLogicType,
    actions,
    afterMount,
    connect,
    kea,
    listeners,
    path,
    reducers,
    selectors,
    sharedListeners,
} from 'kea'
import type { BreakPointFunction } from 'kea'

import { gravatarUrl, probeImage } from 'lib/utils/gravatar'
import { userLogic } from 'scenes/userLogic'

import { UserType } from '~/types'

export type GravatarStatus = 'unknown' | 'found' | 'missing'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface profilePictureLogicValues {
    user: UserType | null // userLogic
    gravatarChecking: boolean
    gravatarEmail: string | null
    gravatarRefreshKey: number
    gravatarStatus: GravatarStatus
    usesHedgehogAsProfilePicture: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface profilePictureLogicActions {
    loadUserSuccess: (
        user: UserType | null,
        payload?:
            | {
                  resetOnFailure: boolean | undefined
              }
            | undefined
    ) => {
        payload?: {
            resetOnFailure: boolean | undefined
        }
        user: UserType | null
    } // userLogic
    updateUserSuccess: (
        user: UserType,
        payload?:
            | {
                  failureCallback: (() => void) | undefined
                  successCallback: (() => void) | undefined
                  user: Partial<UserType>
              }
            | undefined
    ) => {
        payload?: {
            failureCallback: (() => void) | undefined
            successCallback: (() => void) | undefined
            user: Partial<UserType>
        }
        user: UserType
    } // userLogic
    checkGravatar: () => {
        value: true
    }
    recheckGravatar: () => {
        refreshKey: number
    }
    setGravatarStatus: (status: GravatarStatus) => {
        status: GravatarStatus
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface profilePictureLogicMeta {
    sharedListeners: {
        probeGravatar: (
            payload: any,
            breakpoint: BreakPointFunction,
            action: {
                type: string
                payload: any
            },
            previousState: any
        ) => void | Promise<void>
        probeGravatarIfUserChanged: (
            payload: any,
            breakpoint: BreakPointFunction,
            action: {
                type: string
                payload: any
            },
            previousState: any
        ) => void | Promise<void>
    }
    __keaTypeGenInternalSelectorTypes: {
        gravatarEmail: (user: UserType | null) => string | null
        usesHedgehogAsProfilePicture: (user: UserType | null) => boolean
    }
}

export type profilePictureLogicType = MakeLogicType<
    profilePictureLogicValues,
    profilePictureLogicActions,
    Record<string, any>,
    profilePictureLogicMeta
>

export const profilePictureLogic = kea<profilePictureLogicType>([
    path(['scenes', 'settings', 'user', 'profilePictureLogic']),
    connect(() => ({
        values: [userLogic, ['user']],
        actions: [userLogic, ['loadUserSuccess', 'updateUserSuccess']],
    })),
    actions({
        checkGravatar: true,
        recheckGravatar: () => ({ refreshKey: Date.now() }),
        setGravatarStatus: (status: GravatarStatus) => ({ status }),
    }),
    reducers({
        gravatarStatus: [
            'unknown' as GravatarStatus,
            {
                checkGravatar: () => 'unknown',
                setGravatarStatus: (_, { status }) => status,
            },
        ],
        gravatarChecking: [
            false,
            {
                checkGravatar: () => true,
                recheckGravatar: () => true,
                setGravatarStatus: () => false,
            },
        ],
        gravatarRefreshKey: [
            0,
            {
                recheckGravatar: (_, { refreshKey }) => refreshKey,
            },
        ],
    }),
    selectors({
        gravatarEmail: [
            (s) => [s.user],
            (user: UserType | null): string | null => user?.email?.trim().toLowerCase() || null,
        ],
        usesHedgehogAsProfilePicture: [
            (s) => [s.user],
            (user: UserType | null): boolean => !!user?.hedgehog_config?.use_as_profile,
        ],
    }),
    sharedListeners(({ values, actions, selectors }) => ({
        probeGravatar: async (_, breakpoint) => {
            const email = values.gravatarEmail
            if (!email) {
                actions.setGravatarStatus('missing')
                return
            }
            if (values.usesHedgehogAsProfilePicture) {
                actions.setGravatarStatus('unknown')
                return
            }
            const loaded = await probeImage(gravatarUrl(email, values.gravatarRefreshKey))
            breakpoint()
            actions.setGravatarStatus(loaded ? 'found' : 'missing')
        },
        probeGravatarIfUserChanged: (_, __, ___, previousState) => {
            const emailChanged = selectors.gravatarEmail(previousState) !== values.gravatarEmail
            const hedgehogChanged =
                selectors.usesHedgehogAsProfilePicture(previousState) !== values.usesHedgehogAsProfilePicture
            if (emailChanged || hedgehogChanged) {
                actions.checkGravatar()
            }
        },
    })),
    listeners(({ sharedListeners }) => ({
        checkGravatar: sharedListeners.probeGravatar,
        recheckGravatar: sharedListeners.probeGravatar,
        loadUserSuccess: sharedListeners.probeGravatarIfUserChanged,
        updateUserSuccess: sharedListeners.probeGravatarIfUserChanged,
    })),
    afterMount(({ actions }) => {
        actions.checkGravatar()
    }),
])