: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,
beforeUnmount,
kea,
key,
listeners,
path,
props,
propsChanged,
reducers,
selectors,
} from 'kea'
import { NotebookVisitStorage, type NotebookVisit } from './notebookVisitStorage'
export interface NotebookChangesLogicProps {
scope: string | null
markdown: string | null
}
function saveCurrentVisit({ scope, markdown }: NotebookChangesLogicProps): void {
if (scope && markdown !== null) {
NotebookVisitStorage.write(scope, markdown)
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookChangesLogicValues {
dismissed: boolean
hasChanges: boolean
previousVisit: NotebookVisit | null
showChanges: boolean
visitInitialized: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookChangesLogicActions {
dismissChanges: () => {
value: true
}
initializeVisit: (visit: NotebookVisit | null) => {
visit: NotebookVisit | null
}
observeVisit: () => {
value: true
}
persistVisit: () => {
value: true
}
setShowChanges: (show: boolean) => {
show: boolean
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookChangesLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
hasChanges: (previousVisit: NotebookVisit | null, dismissed: boolean, arg: string | null) => boolean
}
}
export type notebookChangesLogicType = MakeLogicType<
notebookChangesLogicValues,
notebookChangesLogicActions,
NotebookChangesLogicProps,
notebookChangesLogicMeta
>
export const notebookChangesLogic = kea<notebookChangesLogicType>([
path((key) => ['products', 'notebooks', 'notebookChangesLogic', key]),
props({} as NotebookChangesLogicProps),
key(({ scope }) => scope ?? 'disabled'),
actions({
observeVisit: true,
initializeVisit: (visit: NotebookVisit | null) => ({ visit }),
persistVisit: true,
setShowChanges: (show: boolean) => ({ show }),
dismissChanges: true,
}),
reducers({
previousVisit: [null as NotebookVisit | null, { initializeVisit: (_, { visit }) => visit }],
visitInitialized: [false, { initializeVisit: () => true }],
dismissed: [false, { dismissChanges: () => true }],
showChanges: [false, { setShowChanges: (_, { show }) => show, dismissChanges: () => false }],
}),
selectors({
hasChanges: [
(s) => [s.previousVisit, s.dismissed, (_, props: NotebookChangesLogicProps) => props.markdown],
(previousVisit: NotebookVisit | null, dismissed: boolean, markdown: string | null): boolean =>
!dismissed && !!previousVisit && markdown !== null && previousVisit.markdown !== markdown,
],
}),
listeners(({ actions, values, props }) => ({
observeVisit: () => {
if (!props.scope || props.markdown === null) {
return
}
if (!values.visitInitialized) {
actions.initializeVisit(NotebookVisitStorage.read(props.scope))
}
actions.persistVisit()
},
persistVisit: async (_, breakpoint) => {
await breakpoint(1000)
saveCurrentVisit(props)
},
})),
propsChanged(({ actions, props }, previous) => {
if (props.markdown !== previous.markdown) {
actions.observeVisit()
}
}),
afterMount(({ actions, props, cache }) => {
actions.observeVisit()
cache.disposables.add(
() => {
const save = (): void => saveCurrentVisit(props)
window.addEventListener('pagehide', save)
return () => window.removeEventListener('pagehide', save)
},
'saveVisit',
{ pauseOnPageHidden: false }
)
}),
beforeUnmount(({ props }) => saveCurrentVisit(props)),
])