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.9 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'

import { sidePanelContextLogic } from '~/layout/navigation-3000/sidepanel/sidePanelContextLogic'
import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import type { SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import type { SidePanelTab } from '~/types'

import { captureCanvasAction } from '../canvasAnalytics'
import { CANVAS_PANEL_SIDE_PANEL_TABS, CanvasPanelTab, canvasPanelTab } from './canvasPanelTabs'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface canvasSidePanelLogicValues {
    sceneSidePanelContext: SidePanelSceneContext // sidePanelContextLogic
    selectedTab: SidePanelTab | null // sidePanelStateLogic
    sidePanelOpen: boolean // sidePanelStateLogic
    blocksCanvasId: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface canvasSidePanelLogicActions {
    openSidePanel: (
        tab: SidePanelTab,
        options?: string | undefined
    ) => {
        options: string | undefined
        tab: SidePanelTab
    } // sidePanelStateLogic
    openTab: (
        tab: CanvasPanelTab,
        canvasId: string
    ) => {
        canvasId: string
        tab: CanvasPanelTab
    }
    setBlocksCanvasId: (canvasId: string | null) => {
        canvasId: string | null
    }
    switchTab: (tab: CanvasPanelTab) => {
        tab: CanvasPanelTab
    }
}

export type canvasSidePanelLogicType = MakeLogicType<canvasSidePanelLogicValues, canvasSidePanelLogicActions>

function openCanvasTab(state: any): CanvasPanelTab | null {
    return sidePanelStateLogic.selectors.sidePanelOpen(state)
        ? canvasPanelTab(sidePanelStateLogic.selectors.selectedTab(state))
        : null
}

/** Opens the canvas's tabs in the app side panel, and records how people move between them. */
export const canvasSidePanelLogic = kea<canvasSidePanelLogicType>([
    path(['products', 'canvas', 'frontend', 'sidePanel', 'canvasSidePanelLogic']),
    connect(() => ({
        values: [
            sidePanelStateLogic,
            ['selectedTab', 'sidePanelOpen'],
            sidePanelContextLogic,
            ['sceneSidePanelContext'],
        ],
        actions: [sidePanelStateLogic, ['openSidePanel']],
    })),
    actions({
        /** Something asked for a tab, for example a comment anchor clicked in the canvas. */
        openTab: (tab: CanvasPanelTab, canvasId: string) => ({ tab, canvasId }),
        /** Shows a tab without opening a hidden panel, for example the inspector when the author selects a block. */
        switchTab: (tab: CanvasPanelTab) => ({ tab }),
        /** The canvas being edited by hand, the only one whose panel has a Blocks tab. */
        setBlocksCanvasId: (canvasId: string | null) => ({ canvasId }),
    }),
    reducers({
        blocksCanvasId: [
            null as string | null,
            {
                setBlocksCanvasId: (_, { canvasId }) => canvasId,
            },
        ],
    }),
    listeners(({ actions, values }) => ({
        openTab: ({ tab }) => {
            actions.openSidePanel(CANVAS_PANEL_SIDE_PANEL_TABS[tab])
        },
        switchTab: ({ tab }) => {
            if (values.sidePanelOpen) {
                actions.openSidePanel(CANVAS_PANEL_SIDE_PANEL_TABS[tab])
            }
        },
        setBlocksCanvasId: ({ canvasId }) => {
            // The Blocks tab leaves with edit mode, so a panel showing it moves to the chat.
            if (!canvasId && values.sidePanelOpen && values.selectedTab === CANVAS_PANEL_SIDE_PANEL_TABS.blocks) {
                actions.openSidePanel(CANVAS_PANEL_SIDE_PANEL_TABS.chat)
            }
        },
        [sidePanelStateLogic.actionTypes.openSidePanel]: ({ tab }, _, __, previousState) => {
            const canvasId = values.sceneSidePanelContext.canvas_id
            const next = canvasPanelTab(tab)
            const previous = openCanvasTab(previousState)
            if (!canvasId || !next || next === previous) {
                return
            }
            if (previous) {
                captureCanvasAction('panel_tab_change', {
                    dashboard_id: canvasId,
                    surface: 'web_canvas_side_panel',
                    tab: next,
                })
            } else {
                captureCanvasAction('panel_toggle', { dashboard_id: canvasId, open: true, tab: next })
            }
        },
        [sidePanelStateLogic.actionTypes.closeSidePanel]: (_, __, ___, previousState) => {
            // Leaving the canvas closes the panel too, but by then the context names no canvas.
            const canvasId = values.sceneSidePanelContext.canvas_id
            const previous = openCanvasTab(previousState)
            if (canvasId && previous) {
                captureCanvasAction('panel_toggle', { dashboard_id: canvasId, open: false, tab: previous })
            }
        },
    })),
])