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.

2.4 KB
Raw Blame
import './ProjectHomepage.scss'

import { useActions, useValues } from 'kea'

import { CompactList } from 'lib/components/CompactList/CompactList'
import { dayjs } from 'lib/dayjs'
import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { InsightIcon } from 'scenes/saved-insights/InsightIcon'
import { urls } from 'scenes/urls'

import { InsightModel } from '~/types'

import { ProjectHomePageCompactListItem } from './ProjectHomePageCompactListItem'
import { projectHomepageLogic } from './projectHomepageLogic'

interface InsightRowProps {
    insight: InsightModel
    dataAttr?: string
    /** When true, text wraps instead of truncating and the row height grows to fit. */
    allowWrap?: boolean
}

export function InsightRow({ insight, dataAttr, allowWrap }: InsightRowProps): JSX.Element {
    const { reportInsightOpenedFromRecentInsightList } = useActions(eventUsageLogic)

    return (
        <ProjectHomePageCompactListItem
            title={insight.name || insight.derived_name || 'Insight'}
            subtitle={`Last modified ${dayjs(insight.last_modified_at).fromNow()}`}
            prefix={<InsightIcon insight={insight} />}
            to={urls.insightView(insight.short_id)}
            onClick={() => {
                reportInsightOpenedFromRecentInsightList()
            }}
            dataAttr={dataAttr}
            allowWrap={allowWrap}
        />
    )
}

export function RecentInsights(): JSX.Element {
    const { recentInsights, recentInsightsLoading } = useValues(projectHomepageLogic)
    const { loadRecentInsights } = useActions(projectHomepageLogic)
    useOnMountEffect(loadRecentInsights)

    return (
        <>
            <CompactList
                title="Your recently viewed insights"
                viewAllURL={urls.savedInsights()}
                loading={recentInsightsLoading}
                emptyMessage={{
                    title: 'You have no recently viewed insights',
                    description: "Explore this project's insights by clicking below.",
                    buttonText: 'View insights',
                    buttonTo: urls.savedInsights(),
                }}
                items={recentInsights.slice(0, 5)}
                renderRow={(insight: InsightModel, index) => (
                    <InsightRow key={index} insight={insight} dataAttr="recent-insight-item" />
                )}
            />
        </>
    )
}