import { useConstantValueInvariant } from '@contember/react-utils' import { ReactElement, ReactNode } from 'react' import { useEntitySubTree } from '../accessorPropagation' import type { SugaredQualifiedSingleEntity, SugaredUnconstrainedQualifiedSingleEntity, TreeRootId } from '@contember/binding' import { Environment, MarkerFactory, PRIMARY_KEY_NAME, TreeNodeEnvironmentFactory } from '@contember/binding' import { Component } from './Component' import { Entity } from './Entity' import { Field } from './Field' import { SetOrderFieldOnCreate, SetOrderFieldOnCreateOwnProps } from '../accessorSorting' export interface EntitySubTreeAdditionalProps { variables?: Environment.ValuesMapWithFactory } export type EntitySubTreeAdditionalCreationProps = {} | SetOrderFieldOnCreateOwnProps export type EntitySubTreeProps = & { treeRootId?: TreeRootId children?: ReactNode } & EntitySubTreeAdditionalProps & ( | SugaredQualifiedSingleEntity | (SugaredUnconstrainedQualifiedSingleEntity & EntitySubTreeAdditionalCreationProps) ) /** * Creates a single entity subtree in current data binding context. * * @example * ``` * * ``` * * @group Data binding */ export const EntitySubTree = Component( (props: EntitySubTreeProps) => { useConstantValueInvariant(props.isCreating, 'EntitySubTree: cannot update isCreating') const accessor = useEntitySubTree(props, ...props.treeRootId ? [props.treeRootId] : []) // TODO revive top-level hasOneRelationPath // {parameters.value.hasOneRelationPath.length ? ( // {props.children} // ) : ( // props.children // )} return }, { generateBranchMarker: (props, fields, environment) => { if ('isCreating' in props && props.isCreating) { return MarkerFactory.createUnconstrainedEntitySubTreeMarker(props, fields, environment) } return MarkerFactory.createEntitySubTreeMarker(props, fields, environment) }, staticRender: props => ( <> {props.children} {props.isCreating && 'orderField' in props && ( )} ), generateEnvironment: (props, oldEnvironment) => { const environment = oldEnvironment.withVariables(props.variables) return TreeNodeEnvironmentFactory.createEnvironmentForEntitySubtree(environment, props) }, }, 'EntitySubTree', ) as (pros: EntitySubTreeProps) => ReactElement