: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, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { ApiError } from 'lib/api'
import {
usersIntegrationsGithubInstallRequestsDestroy,
usersIntegrationsGithubInstallRequestsRetrieve,
} from '~/generated/core/api'
import type { GitHubInstallRequestItemApi, GitHubInstallRequestListResponseApi } from '~/generated/core/api.schemas'
const INSTALL_REQUESTS_POLL_INTERVAL_MS = 15_000
// A background poll shouldn't keep hitting an endpoint that is down for the whole mount, so give up
// after a few consecutive failures rather than retrying (and reporting) every 15 seconds.
const MAX_CONSECUTIVE_POLL_FAILURES = 3
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubInstallRequestsLogicValues {
approvedInstallRequests: GitHubInstallRequestItemApi[]
consecutiveLoadFailures: number
dismissingRequestIds: string[]
hasPendingInstallRequests: boolean
installRequests: GitHubInstallRequestItemApi[]
installRequestsResponse: GitHubInstallRequestListResponseApi | null
installRequestsResponseLoading: boolean
installUrl: string | null
pendingInstallRequests: GitHubInstallRequestItemApi[]
pollingSubscribers: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubInstallRequestsLogicActions {
dismissInstallRequest: (id: string) => {
id: string
}
dismissInstallRequestFailure: (id: string) => {
id: string
}
dismissInstallRequestSuccess: (id: string) => {
id: string
}
loadInstallRequests: () => any
loadInstallRequestsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadInstallRequestsSuccess: (
installRequestsResponse: GitHubInstallRequestListResponseApi,
payload?: any
) => {
installRequestsResponse: GitHubInstallRequestListResponseApi
payload?: any
}
startPolling: () => {
value: true
}
stopPolling: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface githubInstallRequestsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
installRequests: (
installRequestsResponse: GitHubInstallRequestListResponseApi | null
) => GitHubInstallRequestItemApi[]
installUrl: (installRequestsResponse: GitHubInstallRequestListResponseApi | null) => string | null
pendingInstallRequests: (installRequests: GitHubInstallRequestItemApi[]) => GitHubInstallRequestItemApi[]
approvedInstallRequests: (installRequests: GitHubInstallRequestItemApi[]) => GitHubInstallRequestItemApi[]
hasPendingInstallRequests: (pendingInstallRequests: GitHubInstallRequestItemApi[]) => boolean
}
}
export type githubInstallRequestsLogicType = MakeLogicType<
githubInstallRequestsLogicValues,
githubInstallRequestsLogicActions,
Record<string, any>,
githubInstallRequestsLogicMeta
>
/**
* Durable "waiting for a GitHub org owner" state. GitHub only tells us the outcome through the
* `installation.created` webhook, so while a request is pending and a consumer is mounted we poll
* the list; once every request is resolved or dismissed the poll stops.
*/
export const githubInstallRequestsLogic = kea<githubInstallRequestsLogicType>([
path(['lib', 'integrations', 'githubInstallRequestsLogic']),
actions({
startPolling: true,
stopPolling: true,
dismissInstallRequest: (id: string) => ({ id }),
dismissInstallRequestSuccess: (id: string) => ({ id }),
dismissInstallRequestFailure: (id: string) => ({ id }),
}),
loaders({
installRequestsResponse: [
null as GitHubInstallRequestListResponseApi | null,
{
loadInstallRequests: async () => await usersIntegrationsGithubInstallRequestsRetrieve('@me'),
},
],
}),
reducers({
pollingSubscribers: [
0,
{
startPolling: (state) => state + 1,
stopPolling: (state) => Math.max(0, state - 1),
},
],
consecutiveLoadFailures: [
0,
{
loadInstallRequestsSuccess: () => 0,
loadInstallRequestsFailure: (state) => state + 1,
},
],
// Per-request in-flight state so each Dismiss button can disable itself while its DELETE
// runs, rather than allowing a repeat click to fire a redundant delete + reload.
dismissingRequestIds: [
[] as string[],
{
dismissInstallRequest: (state, { id }) => (state.includes(id) ? state : [...state, id]),
dismissInstallRequestSuccess: (state, { id }) => state.filter((x) => x !== id),
dismissInstallRequestFailure: (state, { id }) => state.filter((x) => x !== id),
},
],
}),
selectors({
installRequests: [
(s) => [s.installRequestsResponse],
(response: GitHubInstallRequestListResponseApi | null): GitHubInstallRequestItemApi[] =>
response?.results ?? [],
],
installUrl: [
(s) => [s.installRequestsResponse],
(response: GitHubInstallRequestListResponseApi | null): string | null => response?.install_url ?? null,
],
pendingInstallRequests: [
(s) => [s.installRequests],
(installRequests: GitHubInstallRequestItemApi[]): GitHubInstallRequestItemApi[] =>
installRequests.filter((r) => r.status === 'pending'),
],
approvedInstallRequests: [
(s) => [s.installRequests],
(installRequests: GitHubInstallRequestItemApi[]): GitHubInstallRequestItemApi[] =>
installRequests.filter((r) => r.status === 'approved'),
],
hasPendingInstallRequests: [
(s) => [s.pendingInstallRequests],
(pendingInstallRequests: GitHubInstallRequestItemApi[]): boolean => pendingInstallRequests.length > 0,
],
}),
listeners(({ actions, values, cache }) => {
const reconcilePolling = (): void => {
// A response that never loaded also keeps the timer armed, so a failed first load retries
// instead of hiding the banner for the rest of the mount.
const shouldPoll =
values.pollingSubscribers > 0 &&
values.consecutiveLoadFailures < MAX_CONSECUTIVE_POLL_FAILURES &&
(values.hasPendingInstallRequests || values.installRequestsResponse === null)
if (shouldPoll && !cache.disposables.registry.has('poll')) {
cache.disposables.add(() => {
const pollTimer = window.setInterval(
() => actions.loadInstallRequests(),
INSTALL_REQUESTS_POLL_INTERVAL_MS
)
return () => clearInterval(pollTimer)
}, 'poll')
} else if (!shouldPoll) {
cache.disposables.dispose('poll')
}
}
return {
// `afterMount` is the only initial load — the banner is the only consumer that mounts this
// logic, so a first subscriber always arrives at a logic that just ran it.
startPolling: reconcilePolling,
stopPolling: reconcilePolling,
loadInstallRequestsSuccess: reconcilePolling,
loadInstallRequestsFailure: reconcilePolling,
dismissInstallRequest: async ({ id }) => {
try {
await usersIntegrationsGithubInstallRequestsDestroy('@me', id)
} catch (e) {
// A 404 means the row is already gone, so the dismiss effectively succeeded.
if (!(e instanceof ApiError && e.status === 404)) {
actions.dismissInstallRequestFailure(id)
throw e
}
}
actions.dismissInstallRequestSuccess(id)
actions.loadInstallRequests()
},
}
}),
afterMount(({ actions }) => {
actions.loadInstallRequests()
}),
])