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.

1.3 KB
Raw Blame
import { useValues } from 'kea'

import { IconDashboard } from '@posthog/icons'

import { IconWithCount } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { liveDashboardIds } from 'scenes/dashboard/liveDashboardIds'

import { dashboardsModel } from '~/models/dashboardsModel'
import { InsightModel } from '~/types'

interface SaveToDashboardProps {
    insight: Partial<InsightModel>
    setOpenModal: (open: boolean) => void
}

export function AddToDashboard({ insight, setOpenModal }: SaveToDashboardProps): JSX.Element | null {
    const { rawDashboards } = useValues(dashboardsModel)
    const dashboards = liveDashboardIds(insight.dashboard_tiles)
        .map((dashboardId) => rawDashboards[dashboardId])
        .filter((d) => !!d)

    return (
        <span className="save-to-dashboard" data-attr="save-to-dashboard-button">
            <LemonButton
                onClick={() => setOpenModal(true)}
                type="secondary"
                icon={
                    <IconWithCount count={dashboards.length} showZero={false}>
                        <IconDashboard />
                    </IconWithCount>
                }
                tooltip={dashboards.length === 0 ? 'Add to dashboard' : 'Manage dashboards'}
            >
                Dashboards
            </LemonButton>
        </span>
    )
}