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.

6.7 KB
Raw Blame
import { BindLogic, useValues } from 'kea'
import { Suspense, useEffect } from 'react'

import { Search } from 'lib/components/Search/Search'
import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'
import { runWhenIdle } from 'lib/utils/async'
import { cn } from 'lib/utils/css-classes'
import { lazyWithRetry } from 'lib/utils/retryImport'
import { maxLogic } from 'scenes/max/maxLogic'
import { SceneContentFallback } from 'scenes/SceneContentFallback'
import { urls } from 'scenes/urls'

import { SceneBreadcrumbBackButton } from '~/layout/scenes/components/SceneBreadcrumbs'

import { aiFirstHomepageLogic } from './aiFirstHomepageLogic'
import { HOMEPAGE_TAB_ID } from './constants'
import { HomepageInput } from './HomepageInput'
import { HomepageSearchResults } from './HomepageSearchResults'
import { HomepageThread } from './HomepageThread'

const ChatHeader = lazyWithRetry(() =>
    import('scenes/max/components/AiFirstMaxInstance').then((m) => ({ default: m.ChatHeader }))
)

const PHASE_ORDER = ['idle', 'moving', 'separator', 'content'] as const

function phaseAtLeast(current: string, target: string): boolean {
    return PHASE_ORDER.indexOf(current as any) >= PHASE_ORDER.indexOf(target as any)
}

export function AiFirstHomepage(): JSX.Element {
    const { mode, animationPhase, query, threadStarted } = useValues(aiFirstHomepageLogic)
    const { conversationId } = useValues(maxLogic({ panelId: HOMEPAGE_TAB_ID }))

    useEffect(
        () =>
            runWhenIdle(() => {
                void import('scenes/max/components/AiFirstMaxInstance').catch(() => {})
            }),
        []
    )

    const isIdle = mode === 'idle'
    const isAi = mode === 'ai'
    const isSearch = mode === 'search'
    const isContent = animationPhase === 'content'

    return (
        <BindLogic logic={maxLogic} props={{ panelId: HOMEPAGE_TAB_ID }}>
            <Search.Root
                logicKey="homepage"
                showAskAiLink={false}
                isActive={isSearch}
                defaultSearchValue={isSearch ? query : ''}
                className="grow overflow-hidden h-full relative"
            >
                {/* Chat header — fixed at top, fades in independently */}
                <div
                    className={cn(
                        // visibility joins the transition so the fade still plays but the hidden
                        // header releases its raster backing (see PanelLayout scrims)
                        'absolute top-0 left-0 right-0 z-10 transition-[opacity,visibility] duration-300 ease-out motion-reduce:duration-0',
                        isAi && phaseAtLeast(animationPhase, 'separator')
                            ? 'opacity-100'
                            : 'opacity-0 pointer-events-none invisible'
                    )}
                >
                    {isAi && (
                        <Suspense
                            fallback={
                                <SceneContentFallback>
                                    <LemonSkeleton className="h-10 w-full" />
                                </SceneContentFallback>
                            }
                        >
                            <ChatHeader conversationId={conversationId} hideBorder>
                                <SceneBreadcrumbBackButton
                                    forceBackTo={{
                                        name: 'Project homepage',
                                        path: urls.projectHomepage(),
                                        type: 'projectTree',
                                        key: 'projectHomepage',
                                    }}
                                />
                            </ChatHeader>
                        </Suspense>
                    )}
                </div>

                <div className="flex flex-col grow overflow-hidden h-full">
                    {/* Top spacer — grows in idle (centering), collapses for AI and search */}
                    <div
                        className={cn(
                            'basis-0 transition-[flex-grow] duration-300 ease-out motion-reduce:duration-0',
                            isIdle || (isAi && !isContent) ? 'grow' : 'grow-0'
                        )}
                    />

                    {/* Thread container — fades in when AI content phase, with top padding for fixed header */}
                    <div
                        className={cn(
                            'basis-0 min-h-0 flex flex-col transition-opacity duration-300 ease-out motion-reduce:duration-0',
                            isAi && isContent ? 'grow opacity-100 pt-12' : 'grow-0 opacity-0'
                        )}
                    >
                        {isAi && threadStarted && <HomepageThread />}
                    </div>

                    {/* Top separator — hidden in search (flush to top edge) */}
                    <div
                        className={cn(
                            'border-b transition-opacity duration-200 motion-reduce:duration-0',
                            !isIdle && !isSearch && phaseAtLeast(animationPhase, 'separator')
                                ? 'opacity-100'
                                : 'opacity-0'
                        )}
                    />

                    {/* Input — always rendered, position determined by spacers */}
                    <HomepageInput />

                    {/* Bottom separator — hidden in AI (flush to bottom edge) */}
                    <div
                        className={cn(
                            'border-b transition-opacity duration-200 motion-reduce:duration-0',
                            !isIdle && !isAi && phaseAtLeast(animationPhase, 'separator') ? 'opacity-100' : 'opacity-0'
                        )}
                    />

                    {/* Results container — fades in when search content phase */}
                    <div
                        className={cn(
                            'basis-0 min-h-0 flex flex-col transition-opacity duration-300 ease-out motion-reduce:duration-0',
                            isSearch && isContent ? 'grow opacity-100' : 'grow-0 opacity-0'
                        )}
                    >
                        {isSearch && <HomepageSearchResults />}
                    </div>

                    {/* Bottom spacer — grows in idle (centering), collapses for search and AI */}
                    <div
                        className={cn(
                            'basis-0 transition-[flex-grow] duration-300 ease-out motion-reduce:duration-0',
                            isIdle || (isSearch && !isContent) ? 'grow' : 'grow-0'
                        )}
                    />
                </div>
            </Search.Root>
        </BindLogic>
    )
}