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.

3.3 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
import posthog from 'posthog-js'

import { MCP_DASHBOARD_CARDS, McpDashboardCardId } from './dashboardCards'

const teamId = window.POSTHOG_APP_CONTEXT?.current_team?.id

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpDashboardCardsLogicValues {
    hiddenCardIds: McpDashboardCardId[]
    hiddenCards: McpDashboardCardId[]
    isCardVisible: (id: McpDashboardCardId) => boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpDashboardCardsLogicActions {
    showAllCards: () => {
        value: true
    }
    toggleCard: (id: McpDashboardCardId) => {
        id: McpDashboardCardId
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpDashboardCardsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        hiddenCards: (hiddenCardIds: McpDashboardCardId[]) => McpDashboardCardId[]
        isCardVisible: (hiddenCardIds: McpDashboardCardId[]) => (id: McpDashboardCardId) => boolean
    }
}

export type mcpDashboardCardsLogicType = MakeLogicType<
    mcpDashboardCardsLogicValues,
    mcpDashboardCardsLogicActions,
    Record<string, any>,
    mcpDashboardCardsLogicMeta
>

export const mcpDashboardCardsLogic = kea<mcpDashboardCardsLogicType>([
    path(['products', 'mcp_analytics', 'frontend', 'dashboard', 'mcpDashboardCardsLogic']),
    actions({
        toggleCard: (id: McpDashboardCardId) => ({ id }),
        showAllCards: true,
    }),
    reducers({
        // Stores hidden ids, so a card shipped later shows by default.
        hiddenCardIds: [
            [] as McpDashboardCardId[],
            { persist: true, prefix: `${teamId}__` },
            {
                toggleCard: (hidden, { id }) =>
                    hidden.includes(id) ? hidden.filter((h) => h !== id) : [...hidden, id],
                showAllCards: () => [],
            },
        ],
    }),
    selectors({
        // Persisted ids can outlive the card they named, so read them against the live list.
        hiddenCards: [
            (s) => [s.hiddenCardIds],
            (hiddenCardIds: McpDashboardCardId[]): McpDashboardCardId[] =>
                MCP_DASHBOARD_CARDS.map(({ id }) => id).filter((id) => hiddenCardIds.includes(id)),
        ],
        isCardVisible: [
            (s) => [s.hiddenCardIds],
            (hiddenCardIds: McpDashboardCardId[]) =>
                (id: McpDashboardCardId): boolean =>
                    !hiddenCardIds.includes(id),
        ],
    }),
    listeners(({ values, selectors }) => ({
        toggleCard: ({ id }) => {
            posthog.capture('mcp analytics dashboard card toggled', { card: id, hidden: !values.isCardVisible(id) })
        },
        showAllCards: (_, __, ___, previousState) => {
            posthog.capture('mcp analytics dashboard cards reset', {
                hidden_count: selectors.hiddenCards(previousState).length,
            })
        },
    })),
    afterMount(({ values }) => {
        // Toggles only record changes; this records what each visit actually shows.
        posthog.capture('mcp analytics dashboard viewed', {
            hidden_cards: values.hiddenCards,
            hidden_count: values.hiddenCards.length,
        })
    }),
])