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.

7.5 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'

import { actionsLogic } from '~/toolbar/shared/actions/actionsLogic'
import { actionsTabLogic } from '~/toolbar/shared/actions/actionsTabLogic'

import type { ActionType, ElementType } from '../../../../types'
import type { ActionForm } from '../../../shared/types'
import type { ActionSaveResult, PageToolbarActionsState } from '../../messaging/pageToolbarBridge'

export type PageActionsCommand =
    | { type: 'select-action'; id: number | null }
    | { type: 'new-action'; fromSelectedElement: boolean }
    | { type: 'set-action-form'; form: ActionForm }
    | { type: 'inspect-for-action-step'; index: number | null }
    | { type: 'save-action' }

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface extensionPageActionsLogicValues {
    allActionsLoading: boolean // actionsLogic
    sortedActions: ActionType[] // actionsLogic
    actionForm: ActionForm // actionsTabLogic
    actionFormElementsChains: Record<number, ElementType[]> // actionsTabLogic
    inspectingElement: number | null // actionsTabLogic
    isActionFormSubmitting: boolean // actionsTabLogic
    actionsList: PageToolbarActionsState['actions']
    formRevision: number
    pageState: PageToolbarActionsState
    saveResult: ActionSaveResult | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface extensionPageActionsLogicActions {
    actionCreatedSuccess: (action: ActionType) => {
        action: ActionType
    } // actionsTabLogic
    inspectElementSelected: (
        element: HTMLElement,
        index: number | null
    ) => {
        element: HTMLElement
        index: number | null
    } // actionsTabLogic
    newAction: (element?: HTMLElement | undefined) => {
        element: HTMLElement | null
    } // actionsTabLogic
    selectAction: (id: number | null) => {
        id: number | null
    } // actionsTabLogic
    submitActionFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    } // actionsTabLogic
    handleCommand: (command: PageActionsCommand) => {
        command: PageActionsCommand
    }
    setSaveResult: (saveResult: ActionSaveResult | null) => {
        saveResult: ActionSaveResult | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface extensionPageActionsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        actionsList: (sortedActions: ActionType[]) => PageToolbarActionsState['actions']
        pageState: (
            allActionsLoading: boolean,
            actionsList: {
                id: number
                name: string | null
            }[],
            selectedActionId: number | 'new' | null,
            actionForm: ActionForm,
            formRevision: number,
            actionFormElementsChains: Record<number, ElementType[]>,
            inspectingElement: number | null,
            isActionFormSubmitting: boolean,
            saveResult: ActionSaveResult | null
        ) => PageToolbarActionsState
    }
}

export type extensionPageActionsLogicType = MakeLogicType<
    extensionPageActionsLogicValues,
    extensionPageActionsLogicActions,
    Record<string, any>,
    extensionPageActionsLogicMeta
>

export const extensionPageActionsLogic = kea<extensionPageActionsLogicType>([
    path(['toolbar', 'extension', 'extensionPageActionsLogic']),
    connect(() => ({
        values: [
            actionsLogic,
            ['sortedActions', 'allActionsLoading'],
            actionsTabLogic,
            ['actionForm', 'actionFormElementsChains', 'inspectingElement', 'isActionFormSubmitting'],
        ],
        actions: [
            actionsTabLogic,
            ['actionCreatedSuccess', 'submitActionFormFailure', 'selectAction', 'newAction', 'inspectElementSelected'],
        ],
    })),
    actions({
        handleCommand: (command: PageActionsCommand) => ({ command }),
        setSaveResult: (saveResult: ActionSaveResult | null) => ({ saveResult }),
    }),
    reducers({
        saveResult: [
            null as ActionSaveResult | null,
            {
                setSaveResult: (_, { saveResult }) => saveResult,
                actionCreatedSuccess: (_, { action }) => ({
                    status: 'saved' as const,
                    actionId: action.id,
                    name: action.name ?? null,
                }),
                submitActionFormFailure: (_, { error }) => ({
                    status: 'failed' as const,
                    error: error?.message || 'Unknown error',
                }),
                selectAction: () => null,
                newAction: () => null,
            },
        ],
        // The panel edits its own copy of the form, and takes the page's copy only when the page changed it.
        formRevision: [
            0,
            {
                selectAction: (revision) => revision + 1,
                newAction: (revision) => revision + 1,
                inspectElementSelected: (revision) => revision + 1,
            },
        ],
    }),
    selectors({
        actionsList: [
            (s) => [s.sortedActions],
            (sortedActions: ActionType[]): PageToolbarActionsState['actions'] =>
                sortedActions.map(({ id, name }) => ({ id, name: name ?? null })),
        ],
        pageState: [
            (s) => [
                s.allActionsLoading,
                s.actionsList,
                // kea-typegen drops the 'new' literal when it copies a connected value's type, so read the selector.
                actionsTabLogic.selectors.selectedActionId,
                s.actionForm,
                s.formRevision,
                s.actionFormElementsChains,
                s.inspectingElement,
                s.isActionFormSubmitting,
                s.saveResult,
            ],
            (
                allActionsLoading: boolean,
                actionsList: PageToolbarActionsState['actions'],
                selectedActionId: number | 'new' | null,
                actionForm: ActionForm,
                formRevision: number,
                actionFormElementsChains: Record<number, ElementType[]>,
                inspectingElement: number | null,
                isActionFormSubmitting: boolean,
                saveResult: ActionSaveResult | null
            ): PageToolbarActionsState => ({
                allActionsLoading,
                actions: actionsList,
                selectedActionId,
                actionForm,
                formRevision,
                actionFormElementsChains,
                inspectingElement,
                saving: isActionFormSubmitting,
                saveResult,
            }),
        ],
    }),
    listeners(({ actions }) => ({
        handleCommand: ({ command }) => {
            switch (command.type) {
                case 'select-action':
                    actionsTabLogic.actions.selectAction(command.id)
                    return
                case 'set-action-form':
                    actionsTabLogic.actions.setActionFormValues(command.form)
                    return
                case 'inspect-for-action-step':
                    actionsTabLogic.actions.inspectForElementWithIndex(command.index)
                    return
                case 'save-action':
                    actions.setSaveResult(null)
                    actionsTabLogic.actions.submitActionForm()
                    return
            }
        },
    })),
])