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.

9.2 KB
Raw Blame
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
import { CSSProperties, useEffect } from 'react'
import { List, useListRef } from 'react-window'

import { IconHome, IconPinFilled, IconUser } from '@posthog/icons'

import { addToDashboardModalLogic } from 'lib/components/AddToDashboard/addToDashboardModalLogic'
import { AutoSizer } from 'lib/components/AutoSizer'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'
import { LemonModal } from 'lib/lemon-ui/LemonModal'
import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'
import { Link } from 'lib/lemon-ui/Link'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { pluralize } from 'lib/utils/strings'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { dashboardsModel } from '~/models/dashboardsModel'
import { DashboardBasicType, InsightLogicProps } from '~/types'

interface DashboardRelationRowProps {
    dashboard: DashboardBasicType
    insightProps: InsightLogicProps
    canEditInsight: boolean
    isHighlighted: boolean
    isAlreadyOnDashboard: boolean
    currentUserUuid: string | null
    style: CSSProperties
}

const DashboardRelationRow = ({
    style,
    isHighlighted,
    isAlreadyOnDashboard,
    dashboard,
    insightProps,
    canEditInsight,
    currentUserUuid,
}: DashboardRelationRowProps): JSX.Element => {
    const { addToDashboard, removeFromDashboard } = useActions(addToDashboardModalLogic(insightProps))
    const { dashboardWithActiveAPICall } = useValues(addToDashboardModalLogic(insightProps))

    const { currentTeam } = useValues(teamLogic)
    const isPrimary = dashboard.id === currentTeam?.primary_dashboard
    const isMine = Boolean(currentUserUuid && dashboard.created_by?.uuid === currentUserUuid)
    return (
        <div
            data-attr="dashboard-list-item"
            /* eslint-disable-next-line react/forbid-dom-props */
            style={style}
            className={clsx('flex items-center deprecated-space-x-2', isHighlighted && 'highlighted')}
        >
            <Link
                to={urls.dashboard(dashboard.id)}
                className="overflow-hidden text-ellipsis whitespace-nowrap"
                title={dashboard.name}
            >
                {dashboard.name || 'Untitled'}
            </Link>
            {dashboard.pinned && (
                <Tooltip title="Pinned dashboard">
                    <span className="flex shrink-0 items-center">
                        <IconPinFilled className="text-secondary text-base" />
                    </span>
                </Tooltip>
            )}
            {isMine && (
                <Tooltip title="Your dashboard">
                    <span className="flex shrink-0 items-center">
                        <IconUser className="text-secondary text-base" />
                    </span>
                </Tooltip>
            )}
            {isPrimary && (
                <Tooltip title="Primary dashboards are shown on the project home page">
                    <span className="flex items-center">
                        <IconHome className="text-warning text-base" />
                    </span>
                </Tooltip>
            )}
            <span className="grow" />
            <LemonButton
                type="secondary"
                status={isAlreadyOnDashboard ? 'danger' : 'default'}
                aria-label={`${isAlreadyOnDashboard ? 'Remove from' : 'Add to'} ${dashboard.name || 'Untitled'} dashboard`}
                loading={dashboardWithActiveAPICall === dashboard.id}
                disabledReason={
                    !canEditInsight
                        ? "You don't have permission to edit this dashboard"
                        : dashboardWithActiveAPICall
                          ? 'Loading...'
                          : ''
                }
                size="small"
                onClick={(e) => {
                    e.preventDefault()
                    isAlreadyOnDashboard ? removeFromDashboard(dashboard.id) : addToDashboard(dashboard.id)
                }}
            >
                {isAlreadyOnDashboard ? 'Remove' : 'Add'}
            </LemonButton>
        </div>
    )
}

interface DashboardRowProps {
    orderedDashboards: DashboardBasicType[]
    currentDashboards: DashboardBasicType[]
    insightProps: InsightLogicProps
    canEditInsight: boolean
    scrollIndex: number
    currentUserUuid: string | null
}

