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.5 KB
Raw Blame
import clsx from 'clsx'
import { useState } from 'react'

import { IconCode, IconWarning, IconX } from '@posthog/icons'
import { Link, Tooltip } from '@posthog/lemon-ui'

import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { IconBranch } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'
import { getRegionForHost, getStoredSession, OAUTH_REGIONS } from 'lib/utils/oauthClient'

export interface DebugNoticeProps {
    isCollapsed?: boolean
}

export function DebugNotice({ isCollapsed }: DebugNoticeProps): JSX.Element | null {
    const [debugInfo, setDebugInfo] = useState<{ branch: string; revision: string } | undefined>()
    const [noticeHidden, setNoticeHidden] = useState(false)

    useOnMountEffect(() => {
        const bottomNotice = document.getElementById('bottom-notice')
        const bottomNoticeRevision = document.getElementById('bottom-notice-revision')?.textContent
        const bottomNoticeBranch = document.getElementById('bottom-notice-branch')?.textContent

        if (bottomNotice && bottomNoticeRevision && bottomNoticeBranch) {
            setDebugInfo({
                branch: bottomNoticeBranch || 'unknown',
                revision: bottomNoticeRevision || 'unknown',
            })

            bottomNotice.remove()
        }
    })

    if (!debugInfo || noticeHidden) {
        return null
    }

    const oauthSession = getStoredSession()
    const oauthRegion = oauthSession ? getRegionForHost(oauthSession.backendHost) : null
    const modeLabel = oauthRegion ? `OAuth mode ${OAUTH_REGIONS[oauthRegion].flag}` : 'DEBUG mode'

    if (isCollapsed) {
        return (
            <ButtonPrimitive
                iconOnly
                tooltip={
                    <div className="font-mono">
                        <div>
                            <strong>{modeLabel}!</strong>
                        </div>
                        <div>
                            Branch: <b>{debugInfo.branch}</b>
                        </div>
                        <div>
                            Revision: <b>{debugInfo.revision}</b>
                        </div>
                        <div className="italic">Click to hide</div>
                    </div>
                }
                onClick={() => setNoticeHidden(true)}
            >
                <IconBranch className="text-secondary" />
            </ButtonPrimitive>
        )
    }
    return (
        <div className="mb-2 border rounded bg-primary overflow-hidden w-full font-mono text-xs *:flex *:items-center *:gap-2 *:pl-2 *:pr-0.5 *:h-7 *:border-l-4">
            <div className="border-brand-blue justify-between">
                <Tooltip
                    title={
                        oauthRegion
                            ? 'Due to intentional limitations of OAuth access, not everything may work'
                            : undefined
                    }
                    placement="top-start"
                >
                    <div
                        className={clsx(
                            'flex items-center gap-2 min-w-0 ',
                            oauthRegion ? 'cursor-help' : 'cursor-default'
                        )}
                    >
                        {oauthRegion && <IconWarning className="text-base" />}
                        <span className="font-bold">{modeLabel}</span>
                    </div>
                </Tooltip>
                <LemonButton
                    icon={<IconX />}
                    tooltip="Dismiss"
                    tooltipPlacement="right"
                    size="xsmall"
                    onClick={() => setNoticeHidden(true)}
                />
            </div>
            <Tooltip title="Branch" placement="right">
                <div className="w-fit border-brand-red truncate max-w-full">
                    <IconBranch className="text-base" />
                    <span className="min-w-0 flex-1 truncate font-bold">{debugInfo.branch}</span>
                </div>
            </Tooltip>
            <Tooltip title="Revision" placement="right">
                <div className="w-fit border-brand-yellow">
                    <IconCode className="text-base" />
                    <span className="min-w-0 flex-1 truncate font-bold">{debugInfo.revision}</span>
                </div>
            </Tooltip>
            {window.location.port !== '8010' && (
                <Tooltip
                    title={
                        <>
                            You're currently using the app over port 8000,
                            <br />
                            which only serves the web app, without capture (/e/).
                            <br />
                            Use port 8010 for full PostHog, proxied via Caddy.
                        </>
                    }
                    placement="right"
                >
                    <div className="flex items-center gap-2 w-fit px-2 h-8 border-l-4 border-brand-key">
                        <IconWarning className="text-lg" />
                        <Link
                            to={window.location.href.replace(`:${window.location.port}`, ':8010')}
                            className="font-semibold text-default underline min-w-0 flex-1 truncate"
                        >
                            Click here to fix port!
                        </Link>
                    </div>
                </Tooltip>
            )}
        </div>
    )
}