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 { toParams } from 'lib/utils/url'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'

import { AvailableFeature, ChangeRequest, ChangeRequestState } from '~/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pendingApprovalsLogicValues {
    currentTeamId: number | null // teamLogic
    hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
    actionableChangeRequests: ChangeRequest[]
    actionableCount: number
    unresolvedChangeRequests: ChangeRequest[]
    unresolvedChangeRequestsLoading: boolean
    unresolvedCount: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pendingApprovalsLogicActions {
    loadUnresolvedChangeRequests: () => any
    loadUnresolvedChangeRequestsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadUnresolvedChangeRequestsSuccess: (
        unresolvedChangeRequests: ChangeRequest[],
        payload?: any
    ) => {
        unresolvedChangeRequests: ChangeRequest[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pendingApprovalsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        actionableChangeRequests: (unresolvedChangeRequests: ChangeRequest[]) => ChangeRequest[]
        actionableCount: (actionableChangeRequests: ChangeRequest[]) => number
        unresolvedCount: (unresolvedChangeRequests: ChangeRequest[]) => number
    }
}

export type pendingApprovalsLogicType = MakeLogicType<
    pendingApprovalsLogicValues,
    pendingApprovalsLogicActions,
    Record<string, any>,
    pendingApprovalsLogicMeta
>

export const pendingApprovalsLogic = kea<pendingApprovalsLogicType>([
    path(['scenes', 'approvals', 'pendingApprovalsLogic']),

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

    loaders(({ values }) => ({
        // Fetches both pending and approved-but-not-yet-applied CRs
        unresolvedChangeRequests: [
            [] as ChangeRequest[],
            {
                loadUnresolvedChangeRequests: async () => {
                    if (!values.currentTeamId || !values.hasAvailableFeature(AvailableFeature.APPROVALS)) {
                        return []
                    }

                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use changeRequestsList() from 'products/platform_features/frontend/generated/api' instead.
                    const response = await api.get(
                        `api/projects/${values.currentTeamId}/change_requests?${toParams({ state: 'pending,approved' })}`
                    )
                    return response.results || []
                },
            },
        ],
    })),

    selectors({
        actionableChangeRequests: [
            (s) => [s.unresolvedChangeRequests],
            (changeRequests: ChangeRequest[]): ChangeRequest[] =>
                changeRequests.filter(
                    (cr) => cr.state === ChangeRequestState.Pending && cr.can_approve && !cr.user_decision
                ),
        ],
        actionableCount: [
            (s) => [s.actionableChangeRequests],
            (actionable: ChangeRequest[]): number => actionable.length,
        ],
        unresolvedCount: [
            (s) => [s.unresolvedChangeRequests],
            (unresolved: ChangeRequest[]): number => unresolved.length,
        ],
    }),

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