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.

19.4 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import { createElement, Fragment } from 'react'

import { LemonDialog, lemonToast } from '@posthog/lemon-ui'

import api from 'lib/api'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import type { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'

import { DashboardTemplateEditorType, DashboardTemplateType, MonacoMarker, NonPortableReferences } from '~/types'

import { dashboardTemplatesLogic } from './dashboards/templates/dashboardTemplatesLogic'

function refreshDashboardTemplateListsAfterMutation(): void {
    dashboardTemplatesLogic.findMounted({ scope: 'default', templatesTabList: true })?.actions.getAllTemplates()
    dashboardTemplatesLogic.findMounted({ scope: 'default' })?.actions.getAllTemplates()
    dashboardTemplatesLogic.findMounted({ scope: 'feature_flag' })?.actions.getAllTemplates()
    dashboardTemplatesLogic
        .findMounted({ scope: 'default', listQuery: { is_featured: true } })
        ?.actions.getAllTemplates()
}

/** Human-readable phrases for the project-specific references that may not resolve when sharing a template org-wide. */
function describeNonPortableReferences(refs: NonPortableReferences | null | undefined): string[] {
    const warnings: string[] = []
    if (!refs) {
        return warnings
    }
    if (refs.actions > 0) {
        warnings.push(`${refs.actions} action${refs.actions === 1 ? '' : 's'}`)
    }
    if (refs.cohorts > 0) {
        warnings.push(`${refs.cohorts} cohort${refs.cohorts === 1 ? '' : 's'}`)
    }
    if (refs.warehouse_tables.length > 0) {
        warnings.push(
            `${refs.warehouse_tables.length === 1 ? 'data warehouse table' : 'data warehouse tables'} ${refs.warehouse_tables.join(', ')}`
        )
    }
    return warnings
}

function parseDashboardTemplateEditorPayload(raw: string | undefined): DashboardTemplateEditorType | undefined {
    const trimmed = raw?.trim()
    if (!trimmed) {
        return undefined
    }
    try {
        return JSON.parse(trimmed) as DashboardTemplateEditorType
    } catch {
        return undefined
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardTemplateEditorLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    dashboardTemplate: DashboardTemplateEditorType | null | undefined
    dashboardTemplateLoading: boolean
    editorValue: string
    id: string | null
    isOpenNewDashboardTemplateModal: boolean
    templateSchema: Record<string, any> | null
    templateSchemaLoading: boolean
    validationErrors: string[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardTemplateEditorLogicActions {
    clear: () => {
        value: true
    }
    closeDashboardTemplateEditor: () => {
        value: true
    }
    createDashboardTemplate: () => any
    createDashboardTemplateFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    createDashboardTemplateSuccess: (
        dashboardTemplate: DashboardTemplateEditorType | undefined,
        payload?: any
    ) => {
        dashboardTemplate: DashboardTemplateEditorType | undefined
        payload?: any
    }
    deleteDashboardTemplate: ({ id, templateName }: { id: string; templateName: string }) => {
        id: string
        templateName: string
    }
    deleteDashboardTemplateFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    deleteDashboardTemplateSuccess: (
        dashboardTemplate: null,
        payload?: {
            id: string
            templateName: string
        }
    ) => {
        dashboardTemplate: null
        payload?: {
            id: string
            templateName: string
        }
    }
    getDashboardTemplate: (id: string) => string
    getDashboardTemplateFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    getDashboardTemplateSuccess: (
        dashboardTemplate: DashboardTemplateType,
        payload?: string
    ) => {
        dashboardTemplate: DashboardTemplateType
        payload?: string
    }
    getTemplateSchema: () => any
    getTemplateSchemaFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    getTemplateSchemaSuccess: (
        templateSchema: Record<string, any>,
        payload?: any
    ) => {
        templateSchema: Record<string, any>
        payload?: any
    }
    openDashboardTemplateEditor: () => {
        value: true
    }
    setDashboardTemplate: (dashboardTemplate: DashboardTemplateEditorType) => {
        dashboardTemplate: DashboardTemplateEditorType
    }
    setDashboardTemplateId: (
        id: string | null,
        options?: {
            hydrateEditorFromApi?: boolean
        }
    ) => {
        hydrateEditorFromApi: boolean
        id: string | null
    }
    setEditorValue: (value: string) => {
        value: string
    }
    toggleTemplateOrganizationScope: (template: DashboardTemplateType) => {
        template: DashboardTemplateType
    }
    updateDashboardTemplate: ({
        id,
        dashboardTemplateUpdates,
    }: {
        dashboardTemplateUpdates?: Partial<DashboardTemplateType>
        id: string
    }) => {
        id: string
        dashboardTemplateUpdates?: Partial<DashboardTemplateType>
    }
    updateDashboardTemplateFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    updateDashboardTemplateSuccess: (
        dashboardTemplate: DashboardTemplateEditorType | undefined,
        payload?: {
            id: string
            dashboardTemplateUpdates?: Partial<DashboardTemplateType>
        }
    ) => {
        dashboardTemplate: DashboardTemplateEditorType | undefined
        payload?: {
            id: string
            dashboardTemplateUpdates?: Partial<DashboardTemplateType>
        }
    }
    updateValidationErrors: (markers: MonacoMarker[] | undefined) => {
        markers: MonacoMarker[] | undefined
    }
}

export type dashboardTemplateEditorLogicType = MakeLogicType<
    dashboardTemplateEditorLogicValues,
    dashboardTemplateEditorLogicActions
>

export const dashboardTemplateEditorLogic = kea<dashboardTemplateEditorLogicType>([
    path(['scenes', 'dashboard', 'dashboardTemplateEditorLogic']),
    connect(() => ({
        values: [featureFlagLogic, ['featureFlags']],
    })),
    actions({
        setEditorValue: (value: string) => ({ value }),
        setDashboardTemplate: (dashboardTemplate: DashboardTemplateEditorType) => ({
            dashboardTemplate,
        }),
        clear: true,
        setDashboardTemplateId: (id: string | null, options?: { hydrateEditorFromApi?: boolean }) => ({
            id,
            /** When false, editor JSON was already set (e.g. metadata modal merge); avoid GET overwriting it. Default true. */
            hydrateEditorFromApi: options?.hydrateEditorFromApi ?? true,
        }),
        openDashboardTemplateEditor: true,
        closeDashboardTemplateEditor: true,
        updateValidationErrors: (markers: MonacoMarker[] | undefined) => ({ markers }),
        /** Promote a team template to organization-wide, or demote it back. Confirms before either to avoid
         * accidental changes, and warns on promote when the template embeds project-specific references. */
        toggleTemplateOrganizationScope: (template: DashboardTemplateType) => ({ template }),
    }),
    reducers({
        editorValue: [
            '' as string,
            {
                setEditorValue: (_, { value }) => value,
                clear: () => '',
            },
        ],
        dashboardTemplate: [
            null as DashboardTemplateEditorType | null,
            {
                clear: () => null,
                setDashboardTemplate: (_, { dashboardTemplate }) => dashboardTemplate,
            },
        ],
        validationErrors: [
            [] as string[],
            {
                updateValidationErrors: (_, { markers }): string[] => {
                    if (!markers || markers.length === 0) {
                        return []
                    }
                    return markers.map((marker: MonacoMarker) => marker.message)
                },
                clear: () => [],
            },
        ],
        id: [
            null as string | null,
            {
                setDashboardTemplateId: (_, { id }) => id,
                clear: () => null,
            },
        ],
        isOpenNewDashboardTemplateModal: [
            false as boolean,
            {
                openDashboardTemplateEditor: () => true,
                closeDashboardTemplateEditor: () => false,
            },
        ],
    }),
    loaders(({ values }) => ({
        dashboardTemplate: [
            undefined as DashboardTemplateEditorType | undefined | null,
            {
                createDashboardTemplate: async (): Promise<DashboardTemplateEditorType | undefined> => {
                    const raw = values.editorValue?.trim()
                    let payload: DashboardTemplateEditorType | undefined
                    if (raw) {
                        payload = parseDashboardTemplateEditorPayload(values.editorValue)
                        if (!payload) {
                            lemonToast.error('Unable to parse dashboard template JSON')
                            return
                        }
                    } else {
                        payload = values.dashboardTemplate ?? undefined
                    }
                    if (!payload) {
                        lemonToast.error('Unable to create dashboard template')
                        return
                    }
                    const response = await api.dashboardTemplates.create(payload)
                    lemonToast.success('Dashboard template created')
                    return response
                },
                getDashboardTemplate: async (id: string): Promise<DashboardTemplateType> => {
                    const response = await api.dashboardTemplates.get(id)
                    return response
                },
                updateDashboardTemplate: async ({
                    id,
                    dashboardTemplateUpdates,
                }: {
                    id: string
                    dashboardTemplateUpdates?: Partial<DashboardTemplateType>
                }): Promise<DashboardTemplateEditorType | undefined> => {
                    let response = null
                    if (dashboardTemplateUpdates) {
                        response = await api.dashboardTemplates.update(id, dashboardTemplateUpdates)
                    } else {
                        const raw = values.editorValue?.trim()
                        let payload: DashboardTemplateEditorType | undefined
                        if (raw) {
                            payload = parseDashboardTemplateEditorPayload(values.editorValue)
                            if (!payload) {
                                lemonToast.error('Unable to parse dashboard template JSON')
                                return
                            }
                        } else {
                            payload = values.dashboardTemplate ?? undefined
                        }
                        if (!payload) {
                            lemonToast.error('Unable to update dashboard template')
                            return
                        }
                        response = await api.dashboardTemplates.update(id, payload)
                    }
                    lemonToast.success('Dashboard template updated')
                    return response
                },
                deleteDashboardTemplate: async ({
                    id,
                    templateName,
                }: {
                    id: string
                    templateName: string
                }): Promise<null> => {
                    try {
                        await api.dashboardTemplates.update(id, { deleted: true })
                    } catch (e: any) {
                        lemonToast.error(e?.detail || e?.message || 'Could not delete dashboard template')
                        throw e
                    }
                    const trimmedName = templateName.trim()
                    lemonToast.info(
                        trimmedName
                            ? createElement(
                                  Fragment,
                                  null,
                                  'Dashboard template ',
                                  createElement('b', null, trimmedName),
                                  ' has been deleted'
                              )
                            : 'Dashboard template has been deleted',
                        {
                            toastId: `delete-dashboard-template-${id}`,
                            button: {
                                label: 'Undo',
                                dataAttr: 'undo-dashboard-template-delete',
                                action: async () => {
                                    try {
                                        await api.dashboardTemplates.update(id, { deleted: false })
                                        refreshDashboardTemplateListsAfterMutation()
                                        lemonToast.success(
                                            trimmedName
                                                ? createElement(
                                                      Fragment,
                                                      null,
                                                      'Dashboard template ',
                                                      createElement('b', null, trimmedName),
                                                      ' has been restored'
                                                  )
                                                : 'Dashboard template has been restored',
                                            { toastId: `undo-dashboard-template-${id}` }
                                        )
                                    } catch (err: any) {
                                        lemonToast.error(
                                            err?.detail || err?.message || 'Could not restore dashboard template'
                                        )
                                    }
                                },
                            },
                        }
                    )
                    return null
                },
            },
        ],
        templateSchema: [
            null as Record<string, any> | null,
            {
                getTemplateSchema: async (): Promise<Record<string, any>> => {
                    return await api.dashboardTemplates.getSchema()
                },
            },
        ],
    })),
    listeners(({ values, actions }) => ({
        createDashboardTemplateSuccess: async () => {
            actions.closeDashboardTemplateEditor()
            refreshDashboardTemplateListsAfterMutation()
        },
        updateDashboardTemplateSuccess: async () => {
            actions.closeDashboardTemplateEditor()
            refreshDashboardTemplateListsAfterMutation()
        },
        deleteDashboardTemplateSuccess: async () => {
            refreshDashboardTemplateListsAfterMutation()
        },
        openDashboardTemplateEditor: () => {
            if (!values.templateSchema && !values.templateSchemaLoading) {
                actions.getTemplateSchema()
            }
        },
        closeDashboardTemplateEditor: () => {
            actions.clear()
        },
        setDashboardTemplateId: ({ id, hydrateEditorFromApi }) => {
            if (id && hydrateEditorFromApi) {
                actions.getDashboardTemplate(id)
            }
        },
        getDashboardTemplateSuccess: async ({ dashboardTemplate }) => {
            if (dashboardTemplate) {
                actions.setEditorValue(JSON.stringify(dashboardTemplate))
            }
        },
        setEditorValue: async ({ value }, breakpoint) => {
            await breakpoint(500)
            if (values.validationErrors.length == 0 && value?.length) {
                try {
                    const dashboardTemplate = JSON.parse(value)
                    actions.setDashboardTemplate(dashboardTemplate)
                } catch (error) {
                    console.error('error', error)
                    lemonToast.error('Unable to parse dashboard template')
                }
            }
        },
        updateValidationErrors: async ({ markers }) => {
            // used to handle the race condition between the editor updating and the validation errors updating
            // otherwise the dashboard template might not be updated with the latest value
            if (!markers?.length) {
                actions.setEditorValue(values.editorValue)
            }
        },
        setDashboardTemplate: async ({ dashboardTemplate }) => {
            if (dashboardTemplate) {
                actions.setEditorValue(JSON.stringify(dashboardTemplate, null, 4))
            }
        },
        toggleTemplateOrganizationScope: async ({ template }) => {
            const { id, scope } = template
            if (id === undefined) {
                console.error('Dashboard template id not defined')
                return
            }
            if (scope === 'organization') {
                LemonDialog.open({
                    title: 'Make this template visible to this project only?',
                    description:
                        'It will no longer be shared with the other projects in your organization. They will lose access to it.',
                    primaryButton: {
                        children: 'Make project-only',
                        onClick: () =>
                            actions.updateDashboardTemplate({ id, dashboardTemplateUpdates: { scope: 'team' } }),
                    },
                    secondaryButton: { children: 'Cancel' },
                })
                return
            }
            // `non_portable_references` is only computed on single-template retrieve, so fetch before warning.
            let refs: NonPortableReferences | null | undefined = template.non_portable_references
            if (!refs) {
                try {
                    refs = (await api.dashboardTemplates.get(id)).non_portable_references
                } catch {
                    refs = undefined
                }
            }
            const warnings = describeNonPortableReferences(refs)
            if (warnings.length === 0) {
                actions.updateDashboardTemplate({ id, dashboardTemplateUpdates: { scope: 'organization' } })
                return
            }
            LemonDialog.open({
                title: 'Share this template with your whole organization?',
                description: `This template references items specific to this project (${warnings.join(
                    ', '
                )}). Insights using them may show errors in other projects — events and properties work everywhere.`,
                primaryButton: {
                    children: 'Share with organization',
                    onClick: () =>
                        actions.updateDashboardTemplate({ id, dashboardTemplateUpdates: { scope: 'organization' } }),
                },
                secondaryButton: { children: 'Cancel' },
            })
        },
    })),
])