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.

15.4 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
import React, { useEffect, useRef } from 'react'

import { IconArrowLeft } from '@posthog/icons'
import { LemonButton } from '@posthog/lemon-ui'

import { useKeyboardHotkeys } from 'lib/hooks/useKeyboardHotkeys'
import { SceneExport } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'

import { CardSkeleton } from './components/cards/CardSkeleton'
import { ScoutDetailView } from './components/config/scouts/ScoutDetailView'
import { ScoutsRoster } from './components/config/scouts/ScoutsRoster'
import { ScoutsRosterActions } from './components/config/scouts/ScoutsRosterActions'
import { ReportDetail, ReportDetailSkeleton } from './components/detail/ReportDetail'
import { FindingsPanel } from './components/findings/FindingsPanel'
import { InboxOnboardingBanner } from './components/onboarding/InboxOnboardingBanner'
import { InboxOnboardingEntry } from './components/onboarding/InboxOnboardingEntry'
import { ScratchpadPanel } from './components/scratchpad/ScratchpadPanel'
import { InboxTabBar } from './components/shell/InboxTabBar'
import { InboxTriageButton } from './components/shell/InboxTriageButton'
import { InboxValueSummary } from './components/shell/InboxValueSummary'
import { ReportsTab } from './components/tabs/ReportsTab'
import { RunsTab } from './components/tabs/RunsTab'
import { SettingsTab } from './components/tabs/SettingsTab'
import { SourcesTab } from './components/tabs/SourcesTab'
import { InboxTriageView } from './components/triage/InboxTriageView'
import { captureInboxPanelViewed } from './inboxAnalytics'
import { inboxSceneLogic } from './inboxSceneLogic'
import { inboxOnboardingLogic } from './logics/inboxOnboardingLogic'
import { scoutFleetLogic } from './logics/scoutFleetLogic'
import { INBOX_TAB_DESCRIPTION, InboxTabKey, SignalReport } from './types'

export const scene: SceneExport = {
    component: InboxScene,
    logic: inboxSceneLogic,
}

const LazyScoutCreateModal = React.lazy(async () => {
    const { ScoutCreateModal } = await import('./components/config/scouts/ScoutCreateModal')
    return { default: ScoutCreateModal }
})

/** Hosts the `#createScout=` modal at the scene level, so it opens from any tab, not just the roster. */
function ScoutTemplateDraftModal(): JSX.Element | null {
    const { scoutTemplateDraft } = useValues(inboxSceneLogic)
    const { setScoutTemplateDraft } = useActions(inboxSceneLogic)

    if (!scoutTemplateDraft) {
        return null
    }
    return (
        <React.Suspense fallback={null}>
            <LazyScoutCreateModal
                isOpen
                initialValues={scoutTemplateDraft}
                onClose={() => setScoutTemplateDraft(null)}
                onCreated={() => {
                    // Refresh the fleet list only if it's already mounted — never mount it from here.
                    scoutFleetLogic.findMounted()?.actions.loadScoutConfigs()
                }}
            />
        </React.Suspense>
    )
}

function ActiveTabBody({ tab }: { tab: InboxTabKey }): JSX.Element | null {
    switch (tab) {
        case 'reports':
            return <ReportsTab />
        case 'scouts':
            return <ScoutsRoster />
        case 'sources':
            return <SourcesTab />
        case 'settings':
            return <SettingsTab />
        // Old-layout tab segments are redirected before they can become active.
        default:
            return null
    }
}

function InboxListView(): JSX.Element {
    const { activeTab } = useValues(inboxSceneLogic)
    const { onboardingMode } = useValues(inboxOnboardingLogic)
    // Self-driving isn't set up and the inbox is empty: the welcome page replaces the whole list
    // view, tab bar included, so the onboarding is the whole story – just run the one command.
    const onboarding = onboardingMode === 'takeover'
    // The takeover verdict is still settling: commit to neither UI. Rendering the tab bar here is
    // what caused the normal inbox to flash in and get replaced by the welcome page.
    const pending = onboardingMode === 'pending'

    // These tabs render no report list, so `Inbox viewed` never fires for them.
    // Keep Settings attributed to `config` so existing panel breakdowns remain continuous.
    const panelViewFiredRef = useRef<InboxTabKey | null>(null)
    useEffect(() => {
        if ((activeTab !== 'settings' && activeTab !== 'sources') || onboarding || pending) {
            panelViewFiredRef.current = null
            return
        }
        if (panelViewFiredRef.current === activeTab) {
            return
        }
        panelViewFiredRef.current = activeTab
        captureInboxPanelViewed({ panel: activeTab === 'settings' ? 'config' : 'sources' })
    }, [activeTab, onboarding, pending])

    return (
        <div className="flex flex-col min-h-0 flex-1 min-w-0">
            {/* pl-5 (20px) aligns the first tab label with the SceneTitleSection description above. */}
            {!onboarding && !pending && (
                <div className="flex items-center gap-2 border-b border-primary pl-5 pr-6 shrink-0">
                    <InboxTabBar />
                    {/* Pairs with the header Triage button, which hides below the same @2xl/inbox width. */}
                    {activeTab === 'reports' && (
                        <div className="ml-auto shrink-0 @2xl/inbox:hidden">
                            <InboxTriageButton compact />
                        </div>
                    )}
                </div>
            )}
            <div className="flex-1 overflow-auto min-h-0">
                {pending ? (
                    <div className="mx-auto max-w-4xl px-6 py-4">
                        <CardSkeleton count={4} variant="cards" />
                    </div>
                ) : onboarding ? (
                    <InboxOnboardingEntry />
                ) : (
                    <ActiveTabBody tab={activeTab} />
                )}
            </div>
        </div>
    )
}

