: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,
afterMount,
isBreakpoint,
kea,
key,
listeners,
path,
props,
reducers,
selectors,
} from 'kea'
import { ApiConfig } from 'lib/api'
import { integrationsGithubReposRetrieve } from 'products/integrations/frontend/generated/api'
import type { GitHubRepoApi } from 'products/integrations/frontend/generated/api.schemas'
const PAGE_SIZE = 50
const FULL_LIST_PAGE_SIZE = 500
const SEARCH_DEBOUNCE_MS = 300
export interface GithubRepositorySearchLogicProps {
id: number
/** Keep requesting pages until the installation's repository list is exhausted. */
loadAll?: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubRepositorySearchLogicValues {
currentOffset: number
error: string | null
hasMore: boolean
loading: boolean
repositories: GitHubRepoApi[]
repositoryNames: string[]
searchQuery: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubRepositorySearchLogicActions {
loadMore: () => {
value: true
}
loadPage: (offset: number) => {
offset: number
}
loadPageFailure: (error: string) => {
error: string
}
loadPageSuccess: (
repositories: GitHubRepoApi[],
hasMore: boolean,
offset: number
) => {
hasMore: boolean
offset: number
repositories: GitHubRepoApi[]
}
refresh: () => {
value: true
}
setSearchQuery: (searchQuery: string) => {
searchQuery: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubRepositorySearchLogicMeta {
key: number | string
__keaTypeGenInternalSelectorTypes: {
repositoryNames: (repositories: GitHubRepoApi[]) => string[]
}
}
export type githubRepositorySearchLogicType = MakeLogicType<
githubRepositorySearchLogicValues,
githubRepositorySearchLogicActions,
GithubRepositorySearchLogicProps,
githubRepositorySearchLogicMeta
>
/**
* Server-side searchable, paginated GitHub repository source for a single integration. Unlike
* {@link githubIntegrationLogic} — which eagerly bulk-loads every repository in 500-row pages and ignores
* the search term — this drives the `github_repos` endpoint's `search`/`limit`/`offset` params directly so
* a repo picker can search across very large accounts without loading the whole list. Pass `loadAll` when
* the caller has to offer every repository. Keyed by integration id so concurrent pickers stay independent;
* `loadAll` is part of the key so a full-list picker does not share a first-page instance.
*/
export const githubRepositorySearchLogic = kea<githubRepositorySearchLogicType>([
props({} as GithubRepositorySearchLogicProps),
key((props) => (props.loadAll ? `all:${props.id}` : props.id)),
path((key) => ['lib', 'integrations', 'githubRepositorySearchLogic', key]),
actions({
setSearchQuery: (searchQuery: string) => ({ searchQuery }),
loadPage: (offset: number) => ({ offset }),
loadPageSuccess: (repositories: GitHubRepoApi[], hasMore: boolean, offset: number) => ({
repositories,
hasMore,
offset,
}),
loadPageFailure: (error: string) => ({ error }),
loadMore: true,
refresh: true,
}),
reducers(({ props }) => ({
searchQuery: [
'',
{
setSearchQuery: (_, { searchQuery }) => searchQuery,
},
],
repositories: [
[] as GitHubRepoApi[],
{
// A new query or refresh starts a fresh list; the first page (offset 0) replaces, later pages
// append (deduped by id) so "Load more" extends the current result set.
setSearchQuery: () => [],
refresh: () => [],
loadPageSuccess: (state, { repositories, offset }) => {
if (offset === 0) {
return repositories
}
const seenIds = new Set(state.map((r) => r.id))
return [...state, ...repositories.filter((r: GitHubRepoApi) => !seenIds.has(r.id))]
},
},
],
loading: [
false,
{
setSearchQuery: () => true,
refresh: () => true,
loadPage: () => true,
// Stay in the loading state between pages so the first page does not look like the full list.
loadPageSuccess: (_, { hasMore }) => !!(props.loadAll && hasMore),
loadPageFailure: () => false,
},
],
hasMore: [
false,
{
loadPageSuccess: (_, { hasMore }) => hasMore,
loadPageFailure: () => false,
},
],
error: [
null as string | null,
{
setSearchQuery: () => null,
refresh: () => null,
loadPage: () => null,
loadPageSuccess: () => null,
loadPageFailure: (_, { error }) => error,
},
],
currentOffset: [
0,
{
setSearchQuery: () => 0,
refresh: () => 0,
loadPageSuccess: (_, { offset }) => offset + (props.loadAll ? FULL_LIST_PAGE_SIZE : PAGE_SIZE),
},
],
})),
selectors({
// The picker selects on `owner/repo` (matches the Task API's repository format).
repositoryNames: [
(s) => [s.repositories],
(repositories: GitHubRepoApi[]): string[] => repositories.map((r) => r.full_name),
],
}),
listeners(({ actions, values, props }) => ({
// Debounce keystrokes so a multi-character search fires one request, not one per character.
setSearchQuery: async (_, breakpoint) => {
await breakpoint(SEARCH_DEBOUNCE_MS)
actions.loadPage(0)
},
refresh: () => {
actions.loadPage(0)
},
loadMore: () => {
if (values.hasMore && !values.loading) {
actions.loadPage(values.currentOffset)
}
},
loadPageSuccess: ({ hasMore, repositories }) => {
// An allowlist picker has to offer every repo. An empty page means the offset ran past the end.
if (props.loadAll && hasMore && repositories.length > 0) {
actions.loadPage(values.currentOffset)
}
},
loadPage: async ({ offset }, breakpoint) => {
// Snapshot the query driving this request so a response that resolves after the search
// has moved on (e.g. a slow "Load more" landing mid-typing) can be discarded instead of
// committing stale repositories onto the new result set.
const query = values.searchQuery.trim()
try {
const response = await integrationsGithubReposRetrieve(String(ApiConfig.getCurrentTeamId()), props.id, {
limit: props.loadAll ? FULL_LIST_PAGE_SIZE : PAGE_SIZE,
offset,
search: query || undefined,
})
await breakpoint()
if (query !== values.searchQuery.trim()) {
return
}
actions.loadPageSuccess(response.repositories, response.has_more, offset)
} catch (e: any) {
if (isBreakpoint(e)) {
throw e
}
actions.loadPageFailure(e?.detail || e?.message || 'Failed to load repositories.')
}
},
})),
afterMount(({ actions }) => {
actions.loadPage(0)
}),
])