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.8 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import { ApiError } from 'lib/api-error'
import { teamLogic } from 'scenes/teamLogic'

import { cohortsUsedInRetrieve } from 'products/cohorts/frontend/generated/api'

import { CohortUsedInSection, cohortUsedInSections } from './cohortUsedIn'

export interface CohortDeleteDialogLogicProps {
    cohortId: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortDeleteDialogLogicValues {
    currentProjectId: number | string // teamLogic
    blockers: CohortUsedInSection[] | null
    blockersLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortDeleteDialogLogicActions {
    loadBlockers: () => any
    loadBlockersFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadBlockersSuccess: (
        blockers: CohortUsedInSection[],
        payload?: any
    ) => {
        blockers: CohortUsedInSection[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortDeleteDialogLogicMeta {
    key: number
}

export type cohortDeleteDialogLogicType = MakeLogicType<
    cohortDeleteDialogLogicValues,
    cohortDeleteDialogLogicActions,
    CohortDeleteDialogLogicProps,
    cohortDeleteDialogLogicMeta
>

export const cohortDeleteDialogLogic = kea<cohortDeleteDialogLogicType>([
    path(['scenes', 'cohorts', 'cohortDeleteDialogLogic']),
    props({} as CohortDeleteDialogLogicProps),
    key((props) => props.cohortId),
    connect(() => ({
        values: [teamLogic, ['currentProjectId']],
    })),
    loaders(({ props, values }) => ({
        blockers: [
            null as CohortUsedInSection[] | null,
            {
                loadBlockers: async () => {
                    try {
                        const usedIn = await cohortsUsedInRetrieve(String(values.currentProjectId), props.cohortId)
                        return cohortUsedInSections(usedIn, { blockingOnly: true })
                    } catch (error) {
                        // A pre-check the user cannot see must not stand between them and a delete
                        // the server may well accept, so treat a failure as "nothing known".
                        if (!(error instanceof ApiError) || error.status !== 404) {
                            posthog.captureException(error, { feature: 'cohort-delete-precheck' })
                        }
                        return []
                    }
                },
            },
        ],
    })),
    afterMount(({ actions }) => {
        actions.loadBlockers()
    }),
])