/**
 * Detail view: replaces the list full-width. Renders the unified `ReportDetail`, which owns its own
 * header (back link, actions) and the evidence-first two-column layout.
 */
function InboxDetailView({ report }: { report: SignalReport }): JSX.Element {
    const { reportDetailScrolled } = useActions(inboxSceneLogic)
    // Report only the first scroll per report to the logic (which fires `Inbox report scrolled` once),
    // so a fast native scroll doesn't dispatch an action on every frame.
    const scrolledReportRef = useRef<string | null>(null)

    return (
        <div
            className="-mt-2 flex flex-col min-h-0 flex-1 overflow-auto"
            onScroll={() => {
                if (scrolledReportRef.current !== report.id) {
                    scrolledReportRef.current = report.id
                    reportDetailScrolled()
                }
            }}
        >
            {/* Key on the report so per-report detail state (e.g. the active diff tab) resets on navigation. */}
            <ReportDetail key={report.id} report={report} />
        </div>
    )
}

/**
 * Shared chrome for the full-width scout panels (scratchpad, findings, runs): a "Scouts" back link
 * over the panel body, rendered full-width over the list like the scout detail.
 */
function InboxPanelView({ onBack, children }: { onBack: () => void; children: JSX.Element }): JSX.Element {
    return (
        <div className="flex flex-col min-h-0 flex-1 overflow-auto">
            <div className="px-4 pt-3">
                <LemonButton
                    type="tertiary"
                    size="small"
                    icon={<IconArrowLeft />}
                    onClick={onBack}
                    className="self-start"
                >
                    Scouts
                </LemonButton>
            </div>
            {children}
        </div>
    )
}

/** The runs panel body, with its own once-per-open view event since it renders no report list. */
function InboxRunsPanel(): JSX.Element {
    const { signalRuns, signalRunsLoading } = useValues(inboxSceneLogic)
    // Hold until the load settles so the count is real.
    const panelViewFiredRef = useRef(false)
    useEffect(() => {
        if (signalRunsLoading || panelViewFiredRef.current) {
            return
        }
        panelViewFiredRef.current = true
        captureInboxPanelViewed({ panel: 'runs', itemCount: signalRuns.length })
    }, [signalRunsLoading, signalRuns.length])

    return <RunsTab runs={signalRuns} loading={signalRunsLoading} />
}

