: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import {
MakeLogicType,
actions,
events,
isBreakpoint,
kea,
key,
listeners,
path,
props,
propsChanged,
reducers,
} from 'kea'
import { usersIntegrationsGithubReposRetrieve } from '~/generated/core/api'
import type { GitHubRepoApi } from 'products/integrations/frontend/generated/api.schemas'
const PAGE_SIZE = 500
export interface UserGitHubIntegrationLogicProps {
installationId: string
/** GitHub's scope for the installation. "all" is summarized by its total instead of being paged through. */
repositorySelection?: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface userGithubIntegrationLogicValues {
currentOffset: number
repositories: GitHubRepoApi[]
repositoriesLoading: boolean
repositoriesTotal: number | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface userGithubIntegrationLogicActions {
loadRepositories: () => {
value: true
}
loadRepositoriesPage: (offset: number) => {
offset: number
}
loadRepositoriesPageFailure: () => {
value: true
}
loadRepositoriesPageSuccess: (
repositories: GitHubRepoApi[],
hasMore: boolean,
total?: number | null
) => {
hasMore: boolean
repositories: GitHubRepoApi[]
total: number | null
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface userGithubIntegrationLogicMeta {
key: string
}
export type userGithubIntegrationLogicType = MakeLogicType<
userGithubIntegrationLogicValues,
userGithubIntegrationLogicActions,
UserGitHubIntegrationLogicProps,
userGithubIntegrationLogicMeta
>
export const userGithubIntegrationLogic = kea<userGithubIntegrationLogicType>([
path(['lib', 'integrations', 'userGithubIntegrationLogic']),
props({} as UserGitHubIntegrationLogicProps),
key((props) => props.installationId),
actions({
loadRepositories: true,
loadRepositoriesPage: (offset: number) => ({ offset }),
loadRepositoriesPageSuccess: (
repositories: GitHubRepoApi[],
hasMore: boolean,
total: number | null = null
) => ({
repositories,
hasMore,
total,
}),
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,
},
],
repositoriesTotal: [
null as number | null,
{
// Clear the cached total when a reload starts so a scope change can't briefly (or, on a
// slow/failed refetch, indefinitely) show the previous selection's count.
loadRepositories: () => null,
loadRepositoriesPageSuccess: (state, { total }) => total ?? state,
},
],
currentOffset: [
0,
{
loadRepositories: () => 0,
loadRepositoriesPageSuccess: (state) => state + PAGE_SIZE,
},
],
}),
listeners(({ actions, values, props: logicProps }) => ({
loadRepositories: () => {
actions.loadRepositoriesPage(0)
},
loadRepositoriesPageSuccess: ({ hasMore }) => {
if (hasMore) {
actions.loadRepositoriesPage(values.currentOffset)
}
},
loadRepositoriesPage: async ({ offset }, breakpoint) => {
try {
const response = await usersIntegrationsGithubReposRetrieve('@me', logicProps.installationId, {
limit: PAGE_SIZE,
offset,
})
await breakpoint()
const summarizedByTotal = logicProps.repositorySelection === 'all'
actions.loadRepositoriesPageSuccess(
response.repositories,
summarizedByTotal ? false : response.has_more,
response.total ?? null
)
} catch (e: any) {
if (isBreakpoint(e)) {
throw e
}
actions.loadRepositoriesPageFailure()
}
},
})),
// The logic is keyed by installation id, so a scope change (all vs selected) reuses this instance:
// polling and the focus refetch can flip repository_selection in place, and the cached list and
// total would otherwise keep describing the previous scope until a full page reload.
propsChanged(({ actions, props }, previousProps) => {
if (props.installationId && props.repositorySelection !== previousProps.repositorySelection) {
actions.loadRepositories()
}
}),
events(({ actions, props }) => ({
afterMount: () => {
if (props.installationId) {
actions.loadRepositories()
}
},
})),
])