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.

7.6 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

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

import { Breadcrumb } from '~/types'

import { identityMatchingLinksList, identityMatchingLinksRunsRetrieve } from './generated/api'
import type {
    IdentityMatchingLinkApi,
    IdentityMatchingLinksListParams,
    IdentityMatchingLinksListTier,
    IdentityMatchingLinksResponseApi,
    IdentityMatchingRunApi,
    IdentityMatchingRunsResponseApi,
} from './generated/api.schemas'

export interface IdentityMatchingFilters {
    jobId: string | null
    modelVersion: string | null
    tier: IdentityMatchingLinksListTier | null
    minScore: number | null
    search: string
}

const DEFAULT_FILTERS: IdentityMatchingFilters = {
    jobId: null,
    modelVersion: null,
    tier: null,
    minScore: null,
    search: '',
}

export const LINKS_PAGE_SIZE = 50

export type IdentityMatchingTab = 'links' | 'paid_attribution' | 'runs'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface identityMatchingLogicValues {
    activeTab: IdentityMatchingTab
    breadcrumbs: Breadcrumb[]
    filters: IdentityMatchingFilters
    links: IdentityMatchingLinkApi[]
    linksCount: number
    linksResponse: IdentityMatchingLinksResponseApi | null
    linksResponseLoading: boolean
    modelVersions: string[]
    page: number
    runs: IdentityMatchingRunApi[]
    runsResponse: IdentityMatchingRunsResponseApi | null
    runsResponseLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface identityMatchingLogicActions {
    loadLinks: (_: unknown) => unknown
    loadLinksFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadLinksSuccess: (
        linksResponse: IdentityMatchingLinksResponseApi | null,
        payload?: unknown
    ) => {
        linksResponse: IdentityMatchingLinksResponseApi | null
        payload?: unknown
    }
    loadRuns: () => any
    loadRunsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRunsSuccess: (
        runsResponse: IdentityMatchingRunsResponseApi,
        payload?: any
    ) => {
        runsResponse: IdentityMatchingRunsResponseApi
        payload?: any
    }
    setActiveTab: (tab: IdentityMatchingTab) => {
        tab: IdentityMatchingTab
    }
    setFilters: (filters: Partial<IdentityMatchingFilters>) => {
        filters: Partial<IdentityMatchingFilters>
    }
    setPage: (page: number) => {
        page: number
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface identityMatchingLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        links: (linksResponse: IdentityMatchingLinksResponseApi | null) => IdentityMatchingLinkApi[]
        linksCount: (linksResponse: IdentityMatchingLinksResponseApi | null) => number
        runs: (runsResponse: IdentityMatchingRunsResponseApi | null) => IdentityMatchingRunApi[]
        modelVersions: (runs: IdentityMatchingRunApi[]) => string[]
    }
}

export type identityMatchingLogicType = MakeLogicType<
    identityMatchingLogicValues,
    identityMatchingLogicActions,
    Record<string, any>,
    identityMatchingLogicMeta
>

export const identityMatchingLogic = kea<identityMatchingLogicType>([
    path(['products', 'growth', 'frontend', 'identityMatchingLogic']),
    actions({
        setFilters: (filters: Partial<IdentityMatchingFilters>) => ({ filters }),
        setPage: (page: number) => ({ page }),
        setActiveTab: (tab: IdentityMatchingTab) => ({ tab }),
    }),
    loaders(({ values, cache }) => ({
        runsResponse: {
            __default: null as IdentityMatchingRunsResponseApi | null,
            loadRuns: async () => {
                const projectId = String(teamLogic.values.currentTeamId)
                return await identityMatchingLinksRunsRetrieve(projectId)
            },
        },
        linksResponse: {
            __default: null as IdentityMatchingLinksResponseApi | null,
            loadLinks: async (_: unknown, breakpoint) => {
                await breakpoint(300)
                const projectId = String(teamLogic.values.currentTeamId)
                const { jobId, modelVersion, tier, minScore, search } = values.filters
                const offset = (values.page - 1) * LINKS_PAGE_SIZE
                const params: IdentityMatchingLinksListParams = { limit: LINKS_PAGE_SIZE, offset }
                if (jobId) {
                    params.job_id = jobId
                }
                if (modelVersion) {
                    params.model_version = modelVersion
                }
                if (tier) {
                    params.tier = tier
                }
                if (minScore !== null) {
                    params.min_score = minScore
                }
                if (search.trim()) {
                    params.search = search.trim()
                }
                const requestId = (cache.loadLinksSeq = (cache.loadLinksSeq ?? 0) + 1)
                const response = await identityMatchingLinksList(projectId, params)
                breakpoint()
                if (requestId !== cache.loadLinksSeq) {
                    return values.linksResponse
                }
                return response
            },
        },
    })),
    reducers({
        filters: [
            DEFAULT_FILTERS,
            {
                setFilters: (state, { filters }) => ({ ...state, ...filters }),
            },
        ],
        page: [
            1,
            {
                setPage: (_, { page }) => page,
                setFilters: () => 1,
            },
        ],
        activeTab: [
            'links' as IdentityMatchingTab,
            {
                setActiveTab: (_, { tab }) => tab,
            },
        ],
    }),
    selectors({
        links: [
            (s) => [s.linksResponse],
            (linksResponse: IdentityMatchingLinksResponseApi | null): IdentityMatchingLinkApi[] =>
                linksResponse?.results ?? [],
        ],
        linksCount: [
            (s) => [s.linksResponse],
            (linksResponse: IdentityMatchingLinksResponseApi | null): number => linksResponse?.count ?? 0,
        ],
        runs: [
            (s) => [s.runsResponse],
            (runsResponse: IdentityMatchingRunsResponseApi | null): IdentityMatchingRunApi[] =>
                runsResponse?.results ?? [],
        ],
        modelVersions: [
            (s) => [s.runs],
            (runs: IdentityMatchingRunApi[]): string[] => {
                const versions = new Set<string>()
                for (const run of runs) {
                    for (const model of run.models) {
                        versions.add(model.model_version)
                    }
                }
                return Array.from(versions).sort()
            },
        ],
        breadcrumbs: [
            () => [],
            (): Breadcrumb[] => [
                {
                    key: 'IdentityMatching',
                    name: 'Identity matching',
                    path: urls.identityMatching(),
                    iconType: 'persons',
                },
            ],
        ],
    }),
    listeners(({ actions }) => ({
        setFilters: () => {
            actions.loadLinks(null)
        },
        setPage: () => {
            actions.loadLinks(null)
        },
    })),
    afterMount(({ actions }) => {
        actions.loadRuns()
        actions.loadLinks(null)
    }),
])