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.

4.3 KB
Raw Blame
import { MakeLogicType, connect, kea, key, path, props, selectors } from 'kea'

import { accessLevelSatisfied } from 'lib/utils/accessControlUtils'
import { objectsEqual } from 'lib/utils/objects'
import { liveDashboardIds } from 'scenes/dashboard/liveDashboardIds'

import { dashboardsModel } from '~/models/dashboardsModel'
import { AccessControlLevel, AccessControlResourceType, DashboardBasicType, DashboardTileBasicType } from '~/types'

import type { DashboardType } from '../../../../types'
import type { DashboardWidgetPlacementDestination } from './DashboardWidgetPlacementMenu'

/** Stable key for kea; e.g. insight short_id, `text-${id}`, `button-${id}`, or `text-tile-${tileId}`. */
export interface DashboardWidgetMenusLogicProps {
    instanceKey: string
    dashboardId: number | null | undefined
    dashboard_tiles: DashboardTileBasicType[] | null | undefined
}

function canEditDestinationDashboard(d: DashboardBasicType): boolean {
    return d.user_access_level
        ? accessLevelSatisfied(AccessControlResourceType.Dashboard, d.user_access_level, AccessControlLevel.Editor)
        : true
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardWidgetMenusLogicValues {
    nameSortedDashboards: (DashboardBasicType | DashboardType)[] // dashboardsModel
    copyToDestinations: DashboardWidgetPlacementDestination[]
    dashboardIdsWithThisWidget: Set<number>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardWidgetMenusLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        dashboardIdsWithThisWidget: (
            dashboard_tiles: DashboardTileBasicType[] | null | undefined,
            dashboardId: number | null | undefined
        ) => Set<number>
        copyToDestinations: (
            nameSortedDashboards: (DashboardBasicType | DashboardType)[],
            dashboardIdsWithThisWidget: Set<number>,
            dashboardId: number | null | undefined
        ) => DashboardWidgetPlacementDestination[]
    }
}

export type dashboardWidgetMenusLogicType = MakeLogicType<
    dashboardWidgetMenusLogicValues,
    {},
    DashboardWidgetMenusLogicProps,
    dashboardWidgetMenusLogicMeta
>

/** Copy / Move dashboard submenus for insight, text, and button widgets (placement + ACL). */
export const dashboardWidgetMenusLogic = kea<dashboardWidgetMenusLogicType>([
    path(['lib', 'components', 'Cards', 'InsightCard', 'dashboardWidgetMenusLogic']),
    props({} as DashboardWidgetMenusLogicProps),
    key((props) => `${props.instanceKey}-${props.dashboardId ?? 'nd'}`),
    connect(() => ({
        values: [dashboardsModel, ['nameSortedDashboards']],
    })),
    selectors(() => ({
        /** Dashboards where this widget (insight / text / button / …) is already placed. */
        dashboardIdsWithThisWidget: [
            (_, p) => [p.dashboard_tiles, p.dashboardId],
            (
                dashboard_tiles: DashboardTileBasicType[] | null | undefined,
                dashboardId: number | null | undefined
            ): Set<number> => {
                const set = new Set(liveDashboardIds(dashboard_tiles).map(Number))
                if (dashboardId != null) {
                    set.add(Number(dashboardId))
                }
                return set
            },
            { resultEqualityCheck: objectsEqual },
        ],
        /** Destinations for Copy to and Move to submenus (disabled rows explain why a target is unavailable). */
        copyToDestinations: [
            (s, p) => [s.nameSortedDashboards, s.dashboardIdsWithThisWidget, p.dashboardId],
            (
                nameSortedDashboards: (DashboardBasicType | import('~/types').DashboardType)[],
                dashboardIdsWithThisWidget: Set<number>,
                dashboardId: number | null | undefined
            ): DashboardWidgetPlacementDestination[] => {
                return nameSortedDashboards
                    .filter((d) => (dashboardId == null || d.id !== dashboardId) && canEditDestinationDashboard(d))
                    .map((d) => ({
                        dashboard: d,
                        disabledReason: dashboardIdsWithThisWidget.has(Number(d.id))
                            ? 'Already on this dashboard'
                            : undefined,
                    }))
            },
        ],
    })),
])