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.

1.4 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { type MouseEvent, useCallback } from 'react'

import { type OpenTraceDialogOptions, traceDialogOpenLogic } from 'lib/components/ViewTraceButton/traceDialogOpenLogic'
import type { TraceLinkParams } from 'lib/components/ViewTraceButton/traceUrl'

export type OpenTraceDialogHandler = (
    event: MouseEvent | null,
    trace: TraceLinkParams,
    options: OpenTraceDialogOptions
) => boolean

/**
 * Opens a trace in the dialog in place of navigating to Tracing. Returns false when it did not, so
 * the caller's link or navigation goes ahead: while the dialog flag is off, and for modifier and
 * non-primary clicks. Cmd and ctrl then open Tracing in a new tab, and shift opens it in place.
 */
export function useOpenTraceDialog(): OpenTraceDialogHandler {
    const { dialogEnabled } = useValues(traceDialogOpenLogic)
    const { openTraceDialog } = useActions(traceDialogOpenLogic)

    // Stable while the flag holds, so callers can list it in a memo's dependencies.
    return useCallback(
        (event, trace, options) => {
            if (!dialogEnabled) {
                return false
            }
            if (event && (event.metaKey || event.ctrlKey || event.shiftKey || event.button !== 0)) {
                return false
            }
            event?.preventDefault()
            openTraceDialog(trace, options)
            return true
        },
        [dialogEnabled, openTraceDialog]
    )
}