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.

2.7 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import { dashboardsList } from '@posthog/products-dashboards/frontend/generated/api'
import type { DashboardBasicApi } from '@posthog/products-dashboards/frontend/generated/api.schemas'

import { isUserLoggedIn } from 'lib/utils/getAppContext'
import { teamLogic } from 'scenes/teamLogic'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pinnedDashboardsModelValues {
    pinnedDashboards: DashboardBasicApi[]
    pinnedDashboardsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pinnedDashboardsModelActions {
    loadCurrentTeamSuccess: (
        currentTeam: null | import('~/types').TeamPublicType,
        payload?: any
    ) => {
        currentTeam: null | import('~/types').TeamPublicType
        payload?: any
    } // teamLogic
    loadPinnedDashboards: () => any
    loadPinnedDashboardsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPinnedDashboardsSuccess: (
        pinnedDashboards: DashboardBasicApi[],
        payload?: any
    ) => {
        pinnedDashboards: DashboardBasicApi[]
        payload?: any
    }
}

export type pinnedDashboardsModelType = MakeLogicType<pinnedDashboardsModelValues, pinnedDashboardsModelActions>

export const pinnedDashboardsModel = kea<pinnedDashboardsModelType>([
    path(['models', 'pinnedDashboardsModel']),
    connect(() => ({
        actions: [teamLogic, ['loadCurrentTeamSuccess']],
    })),
    loaders(() => ({
        pinnedDashboards: [
            [] as DashboardBasicApi[],
            {
                loadPinnedDashboards: async () => {
                    if (!isUserLoggedIn() || !teamLogic.values.currentTeam) {
                        return []
                    }

                    const response = await dashboardsList(String(teamLogic.values.currentTeamId), {
                        pinned: true,
                        limit: 4,
                        exclude_generated: true,
                    })
                    return response.results
                },
            },
        ],
    })),
    reducers({
        pinnedDashboards: [
            [] as DashboardBasicApi[],
            {
                loadCurrentTeamSuccess: () => [],
            },
        ],
    }),
    listeners(({ actions }) => ({
        loadCurrentTeamSuccess: ({ currentTeam }) => {
            if (currentTeam) {
                actions.loadPinnedDashboards()
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadPinnedDashboards()
    }),
])