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.8 KB
Raw Blame
import { MakeLogicType, afterMount, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import { RawAnnotationType } from '~/types'

import { annotationsList } from 'products/annotations/frontend/generated/api'
import type { AnnotationsListParams } from 'products/annotations/frontend/generated/api.schemas'

export interface ProjectAnnotationsLogicProps {
    /** Null when the chart belongs to the current project, whose annotations `annotationsModel` already holds. */
    projectId: number | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectAnnotationsLogicValues {
    rawAnnotations: RawAnnotationType[]
    rawAnnotationsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectAnnotationsLogicActions {
    loadAnnotations: () => any
    loadAnnotationsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadAnnotationsSuccess: (
        rawAnnotations: RawAnnotationType[],
        payload?: any
    ) => {
        rawAnnotations: RawAnnotationType[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectAnnotationsLogicMeta {
    key: number | 'current'
}

export type projectAnnotationsLogicType = MakeLogicType<
    projectAnnotationsLogicValues,
    projectAnnotationsLogicActions,
    ProjectAnnotationsLogicProps,
    projectAnnotationsLogicMeta
>

/** `annotationsModel` holds only the current project's annotations, so a chart from another project reads them here. */
export const projectAnnotationsLogic = kea<projectAnnotationsLogicType>([
    props({} as ProjectAnnotationsLogicProps),
    key(({ projectId }) => projectId ?? 'current'),
    path((key) => ['lib', 'components', 'AnnotationsOverlay', 'projectAnnotationsLogic', key]),
    loaders(({ props }) => ({
        rawAnnotations: [
            [] as RawAnnotationType[],
            {
                loadAnnotations: async () => {
                    if (props.projectId === null) {
                        return []
                    }
                    // The endpoint filters on this parameter, but its schema does not declare it.
                    const params = { hidden_in_user_interface: false } as AnnotationsListParams
                    const response = await annotationsList(String(props.projectId), params)
                    // The overlay works on the handwritten annotation type, which describes this same payload.
                    return response.results as unknown as RawAnnotationType[]
                },
            },
        ],
    })),
    afterMount(({ actions, props }) => {
        if (props.projectId !== null) {
            actions.loadAnnotations()
        }
    }),
])