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.6 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { OrganizationMemberType } from '~/types'

import { membersProjectAccessRetrieve } from 'products/access_control/frontend/generated/api'
import type { MemberProjectAccessEntryApi } from 'products/access_control/frontend/generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface memberProjectAccessLogicValues {
    modalMember: OrganizationMemberType | null
    modalProjects: MemberProjectAccessEntryApi[] | null
    projectAccess: Record<string, MemberProjectAccessEntryApi[]> | null
    projectAccessLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface memberProjectAccessLogicActions {
    closeProjectAccessModal: () => {
        value: true
    }
    loadProjectAccess: () => {
        value: true
    }
    loadProjectAccessFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadProjectAccessSuccess: (
        projectAccess: {
            [k: string]: MemberProjectAccessEntryApi[]
        },
        payload?: {
            value: true
        }
    ) => {
        projectAccess: {
            [k: string]: MemberProjectAccessEntryApi[]
        }
        payload?: {
            value: true
        }
    }
    openProjectAccessModal: (member: OrganizationMemberType) => {
        member: OrganizationMemberType
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface memberProjectAccessLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        modalProjects: (
            modalMember: OrganizationMemberType | null,
            projectAccess: Record<string, MemberProjectAccessEntryApi[]> | null
        ) => MemberProjectAccessEntryApi[] | null
    }
}

export type memberProjectAccessLogicType = MakeLogicType<
    memberProjectAccessLogicValues,
    memberProjectAccessLogicActions,
    Record<string, any>,
    memberProjectAccessLogicMeta
>

export const memberProjectAccessLogic = kea<memberProjectAccessLogicType>([
    path(['scenes', 'settings', 'organization', 'memberProjectAccessLogic']),
    actions({
        loadProjectAccess: true,
        openProjectAccessModal: (member: OrganizationMemberType) => ({ member }),
        closeProjectAccessModal: true,
    }),
    loaders({
        projectAccess: {
            __default: null as Record<string, MemberProjectAccessEntryApi[]> | null,
            loadProjectAccess: async () => {
                const response = await membersProjectAccessRetrieve('@current')
                return Object.fromEntries(
                    response.results.map((entry) => [entry.organization_membership_id, entry.projects])
                )
            },
        },
    }),
    reducers({
        modalMember: [
            null as OrganizationMemberType | null,
            {
                openProjectAccessModal: (_, { member }) => member,
                closeProjectAccessModal: () => null,
            },
        ],
    }),
    selectors({
        modalProjects: [
            (s) => [s.modalMember, s.projectAccess],
            (
                modalMember: OrganizationMemberType | null,
                projectAccess: Record<string, MemberProjectAccessEntryApi[]> | null
            ): MemberProjectAccessEntryApi[] | null =>
                modalMember && projectAccess ? (projectAccess[modalMember.id] ?? null) : null,
        ],
    }),
    afterMount(({ actions }) => {
        actions.loadProjectAccess()
    }),
])