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.7 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path } from 'kea'
import type { PostHog } from 'posthog-js'

import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'
import { webVitalsToolbarLogic } from '~/toolbar/shared/web-vitals/webVitalsToolbarLogic'

import type { PageToolbarWebVitalsState } from '../../messaging/pageToolbarBridge'

export type PageWebVitalsCommand = { type: 'reload-web-vitals' }

const NO_WEB_VITALS = { LCP: undefined, FCP: undefined, CLS: undefined, INP: undefined }

let lastPageState: PageToolbarWebVitalsState | null = null

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface extensionPageWebVitalsLogicValues {
    posthog: PostHog | null // toolbarConfigLogic
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface extensionPageWebVitalsLogicActions {
    openWebVitals: () => {
        value: true
    }
    reloadWebVitals: () => {
        value: true
    }
}

export type extensionPageWebVitalsLogicType = MakeLogicType<
    extensionPageWebVitalsLogicValues,
    extensionPageWebVitalsLogicActions
>

/** Runs in the content script. The toolbar's web vitals logic mounts the first time the user opens the tool. */
export const extensionPageWebVitalsLogic = kea<extensionPageWebVitalsLogicType>([
    path(['toolbar', 'extension', 'extensionPageWebVitalsLogic']),
    connect(() => ({
        values: [toolbarConfigLogic, ['posthog']],
    })),
    actions({
        openWebVitals: true,
        reloadWebVitals: true,
    }),
    listeners(() => ({
        openWebVitals: () => {
            if (!webVitalsToolbarLogic.findMounted()) {
                webVitalsToolbarLogic.mount()
            }
        },
        reloadWebVitals: () => {
            webVitalsToolbarLogic.findMounted()?.actions.getWebVitals()
        },
    })),
])

/** Keeps the previous object while nothing changed, so the host doesn't post the slice again. */
export function getPageWebVitalsState(posthog: PostHog | null): PageToolbarWebVitalsState {
    const webVitals = webVitalsToolbarLogic.findMounted()?.values
    const next: PageToolbarWebVitalsState = {
        autocaptureEnabled: !!posthog?.webVitalsAutocapture?.isEnabled,
        pathname: window.location.pathname,
        localWebVitals: webVitals?.localWebVitals ?? NO_WEB_VITALS,
        remoteWebVitals: webVitals?.remoteWebVitals ?? NO_WEB_VITALS,
    }
    const unchanged =
        lastPageState &&
        (Object.keys(next) as (keyof PageToolbarWebVitalsState)[]).every((key) => lastPageState?.[key] === next[key])
    lastPageState = unchanged ? lastPageState : next
    return lastPageState as PageToolbarWebVitalsState
}