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.

6.0 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import { readableErrorMessage } from 'lib/api-error'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import {
    workloadTrustPolicyRequestsApproveCreate,
    workloadTrustPolicyRequestsRejectCreate,
    workloadTrustPolicyRequestsRetrieve,
} from '~/generated/core/api'
import type { WorkloadTrustPolicyRequestApi } from '~/generated/core/api.schemas'
import { Breadcrumb } from '~/types'

export interface WorkloadTrustPolicyRequestLogicProps {
    id: string
}

async function resolveRequest(
    resolve: () => Promise<WorkloadTrustPolicyRequestApi>,
    event: string,
    successMessage: string,
    failureMessage: string
): Promise<WorkloadTrustPolicyRequestApi> {
    try {
        const resolved = await resolve()
        posthog.capture(event)
        lemonToast.success(successMessage)
        return resolved
    } catch (error) {
        lemonToast.error(readableErrorMessage(error) ?? failureMessage)
        throw error
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface workloadTrustPolicyRequestLogicValues {
    currentTeamId: number | null // teamLogic
    breadcrumbs: Breadcrumb[]
    policyRequest: WorkloadTrustPolicyRequestApi | null
    policyRequestLoading: boolean
    policyRequestMissing: boolean
    resolving: 'approve' | 'reject' | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface workloadTrustPolicyRequestLogicActions {
    approve: () => any
    approveFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    approveSuccess: (
        policyRequest: WorkloadTrustPolicyRequestApi,
        payload?: any
    ) => {
        policyRequest: WorkloadTrustPolicyRequestApi
        payload?: any
    }
    loadPolicyRequest: () => any
    loadPolicyRequestFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPolicyRequestSuccess: (
        policyRequest: WorkloadTrustPolicyRequestApi,
        payload?: any
    ) => {
        policyRequest: WorkloadTrustPolicyRequestApi
        payload?: any
    }
    reject: () => any
    rejectFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    rejectSuccess: (
        policyRequest: WorkloadTrustPolicyRequestApi,
        payload?: any
    ) => {
        policyRequest: WorkloadTrustPolicyRequestApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface workloadTrustPolicyRequestLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        breadcrumbs: (policyRequest: WorkloadTrustPolicyRequestApi | null) => Breadcrumb[]
    }
}

export type workloadTrustPolicyRequestLogicType = MakeLogicType<
    workloadTrustPolicyRequestLogicValues,
    workloadTrustPolicyRequestLogicActions,
    WorkloadTrustPolicyRequestLogicProps,
    workloadTrustPolicyRequestLogicMeta
>

export const workloadTrustPolicyRequestLogic = kea<workloadTrustPolicyRequestLogicType>([
    path(['scenes', 'workload-trust-policy-request', 'workloadTrustPolicyRequestLogic']),
    props({} as WorkloadTrustPolicyRequestLogicProps),
    key(({ id }) => id),
    connect(() => ({
        values: [teamLogic, ['currentTeamId']],
    })),

    loaders(({ props, values }) => ({
        policyRequest: [
            null as WorkloadTrustPolicyRequestApi | null,
            {
                loadPolicyRequest: async () =>
                    await workloadTrustPolicyRequestsRetrieve(String(values.currentTeamId), props.id),
                approve: async () =>
                    await resolveRequest(
                        () => workloadTrustPolicyRequestsApproveCreate(String(values.currentTeamId), props.id),
                        'workload trust policy request approved',
                        'Request approved. The workload is now trusted.',
                        "Couldn't approve the request. Try again."
                    ),
                reject: async () =>
                    await resolveRequest(
                        () => workloadTrustPolicyRequestsRejectCreate(String(values.currentTeamId), props.id),
                        'workload trust policy request rejected',
                        'Request rejected',
                        "Couldn't reject the request. Try again."
                    ),
            },
        ],
    })),

    reducers({
        resolving: [
            null as 'approve' | 'reject' | null,
            {
                approve: () => 'approve',
                reject: () => 'reject',
                approveSuccess: () => null,
                approveFailure: () => null,
                rejectSuccess: () => null,
                rejectFailure: () => null,
            },
        ],
        policyRequestMissing: [
            false,
            {
                loadPolicyRequest: () => false,
                loadPolicyRequestFailure: () => true,
            },
        ],
    }),

    selectors({
        breadcrumbs: [
            (s) => [s.policyRequest],
            (policyRequest: WorkloadTrustPolicyRequestApi | null): Breadcrumb[] => [
                {
                    key: Scene.Settings,
                    name: 'Trusted workloads',
                    path: urls.settings('environment-trusted-workloads'),
                },
                {
                    key: Scene.WorkloadTrustPolicyRequest,
                    name: policyRequest ? `Request: ${policyRequest.name}` : 'Policy request',
                },
            ],
        ],
    }),

    afterMount(({ actions }) => {
        actions.loadPolicyRequest()
        posthog.capture('workload trust policy request viewed')
    }),
])