: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, beforeUnmount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { urlToAction } from 'kea-router'
import { urls } from 'scenes/urls'
import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { ActivityScope, Breadcrumb, SidePanelTab } from '~/types'
import type {
ReplayObservationApi,
ObservationStatusEnumApi,
VisionObservationsRetrieveParams,
} from '../generated/api.schemas'
import { VISION_ROOT_BREADCRUMB } from '../utils/breadcrumbs'
export type ObservationResultTab = 'summary' | 'timeline'
export interface ObservationsPage {
rows: ReplayObservationApi[]
number: number
pageSize: number
total: number
filterParams: VisionObservationsRetrieveParams
}
/** Why notes are off for an observation, or null when they are on. */
export function observationNotesDisabledReason(status: ObservationStatusEnumApi | null): string | null {
if (status === 'succeeded') {
return null
}
if (status === null) {
return 'Loading the observation...'
}
// A retry deletes an unfinished scan and gives the new scan a new id, which would strand its notes.
return status === 'pending' || status === 'running'
? 'You can add notes when the scan finishes.'
: 'You can add notes after the scan succeeds. A retry replaces this scan, so notes on it would be lost.'
}
/** The notes button label. The count comes from the side panel's discussion logic, which reads the global
* scene, so the buttons read it rather than this scene logic connecting to it. */
export function notesButtonLabel(disabledReason: string | null, noteCount: number): string {
return !disabledReason && noteCount > 0 ? `Notes (${noteCount})` : 'Notes'
}
/** The page the scanner table last loaded, so a row opened from it paints before its own read lands. */
export const lastObservationsPage: { current: ObservationsPage | null } = { current: null }
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface replayObservationSceneLogicValues {
selectedTab: SidePanelTab | null // sidePanelStateLogic
sidePanelOpen: boolean // sidePanelStateLogic
breadcrumbs: Breadcrumb[]
notesDisabledReason: string | null
notesPanelOpen: boolean
observationId: string
observationStatus: ObservationStatusEnumApi | null
parentBreadcrumb: Breadcrumb | null
resultTab: ObservationResultTab
sidePanelContext: SidePanelSceneContext
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface replayObservationSceneLogicActions {
closeSidePanel: (tab?: SidePanelTab | undefined) => {
tab: SidePanelTab | undefined
} // sidePanelStateLogic
openSidePanel: (
tab: SidePanelTab,
options?: string | undefined
) => {
options: string | undefined
tab: SidePanelTab
} // sidePanelStateLogic
setObservationId: (observationId: string) => {
observationId: string
}
setObservationStatus: (observationStatus: ObservationStatusEnumApi) => {
observationStatus: ObservationStatusEnumApi
}
setParentBreadcrumb: (parentBreadcrumb: Breadcrumb | null) => {
parentBreadcrumb: Breadcrumb | null
}
setResultTab: (resultTab: ObservationResultTab) => {
resultTab: ObservationResultTab
}
toggleNotesPanel: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface replayObservationSceneLogicMeta {
__keaTypeGenInternalSelectorTypes: {
breadcrumbs: (observationId: string, parentBreadcrumb: Breadcrumb | null) => Breadcrumb[]
notesDisabledReason: (observationStatus: ObservationStatusEnumApi | null) => string | null
notesPanelOpen: (sidePanelOpen: boolean, selectedTab: SidePanelTab | null) => boolean
sidePanelContext: (observationId: string, notesDisabledReason: string | null) => SidePanelSceneContext
}
}
export type replayObservationSceneLogicType = MakeLogicType<
replayObservationSceneLogicValues,
replayObservationSceneLogicActions,
Record<string, any>,
replayObservationSceneLogicMeta
>
export const replayObservationSceneLogic = kea<replayObservationSceneLogicType>([
path(['products', 'replay_vision', 'frontend', 'observations', 'replayObservationSceneLogic']),
connect(() => ({
values: [sidePanelStateLogic, ['sidePanelOpen', 'selectedTab']],
actions: [sidePanelStateLogic, ['openSidePanel', 'closeSidePanel']],
})),
actions({
setObservationId: (observationId: string) => ({ observationId }),
// Pushed by replayObservationLogic once the observation loads. The scene's back button returns
// to this crumb, so where it points decides where "back" lands.
setParentBreadcrumb: (parentBreadcrumb: Breadcrumb | null) => ({ parentBreadcrumb }),
setResultTab: (resultTab: ObservationResultTab) => ({ resultTab }),
// Pushed by replayObservationLogic too, so the discussion panel knows whether notes are allowed.
setObservationStatus: (observationStatus: ObservationStatusEnumApi) => ({ observationStatus }),
toggleNotesPanel: true,
}),
reducers({
observationId: [
'' as string,
{
setObservationId: (_, { observationId }) => observationId,
},
],
parentBreadcrumb: [
null as Breadcrumb | null,
{
setParentBreadcrumb: (_, { parentBreadcrumb }) => parentBreadcrumb,
// Clear when navigating to a different observation so we don't briefly show the previous parent.
setObservationId: () => null,
},
],
observationStatus: [
null as ObservationStatusEnumApi | null,
{
setObservationStatus: (_, { observationStatus }) => observationStatus ?? null,
setObservationId: () => null,
},
],
// Kept across previous and next, so a reviewer can page through timelines.
resultTab: [
'summary' as ObservationResultTab,
{
setResultTab: (_, { resultTab }) => resultTab,
},
],
}),
selectors({
breadcrumbs: [
(s) => [s.observationId, s.parentBreadcrumb],
(observationId: string, parentBreadcrumb: Breadcrumb | null): Breadcrumb[] => [
VISION_ROOT_BREADCRUMB,
...(parentBreadcrumb ? [parentBreadcrumb] : []),
{
key: observationId ? `observation-${observationId}` : 'observation',
name: 'Observation',
path: urls.replayVisionObservation(observationId),
},
],
],
notesDisabledReason: [
(s) => [s.observationStatus],
(observationStatus: ObservationStatusEnumApi | null): string | null =>
observationNotesDisabledReason(observationStatus),
],
notesPanelOpen: [
(s) => [s.sidePanelOpen, s.selectedTab],
(sidePanelOpen: boolean, selectedTab: SidePanelTab | null): boolean =>
sidePanelOpen && selectedTab === SidePanelTab.Discussion,
],
[SIDE_PANEL_CONTEXT_KEY]: [
(s) => [s.observationId, s.notesDisabledReason],
(observationId: string, disabledReason: string | null): SidePanelSceneContext => ({
activity_scope: ActivityScope.REPLAY_OBSERVATION,
activity_item_id: observationId,
discussions_disabled: disabledReason !== null,
discussions_disabled_reason: disabledReason ?? undefined,
}),
],
}),
listeners(({ actions, values }) => ({
toggleNotesPanel: () => {
if (values.notesPanelOpen) {
actions.closeSidePanel()
} else {
actions.openSidePanel(SidePanelTab.Discussion)
}
},
})),
urlToAction(({ actions, values }) => ({
[urls.replayVisionObservation(':observationId')]: ({ observationId }) => {
const next = observationId || ''
if (next !== values.observationId) {
actions.setObservationId(next)
}
},
})),
// Siblings opened within the scene still read the page; a later deep link must not seed a stale row.
beforeUnmount(() => {
lastObservationsPage.current = null
}),
])