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

import { commandLogic } from 'lib/components/Command/commandLogic'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { removeProjectIdIfPresent } from 'lib/utils/kea-router'

import type { FeatureFlagsSet } from '../../lib/logic/featureFlagLogic'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface terminalDockLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    dockOpen: boolean
    hasOpened: boolean
    requestedFolder: string | null
    terminalEnabled: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface terminalDockLogicActions {
    focusTerminal: () => {
        value: true
    }
    openInTerminal: (folder: string) => {
        folder: string
    }
    setDockOpen: (open: boolean) => {
        open: boolean
    }
    setRequestedFolder: (folder: string | null) => {
        folder: string | null
    }
    toggleTerminal: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface terminalDockLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        terminalEnabled: (featureFlags: FeatureFlagsSet) => boolean
    }
}

export type terminalDockLogicType = MakeLogicType<
    terminalDockLogicValues,
    terminalDockLogicActions,
    Record<string, any>,
    terminalDockLogicMeta
>

function isTerminalPage(): boolean {
    return removeProjectIdIfPresent(router.values.location.pathname) === '/terminal'
}

export const terminalDockLogic = kea<terminalDockLogicType>([
    path(['scenes', 'terminal', 'terminalDockLogic']),
    connect({ values: [featureFlagLogic, ['featureFlags']] }),
    actions({
        openInTerminal: (folder: string) => ({ folder }),
        setRequestedFolder: (folder: string | null) => ({ folder }),
        toggleTerminal: true,
        focusTerminal: true,
        setDockOpen: (open: boolean) => ({ open }),
    }),
    reducers({
        requestedFolder: [null as string | null, { setRequestedFolder: (_, { folder }) => folder }],
        dockOpen: [false, { setDockOpen: (_, { open }) => open }],
        hasOpened: [false, { setDockOpen: (state, { open }) => state || open }],
    }),
    selectors({
        terminalEnabled: [
            (s) => [s.featureFlags],
            (featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.POSTHOG_TERMINAL],
        ],
    }),
    listeners(({ actions, values, cache }) => ({
        openInTerminal: async ({ folder }, breakpoint) => {
            if (!values.terminalEnabled) {
                return
            }
            // The full-page terminal hides the dock, so opening it there makes it appear on the next page.
            if (!values.dockOpen && !isTerminalPage()) {
                actions.setDockOpen(true)
            }
            // Opening the dock follows the current page's folder, so the folder request must come after it.
            actions.setRequestedFolder(folder)
            // Menus restore focus to their trigger when they close.
            await breakpoint(100)
            if (values.terminalEnabled && (values.dockOpen || isTerminalPage())) {
                actions.focusTerminal()
            }
        },
        toggleTerminal: () => {
            if (!values.terminalEnabled) {
                return
            }
            if (removeProjectIdIfPresent(router.values.location.pathname) === '/terminal') {
                actions.focusTerminal()
            } else {
                actions.setDockOpen(!values.dockOpen)
            }
        },
        setDockOpen: ({ open }) => {
            if (open) {
                cache.previousFocus = document.activeElement?.closest('[role="dialog"]')
                    ? cache.commandFocus
                    : document.activeElement
            } else {
                if (cache.previousFocus?.isConnected) {
                    cache.previousFocus.focus()
                }
            }
        },
        [commandLogic.actionTypes.openCommand]: () => {
            cache.commandFocus = document.activeElement
        },
        [commandLogic.actionTypes.toggleCommand]: () => {
            if (commandLogic.values.isCommandOpen) {
                cache.commandFocus = document.activeElement
            }
        },
        [featureFlagLogic.actionTypes.setFeatureFlags]: () => {
            if (!values.terminalEnabled && values.dockOpen) {
                actions.setDockOpen(false)
            }
        },
    })),
])