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

import { createFuse } from 'lib/utils/fuseSearch'
import { permanentlyMount } from 'lib/utils/kea-logic-builders'

import { toolbarApi } from '~/toolbar/shared/toolbarApi'
import { ActionType } from '~/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsLogicValues {
    actionCount: number
    allActions: ActionType[]
    allActionsLoading: boolean
    searchTerm: string
    sortedActions: ActionType[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsLogicActions {
    deleteAction: ({ id }: { id: number }) => {
        id: number
    }
    deleteActionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    deleteActionSuccess: (
        allActions: ActionType[],
        payload?: {
            id: number
        }
    ) => {
        allActions: ActionType[]
        payload?: {
            id: number
        }
    }
    getActions: (_?: any) => any
    getActionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    getActionsSuccess: (
        allActions: ActionType[],
        payload?: any
    ) => {
        allActions: ActionType[]
        payload?: any
    }
    setSearchTerm: (searchTerm: string) => {
        searchTerm: string
    }
    updateAction: ({ action }: { action: ActionType }) => {
        action: ActionType
    }
    updateActionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    updateActionSuccess: (
        allActions: ActionType[],
        payload?: {
            action: ActionType
        }
    ) => {
        allActions: ActionType[]
        payload?: {
            action: ActionType
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        sortedActions: (allActions: ActionType[], searchTerm: string) => ActionType[]
        actionCount: (allActions: ActionType[]) => number
    }
}

export type actionsLogicType = MakeLogicType<
    actionsLogicValues,
    actionsLogicActions,
    Record<string, any>,
    actionsLogicMeta
>

export const actionsLogic = kea<actionsLogicType>([
    path(['toolbar', 'actions', 'actionsLogic']),
    actions({
        setSearchTerm: (searchTerm: string) => ({ searchTerm }),
    }),
    loaders(({ values }) => ({
        allActions: [
            [] as ActionType[],
            {
                // oxlint-disable-next-line @typescript-eslint/no-unused-vars
                getActions: async (_ = null, breakpoint: () => void) => {
                    const result = await toolbarApi.actions.list({
                        context: 'load_actions',
                        reauthenticateOnForbidden: true,
                    })
                    breakpoint()

                    if (!result.ok || !Array.isArray(result.data.results)) {
                        return values.allActions
                    }
                    return result.data.results
                },
                updateAction: ({ action }: { action: ActionType }) => {
                    return values.allActions.filter((r) => r.id !== action.id).concat([action])
                },
                deleteAction: ({ id }: { id: number }) => {
                    return values.allActions.filter((r) => r.id !== id)
                },
            },
        ],
    })),
    reducers({
        searchTerm: [
            '',
            {
                setSearchTerm: (_, { searchTerm }) => searchTerm,
            },
        ],
    }),
    selectors({
        sortedActions: [
            (s) => [s.allActions, s.searchTerm],
            (allActions: ActionType[], searchTerm: string) => {
                const filteredActions = searchTerm
                    ? createFuse(allActions, {
                          keys: ['name'],
                      })
                          .search(searchTerm)
                          .map(({ item }) => item)
                    : allActions
                return [...filteredActions].sort((a, b) => (a.name ?? 'Untitled').localeCompare(b.name ?? 'Untitled'))
            },
        ],
        actionCount: [(s) => [s.allActions], (allActions: ActionType[]) => allActions.length],
    }),
    permanentlyMount(),
])