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.

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

import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { SceneConfig } from 'scenes/sceneTypes'

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

import type { FeatureFlagsSet } from '../../lib/logic/featureFlagLogic'
import { panelLayoutLogic } from '../panel-layout/panelLayoutLogic'

export type SceneLayoutContainerRef = React.RefObject<HTMLElement> | null

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneLayoutLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    mainContentRect: DOMRect | null // panelLayoutLogic
    selectedTab: SidePanelTab | null // sidePanelStateLogic
    sidePanelOpen: boolean // sidePanelStateLogic
    sceneLayoutConfig: SceneConfig | null
    scenePanelElement: HTMLElement | null
    scenePanelIsPresent: boolean
    scenePanelOpen: boolean
    scenePanelOpenManual: boolean
    sceneTakeoverActive: boolean
    sceneTakeoverElement: HTMLElement | null
    showDescription: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneLayoutLogicActions {
    registerScenePanelElement: (element: HTMLElement | null) => {
        element: HTMLElement | null
    }
    registerSceneTakeoverElement: (element: HTMLElement | null) => {
        element: HTMLElement | null
    }
    setSceneLayoutConfig: (config: SceneConfig) => {
        config: SceneConfig
    }
    setScenePanelIsPresent: (active: boolean) => {
        active: boolean
    }
    setScenePanelOpen: (open: boolean) => {
        open: boolean
    }
    setSceneTakeoverActive: (active: boolean) => {
        active: boolean
    }
    toggleShowDescription: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneLayoutLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        scenePanelOpen: (
            scenePanelOpenManual: boolean,
            sidePanelOpen: boolean,
            selectedTab: SidePanelTab | null
        ) => boolean
    }
}

export type sceneLayoutLogicType = MakeLogicType<
    sceneLayoutLogicValues,
    sceneLayoutLogicActions,
    Record<string, any>,
    sceneLayoutLogicMeta
>

export const sceneLayoutLogic = kea<sceneLayoutLogicType>([
    path(['layout', 'scene-layout', 'sceneLayoutLogic']),
    connect(() => ({
        values: [
            featureFlagLogic,
            ['featureFlags'],
            panelLayoutLogic,
            ['mainContentRect'],
            sidePanelStateLogic,
            ['sidePanelOpen', 'selectedTab'],
        ],
    })),
    actions({
        registerScenePanelElement: (element: HTMLElement | null) => ({ element }),
        registerSceneTakeoverElement: (element: HTMLElement | null) => ({ element }),
        setScenePanelIsPresent: (active: boolean) => ({ active }),
        setScenePanelOpen: (open: boolean) => ({ open }),
        setSceneTakeoverActive: (active: boolean) => ({ active }),
        setSceneLayoutConfig: (config: SceneConfig) => ({ config }),
        toggleShowDescription: true,
    }),
    reducers({
        showDescription: [
            true,
            { persist: true },
            {
                toggleShowDescription: (state) => !state,
            },
        ],
        scenePanelElement: [
            null as HTMLElement | null,
            {
                registerScenePanelElement: (_, { element }) => element,
            },
        ],
        // Host for fullscreen-in-scene surfaces (the email editor): they portal here to fill the
        // main well while the navigation and the side panel stay visible and usable, which a
        // LemonModal overlay (fixed, z above the side panel) cannot do.
        sceneTakeoverElement: [
            null as HTMLElement | null,
            {
                registerSceneTakeoverElement: (_, { element }) => element,
            },
        ],
        // Drives the host's visibility: an empty always-mounted host would still cover the scene
        // and swallow clicks, so the portaling surface toggles this with its own open state.
        sceneTakeoverActive: [
            false,
            {
                setSceneTakeoverActive: (_, { active }) => active,
            },
        ],
        scenePanelIsPresent: [
            false,
            {
                setScenePanelIsPresent: (_, { active }) => active,
            },
        ],
        scenePanelOpenManual: [
            false,
            {
                setScenePanelOpen: (_, { open }) => open,
            },
        ],
        sceneLayoutConfig: [
            null as SceneConfig | null,
            {
                setSceneLayoutConfig: (_, { config }) => config,
            },
        ],
    }),
    selectors({
        scenePanelOpen: [
            (s) => [s.scenePanelOpenManual, s.sidePanelOpen, s.selectedTab],
            (scenePanelOpenManual: boolean, sidePanelOpen: boolean, selectedTab: SidePanelTab | null): boolean =>
                scenePanelOpenManual || (sidePanelOpen && selectedTab === SidePanelTab.Info),
        ],
    }),
    listeners(({ values }) => ({
        registerScenePanelElement: () => {
            refreshSceneElementSelectorCaches(values)
        },
        registerSceneTakeoverElement: () => {
            refreshSceneElementSelectorCaches(values)
        },
    })),
])

// The memoized element selectors only recompute when evaluated, and once the last consumer
// unmounts nothing subscribes to them — so without this read their cached result keeps the
// previous (now detached) container alive, which pins the departed scene's entire fiber and
// DOM tree via the element's React fiber expandos.
function refreshSceneElementSelectorCaches(values: sceneLayoutLogicType['values']): void {
    void values.scenePanelElement
    void values.sceneTakeoverElement
}