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

import { ApiError } from 'lib/api-error'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import {
    metricsSuggestedDashboardsDashboardCreate,
    metricsSuggestedDashboardsList,
} from 'products/metrics/frontend/generated/api'
import type { MetricsSuggestedDashboardApi } from 'products/metrics/frontend/generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface suggestedDashboardsLogicValues {
    currentTeamId: number | null // teamLogic
    newSuggestionCount: number
    openingId: string | null
    suggestions: MetricsSuggestedDashboardApi[] | null
    suggestionsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface suggestedDashboardsLogicActions {
    loadSuggestions: () => any
    loadSuggestionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSuggestionsSuccess: (
        suggestions: MetricsSuggestedDashboardApi[],
        payload?: any
    ) => {
        suggestions: MetricsSuggestedDashboardApi[]
        payload?: any
    }
    menuOpened: () => {
        value: true
    }
    openSuggestion: (suggestion: MetricsSuggestedDashboardApi) => {
        suggestion: MetricsSuggestedDashboardApi
    }
    setOpeningId: (id: string | null) => {
        id: string | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface suggestedDashboardsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        newSuggestionCount: (suggestions: MetricsSuggestedDashboardApi[] | null) => number
    }
}

export type suggestedDashboardsLogicType = MakeLogicType<
    suggestedDashboardsLogicValues,
    suggestedDashboardsLogicActions,
    Record<string, any>,
    suggestedDashboardsLogicMeta
>

export const suggestedDashboardsLogic = kea<suggestedDashboardsLogicType>([
    path(['products', 'metrics', 'frontend', 'suggestedDashboards', 'suggestedDashboardsLogic']),
    connect(() => ({
        values: [teamLogic, ['currentTeamId']],
    })),
    actions({
        menuOpened: true,
        openSuggestion: (suggestion: MetricsSuggestedDashboardApi) => ({ suggestion }),
        setOpeningId: (id: string | null) => ({ id }),
    }),
    loaders(({ values }) => ({
        suggestions: [
            null as MetricsSuggestedDashboardApi[] | null,
            {
                loadSuggestions: async () => metricsSuggestedDashboardsList(String(values.currentTeamId)),
            },
        ],
    })),
    reducers({
        openingId: [
            null as string | null,
            {
                setOpeningId: (_, { id }) => id,
            },
        ],
    }),
    selectors({
        newSuggestionCount: [
            (s) => [s.suggestions],
            (suggestions: MetricsSuggestedDashboardApi[] | null): number =>
                (suggestions ?? []).filter((item) => item.dashboard_id === null).length,
        ],
    }),
    listeners(({ actions, values }) => ({
        menuOpened: () => {
            posthog.capture('metrics suggested dashboards opened', {
                suggestion_count: values.suggestions?.length ?? 0,
            })
            actions.loadSuggestions()
        },
        openSuggestion: async ({ suggestion }) => {
            if (values.openingId) {
                return
            }
            if (suggestion.dashboard_id !== null) {
                router.actions.push(urls.dashboard(suggestion.dashboard_id))
                return
            }
            actions.setOpeningId(suggestion.id)
            try {
                const { dashboard_id } = await metricsSuggestedDashboardsDashboardCreate(
                    String(values.currentTeamId),
                    suggestion.id
                )
                router.actions.push(urls.dashboard(dashboard_id))
                actions.loadSuggestions()
            } catch (error) {
                lemonToast.error(
                    error instanceof ApiError && error.detail ? error.detail : 'Could not create the dashboard.'
                )
            } finally {
                actions.setOpeningId(null)
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadSuggestions()
    }),
])