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

import api from 'lib/api'

import { HogFunctionIconResponse } from '~/types'

export interface HogFunctionIconLogicProps {
    logicKey: string
    src?: string
    onChange?: (src: string) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionIconLogicValues {
    possibleIcons: HogFunctionIconResponse[] | null
    possibleIconsLoading: boolean
    searchTerm: string | null
    showPopover: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionIconLogicActions {
    loadPossibleIcons: () => {
        value: true
    }
    loadPossibleIconsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPossibleIconsSuccess: (
        possibleIcons: {
            id: string
            name: string
            url: string
        }[],
        payload?: {
            value: true
        }
    ) => {
        possibleIcons: {
            id: string
            name: string
            url: string
        }[]
        payload?: {
            value: true
        }
    }
    setSearchTerm: (search: string) => {
        search: string
    }
    setShowPopover: (show: boolean) => {
        show: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionIconLogicMeta {
    key: string
}

export type hogFunctionIconLogicType = MakeLogicType<
    hogFunctionIconLogicValues,
    hogFunctionIconLogicActions,
    HogFunctionIconLogicProps,
    hogFunctionIconLogicMeta
>

export const hogFunctionIconLogic = kea<hogFunctionIconLogicType>([
    props({} as HogFunctionIconLogicProps),
    key((props) => props.logicKey ?? 'default'),
    path((key) => ['scenes', 'pipeline', 'hogfunctions', 'hogFunctionIconLogic', key]),

    actions({
        loadPossibleIcons: true,
        setShowPopover: (show: boolean) => ({ show }),
        setSearchTerm: (search: string) => ({ search }),
    }),

    reducers({
        showPopover: [
            false,
            {
                setShowPopover: (_, { show }) => show,
            },
        ],

        searchTerm: [
            null as string | null,
            {
                setSearchTerm: (_, { search }) => search,
                setShowPopover: () => null,
            },
        ],
    }),

    loaders(({ values }) => ({
        possibleIcons: [
            null as HogFunctionIconResponse[] | null,
            {
                loadPossibleIcons: async (_, breakpoint) => {
                    const search = values.searchTerm

                    if (!search) {
                        return []
                    }

                    await breakpoint(1000)
                    const res = await api.hogFunctions.listIcons({ query: search })
                    return res.map((icon) => ({
                        ...icon,
                        url: icon.url + '&temp=true',
                    }))
                },
            },
        ],
    })),

    listeners(({ actions }) => ({
        setShowPopover: ({ show }) => {
            if (show) {
                actions.loadPossibleIcons()
            }
        },

        setSearchTerm: () => {
            actions.loadPossibleIcons()
        },
    })),
])