import { useConstantLengthInvariant, useConstantValueInvariant } from '@contember/react-utils' import { useCallback } from 'react' import { useEntityKey, useGetEntityByKey } from './index' import type { FieldAccessor } from '@contember/binding' import { useOnConnectionUpdate } from '../entityEvents' import type { FieldValue, SugaredRelativeSingleField } from '@contember/binding' import { useAccessorUpdateSubscription } from './useAccessorUpdateSubscription' import { useDesugaredRelativeSingleField } from './useDesugaredRelativeSingleField' function useField( sugaredRelativeSingleField: string | SugaredRelativeSingleField, ): FieldAccessor function useField( sugaredRelativeSingleField: string | SugaredRelativeSingleField | undefined, ): FieldAccessor | undefined function useField( sugaredRelativeSingleField: string | SugaredRelativeSingleField | undefined, ): FieldAccessor | undefined { const relativeSingleField = useDesugaredRelativeSingleField(sugaredRelativeSingleField) useConstantValueInvariant( !!relativeSingleField, 'useField: cannot alternate between providing and omitting the argument.', ) const entityKey = useEntityKey() const getEntityByKey = useGetEntityByKey() const getField = useCallback(() => { const parent = getEntityByKey(entityKey) return parent.getField(relativeSingleField!) }, [entityKey, getEntityByKey, relativeSingleField]) if (relativeSingleField) { // eslint-disable-next-line react-hooks/rules-of-hooks useConstantLengthInvariant( relativeSingleField.hasOneRelationPath, 'Cannot change the length of the hasOneRelation path!', ) // eslint-disable-next-line react-hooks/rules-of-hooks const [field, { update }] = useAccessorUpdateSubscription(getField) if (relativeSingleField.hasOneRelationPath.length) { // eslint-disable-next-line react-hooks/rules-of-hooks useOnConnectionUpdate(relativeSingleField.hasOneRelationPath[0].field, update) } return field } return undefined } export { useField }