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.

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

import { teamLogic } from 'scenes/teamLogic'

import type { DataTableNode, DataVisualizationNode } from '~/queries/schema/schema-general'

import { aeoPromptsOverview } from 'products/web_analytics/frontend/generated/api'
import type { AEOPromptSetOverviewApi } from 'products/web_analytics/frontend/generated/api.schemas'

import { ContentAutopilotWorkspaceTab, contentAutopilotLogic } from './contentAutopilotLogic'
import { contentAutopilotPromptDetailLogic } from './contentAutopilotPromptDetailLogic'
import {
    ContentAutopilotPromptsError,
    contentAutopilotPromptsLogic,
    promptsErrorFrom,
} from './contentAutopilotPromptsLogic'
import { PromptSetWindow, promptSetCitationQuery, promptSetSourcesQuery } from './promptReportQueries'
import { PromptsOverviewKpis, promptsOverviewHasChecks, promptsOverviewKpis } from './promptsOverview'

export const OVERVIEW_DAYS = 30

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface contentAutopilotPromptsOverviewLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    citationQuery: DataVisualizationNode | null
    hasChecks: boolean
    overview: AEOPromptSetOverviewApi | null
    overviewError: ContentAutopilotPromptsError | null
    overviewKpis: PromptsOverviewKpis | null
    overviewLoading: boolean
    overviewWindow: PromptSetWindow | null
    sourcesQuery: DataTableNode | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface contentAutopilotPromptsOverviewLogicActions {
    setWorkspaceTab: (workspaceTab: ContentAutopilotWorkspaceTab) => {
        workspaceTab: ContentAutopilotWorkspaceTab
    } // contentAutopilotLogic
    savePromptEditSuccess: (prompt: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptApi) => {
        prompt: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptApi
    } // contentAutopilotPromptDetailLogic
    setPromptArchivedSuccess: (
        prompt: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptApi
    ) => {
        prompt: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptApi
    } // contentAutopilotPromptDetailLogic
    addPromptsSuccess: (
        result: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptImportResultApi
    ) => {
        result: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptImportResultApi
    } // contentAutopilotPromptsLogic
    setPromptActiveSuccess: (prompt: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptApi) => {
        prompt: import('products/web_analytics/frontend/generated/api.schemas').AEOPromptApi
    } // contentAutopilotPromptsLogic
    setPromptsTopic: (topic: string | null) => {
        topic: string | null
    } // contentAutopilotPromptsLogic
    loadPromptSetOverview: () => {
        value: true
    }
    loadPromptSetOverviewFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPromptSetOverviewSuccess: (
        overview: AEOPromptSetOverviewApi,
        payload?: {
            value: true
        }
    ) => {
        overview: AEOPromptSetOverviewApi
        payload?: {
            value: true
        }
    }
    selectOverviewTopic: (topic: string) => {
        topic: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface contentAutopilotPromptsOverviewLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        overviewKpis: (overview: AEOPromptSetOverviewApi | null) => PromptsOverviewKpis | null
        hasChecks: (overview: AEOPromptSetOverviewApi | null) => boolean
        overviewWindow: (overview: AEOPromptSetOverviewApi | null) => PromptSetWindow | null
        citationQuery: (overviewWindow: PromptSetWindow | null) => DataVisualizationNode | null
        sourcesQuery: (overviewWindow: PromptSetWindow | null) => DataTableNode | null
    }
}

export type contentAutopilotPromptsOverviewLogicType = MakeLogicType<
    contentAutopilotPromptsOverviewLogicValues,
    contentAutopilotPromptsOverviewLogicActions,
    Record<string, any>,
    contentAutopilotPromptsOverviewLogicMeta
>

export const contentAutopilotPromptsOverviewLogic = kea<contentAutopilotPromptsOverviewLogicType>([
    path(['products', 'webAnalytics', 'contentAutopilot', 'contentAutopilotPromptsOverviewLogic']),
    connect(() => ({
        values: [teamLogic, ['currentTeamIdStrict']],
        actions: [
            contentAutopilotPromptsLogic,
            ['setPromptsTopic', 'setPromptActiveSuccess', 'addPromptsSuccess'],
            contentAutopilotPromptDetailLogic,
            ['setPromptArchivedSuccess', 'savePromptEditSuccess'],
            contentAutopilotLogic,
            ['setWorkspaceTab'],
        ],
    })),
    actions({
        loadPromptSetOverview: true,
        selectOverviewTopic: (topic: string) => ({ topic }),
    }),
    loaders(({ values }) => ({
        overview: [
            null as AEOPromptSetOverviewApi | null,
            {
                loadPromptSetOverview: async (_, breakpoint) => {
                    try {
                        const overview = await aeoPromptsOverview(String(values.currentTeamIdStrict), {
                            days: OVERVIEW_DAYS,
                        })
                        breakpoint()
                        return overview
                    } catch (error) {
                        // A superseded request's failure must not replace the newer overview with an error
                        breakpoint()
                        throw error
                    }
                },
            },
        ],
    })),
    reducers({
        overviewError: [
            null as ContentAutopilotPromptsError | null,
            {
                loadPromptSetOverview: () => null,
                loadPromptSetOverviewFailure: (_, { errorObject }) => promptsErrorFrom(errorObject),
            },
        ],
    }),
    selectors({
        overviewKpis: [
            (s) => [s.overview],
            (overview: AEOPromptSetOverviewApi | null): PromptsOverviewKpis | null =>
                overview ? promptsOverviewKpis(overview) : null,
        ],
        hasChecks: [
            (s) => [s.overview],
            (overview: AEOPromptSetOverviewApi | null): boolean =>
                overview ? promptsOverviewHasChecks(overview) : false,
        ],
        overviewWindow: [
            (s) => [s.overview],
            (overview: AEOPromptSetOverviewApi | null): PromptSetWindow | null =>
                overview ? { from: overview.window_from } : null,
        ],
        citationQuery: [
            (s) => [s.overviewWindow],
            (overviewWindow: PromptSetWindow | null): DataVisualizationNode | null =>
                overviewWindow ? promptSetCitationQuery(overviewWindow) : null,
        ],
        sourcesQuery: [
            (s) => [s.overviewWindow],
            (overviewWindow: PromptSetWindow | null): DataTableNode | null =>
                overviewWindow ? promptSetSourcesQuery(overviewWindow) : null,
        ],
    }),
    listeners(({ actions }) => ({
        selectOverviewTopic: ({ topic }) => {
            // pinned: analytics event name, renaming it breaks dashboards
            posthog.capture('web analytics content autopilot prompts overview topic selected')
            actions.setPromptsTopic(topic)
            actions.setWorkspaceTab('prompts')
        },
        // Adding, editing, turning on or off, or archiving a prompt changes the counts.
        setPromptActiveSuccess: () => actions.loadPromptSetOverview(),
        setPromptArchivedSuccess: () => actions.loadPromptSetOverview(),
        addPromptsSuccess: () => actions.loadPromptSetOverview(),
        savePromptEditSuccess: () => actions.loadPromptSetOverview(),
    })),
    afterMount(({ actions }) => {
        actions.loadPromptSetOverview()
    }),
])