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.

4.3 KB
Raw Blame
import './NotebookPanel.scss'

import { useActions, useValues } from 'kea'
import { useState } from 'react'

import { IconExpand45 } from '@posthog/icons'
import { Link } from '@posthog/lemon-ui'

import { useResizeBreakpoints } from 'lib/hooks/useResizeObserver'
import { cn } from 'lib/utils/css-classes'
import { urls } from 'scenes/urls'

import { SidePanelPaneHeader } from '~/layout/navigation-3000/sidepanel/components/SidePanelPaneHeader'
import { SidePanelContentContainer } from '~/layout/navigation-3000/sidepanel/SidePanelContentContainer'

import { Notebook } from '../Notebook/Notebook'
import { NotebookListMini } from '../Notebook/NotebookListMini'
import { notebookLogic } from '../Notebook/notebookLogic'
import { NotebookExpandButton, NotebookPresence, NotebookSyncInfo } from '../Notebook/NotebookMeta'
import { NotebookMenu } from '../NotebookMenu'
import { NotebookTarget } from '../types'
import { NotebookPanelDropzone } from './NotebookPanelDropzone'
import { notebookPanelLogic } from './notebookPanelLogic'

export function NotebookPanel(): JSX.Element | null {
    const [collapsedToolbarContainer, setCollapsedToolbarContainer] = useState<HTMLDivElement | null>(null)
    const { selectedNotebook, droppedResource } = useValues(notebookPanelLogic)
    const { selectNotebook, closeSidePanel } = useActions(notebookPanelLogic)
    const { notebook } = useValues(notebookLogic({ shortId: selectedNotebook, target: NotebookTarget.Popover }))
    const editable = !notebook?.is_template
    const { ref, size } = useResizeBreakpoints({
        0: 'small',
        832: 'medium',
    })

    const contentWidthHasEffect = size === 'medium'

    return (
        <div ref={ref} className={cn('NotebookPanel', 'bg-transparent')}>
            {!droppedResource ? (
                <>
                    <SidePanelContentContainer>
                        <SidePanelPaneHeader title="Notebooks" className="h-auto min-h-10 flex-wrap gap-y-1">
                            <div className="flex gap-1 overflow-hidden">
                                <NotebookListMini
                                    selectedNotebookId={selectedNotebook}
                                    onSelectNotebook={(notebook) => {
                                        selectNotebook(notebook.short_id)
                                    }}
                                    buttonProps={{ className: 'max-w-[120px]', truncate: true }}
                                />

                                {selectedNotebook && <NotebookSyncInfo shortId={selectedNotebook} />}
                            </div>

                            <div className="flex-1" />
                            <div className="flex flex-wrap items-center justify-end gap-1 ml-auto">
                                <div ref={setCollapsedToolbarContainer} className="shrink-0 empty:hidden" />
                                {selectedNotebook && <NotebookPresence shortId={selectedNotebook} />}
                                <NotebookMenu shortId={selectedNotebook} inPanel />
                                {contentWidthHasEffect && <NotebookExpandButton size="small" inPanel={true} />}
                                <Link
                                    buttonProps={{
                                        iconOnly: true,
                                    }}
                                    to={urls.notebook(selectedNotebook)}
                                    onClick={() => closeSidePanel()}
                                    target="_blank"
                                    tooltip="Open as main focus"
                                    tooltipPlacement="bottom-end"
                                >
                                    <IconExpand45 className="text-tertiary size-3 group-hover:text-primary z-10" />
                                </Link>
                            </div>
                        </SidePanelPaneHeader>
                        <Notebook
                            key={selectedNotebook}
                            shortId={selectedNotebook}
                            editable={editable}
                            collapsedToolbarContainer={collapsedToolbarContainer}
                        />
                    </SidePanelContentContainer>
                </>
            ) : null}

            <NotebookPanelDropzone />
        </div>
    )
}