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

import api, { ApiConfig } from 'lib/api'
import { fullName } from 'lib/utils/strings'

import { getPersonalApiKeysListUrl } from 'products/platform_features/frontend/generated/api'
import type { OrganizationPersonalAPIKeyApi } from 'products/platform_features/frontend/generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationPersonalAPIKeysLogicValues {
    filteredKeys: OrganizationPersonalAPIKeyApi[]
    keys: OrganizationPersonalAPIKeyApi[]
    keysLoading: boolean
    search: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationPersonalAPIKeysLogicActions {
    loadKeys: () => any
    loadKeysFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadKeysSuccess: (
        keys: OrganizationPersonalAPIKeyApi[],
        payload?: any
    ) => {
        keys: OrganizationPersonalAPIKeyApi[]
        payload?: any
    }
    setSearch: (search: string) => {
        search: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationPersonalAPIKeysLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        filteredKeys: (keys: OrganizationPersonalAPIKeyApi[], search: string) => OrganizationPersonalAPIKeyApi[]
    }
}

export type organizationPersonalAPIKeysLogicType = MakeLogicType<
    organizationPersonalAPIKeysLogicValues,
    organizationPersonalAPIKeysLogicActions,
    Record<string, any>,
    organizationPersonalAPIKeysLogicMeta
>

export const organizationPersonalAPIKeysLogic = kea<organizationPersonalAPIKeysLogicType>([
    path(['scenes', 'settings', 'organization', 'organizationPersonalAPIKeysLogic']),
    actions({
        setSearch: (search: string) => ({ search }),
    }),
    loaders({
        keys: [
            [] as OrganizationPersonalAPIKeyApi[],
            {
                loadKeys: async () => {
                    // Page through everything — an incomplete list would be a security-audit blind spot.
                    const url = getPersonalApiKeysListUrl(ApiConfig.getCurrentOrganizationId())
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                    return await api.loadPaginatedResults<OrganizationPersonalAPIKeyApi>(url)
                },
            },
        ],
    }),
    reducers({
        search: ['', { setSearch: (_, { search }) => search }],
    }),
    selectors({
        // Client-side filtering — the full list is already loaded, so this stays in the browser.
        filteredKeys: [
            (s) => [s.keys, s.search],
            (keys: OrganizationPersonalAPIKeyApi[], search: string): OrganizationPersonalAPIKeyApi[] => {
                const term = search.trim().toLowerCase()
                if (!term) {
                    return keys
                }
                return keys.filter((key) =>
                    [fullName(key.owner), key.owner.email, ...key.scopes].some((field) =>
                        field.toLowerCase().includes(term)
                    )
                )
            },
        ],
    }),
    afterMount(({ actions }) => {
        actions.loadKeys()
    }),
])