import {DocumentId, getPublishedId} from '@sanity/id-utils' import {Box, Button, Card, Dialog, Stack, Text} from '@sanity/ui' import {useCallback, useContext, useMemo, useState} from 'react' import {SchemeContext, TreeContext} from '../../context' import {canMoveConcept, findConcept} from '../../core/tree/moveConcept' import {useMoveConcept} from '../../hooks' import type {ChildConceptTerm, ConceptSchemeDocument} from '../../types' import {InputHierarchy} from '../inputs/InputHierarchy' /** * #### Move Concept Dialog * Picks a new broader term for a concept, from the same tree browser the * reference inputs use. With no `parentId` it is an orphan being brought into * the hierarchy, so there is no broader term to drop and that option is absent. A destination the hierarchy cannot take — the concept * itself, one of its own narrower terms, a term it already has, or one deep * enough to push the subtree out of view — is explained in place rather than * applied, and the dialog stays open so another can be chosen. * * The tree it validates against is the one on screen, read from `TreeContext`, * so a move is judged against what the editor is actually looking at, including * any moves still settling. */ export function MoveConceptDialog({ concept, parentId, onClose, }: { concept: ChildConceptTerm /** The broader term being left, or `null` for an orphan being given one. */ parentId: string | null onClose: () => void }) { const scheme: ConceptSchemeDocument = useContext(SchemeContext) || ({} as ConceptSchemeDocument) const {concepts} = useContext(TreeContext) const moveConcept = useMoveConcept(scheme) const [refusal, setRefusal] = useState(null) const tree = concepts ?? null const label = concept.prefLabel || '[new concept]' const parent = useMemo( () => (parentId === null ? undefined : findConcept(tree, parentId)), [tree, parentId], ) const parentLabel = parent?.prefLabel || 'its broader term' const from = useMemo( () => (parentId === null ? null : {id: parentId, prefLabel: parent?.prefLabel}), [parentId, parent?.prefLabel], ) const handleSelect = useCallback( (selected: {_ref: string; _type: 'reference'; _originalId?: string}) => { const toParentId = getPublishedId(DocumentId(selected._ref)) const check = canMoveConcept({ data: tree, conceptId: concept.id, fromParentId: parentId, toParentId, }) if (!check.allowed) { setRefusal(check.reason) return } moveConcept(concept, from, { id: toParentId, _originalId: selected._originalId, prefLabel: findConcept(tree, toParentId)?.prefLabel, }) onClose() }, [tree, concept, parentId, from, moveConcept, onClose], ) const handleDetach = useCallback(() => { moveConcept(concept, from, null) onClose() }, [concept, from, moveConcept, onClose]) return ( {`Choose the concept that should become the broader term for “${label}”:`} {refusal && ( {refusal} )} {/* The browser itself, not a nested TreeView: the dialog already sits inside the scheme and perspective contexts a TreeView would set up, so going straight to InputHierarchy avoids re-reading the Studio perspective and re-providing what is already here. */} {/* Nothing to drop when the concept has no broader term to begin with. */} {parentId !== null && ( {`Or remove “${label}” from this point in the hierarchy. It will either become a top level term, or remain a child of any other broader terms assigned to it.`} ) }