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

import type { Node } from '~/queries/schema/schema-general'

const NO_TAB = '__no_tab__'

export type ExpandedRowsByTabAndVizKey = Record<string, Record<string, number[]>>
export type SavedQueriesByTabAndScene = Record<string, Record<string, Node>>
export type ChatDraftsByTab = Record<string, string>

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tabUiStateLogicValues {
    chatDraftFor: (tabId: string | undefined) => string
    chatDraftsByTab: ChatDraftsByTab
    expandedRowsByTabAndVizKey: ExpandedRowsByTabAndVizKey
    expandedRowsFor: (tabId: string | undefined, vizKey: string) => number[]
    savedQueriesByTabAndScene: SavedQueriesByTabAndScene
    savedQueryFor: (tabId: string | undefined, sceneKey: string) => Node | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tabUiStateLogicActions {
    clearTabUiState: (tabId: string) => {
        tabId: string
    }
    setChatDraftForTab: (
        tabId: string | undefined,
        draft: string
    ) => {
        draft: string
        tabId: string
    }
    setSavedQueryForTab: (
        tabId: string | undefined,
        sceneKey: string,
        query: Node | null
    ) => {
        query: Node<Record<string, any>> | null
        sceneKey: string
        tabId: string
    }
    toggleExpandedRow: (
        tabId: string | undefined,
        vizKey: string,
        rowIndex: number
    ) => {
        rowIndex: number
        tabId: string
        vizKey: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tabUiStateLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        expandedRowsFor: (
            expandedRowsByTabAndVizKey: ExpandedRowsByTabAndVizKey
        ) => (tabId: string | undefined, vizKey: string) => number[]
        savedQueryFor: (
            savedQueriesByTabAndScene: SavedQueriesByTabAndScene
        ) => (tabId: string | undefined, sceneKey: string) => Node | null
        chatDraftFor: (chatDraftsByTab: ChatDraftsByTab) => (tabId: string | undefined) => string
    }
}

export type tabUiStateLogicType = MakeLogicType<
    tabUiStateLogicValues,
    tabUiStateLogicActions,
    Record<string, any>,
    tabUiStateLogicMeta
>

export const tabUiStateLogic = kea<tabUiStateLogicType>([
    path(['lib', 'logic', 'tabUiStateLogic']),
    actions({
        toggleExpandedRow: (tabId: string | undefined, vizKey: string, rowIndex: number) => ({
            tabId: tabId ?? NO_TAB,
            vizKey,
            rowIndex,
        }),
        clearTabUiState: (tabId: string) => ({ tabId }),
        setSavedQueryForTab: (tabId: string | undefined, sceneKey: string, query: Node | null) => ({
            tabId: tabId ?? NO_TAB,
            sceneKey,
            query,
        }),
        setChatDraftForTab: (tabId: string | undefined, draft: string) => ({
            tabId: tabId ?? NO_TAB,
            draft,
        }),
    }),
    reducers({
        expandedRowsByTabAndVizKey: [
            {} as ExpandedRowsByTabAndVizKey,
            {
                toggleExpandedRow: (state, { tabId, vizKey, rowIndex }) => {
                    const tabState = state[tabId] ?? {}
                    const existing = tabState[vizKey] ?? []
                    const next = existing.includes(rowIndex)
                        ? existing.filter((r: number) => r !== rowIndex)
                        : [...existing, rowIndex]
                    return { ...state, [tabId]: { ...tabState, [vizKey]: next } }
                },
                clearTabUiState: (state, { tabId }) => {
                    if (!(tabId in state)) {
                        return state
                    }
                    const next = { ...state }
                    delete next[tabId]
                    return next
                },
            },
        ],
        savedQueriesByTabAndScene: [
            {} as SavedQueriesByTabAndScene,
            {
                setSavedQueryForTab: (state, { tabId, sceneKey, query }) => {
                    if (query === null) {
                        // Clear: drop the scene slot, and the whole tab entry if it becomes empty.
                        const tabState = state[tabId]
                        if (!tabState || !(sceneKey in tabState)) {
                            return state
                        }
                        const nextTabState = { ...tabState }
                        delete nextTabState[sceneKey]
                        if (Object.keys(nextTabState).length === 0) {
                            const next = { ...state }
                            delete next[tabId]
                            return next
                        }
                        return { ...state, [tabId]: nextTabState }
                    }
                    return {
                        ...state,
                        [tabId]: { ...state[tabId], [sceneKey]: query },
                    }
                },
                clearTabUiState: (state, { tabId }) => {
                    if (!(tabId in state)) {
                        return state
                    }
                    const next = { ...state }
                    delete next[tabId]
                    return next
                },
            },
        ],
        chatDraftsByTab: [
            {} as ChatDraftsByTab,
            {
                setChatDraftForTab: (state, { tabId, draft }) => {
                    if (draft === '') {
                        if (!(tabId in state)) {
                            return state
                        }
                        const next = { ...state }
                        delete next[tabId]
                        return next
                    }
                    return { ...state, [tabId]: draft }
                },
                clearTabUiState: (state, { tabId }) => {
                    if (!(tabId in state)) {
                        return state
                    }
                    const next = { ...state }
                    delete next[tabId]
                    return next
                },
            },
        ],
    }),
    selectors({
        expandedRowsFor: [
            (s) => [s.expandedRowsByTabAndVizKey],
            (state: ExpandedRowsByTabAndVizKey): ((tabId: string | undefined, vizKey: string) => number[]) =>
                (tabId, vizKey) =>
                    state[tabId ?? NO_TAB]?.[vizKey] ?? [],
        ],
        savedQueryFor: [
            (s) => [s.savedQueriesByTabAndScene],
            (state: SavedQueriesByTabAndScene): ((tabId: string | undefined, sceneKey: string) => Node | null) =>
                (tabId, sceneKey) =>
                    state[tabId ?? NO_TAB]?.[sceneKey] ?? null,
        ],
        chatDraftFor: [
            (s) => [s.chatDraftsByTab],
            (state: ChatDraftsByTab): ((tabId: string | undefined) => string) =>
                (tabId) =>
                    state[tabId ?? NO_TAB] ?? '',
        ],
    }),
])