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.

4.7 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

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

export interface ResolutionChangeSubject {
    type: 'default' | 'role' | 'member'
    id: string | null
    name: string
}

/** The wire form of the backend's ResolvedAccess */
export interface ResolutionChangeLevel {
    access_level: string
    source: string
    source_subject: string | null
    /** Name of the member or role whose row decides, when one does */
    subject_name: string | null
}

export interface ResolutionChange {
    subject: ResolutionChangeSubject
    scope: 'object' | 'resource'
    resource: string
    project_id: number
    project_name: string
    object_id: string | null
    object_name: string | null
    object_short_id: string | null
    current: ResolutionChangeLevel
    proposed: ResolutionChangeLevel
    direction: 'gains' | 'loses'
}

export interface ResolutionPreviewSummary {
    total: number
    gains: number
    loses: number
    resources: number
    objects: number
}

export interface ResolutionPreviewResponse {
    changes: ResolutionChange[]
    summary: ResolutionPreviewSummary
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface resolutionPreviewLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    accepted: boolean
    acceptedLoading: boolean
    preview: ResolutionPreviewResponse | null
    previewForbidden: boolean
    previewLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface resolutionPreviewLogicActions {
    loadCurrentOrganization: () => any // organizationLogic
    acceptResolution: () => any
    acceptResolutionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    acceptResolutionSuccess: (
        accepted: boolean,
        payload?: any
    ) => {
        accepted: boolean
        payload?: any
    }
    loadPreview: () => any
    loadPreviewFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPreviewSuccess: (
        preview: ResolutionPreviewResponse,
        payload?: any
    ) => {
        preview: ResolutionPreviewResponse
        payload?: any
    }
}

export type resolutionPreviewLogicType = MakeLogicType<resolutionPreviewLogicValues, resolutionPreviewLogicActions>

export const resolutionPreviewLogic = kea<resolutionPreviewLogicType>([
    path([
        'layout',
        'navigation-3000',
        'sidepanel',
        'panels',
        'access_control',
        'ResolutionPreview',
        'resolutionPreviewLogic',
    ]),
    connect(() => ({
        values: [teamLogic, ['currentTeamIdStrict']],
        actions: [organizationLogic, ['loadCurrentOrganization']],
    })),
    loaders(({ values }) => ({
        preview: [
            null as ResolutionPreviewResponse | null,
            {
                loadPreview: async () =>
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                    await api.get<ResolutionPreviewResponse>(
                        `api/projects/${values.currentTeamIdStrict}/access_control_resolution_preview`
                    ),
            },
        ],
        accepted: [
            false,
            {
                acceptResolution: async () => {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                    await api.create(`api/projects/${values.currentTeamIdStrict}/access_control_resolution_accept`)
                    return true
                },
            },
        ],
    })),
    reducers({
        previewForbidden: [
            false,
            {
                loadPreview: () => false,
                loadPreviewFailure: (_, { errorObject }) => errorObject?.status === 403,
            },
        ],
    }),
    listeners(({ actions }) => ({
        // The organization carries the setting the page renders from
        acceptResolutionSuccess: () => actions.loadCurrentOrganization(),
    })),
    afterMount(({ actions }) => {
        actions.loadPreview()
    }),
])