const DashboardRow = ({
    index,
    style,
    orderedDashboards,
    currentDashboards,
    insightProps,
    canEditInsight,
    scrollIndex,
    currentUserUuid,
}: {
    ariaAttributes: Record<string, unknown>
    index: number
    style: CSSProperties
} & DashboardRowProps): JSX.Element => {
    return (
        <DashboardRelationRow
            dashboard={orderedDashboards[index]}
            insightProps={insightProps}
            canEditInsight={canEditInsight}
            isHighlighted={index === scrollIndex}
            isAlreadyOnDashboard={currentDashboards.some(
                (currentDashboard) => currentDashboard.id === orderedDashboards[index].id
            )}
            currentUserUuid={currentUserUuid}
            style={style}
        />
    )
}

interface SaveToDashboardModalProps {
    isOpen: boolean
    closeModal: () => void
    insightProps: InsightLogicProps
    canEditInsight: boolean
    'data-attr'?: string
}

export function AddToDashboardModal({
    isOpen,
    closeModal,
    insightProps,
    canEditInsight,
    'data-attr': dataAttr,
}: SaveToDashboardModalProps): JSX.Element {
    const logic = addToDashboardModalLogic(insightProps)

    const { searchQuery, currentDashboards, orderedDashboards, scrollIndex, user } = useValues(logic)
    const { setSearchQuery, addNewDashboard } = useActions(logic)
    const { dashboardsLoading } = useValues(dashboardsModel)
    const { loadDashboardsIfNeeded } = useActions(dashboardsModel)
    const listRef = useListRef(null)

    useEffect(() => {
        if (scrollIndex >= 0 && listRef.current) {
            listRef.current.scrollToRow({ index: scrollIndex, align: 'smart' })
        }
    }, [scrollIndex, listRef.current])

    useEffect(() => {
        if (isOpen) {
            loadDashboardsIfNeeded()
        }
    }, [isOpen, loadDashboardsIfNeeded])

    const rowProps: DashboardRowProps = {
        orderedDashboards,
        currentDashboards,
        insightProps,
        canEditInsight,
        scrollIndex,
        currentUserUuid: user?.uuid ?? null,
    }

    return (
        <LemonModal
            onClose={() => {
                closeModal()
                setSearchQuery('')
            }}
            isOpen={isOpen}
            title="Add to dashboard"
            data-attr={dataAttr}
            footer={
                <>
                    <div className="flex-1">
                        <LemonButton
                            type="secondary"
                            onClick={addNewDashboard}
                            disabledReason={
                                !canEditInsight
                                    ? 'You do not have permission to add this insight to dashboards'
                                    : undefined
                            }
                        >
                            Add to a new dashboard
                        </LemonButton>
                    </div>
                    <LemonButton type="secondary" onClick={closeModal}>
                        Close
                    </LemonButton>
                </>
            }
        >
            <div className="deprecated-space-y-2 w-192 max-w-full">
                <LemonInput
                    data-attr="dashboard-searchfield"
                    type="search"
                    fullWidth
                    placeholder="Search for dashboards..."
                    value={searchQuery}
                    onChange={(newValue) => setSearchQuery(newValue)}
                    autoFocus
                />
                <div className="text-secondary">
                    This insight is referenced on <strong className="text-text-3000">{currentDashboards.length}</strong>{' '}
                    {pluralize(currentDashboards.length, 'dashboard', 'dashboards', false)}
                </div>
                <div className="min-h-[420px]">
                    {dashboardsLoading ? (
                        <LemonSkeleton.Row repeat={8} />
                    ) : (
                        <AutoSizer
                            renderProp={({ height, width }) =>
                                height && width ? (
                                    <List<DashboardRowProps>
                                        listRef={listRef}
                                        style={{ width, height }}
                                        rowCount={orderedDashboards.length}
                                        overscanCount={100}
                                        rowHeight={40}
                                        rowComponent={DashboardRow}
                                        rowProps={rowProps}
                                    />
                                ) : null
                            }
                        />
                    )}
                </div>
            </div>
        </LemonModal>
    )
}