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

import api from 'lib/api'

import type { GitHubRepoApi } from 'products/integrations/frontend/generated/api.schemas'

const PAGE_SIZE = 500

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubIntegrationLogicValues {
    currentOffset: number
    repositories: GitHubRepoApi[]
    repositoriesLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubIntegrationLogicActions {
    loadRepositories: () => {
        value: true
    }
    loadRepositoriesPage: (offset: number) => {
        offset: number
    }
    loadRepositoriesPageFailure: () => {
        value: true
    }
    loadRepositoriesPageSuccess: (
        repositories: GitHubRepoApi[],
        hasMore: boolean
    ) => {
        hasMore: boolean
        repositories: GitHubRepoApi[]
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubIntegrationLogicProps {
    id: number
}

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

export type githubIntegrationLogicType = MakeLogicType<
    githubIntegrationLogicValues,
    githubIntegrationLogicActions,
    githubIntegrationLogicProps,
    githubIntegrationLogicMeta
>

export const githubIntegrationLogic = kea<githubIntegrationLogicType>([
    props({} as { id: number }),
    key((props) => props.id),
    path((key) => ['lib', 'integrations', 'githubIntegrationLogic', key]),

    actions({
        loadRepositories: true,
        loadRepositoriesPage: (offset: number) => ({ offset }),
        loadRepositoriesPageSuccess: (repositories: GitHubRepoApi[], hasMore: boolean) => ({
            repositories,
            hasMore,
        }),
        loadRepositoriesPageFailure: true,
    }),

    reducers({
        repositories: [
            [] as GitHubRepoApi[],
            {
                loadRepositories: () => [],
                loadRepositoriesPageSuccess: (state, { repositories }) => {
                    const seenIds = new Set(state.map((r) => r.id))
                    const newRepos = repositories.filter((r) => !seenIds.has(r.id))
                    return [...state, ...newRepos]
                },
            },
        ],
        repositoriesLoading: [
            false,
            {
                loadRepositories: () => true,
                loadRepositoriesPageSuccess: (_, { hasMore }) => hasMore,
                loadRepositoriesPageFailure: () => false,
            },
        ],
        currentOffset: [
            0,
            {
                loadRepositories: () => 0,
                loadRepositoriesPageSuccess: (state) => state + PAGE_SIZE,
            },
        ],
    }),

    listeners(({ actions, values, props }) => ({
        loadRepositories: () => {
            actions.loadRepositoriesPage(0)
        },
        loadRepositoriesPageSuccess: ({ hasMore }) => {
            if (hasMore) {
                actions.loadRepositoriesPage(values.currentOffset)
            }
        },
        loadRepositoriesPage: async ({ offset }, breakpoint) => {
            try {
                const response = await api.integrations.githubRepositories(props.id, {
                    limit: PAGE_SIZE,
                    offset,
                })
                await breakpoint()
                actions.loadRepositoriesPageSuccess(response.repositories, response.has_more)
            } catch (e: any) {
                if (isBreakpoint(e)) {
                    throw e
                }
                actions.loadRepositoriesPageFailure()
            }
        },
    })),
])