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.

2.6 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, path } from 'kea'
import { loaders } from 'kea-loaders'

import { lemonToast } from '@posthog/lemon-ui'

import { ApiConfig } from 'lib/api'
import { userLogic } from 'scenes/userLogic'

import { messagingPreferencesGenerateLinkCreate } from 'products/messaging/frontend/generated/api'

import type { UserType } from '../../../../frontend/src/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface optOutSceneLogicValues {
    user: UserType | null // userLogic
    preferencesUrl: string | null
    preferencesUrlLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface optOutSceneLogicActions {
    loadUnsubscribeLink: () => {
        value: true
    }
    openPreferencesPage: (recipient?: string) => string
    openPreferencesPageFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    openPreferencesPageSuccess: (
        preferencesUrl: string | null,
        payload?: string
    ) => {
        preferencesUrl: string | null
        payload?: string
    }
}

export type optOutSceneLogicType = MakeLogicType<optOutSceneLogicValues, optOutSceneLogicActions>

export const optOutSceneLogic = kea<optOutSceneLogicType>([
    path(['products', 'workflows', 'frontend', 'OptOuts', 'optOutSceneLogic']),
    connect(() => ({
        values: [userLogic, ['user']],
    })),
    actions({
        loadUnsubscribeLink: true,
    }),
    loaders(({ values }) => ({
        preferencesUrl: {
            __default: null as string | null,
            openPreferencesPage: async (recipient?: string): Promise<string | null> => {
                if (!values.user?.email) {
                    lemonToast.error('User email not found')
                    return null
                }

                try {
                    const response = await messagingPreferencesGenerateLinkCreate(
                        String(ApiConfig.getCurrentTeamId()),
                        { recipient: recipient ?? values.user.email }
                    )
                    if (!response.preferences_url) {
                        lemonToast.error('Failed to generate workflows preferences link')
                        return null
                    }
                    window.open(response.preferences_url, '_blank')
                    return response.preferences_url
                } catch {
                    lemonToast.error('Failed to generate workflows preferences link')
                    return null
                }
            },
        },
    })),
])