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

import { teamLogic } from 'scenes/teamLogic'

import { logsViewsList } from 'products/logs/frontend/generated/api'
import type { LogsViewApi } from 'products/logs/frontend/generated/api.schemas'

// One page is plenty — saved views are few, so we never paginate the dropdown.
const SAVED_VIEWS_LIMIT = 100

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsWidgetSavedViewsLogicValues {
    currentProjectId: number | string // teamLogic
    hasLoadedSavedViews: boolean
    savedViewLabelById: Record<string, string>
    savedViewOptions: {
        label: string
        value: string
    }[]
    savedViews: LogsViewApi[]
    savedViewsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsWidgetSavedViewsLogicActions {
    ensureSavedViewsLoaded: () => {
        value: true
    }
    loadSavedViews: () => any
    loadSavedViewsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSavedViewsSuccess: (
        savedViews: LogsViewApi[],
        payload?: any
    ) => {
        savedViews: LogsViewApi[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsWidgetSavedViewsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        savedViewOptions: (savedViews: LogsViewApi[]) => {
            label: string
            value: string
        }[]
        savedViewLabelById: (
            savedViewOptions: {
                label: string
                value: string
            }[]
        ) => Record<string, string>
    }
}

export type logsWidgetSavedViewsLogicType = MakeLogicType<
    logsWidgetSavedViewsLogicValues,
    logsWidgetSavedViewsLogicActions,
    Record<string, any>,
    logsWidgetSavedViewsLogicMeta
>

// Saved logs filters are LogsView rows ("saved views" on the logs page). The fetch is lazy —
// only tiles that actually surface the picker (feature flag on, or a saved view already persisted)
// trigger it, so tiles without the feature never hit the API.
export const logsWidgetSavedViewsLogic = kea<logsWidgetSavedViewsLogicType>([
    path(['products', 'dashboards', 'widgets', 'logs', 'logsWidgetSavedViewsLogic']),
    connect(() => ({
        values: [teamLogic, ['currentProjectId']],
    })),
    actions({
        ensureSavedViewsLoaded: true,
    }),
    reducers({
        hasLoadedSavedViews: [false, { loadSavedViewsSuccess: () => true }],
    }),
    loaders(({ values }) => ({
        savedViews: {
            __default: [] as LogsViewApi[],
            loadSavedViews: async () => {
                const response = await logsViewsList(String(values.currentProjectId), { limit: SAVED_VIEWS_LIMIT })
                return response.results ?? []
            },
        },
    })),
    selectors({
        savedViewOptions: [
            (s) => [s.savedViews],
            (savedViews: LogsViewApi[]): { value: string; label: string }[] =>
                savedViews.map((view) => ({
                    value: view.short_id,
                    label: view.name || 'Unnamed view',
                })),
        ],
        // Single source for resolving a saved-view short_id to its display label, shared by the
        // tile filter bar and the read-only summary.
        savedViewLabelById: [
            (s) => [s.savedViewOptions],
            (
                savedViewOptions: {
                    label: string
                    value: string
                }[]
            ): Record<string, string> =>
                Object.fromEntries(savedViewOptions.map((option) => [option.value, option.label])),
        ],
    }),
    listeners(({ actions, values }) => ({
        ensureSavedViewsLoaded: () => {
            if (!values.hasLoadedSavedViews && !values.savedViewsLoading) {
                actions.loadSavedViews()
            }
        },
    })),
])