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.

5.7 KB
Raw Blame
import { useActions, useValues } from 'kea'
import posthog from 'posthog-js'
import { KeyboardEvent, useEffect, useRef, useState } from 'react'

import { LemonInput, LemonTag, Tooltip } from '@posthog/lemon-ui'

import { Spinner } from 'lib/lemon-ui/Spinner'
import { notebookLogic } from 'scenes/notebooks/Notebook/notebookLogic'

import { isHogQLQuery } from '~/queries/schema/node-guards'

import { NotebookNodeType } from '../../types'
import { notebookNodeLogic } from '../notebookNodeLogic'

export const getCellLabel = (nodeIndex: number | undefined): string | null => {
    if (!nodeIndex) {
        return null
    }
    return `SQL ${nodeIndex}`
}

export function NotebookNodeTitle(): JSX.Element {
    const { isEditable, sqlNodeIndices } = useValues(notebookLogic)
    const { editableTitle, nodeAttributes, title, titlePlaceholder, titleStatus, isEditingTitle, nodeType } =
        useValues(notebookNodeLogic)
    const { updateAttributes, toggleEditingTitle } = useActions(notebookNodeLogic)
    const [newValue, setNewValue] = useState('')
    const initialValueRef = useRef('')

    const isSqlNode =
        nodeType === NotebookNodeType.Query &&
        (isHogQLQuery(nodeAttributes.query) ||
            (nodeAttributes.query.source && isHogQLQuery(nodeAttributes.query.source)))

    const nodeIndex = isSqlNode ? sqlNodeIndices.get(nodeAttributes.nodeId) : undefined
    const cellLabel = getCellLabel(nodeIndex)
    const customTitle = nodeAttributes.title
    const cellTitle = cellLabel ? (customTitle ? `${cellLabel} • ${customTitle}` : cellLabel) : title

    useEffect(() => {
        const prefill = cellLabel ? (nodeAttributes.title ?? '') : nodeAttributes.title || title || ''
        setNewValue(prefill)
        initialValueRef.current = prefill
    }, [isEditingTitle]) // oxlint-disable-line react-hooks/exhaustive-deps

    const commitEdit = (): void => {
        if (newValue !== initialValueRef.current) {
            updateAttributes({ title: newValue || undefined })
            posthog.capture('notebook node title updated')
        }
        toggleEditingTitle(false)
    }

    const onKeyUp = (e: KeyboardEvent<HTMLInputElement>): void => {
        // Esc cancels, enter commits
        if (e.key === 'Escape') {
            toggleEditingTitle(false)
        } else if (e.key === 'Enter') {
            commitEdit()
        }
    }

    const suggestedTaskTitle = (
        <span className="NotebookNodeTitle flex items-center gap-2" title={title}>
            <LemonTag type="warning" size="small">
                Suggested task
            </LemonTag>
            <span className="truncate">{title}</span>
        </span>
    )

    const titleStatusTag = titleStatus ? (
        <LemonTag
            type={titleStatus.type}
            size="small"
            className="uppercase shrink-0"
            icon={titleStatus.loading ? <Spinner textColored /> : undefined}
            disabledReason={titleStatus.loading ? 'Updating status' : undefined}
            title={titleStatus.tooltip}
            onClick={
                titleStatus.loading || !titleStatus.onClick
                    ? undefined
                    : (event) => {
                          event.stopPropagation()
                          titleStatus.onClick?.()
                      }
            }
        >
            {titleStatus.label}
        </LemonTag>
    ) : null

    const cellTitleDisplay = cellLabel ? (
        <span title={cellTitle} className="NotebookNodeTitle flex items-center gap-2 truncate">
            <span className="font-semibold">{cellLabel}</span>
            {customTitle ? <span className="text-muted truncate">{customTitle}</span> : null}
        </span>
    ) : (
        <span title={title} className="NotebookNodeTitle flex items-center gap-2 min-w-0">
            <span className="truncate">{title}</span>
            {titleStatusTag}
        </span>
    )

    return !isEditable || editableTitle === false ? (
        nodeType === NotebookNodeType.TaskCreate ? (
            suggestedTaskTitle
        ) : (
            cellTitleDisplay
        )
    ) : !isEditingTitle ? (
        <Tooltip title="Double click to edit title">
            {nodeType === NotebookNodeType.TaskCreate ? (
                <span
                    title={title}
                    className="NotebookNodeTitle NotebookNodeTitle--editable"
                    onDoubleClick={() => {
                        toggleEditingTitle(true)
                        posthog.capture('notebook editing node title')
                    }}
                >
                    {suggestedTaskTitle}
                </span>
            ) : (
                <span
                    title={cellTitle}
                    className="NotebookNodeTitle NotebookNodeTitle--editable"
                    onDoubleClick={() => {
                        toggleEditingTitle(true)
                        posthog.capture('notebook editing node title')
                    }}
                >
                    {cellLabel ? (
                        <span className="flex items-center gap-2 truncate">
                            <span className="font-semibold">{cellLabel}</span>
                            {customTitle ? <span className="text-muted truncate">{customTitle}</span> : null}
                        </span>
                    ) : (
                        title
                    )}
                </span>
            )}
        </Tooltip>
    ) : (
        <LemonInput
            autoFocus
            placeholder={titlePlaceholder}
            size="small"
            fullWidth
            value={newValue}
            onChange={(e) => setNewValue(e)}
            onBlur={commitEdit}
            onKeyUp={onKeyUp}
            onFocus={(e) => e.target.select()}
        />
    )
}