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.9 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { loaders } from 'kea-loaders'

import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import type { TeamPublicType, TeamType } from '../../types'

export type ConfirmationMessageForm = {
    message: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagConfirmationSettingsLogicValues {
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    confirmationMessage: string | null
    confirmationMessageForm: ConfirmationMessageForm
    confirmationMessageFormAllErrors: Record<string, any>
    confirmationMessageFormChanged: boolean
    confirmationMessageFormErrors: DeepPartialMap<ConfirmationMessageForm, ValidationErrorType>
    confirmationMessageFormHasErrors: boolean
    confirmationMessageFormManualErrors: Record<string, any>
    confirmationMessageFormTouched: boolean
    confirmationMessageFormTouches: Record<string, boolean>
    confirmationMessageFormValidationErrors: DeepPartialMap<ConfirmationMessageForm, ValidationErrorType>
    confirmationMessageLoading: boolean
    isConfirmationMessageFormSubmitting: boolean
    isConfirmationMessageFormValid: boolean
    showConfirmationMessageFormErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagConfirmationSettingsLogicActions {
    loadCurrentTeamSuccess: (
        currentTeam: TeamPublicType | null,
        payload?: any
    ) => {
        currentTeam: TeamPublicType | null
        payload?: any
    } // teamLogic
    updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic
    updateCurrentTeamSuccess: (
        currentTeam: TeamPublicType | TeamType,
        payload?: Partial<TeamType> | undefined
    ) => {
        currentTeam: TeamPublicType | TeamType
        payload?: Partial<TeamType>
    } // teamLogic
    resetConfirmationMessageForm: (values?: ConfirmationMessageForm) => {
        values?: ConfirmationMessageForm
    }
    setConfirmationMessageFormManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setConfirmationMessageFormValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setConfirmationMessageFormValues: (values: DeepPartial<ConfirmationMessageForm>) => {
        values: DeepPartial<ConfirmationMessageForm>
    }
    submitConfirmationMessageForm: () => {
        value: boolean
    }
    submitConfirmationMessageFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitConfirmationMessageFormRequest: (confirmationMessageForm: ConfirmationMessageForm) => {
        confirmationMessageForm: ConfirmationMessageForm
    }
    submitConfirmationMessageFormSuccess: (confirmationMessageForm: ConfirmationMessageForm) => {
        confirmationMessageForm: ConfirmationMessageForm
    }
    touchConfirmationMessageFormField: (key: string) => {
        key: string
    }
    updateConfirmationMessage: (data: ConfirmationMessageForm) => ConfirmationMessageForm
    updateConfirmationMessageFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    updateConfirmationMessageSuccess: (
        confirmationMessage: string,
        payload?: ConfirmationMessageForm
    ) => {
        confirmationMessage: string
        payload?: ConfirmationMessageForm
    }
}

export type featureFlagConfirmationSettingsLogicType = MakeLogicType<
    featureFlagConfirmationSettingsLogicValues,
    featureFlagConfirmationSettingsLogicActions
>

export const featureFlagConfirmationSettingsLogic = kea<featureFlagConfirmationSettingsLogicType>([
    path(['scenes', 'feature-flags', 'featureFlagConfirmationSettingsLogic']),

    connect(() => ({
        values: [teamLogic, ['currentTeam']],
        actions: [teamLogic, ['updateCurrentTeam', 'loadCurrentTeamSuccess', 'updateCurrentTeamSuccess']],
    })),

    loaders(() => ({
        confirmationMessage: {
            __default: null as string | null,
            updateConfirmationMessage: async (data: ConfirmationMessageForm) => {
                await teamLogic.asyncActions.updateCurrentTeam({
                    feature_flag_confirmation_message: data.message,
                })
                lemonToast.success('Confirmation message saved')
                return data.message
            },
        },
    })),

    // isSubmitting is automatically handled by the kea-loaders
    // Use confirmationMessageLoading instead

    forms(({ actions }) => ({
        confirmationMessageForm: {
            defaults: { message: '' } as ConfirmationMessageForm,
            submit: ({ message }) => {
                actions.updateConfirmationMessage({ message })
            },
        },
    })),

    listeners(({ actions }) => ({
        updateCurrentTeamSuccess: ({ currentTeam }) => {
            // Update form value when team data changes
            const message = currentTeam?.feature_flag_confirmation_message || ''
            actions.setConfirmationMessageFormValue('message', message)
        },
        loadCurrentTeamSuccess: ({ currentTeam }) => {
            // Initialize form when team loads
            const message = currentTeam?.feature_flag_confirmation_message || ''
            actions.setConfirmationMessageFormValue('message', message)
        },
    })),

    afterMount(({ actions, values }) => {
        // Initialize form with current team value if already loaded
        if (values.currentTeam) {
            const message = values.currentTeam.feature_flag_confirmation_message || ''
            actions.setConfirmationMessageFormValue('message', message)
        }
    }),
])