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.

8.4 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { combineUrl } from 'kea-router'

import { IconExternal, IconPlus, IconTrash } from '@posthog/icons'
import { LemonButton, LemonSkeleton, LemonTag, Link, Spinner } from '@posthog/lemon-ui'

import { ProductIntroduction } from 'lib/components/ProductIntroduction/ProductIntroduction'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'

import { ProductKey } from '~/queries/schema/schema-general'
import { Conversation, ConversationStatus, ConversationType } from '~/types'

import { openDeleteConversationDialog } from './conversationDialogs'
import { maxLogic } from './maxLogic'
import { formatConversationDate, getSlackThreadUrl } from './utils'

export interface ConversationHistoryProps {
    sidePanel?: boolean
    compact?: boolean
}

export function ConversationHistory({ sidePanel = false, compact = false }: ConversationHistoryProps): JSX.Element {
    const { conversationHistory, conversationHistoryLoading, conversationId } = useValues(maxLogic)
    const { toggleConversationHistory, openConversation, deleteConversation } = useActions(maxLogic)
    const { updateHasSeenProductIntroFor } = useActions(userLogic)

    if (compact) {
        return (
            <div className="flex flex-col gap-1 w-full pb-10">
                {conversationHistory.length > 0 ? (
                    conversationHistory.map((conversation) => (
                        <CompactConversationCard
                            key={conversation.id}
                            conversation={conversation}
                            openConversation={openConversation}
                            isActive={conversation.id === conversationId}
                        />
                    ))
                ) : conversationHistoryLoading ? (
                    <div className="flex flex-col gap-1">
                        <LemonSkeleton className="h-8" />
                        <LemonSkeleton className="h-8 opacity-60" />
                        <LemonSkeleton className="h-8 opacity-30" />
                    </div>
                ) : (
                    <div className="flex flex-col items-center justify-center text-center py-8 text-muted">
                        <p className="text-sm mb-0">No chats yet</p>
                    </div>
                )}
            </div>
        )
    }

    return (
        <div
            className="@container/chat-history flex flex-col gap-4 w-full self-center px-4 py-8 grow max-w-screen-lg"
            data-attr="max-conversation-history"
        >
            {conversationHistory.length > 0 ? (
                conversationHistory.map((conversation) => (
                    <ConversationCard
                        key={conversation.id}
                        conversation={conversation}
                        openConversation={openConversation}
                        sidePanel={sidePanel}
                        deleteConversation={deleteConversation}
                    />
                ))
            ) : conversationHistoryLoading ? (
                <>
                    <LemonSkeleton className="h-14" />
                    <LemonSkeleton className="h-14 opacity-80" />
                    <LemonSkeleton className="h-14 opacity-60" />
                    <LemonSkeleton className="h-14 opacity-40" />
                    <LemonSkeleton className="h-14 opacity-20" />
                    <LemonSkeleton className="h-14 opacity-10" />
                    <LemonSkeleton className="h-14 opacity-5" />
                </>
            ) : (
                <div className="flex items-center flex-1">
                    <ProductIntroduction
                        isEmpty
                        thingName="chat"
                        titleOverride="Start getting things done with PostHog AI"
                        description="PostHog AI is an agent that answers data questions, gets things done in UI, and provides insights from PostHog's documentation."
                        docsURL="https://posthog.com/docs/data/max-ai"
                        actionElementOverride={
                            <LemonButton
                                type="primary"
                                icon={<IconPlus />}
                                onClick={() => {
                                    updateHasSeenProductIntroFor(ProductKey.MAX)
                                    toggleConversationHistory()
                                }}
                            >
                                Send your first message
                            </LemonButton>
                        }
                    />
                </div>
            )}
        </div>
    )
}

interface ConversationCardProps {
    conversation: Conversation
    openConversation: (conversationId: string) => void
    sidePanel: boolean
    deleteConversation: (conversationId: string) => void
}

function ConversationCard({
    conversation,
    openConversation,
    sidePanel,
    deleteConversation,
}: ConversationCardProps): JSX.Element {
    return (
        <Link
            className="p-4 flex flex-row bg-surface-primary rounded-lg gap-2 w-full min-h-14 items-center justify-between"
            data-attr="max-open-conversation"
            to={combineUrl(urls.ai(conversation.id), { from: 'history' }).url}
            onClick={(e) => {
                if (sidePanel) {
                    e.preventDefault()
                    openConversation(conversation.id)
                }
            }}
        >
            <div className="flex items-center gap-2 flex-1 min-w-0">
                <span className="flex-1 line-clamp-1">{conversation.title}</span>
                {conversation.is_internal && <LemonTag type="muted">Impersonated</LemonTag>}
                {conversation.type === ConversationType.DeepResearch && <LemonTag>Research</LemonTag>}
                {conversation.slack_thread_key && (
                    <LemonTag>
                        <Link
                            to={getSlackThreadUrl(conversation.slack_thread_key, conversation.slack_workspace_domain)}
                            target="_blank"
                            className="flex items-center gap-1"
                            onClick={(e) => e.stopPropagation()}
                            tooltip="This chat was started in Slack"
                        >
                            Slack thread <IconExternal />
                        </Link>
                    </LemonTag>
                )}
            </div>
            <div className="flex items-center gap-2 shrink-0">
                {conversation.status === ConversationStatus.InProgress ? (
                    <Spinner className="h-4 w-4" />
                ) : (
                    <span className="text-secondary">{formatConversationDate(conversation.updated_at)}</span>
                )}
                <LemonButton
                    size="small"
                    icon={<IconTrash />}
                    status="danger"
                    data-attr="max-conversation-delete"
                    aria-label="Delete chat"
                    onClick={(e) => {
                        e.preventDefault()
                        e.stopPropagation()
                        openDeleteConversationDialog(() => deleteConversation(conversation.id))
                    }}
                />
            </div>
        </Link>
    )
}

interface CompactConversationCardProps {
    conversation: Conversation
    openConversation: (conversationId: string) => void
    isActive?: boolean
}

function CompactConversationCard({
    conversation,
    openConversation,
    isActive,
}: CompactConversationCardProps): JSX.Element {
    return (
        <Link
            to={combineUrl(urls.ai(conversation.id), { from: 'history' }).url}
            data-attr="max-open-conversation"
            onClick={(e) => {
                e.preventDefault()
                openConversation(conversation.id)
            }}
            buttonProps={{
                active: isActive,
                fullWidth: true,
            }}
            tooltip={conversation.title || 'view conversation'}
            tooltipPlacement="right"
        >
            <span className="flex-1 line-clamp-1 text-primary">{conversation.title}</span>
            {conversation.status === ConversationStatus.InProgress && <Spinner className="h-3 w-3" />}
            <span className="opacity-30 text-xs">{formatConversationDate(conversation.updated_at)}</span>
        </Link>
    )
}