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.

2.7 KB
Raw Blame
import { MakeLogicType, actions, events, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'

import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'

import { oauthConnectedAppsList, oauthConnectedAppsRevokeCreate } from '~/generated/core/api'
import type { ConnectedAppApi } from '~/generated/core/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface connectedAppsLogicValues {
    connectedApps: ConnectedAppApi[]
    connectedAppsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface connectedAppsLogicActions {
    loadConnectedApps: () => any
    loadConnectedAppsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadConnectedAppsSuccess: (
        connectedApps: ConnectedAppApi[],
        payload?: any
    ) => {
        connectedApps: ConnectedAppApi[]
        payload?: any
    }
    revokeApp: (id: string) => {
        id: string
    }
    revokeAppFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    revokeAppSuccess: (
        connectedApps: ConnectedAppApi[],
        payload?: {
            id: string
        }
    ) => {
        connectedApps: ConnectedAppApi[]
        payload?: {
            id: string
        }
    }
}

export type connectedAppsLogicType = MakeLogicType<connectedAppsLogicValues, connectedAppsLogicActions>

export const connectedAppsLogic = kea<connectedAppsLogicType>([
    path(['scenes', 'settings', 'user', 'connectedAppsLogic']),

    actions({
        revokeApp: (id: string) => ({ id }),
    }),

    loaders(({ values }) => ({
        connectedApps: [
            [] as ConnectedAppApi[],
            {
                loadConnectedApps: async () => {
                    return await oauthConnectedAppsList()
                },
                revokeApp: async ({ id }, breakpoint) => {
                    const remainingApps = values.connectedApps.filter((app) => app.id !== id)
                    await oauthConnectedAppsRevokeCreate(id)
                    breakpoint()
                    lemonToast.success('App access revoked')
                    return remainingApps
                },
            },
        ],
    })),

    listeners(({ actions }) => ({
        revokeAppFailure: ({ errorObject }) => {
            // A 404 means the access already lapsed, so the listed row is stale.
            if (errorObject?.status === 404) {
                actions.loadConnectedApps()
            }
        },
    })),

    events(({ actions }) => ({
        afterMount: () => actions.loadConnectedApps(),
    })),
])