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, connect, events, kea, key, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { permanentlyMount } from 'lib/utils/kea-logic-builders'
import { isAuthenticatedTeam, teamLogic } from 'scenes/teamLogic'

import { ActionType } from '~/types'

import type { TeamPublicType, TeamType } from '../types'

export interface ActionsModelProps {
    params?: string
    shouldLoad?: boolean
}

export function findActionName(id: number): string | null {
    return actionsModel.findMounted({ shouldLoad: true })?.values.actions.find((a) => a.id === id)?.name || null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsModelValues {
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    actions: ActionType[]
    actionsById: Partial<Record<number | string, ActionType>>
    actionsGrouped: {
        label: string
        options: {
            label: string | null
            value: number
        }[]
    }[]
    actionsLoaded: boolean
    actionsLoading: boolean
    actionsSorted: ActionType[]
    pin: any
    pinLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsModelActions {
    loadActions: (force?: any) => any
    loadActionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadActionsSuccess: (
        actions: ActionType[],
        payload?: any
    ) => {
        actions: ActionType[]
        payload?: any
    }
    pinAction: (action: ActionType) => ActionType
    pinActionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    pinActionSuccess: (
        pin: void,
        payload?: ActionType
    ) => {
        pin: void
        payload?: ActionType
    }
    unpinAction: (action: ActionType) => ActionType
    unpinActionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    unpinActionSuccess: (
        pin: void,
        payload?: ActionType
    ) => {
        pin: void
        payload?: ActionType
    }
    updateAction: (action: ActionType) => ActionType
    updateActionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    updateActionSuccess: (
        actions: ActionType[],
        payload?: ActionType
    ) => {
        actions: ActionType[]
        payload?: ActionType
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsModelMeta {
    key: 'default' | 'skip-load'
    __keaTypeGenInternalSelectorTypes: {
        actionsGrouped: (actions: ActionType[]) => {
            label: string
            options: {
                label: string | null
                value: number
            }[]
        }[]
        actionsById: (actions: ActionType[]) => Partial<Record<number | string, ActionType>>
        actionsSorted: (actions: ActionType[]) => ActionType[]
    }
}

export type actionsModelType = MakeLogicType<
    actionsModelValues,
    actionsModelActions,
    ActionsModelProps,
    actionsModelMeta
>

export const actionsModel = kea<actionsModelType>([
    props({} as ActionsModelProps),
    key((props) => (props.shouldLoad === false ? 'skip-load' : 'default')),
    path((key) => ['models', 'actionsModel', key]),
    connect(() => ({
        values: [teamLogic, ['currentTeam']],
    })),
    loaders(({ props, values, actions }) => ({
        actions: {
            __default: [] as ActionType[],
            loadActions: async (force = false, breakpoint) => {
                if (!force && values.actionsLoaded) {
                    return values.actions
                }
                const response = await api.actions.list(props.params)
                breakpoint()
                return response.results ?? []
            },
            updateAction: (action: ActionType) => (values.actions || []).map((a) => (action.id === a.id ? action : a)),
        },
        pin: {
            pinAction: async (action: ActionType) => {
                const response = await api.actions.update(action.id, {
                    pinned_at: new Date().toISOString(),
                })
                actions.updateAction(response)
            },
            unpinAction: async (action: ActionType) => {
                const response = await api.actions.update(action.id, {
                    pinned_at: null,
                })
                actions.updateAction(response)
            },
        },
    })),
    reducers({
        actionsLoaded: [
            false,
            {
                loadActionsSuccess: () => true,
            },
        ],
    }),
    selectors(({ selectors }) => ({
        actionsGrouped: [
            () => [selectors.actions],
            (actions: ActionType[]) => {
                return [
                    {
                        label: 'Select an action',
                        options: actions.map((action) => {
                            return { label: action.name, value: action.id }
                        }),
                    },
                ]
            },
        ],
        actionsById: [
            (s) => [s.actions],
            (actions: ActionType[]): Partial<Record<string | number, ActionType>> =>
                Object.fromEntries(actions.map((action) => [action.id, action])),
        ],
        actionsSorted: [
            (s) => [s.actions],
            (actions: ActionType[]): ActionType[] => {
                return actions.sort((a, b) => (b.pinned_at ? 1 : 0) - (a.pinned_at ? 1 : 0))
            },
        ],
    })),
    events(({ props, values, actions }) => ({
        afterMount: () => {
            if (props.shouldLoad !== false && isAuthenticatedTeam(values.currentTeam)) {
                actions.loadActions()
            }
        },
    })),
    permanentlyMount(),
])