export function InboxScene(): JSX.Element {
    const {
        activeTab,
        selectedReportId,
        selectedReport,
        selectedReportLoading,
        selectedScoutSkillName,
        isScratchpadOpen,
        isFindingsOpen,
        isRunsOpen,
        isTriageOpen,
    } = useValues(inboxSceneLogic)
    const { setScratchpadOpen, setFindingsOpen, setRunsOpen } = useActions(inboxSceneLogic)
    const { onboardingMode } = useValues(inboxOnboardingLogic)
    const { searchParams } = useValues(router)

    // Surfaces that embed inbox cards (e.g. the customer analytics feed) set a `?back=` internal path;
    // send the not-found state back there rather than the inbox, mirroring `InboxDetailFrame`.
    const rawBack = searchParams.back
    const backOverride =
        typeof rawBack === 'string' && rawBack.startsWith('/') && !rawBack.startsWith('//') ? rawBack : null

    // Full-width surfaces (report, scout, the scout panels, runs, triage mode) render over the list,
    // but the list view stays *mounted* (just hidden) rather than being unmounted. That keeps
    // `reportListLogic` and the scroll container alive, so clicking "back" lands on the same scroll
    // position with the same loaded pages — instead of remounting and resetting to the first page.
    const showDetail =
        !!selectedReportId ||
        !!selectedScoutSkillName ||
        isScratchpadOpen ||
        isFindingsOpen ||
        isRunsOpen ||
        isTriageOpen

    // `t` opens triage mode from the report list. Not while a surface covers the list, not while
    // the inbox is locked behind onboarding.
    const listInteractive = !showDetail && activeTab === 'reports' && onboardingMode === 'none'
    useKeyboardHotkeys(
        {
            t: { action: () => router.actions.push(urls.inboxTriage()), disabled: !listInteractive },
        },
        [listInteractive]
    )

    // The scout panels replace the list without going through any tab, so they'd otherwise leave
    // no trace at all. The scout detail reports itself (it has the config to describe).
    useEffect(() => {
        if (isFindingsOpen) {
            captureInboxPanelViewed({ panel: 'findings' })
        }
    }, [isFindingsOpen])
    useEffect(() => {
        if (isScratchpadOpen) {
            captureInboxPanelViewed({ panel: 'scratchpad' })
        }
    }, [isScratchpadOpen])

    // Each tab's primary action sits in the scene header rather than inside the tab: one predictable
    // place, and it stays reachable when filters narrow the tab down to nothing. Not while onboarding
    // has the tab locked (or is still deciding): the tab isn't reachable then.
    const tabUnlocked = onboardingMode !== 'takeover' && onboardingMode !== 'pending'
    const headerActions = !tabUnlocked ? undefined : activeTab === 'scouts' ? (
        <ScoutsRosterActions />
    ) : activeTab === 'reports' ? (
        <div className="hidden @2xl/inbox:contents">
            <InboxTriageButton />
        </div>
    ) : undefined

    return (
        <SceneContent className="@container/inbox gap-y-0 border-b-0 flex-1 min-h-0">
            <div className={showDetail ? 'hidden' : 'flex flex-col gap-y-4 flex-1 min-h-0'}>
                <SceneTitleSection
                    name="Self-driving inbox"
                    // The description explains the active tab so new users can orient themselves.
                    // The welcome takeover leads with its own full-size pitch, so a description here
                    // would say the same thing twice. While the verdict is pending no description is
                    // safe to show – it would flash and swap.
                    description={
                        onboardingMode === 'pending' || onboardingMode === 'takeover'
                            ? null
                            : INBOX_TAB_DESCRIPTION[activeTab]
                    }
                    resourceType={{ type: 'inbox' }}
                    actions={headerActions}
                />

                <div className="flex flex-col -mx-4 -mt-4 flex-1 min-h-0">
                    {/* The inbox always renders (its own list skeleton covers loading). When self-driving
                        isn't set up, the list view itself swaps in the welcome page; the banner sits
                        above the otherwise-normal inbox when there's already work to keep. */}
                    {onboardingMode === 'banner' && <InboxOnboardingBanner />}
                    {onboardingMode !== 'takeover' && onboardingMode !== 'pending' && (
                        <InboxValueSummary visible={!showDetail} />
                    )}
                    <InboxListView />
                </div>
            </div>

            {showDetail && (
                <div className="flex flex-col -mx-4 flex-1 min-h-0">
                    {isTriageOpen ? (
                        <InboxTriageView />
                    ) : isFindingsOpen ? (
                        <InboxPanelView onBack={() => setFindingsOpen(false)}>
                            <FindingsPanel />
                        </InboxPanelView>
                    ) : isScratchpadOpen ? (
                        <InboxPanelView onBack={() => setScratchpadOpen(false)}>
                            <ScratchpadPanel />
                        </InboxPanelView>
                    ) : isRunsOpen ? (
                        <InboxPanelView onBack={() => setRunsOpen(false)}>
                            <InboxRunsPanel />
                        </InboxPanelView>
                    ) : selectedScoutSkillName ? (
                        <ScoutDetailView skillName={selectedScoutSkillName} />
                    ) : selectedReport ? (
                        <InboxDetailView report={selectedReport} />
                    ) : selectedReportLoading ? (
                        <div className="-mt-2 flex flex-col min-h-0 flex-1 overflow-auto">
                            <ReportDetailSkeleton />
                        </div>
                    ) : (
                        <div className="flex flex-1 flex-col items-center justify-center gap-3 text-center">
                            <div>
                                <h3 className="m-0 text-base font-semibold">Report not found</h3>
                                <p className="m-0 mt-1 text-sm text-tertiary">
                                    We can't find this report in this project. Check that you have the right project
                                    selected.
                                </p>
                            </div>
                            <LemonButton type="secondary" to={backOverride ?? urls.inbox('reports')}>
                                {backOverride ? 'Back' : 'Back to self-driving inbox'}
                            </LemonButton>
                        </div>
                    )}
                </div>
            )}

            <ScoutTemplateDraftModal />
        </SceneContent>
    )
}

export default InboxScene