: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
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 moveToLogicValues {
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
movingItems: FileSystemEntry[]
showFormErrors: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface moveToLogicActions {
moveItems: (
moves: { item: FileSystemEntry; newPath: string }[],
force: boolean,
projectTreeLogicKey: string
) => {
force: boolean
moves: { item: FileSystemEntry; newPath: string }[]
projectTreeLogicKey: string
} // projectTreeDataLogic
setLastNewFolder: (folder: string | null) => {
folder: string | null
} // projectTreeDataLogic
closeMoveToModal: () => {
value: true
}
closedMoveToModal: () => {
value: true
}
openMoveToModal: (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 moveToLogicType = MakeLogicType<moveToLogicValues, moveToLogicActions>
export const moveToLogic = kea<moveToLogicType>([
path(['lib', 'components', 'MoveTo', 'moveToLogic']),
connect(() => ({
values: [
projectTreeDataLogic,
['lastNewFolder', 'projectTreeRef', 'projectTreeRefEntry'],
featureFlagLogic,
['featureFlags'],
],
actions: [projectTreeDataLogic, ['setLastNewFolder', 'moveItems']],
})),
actions({
openMoveToModal: (items: FileSystemEntry[]) => ({ items }),
closeMoveToModal: true,
closedMoveToModal: true,
}),
reducers({
movingItems: [
[] as FileSystemEntry[],
{
openMoveToModal: (_, { items }) => items,
closeMoveToModal: () => [],
},
],
isOpen: [
false,
{
openMoveToModal: (_, { items }) => items.length > 0,
closedMoveToModal: () => false,
},
],
}),
listeners(({ actions, values }) => ({
setLastNewFolder: ({ folder }) => {
actions.setFormValue('folder', folder)
},
openMoveToModal: ({ 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)
}
},
closeMoveToModal: () => {
actions.closedMoveToModal()
},
})),
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.movingItems.some((item) => item.id === values.projectTreeRefEntry?.id)
? values.projectTreeRef
: null
// One batch for the whole selection, so it reports and undoes as a single move.
const moves = values.movingItems
.map((item) => ({ item, ...calculateMovePath(item, formValues.folder || '') }))
.filter(({ isValidMove }) => isValidMove)
.map(({ item, newPath }) => ({ item, newPath }))
if (moves.length > 0) {
actions.moveItems(moves, 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.closedMoveToModal()
},
},
})),
])