: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, kea, listeners, path, reducers, selectors } from 'kea'
import api from 'lib/api'
import { permanentlyMount } from 'lib/utils/kea-logic-builders'
export enum RecordingExistsState {
Loading = 'loading',
Exists = 'exists',
NotExists = 'not_exists',
Error = 'error',
}
export type RecordingExistsStorage = Record<string, RecordingExistsState>
export interface PendingFetch {
wantsExistence: boolean
}
const BATCH_SIZE = 100
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionRecordingInfoLogicValues {
getRecordingExists: (sessionId: string) => boolean | undefined
isRecordingExistsLoading: (sessionId: string) => boolean
pendingFetches: Record<string, PendingFetch>
recordingExistsStorage: RecordingExistsStorage
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionRecordingInfoLogicActions {
checkRecordingInfo: (sessionId: string) => {
sessionId: string
}
enqueuePending: (
sessionId: string,
want: keyof PendingFetch
) => {
sessionId: string
want: 'wantsExistence'
}
flushPending: () => {
value: true
}
recordExistence: (updates: RecordingExistsStorage) => {
updates: RecordingExistsStorage
}
removePending: (sessionIds: string[]) => {
sessionIds: string[]
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionRecordingInfoLogicMeta {
__keaTypeGenInternalSelectorTypes: {
getRecordingExists: (
recordingExistsStorage: RecordingExistsStorage
) => (sessionId: string) => boolean | undefined
isRecordingExistsLoading: (
recordingExistsStorage: RecordingExistsStorage,
pendingFetches: Record<string, PendingFetch>
) => (sessionId: string) => boolean
}
}
export type sessionRecordingInfoLogicType = MakeLogicType<
sessionRecordingInfoLogicValues,
sessionRecordingInfoLogicActions,
Record<string, any>,
sessionRecordingInfoLogicMeta
>
export const sessionRecordingInfoLogic = kea<sessionRecordingInfoLogicType>([
path(['lib', 'components', 'ViewRecordingButton', 'sessionRecordingInfoLogic']),
actions({
checkRecordingInfo: (sessionId: string) => ({ sessionId }),
enqueuePending: (sessionId: string, want: keyof PendingFetch) => ({ sessionId, want }),
removePending: (sessionIds: string[]) => ({ sessionIds }),
recordExistence: (updates: RecordingExistsStorage) => ({ updates }),
flushPending: true,
}),
reducers({
// Choosing not to persist here as we cache on the BE; client memory shouldn't grow forever.
recordingExistsStorage: [
{} as RecordingExistsStorage,
{
recordExistence: (state, { updates }) => ({ ...state, ...updates }),
},
],
pendingFetches: [
{} as Record<string, PendingFetch>,
{
enqueuePending: (state, { sessionId, want }) => {
const existing = state[sessionId] ?? { wantsExistence: false }
if (existing[want]) {
return state
}
return { ...state, [sessionId]: { ...existing, [want]: true } }
},
removePending: (state, { sessionIds }) => {
const next = { ...state }
for (const id of sessionIds) {
delete next[id]
}
return next
},
},
],
}),
listeners(({ actions, values }) => ({
checkRecordingInfo: ({ sessionId }) => {
const { recordingExistsStorage, pendingFetches } = values
const existenceMissing = !(sessionId in recordingExistsStorage)
if (existenceMissing && !pendingFetches[sessionId]?.wantsExistence) {
actions.enqueuePending(sessionId, 'wantsExistence')
actions.flushPending()
}
},
flushPending: async (_, breakpoint) => {
await breakpoint(10)
const allPending = Object.entries(values.pendingFetches)
if (allPending.length === 0) {
return
}
const batch = allPending.slice(0, BATCH_SIZE)
const sessionIds = batch.map(([id]) => id)
const existenceNeeded = batch.filter(([, want]) => want.wantsExistence).map(([id]) => id)
if (existenceNeeded.length > 0) {
actions.recordExistence(
Object.fromEntries(existenceNeeded.map((id) => [id, RecordingExistsState.Loading]))
)
}
try {
const response = await api.recordings.batchCheckExists(sessionIds)
await breakpoint()
if (existenceNeeded.length > 0) {
const updates: RecordingExistsStorage = {}
for (const id of existenceNeeded) {
updates[id] = response.results[id]
? RecordingExistsState.Exists
: RecordingExistsState.NotExists
}
actions.recordExistence(updates)
}
actions.removePending(sessionIds)
} catch {
if (existenceNeeded.length > 0) {
const errorUpdates: RecordingExistsStorage = Object.fromEntries(
existenceNeeded.map((id) => [id, RecordingExistsState.Error])
)
actions.recordExistence(errorUpdates)
}
actions.removePending(sessionIds)
}
await breakpoint()
if (Object.keys(values.pendingFetches).length > 0) {
actions.flushPending()
}
},
})),
selectors({
getRecordingExists: [
(s) => [s.recordingExistsStorage],
(storage: RecordingExistsStorage) =>
(sessionId: string): boolean | undefined => {
const state = storage[sessionId]
if (state === RecordingExistsState.Exists) {
return true
}
if (state === RecordingExistsState.NotExists) {
return false
}
return undefined
},
],
isRecordingExistsLoading: [
(s) => [s.recordingExistsStorage, s.pendingFetches],
(storage: RecordingExistsStorage, pending: Record<string, PendingFetch>) =>
(sessionId: string): boolean => {
if (pending[sessionId]?.wantsExistence) {
return true
}
return storage[sessionId] === RecordingExistsState.Loading
},
],
}),
permanentlyMount(),
])