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.

6.6 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import { featureFlagLogic } from 'lib/logic/featureFlagLogic'

import { projectTreeDataLogic } from '~/layout/panel-layout/ProjectTree/projectTreeDataLogic'
import { PROJECT_TREE_KEY, projectTreeLogic } from '~/layout/panel-layout/ProjectTree/projectTreeLogic'
import { calculateMovePath, parentPath } from '~/layout/panel-layout/ProjectTree/utils'
import { FileSystemEntry } from '~/queries/schema/schema-general'

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

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface linkToLogicActions {
    linkItem: (
        oldPath: string,
        newPath: string,
        force: boolean,
        projectTreeLogicKey: string
    ) => {
        force: boolean
        newPath: string
        oldPath: string
        projectTreeLogicKey: string
    } // projectTreeDataLogic
    setLastNewFolder: (folder: string | null) => {
        folder: string | null
    } // projectTreeDataLogic
    closeLinkToModal: () => {
        value: true
    }
    closedLinkToModal: () => {
        value: true
    }
    openLinkToModal: (items: FileSystemEntry[]) => {
        items: FileSystemEntry[]
    }
    resetForm: (values?: { folder: string | null }) => {
        values?: {
            folder: string | null
        }
    }
    setFormManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setFormValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setFormValues: (
        values: DeepPartial<{
            folder: string | null
        }>
    ) => {
        values: DeepPartial<{
            folder: string | null
        }>
    }
    submitForm: () => {
        value: boolean
    }
    submitFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitFormRequest: (form: { folder: string | null }) => {
        form: {
            folder: string | null
        }
    }
    submitFormSuccess: (form: { folder: string | null }) => {
        form: {
            folder: string | null
        }
    }
    touchFormField: (key: string) => {
        key: string
    }
}

export type linkToLogicType = MakeLogicType<linkToLogicValues, linkToLogicActions>

export const linkToLogic = kea<linkToLogicType>([
    path(['lib', 'components', 'LinkTo', 'linkToLogic']),
    connect(() => ({
        values: [
            projectTreeDataLogic,
            ['lastNewFolder', 'projectTreeRef', 'projectTreeRefEntry'],
            featureFlagLogic,
            ['featureFlags'],
        ],
        actions: [projectTreeDataLogic, ['setLastNewFolder', 'linkItem']],
    })),
    actions({
        openLinkToModal: (items: FileSystemEntry[]) => ({ items }),
        closeLinkToModal: true,
        closedLinkToModal: true,
    }),
    reducers({
        linkingItems: [
            [] as FileSystemEntry[],
            {
                openLinkToModal: (_, { items }) => items,
                closeLinkToModal: () => [],
            },
        ],
        isOpen: [
            false,
            {
                openLinkToModal: (_, { items }) => items.length > 0,
                closedLinkToModal: () => false,
            },
        ],
    }),
    listeners(({ actions, values }) => ({
        setLastNewFolder: ({ folder }) => {
            actions.setFormValue('folder', folder)
        },
        openLinkToModal: ({ items }) => {
            if (typeof values.lastNewFolder === 'string') {
                actions.setFormValue('folder', values.lastNewFolder)
            } else {
                const itemPath = items[0].path
                const itemFolder = parentPath(itemPath)
                actions.setFormValue('folder', itemFolder)
            }
        },
        closeLinkToModal: () => {
            actions.closedLinkToModal()
        },
    })),
    forms(({ actions, values }) => ({
        form: {
            defaults: {
                folder: null as string | null,
            },
            errors: ({ folder }) => ({
                folder: typeof folder !== 'string' ? 'You need to specify a folder.' : null,
            }),
            submit: (formValues) => {
                // When moving the current item, remember its ref so that we could open the destination folder later on
                const movingCurrentRef =
                    values.projectTreeRefEntry?.id &&
                    values.linkingItems.some((item) => item.id === values.projectTreeRefEntry?.id)
                        ? values.projectTreeRef
                        : null

                if (values.linkingItems.length > 0) {
                    for (const item of values.linkingItems) {
                        const { newPath, isValidMove } = calculateMovePath(item, formValues.folder || '')
                        if (isValidMove) {
                            actions.linkItem(item.path, newPath, false, PROJECT_TREE_KEY)
                        }
                    }
                }

                // Clear the moving items and close the modal
                if (movingCurrentRef) {
                    const logic = projectTreeLogic.findMounted({ key: PROJECT_TREE_KEY })
                    logic?.actions.assureVisibility(movingCurrentRef)
                }

                actions.setLastNewFolder(formValues.folder)
                actions.closedLinkToModal()
            },
        },
    })),
])