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.

3.8 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'

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

import { ApprovalPolicy, AvailableFeature } from '~/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface approvalsGateLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
    activePolicies: ApprovalPolicy[]
    activePoliciesLoading: boolean
    isApprovalRequired: (actionKey: string) => boolean
    isApprovalsFeatureEnabled: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface approvalsGateLogicActions {
    loadActivePolicies: () => any
    loadActivePoliciesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadActivePoliciesSuccess: (
        activePolicies: ApprovalPolicy[],
        payload?: any
    ) => {
        activePolicies: ApprovalPolicy[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface approvalsGateLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        isApprovalsFeatureEnabled: (
            hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean
        ) => boolean
        isApprovalRequired: (
            isApprovalsFeatureEnabled: boolean,
            activePolicies: ApprovalPolicy[]
        ) => (actionKey: string) => boolean
    }
}

export type approvalsGateLogicType = MakeLogicType<
    approvalsGateLogicValues,
    approvalsGateLogicActions,
    Record<string, any>,
    approvalsGateLogicMeta
>

export const approvalsGateLogic = kea<approvalsGateLogicType>([
    path(['lib', 'approvals', 'approvalsGateLogic']),

    connect(() => ({
        values: [userLogic, ['hasAvailableFeature'], teamLogic, ['currentTeamIdStrict']],
    })),

    loaders(({ values }) => ({
        activePolicies: [
            [] as ApprovalPolicy[],
            {
                loadActivePolicies: async () => {
                    if (!values.hasAvailableFeature(AvailableFeature.APPROVALS)) {
                        return []
                    }

                    try {
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use approvalPoliciesList() from 'products/platform_features/frontend/generated/api' instead.
                        const response = await api.get(`api/projects/${values.currentTeamIdStrict}/approval_policies/`)
                        return (response.results || []).filter((p: ApprovalPolicy) => p.enabled)
                    } catch {
                        return []
                    }
                },
            },
        ],
    })),

    selectors({
        isApprovalsFeatureEnabled: [
            (s) => [s.hasAvailableFeature],
            (hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean): boolean =>
                hasAvailableFeature(AvailableFeature.APPROVALS),
        ],

        isApprovalRequired: [
            (s) => [s.isApprovalsFeatureEnabled, s.activePolicies],
            (isEnabled: boolean, policies: ApprovalPolicy[]) =>
                (actionKey: string): boolean => {
                    if (!isEnabled) {
                        return false
                    }
                    return policies.some((p: ApprovalPolicy) => p.action_key === actionKey && p.enabled)
                },
        ],
    }),

    afterMount(({ actions }) => {
        actions.loadActivePolicies()
    }),
])