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.3 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, path, reducers, selectors } from 'kea'
import { actionToUrl, router, urlToAction } from 'kea-router'

import { LemonSelectOption, LemonSelectOptions } from '@posthog/lemon-ui'

import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { annotationsModel } from '~/models/annotationsModel'
import { AnnotationScope, AnnotationType } from '~/types'

import { annotationScopeToName } from './annotationModalLogic'

const isValidAnnotationScope = (scope: string): scope is AnnotationScope => {
    return Object.values(AnnotationScope).includes(scope as AnnotationScope)
}

export const annotationScopesMenuOptions = (): LemonSelectOptions<AnnotationType['scope'] | null> => {
    const scopeOptions: LemonSelectOption<AnnotationType['scope'] | null>[] = Object.values(AnnotationScope).map(
        (scope) => ({
            value: scope,
            label: annotationScopeToName[scope],
        })
    )
    // add any with value null as the first option
    scopeOptions.unshift({
        value: null,
        label: 'Any',
    })
    return scopeOptions
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface annotationsLogicValues {
    annotations: AnnotationType[] // annotationsModel
    annotationsLoading: boolean // annotationsModel
    loadingNext: boolean // annotationsModel
    next: string | null // annotationsModel
    timezone: string // teamLogic
    filteredAnnotations: AnnotationType[]
    scope: AnnotationType['scope'] | null
    shouldShowEmptyState: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface annotationsLogicActions {
    setScope: (scope: AnnotationType['scope'] | null) => {
        scope: AnnotationScope | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface annotationsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        shouldShowEmptyState: (annotations: AnnotationType[], annotationsLoading: boolean) => boolean
        filteredAnnotations: (annotations: AnnotationType[], scope: AnnotationScope | null) => AnnotationType[]
    }
}

export type annotationsLogicType = MakeLogicType<
    annotationsLogicValues,
    annotationsLogicActions,
    Record<string, any>,
    annotationsLogicMeta
>

export const annotationsLogic = kea<annotationsLogicType>([
    path(['scenes', 'annotations', 'annotationsLogic']),
    connect(() => ({
        values: [
            annotationsModel,
            ['annotations', 'annotationsLoading', 'next', 'loadingNext'],
            teamLogic,
            ['timezone'],
        ],
    })),
    actions({
        setScope: (scope: AnnotationType['scope'] | null) => ({ scope }),
    }),
    reducers(() => ({
        scope: [null as AnnotationType['scope'] | null, { setScope: (_, { scope }) => scope }],
    })),
    selectors(() => ({
        shouldShowEmptyState: [
            (s) => [s.annotations, s.annotationsLoading],
            (annotations: AnnotationType[], annotationsLoading: boolean): boolean => {
                return annotations.length === 0 && !annotationsLoading
            },
        ],
        filteredAnnotations: [
            (s) => [s.annotations, s.scope],
            (annotations: AnnotationType[], scope: AnnotationType['scope'] | null): AnnotationType[] => {
                return scope
                    ? annotations.filter((annotation) => {
                          return annotation.scope === scope
                      })
                    : annotations
            },
        ],
    })),
    actionToUrl(() => ({
        setScope: ({ scope }) => {
            return [
                router.values.location.pathname,
                {
                    ...router.values.searchParams,
                    scope: scope ? scope : undefined,
                },
                router.values.hashParams,
                { replace: true },
            ]
        },
    })),
    urlToAction(({ actions, values }) => ({
        [urls.annotations()]: (_, searchParams) => {
            const scope = searchParams.scope
            if (scope && isValidAnnotationScope(scope) && scope !== values.scope) {
                actions.setScope(scope)
            }
            if (!scope && values.scope) {
                actions.setScope(null)
            }
        },
    })),
])