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.5 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, path, reducers } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

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

import { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'

import { projectTreeDataLogic } from '~/layout/panel-layout/ProjectTree/projectTreeDataLogic'
import { joinPath, splitPath } from '~/layout/panel-layout/ProjectTree/utils'
import { FileSystemEntry } from '~/queries/schema/schema-general'

import type { FeatureFlagsSet } from '../../../logic/featureFlagLogic'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface itemSelectModalLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    shortcutData: FileSystemEntry[] // projectTreeDataLogic
    form: {
        item: TreeDataItem | null
    }
    formAllErrors: Record<string, any>
    formChanged: boolean
    formErrors: DeepPartialMap<
        {
            item: TreeDataItem | null
        },
        ValidationErrorType
    >
    formHasErrors: boolean
    formManualErrors: Record<string, any>
    formTouched: boolean
    formTouches: Record<string, boolean>
    formValidationErrors: DeepPartialMap<
        {
            item: TreeDataItem | null
        },
        ValidationErrorType
    >
    isFormSubmitting: boolean
    isFormValid: boolean
    isOpen: boolean
    showFormErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface itemSelectModalLogicActions {
    addShortcutItem: (
        item: FileSystemEntry,
        successMessage?: string | undefined
    ) => {
        item: FileSystemEntry
        successMessage: string | undefined
    } // projectTreeDataLogic
    closeItemSelectModal: () => {
        value: true
    }
    openItemSelectModal: () => {
        value: true
    }
    resetForm: (values?: { item: TreeDataItem | null }) => {
        values?: {
            item: TreeDataItem | null
        }
    }
    setFormManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setFormValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setFormValues: (
        values: DeepPartial<{
            item: TreeDataItem | null
        }>
    ) => {
        values: DeepPartial<{
            item: TreeDataItem | null
        }>
    }
    submitForm: () => {
        value: boolean
    }
    submitFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitFormRequest: (form: { item: TreeDataItem | null }) => {
        form: {
            item: TreeDataItem | null
        }
    }
    submitFormSuccess: (form: { item: TreeDataItem | null }) => {
        form: {
            item: TreeDataItem | null
        }
    }
    touchFormField: (key: string) => {
        key: string
    }
}

export type itemSelectModalLogicType = MakeLogicType<itemSelectModalLogicValues, itemSelectModalLogicActions>

export const itemSelectModalLogic = kea<itemSelectModalLogicType>([
    path(['lib', 'components', 'FileSystem', 'ItemSelectModal', 'itemSelectModalLogic']),
    connect(() => ({
        values: [featureFlagLogic, ['featureFlags'], projectTreeDataLogic, ['shortcutData']],
        actions: [projectTreeDataLogic, ['addShortcutItem']],
    })),
    actions({
        openItemSelectModal: true,
        closeItemSelectModal: true,
    }),
    reducers({
        isOpen: [
            false,
            {
                openItemSelectModal: () => true,
                closeItemSelectModal: () => false,
            },
        ],
    }),
    forms(({ actions, values }) => ({
        form: {
            defaults: {
                item: null as TreeDataItem | null,
            },
            submit: async (formValues) => {
                if (formValues.item?.record) {
                    const item = formValues.item.record as FileSystemEntry
                    const shortcutPath = joinPath([splitPath(item.path).pop() ?? 'Unnamed'])

                    if (values.shortcutData.some((s) => s.path === shortcutPath)) {
                        lemonToast.info('Shortcut already exists')
                        return
                    }

                    await actions.addShortcutItem(item)
                    actions.closeItemSelectModal()
                }
            },
        },
    })),
])