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.

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

import { baseObjectType } from 'scenes/library/libraryUtils'
import { teamLogic } from 'scenes/teamLogic'

import { Noun, groupsModel } from '~/models/groupsModel'
import { CurrencyCode } from '~/queries/schema/schema-general'

import { TodayObjectGlance, fetchGlance } from './todayObjectGlances'

const glanceKey = (type: string | undefined, ref: string): string => `${baseObjectType(type)}:${ref}`

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayObjectGlanceLogicValues {
    aggregationLabel: (groupTypeIndex: number | null | undefined, deferToUserWording?: boolean) => Noun // groupsModel
    baseCurrency: CurrencyCode // teamLogic
    currentProjectId: number | string // teamLogic
    fetched: Record<string, TodayObjectGlance | null>
    glanceFor: (type: string | undefined, ref: string) => TodayObjectGlance | null | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayObjectGlanceLogicActions {
    setGlance: (
        key: string,
        glance: TodayObjectGlance | null
    ) => {
        glance: TodayObjectGlance | null
        key: string
    }
    showGlance: (
        type: string | undefined,
        ref: string
    ) => {
        ref: string
        type: string | undefined
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayObjectGlanceLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        glanceFor: (
            fetched: Record<string, TodayObjectGlance | null>
        ) => (type: string | undefined, ref: string) => TodayObjectGlance | null | undefined
    }
}

export type todayObjectGlanceLogicType = MakeLogicType<
    todayObjectGlanceLogicValues,
    todayObjectGlanceLogicActions,
    Record<string, any>,
    todayObjectGlanceLogicMeta
>

export const todayObjectGlanceLogic = kea<todayObjectGlanceLogicType>([
    path(['layout', 'today', 'todayObjectGlanceLogic']),
    connect(() => ({
        values: [teamLogic, ['baseCurrency', 'currentProjectId'], groupsModel, ['aggregationLabel']],
    })),
    actions({
        showGlance: (type: string | undefined, ref: string) => ({ type, ref }),
        setGlance: (key: string, glance: TodayObjectGlance | null) => ({ key, glance }),
    }),
    reducers({
        fetched: [
            {} as Record<string, TodayObjectGlance | null>,
            { setGlance: (state, { key, glance }) => ({ ...state, [key]: glance }) },
        ],
    }),
    selectors({
        glanceFor: [
            (s) => [s.fetched],
            (fetched: Record<string, TodayObjectGlance | null>) =>
                (type: string | undefined, ref: string): TodayObjectGlance | null | undefined =>
                    fetched[glanceKey(type, ref)],
        ],
    }),
    listeners(({ actions, values, cache }) => ({
        showGlance: async ({ type, ref }) => {
            posthog.capture('today object preview shown', { object_type: baseObjectType(type) })
            const key = glanceKey(type, ref)
            cache.pending ??= new Set<string>()
            cache.failed ??= new Set<string>()
            if ((key in values.fetched && !cache.failed.has(key)) || cache.pending.has(key)) {
                return
            }
            cache.pending.add(key)
            try {
                actions.setGlance(
                    key,
                    await fetchGlance(String(values.currentProjectId), type, ref, {
                        currency: values.baseCurrency,
                        aggregationLabel: values.aggregationLabel,
                    })
                )
                cache.failed.delete(key)
            } catch {
                actions.setGlance(key, null)
                cache.failed.add(key)
            } finally {
                cache.pending.delete(key)
            }
        },
    })),
])