Skip to content

: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.

7.5 KB
Raw Blame
import { MakeLogicType, actions, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'

import { NotebookListItemType, NotebookNodeResource, NotebookNodeType } from '../types'
export const NOTEBOOK_DROPDOWN_LIMIT = 50

export interface NotebookSelectButtonLogicProps {
    /**
     * Is a resource is provided it will be checke and added to the notebook when opened
     * If it is a boolean it simply determines how the popover is rendered
     *  */
    resource?: NotebookNodeResource | boolean
    // allows callers (e.g. storybook) to control starting visibility of the popover
    visible?: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookSelectButtonLogicValues {
    allNotebooks: NotebookListItemType[]
    allNotebooksLoading: boolean
    createdBy: string | null
    notebooksContainingResource: NotebookListItemType[]
    notebooksContainingResourceLoading: boolean
    notebooksLoading: boolean
    notebooksNotContainingResource: NotebookListItemType[]
    searchQuery: string
    showPopover: boolean | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookSelectButtonLogicActions {
    loadAllNotebooks: () => {
        value: true
    }
    loadAllNotebooksFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadAllNotebooksSuccess: (
        allNotebooks: NotebookListItemType[],
        payload?: {
            value: true
        }
    ) => {
        allNotebooks: NotebookListItemType[]
        payload?: {
            value: true
        }
    }
    loadNotebooksContainingResource: () => {
        value: true
    }
    loadNotebooksContainingResourceFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadNotebooksContainingResourceSuccess: (
        notebooksContainingResource: NotebookListItemType[],
        payload?: {
            value: true
        }
    ) => {
        notebooksContainingResource: NotebookListItemType[]
        payload?: {
            value: true
        }
    }
    setCreatedBy: (userUuid: string | null) => {
        userUuid: string | null
    }
    setSearchQuery: (query: string) => {
        query: string
    }
    setShowPopover: (visible: boolean) => {
        visible: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookSelectButtonLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        notebooksNotContainingResource: (
            allNotebooks: NotebookListItemType[],
            notebooksContainingResource: NotebookListItemType[]
        ) => NotebookListItemType[]
        notebooksLoading: (allNotebooksLoading: boolean, notebooksContainingResourceLoading: boolean) => boolean
    }
}

export type notebookSelectButtonLogicType = MakeLogicType<
    notebookSelectButtonLogicValues,
    notebookSelectButtonLogicActions,
    NotebookSelectButtonLogicProps,
    notebookSelectButtonLogicMeta
>

export const notebookSelectButtonLogic = kea<notebookSelectButtonLogicType>([
    path((key) => ['scenes', 'session-recordings', 'NotebookSelectButton', 'multiNotebookSelectButtonLogic', key]),
    props({} as NotebookSelectButtonLogicProps),
    key((props) => JSON.stringify(props.resource || 'load')),
    actions({
        setShowPopover: (visible: boolean) => ({ visible }),
        setSearchQuery: (query: string) => ({ query }),
        setCreatedBy: (userUuid: string | null) => ({ userUuid }),
        loadNotebooksContainingResource: true,
        loadAllNotebooks: true,
    }),
    reducers(({ props }) => ({
        searchQuery: [
            '',
            {
                setSearchQuery: (_, { query }) => query,
            },
        ],
        createdBy: [
            null as string | null,
            {
                setCreatedBy: (_, { userUuid }) => userUuid,
            },
        ],
        showPopover: [
            props.visible,
            {
                setShowPopover: (_, { visible }) => visible,
            },
        ],
    })),
    listeners(({ actions }) => ({
        setSearchQuery: async (_, breakpoint) => {
            await breakpoint(300)
            actions.loadAllNotebooks()
            actions.loadNotebooksContainingResource()
        },
        setCreatedBy: async (_, breakpoint) => {
            // Debouncing similarly as the search query
            await breakpoint(300)
            actions.loadAllNotebooks()
            actions.loadNotebooksContainingResource()
        },
    })),
    loaders(({ props, values }) => ({
        allNotebooks: [
            [] as NotebookListItemType[],
            {
                loadAllNotebooks: async (_, breakpoint) => {
                    await breakpoint(100)
                    const response = await api.notebooks.list({
                        search: values.searchQuery || undefined,
                        created_by: values.createdBy || undefined,
                        order: '-last_modified_at',
                        limit: NOTEBOOK_DROPDOWN_LIMIT,
                    })
                    return response?.results ?? []
                },
            },
        ],
        notebooksContainingResource: [
            [] as NotebookListItemType[],
            {
                loadNotebooksContainingResource: async (_, breakpoint) => {
                    await breakpoint(100)
                    if (!props.resource) {
                        return []
                    }
                    const response = await api.notebooks.list({
                        contains:
                            props.resource && typeof props.resource !== 'boolean'
                                ? [
                                      {
                                          type: props.resource.type,
                                          attrs: {
                                              id:
                                                  props.resource.type === NotebookNodeType.Query
                                                      ? props.resource.attrs.query.shortId
                                                      : props.resource.attrs.id,
                                          },
                                      },
                                  ]
                                : undefined,
                        search: values.searchQuery || undefined,
                        created_by: values.createdBy || undefined,
                        order: '-last_modified_at',
                        limit: NOTEBOOK_DROPDOWN_LIMIT,
                    })
                    return response?.results ?? []
                },
            },
        ],
    })),
    selectors(() => ({
        notebooksNotContainingResource: [
            (s) => [s.allNotebooks, s.notebooksContainingResource],
            (allNotebooks: NotebookListItemType[], notebooksContainingResource: NotebookListItemType[]) =>
                allNotebooks.filter(
                    (notebook) => !notebooksContainingResource.find((n) => n.short_id === notebook.short_id)
                ),
        ],
        notebooksLoading: [
            (s) => [s.allNotebooksLoading, s.notebooksContainingResourceLoading],
            (allNotebooksLoading: boolean, notebooksContainingResourceLoading: boolean) =>
                allNotebooksLoading || notebooksContainingResourceLoading,
        ],
    })),
])