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.2 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

export interface VersionPinResponse {
    requested_version: string | null
    resolved_version: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface jsSnippetVersionPinLogicValues {
    localPin: string
    versionPinResponse: VersionPinResponse | null
    versionPinResponseLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface jsSnippetVersionPinLogicActions {
    loadVersionPin: () => any
    loadVersionPinFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadVersionPinSuccess: (
        versionPinResponse: VersionPinResponse | null,
        payload?: any
    ) => {
        versionPinResponse: VersionPinResponse | null
        payload?: any
    }
    saveVersionPin: ({ pin }: { pin: string | null }) => {
        pin: string | null
    }
    saveVersionPinFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    saveVersionPinSuccess: (
        versionPinResponse: VersionPinResponse | null,
        payload?: {
            pin: string | null
        }
    ) => {
        versionPinResponse: VersionPinResponse | null
        payload?: {
            pin: string | null
        }
    }
    setLocalPin: (localPin: string) => {
        localPin: string
    }
}

export type jsSnippetVersionPinLogicType = MakeLogicType<
    jsSnippetVersionPinLogicValues,
    jsSnippetVersionPinLogicActions
>

export const jsSnippetVersionPinLogic = kea<jsSnippetVersionPinLogicType>([
    path(['scenes', 'settings', 'environment', 'jsSnippetVersionPinLogic']),
    actions({
        setLocalPin: (localPin: string) => ({ localPin }),
    }),
    loaders(({ actions }) => ({
        versionPinResponse: [
            null as VersionPinResponse | null,
            {
                loadVersionPin: async () => {
                    const teamId = teamLogic.values.currentTeamId
                    if (!teamId) {
                        return null
                    }
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                    return await api.get(`api/projects/${teamId}/js-snippet/version`)
                },
                saveVersionPin: async ({ pin }: { pin: string | null }) => {
                    const teamId = teamLogic.values.currentTeamId
                    if (!teamId) {
                        return null
                    }
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                    const response = await api.update(`api/projects/${teamId}/js-snippet/version`, {
                        js_snippet_version: pin,
                    })
                    lemonToast.success('Snippet version updated')
                    actions.setLocalPin(response.requested_version ?? '')
                    return response
                },
            },
        ],
    })),
    reducers({
        localPin: [
            '' as string,
            {
                setLocalPin: (_, { localPin }) => localPin,
                loadVersionPinSuccess: (_, { versionPinResponse }) => versionPinResponse?.requested_version ?? '',
            },
        ],
    }),
    listeners(() => ({
        saveVersionPinFailure: ({ error }) => {
            lemonToast.error(error || 'Failed to update version')
        },
    })),
    afterMount(({ actions }) => {
        actions.loadVersionPin()
    }),
])