import {Flex, Spinner, Stack, Box, Card, Inline, Text, Button} from '@sanity/ui' import {useCallback} from 'react' import {Preview, useSchema, usePerspective, getPublishedId, stringToPath} from 'sanity' import type {UserViewComponent} from 'sanity/structure' import {usePaneRouter} from 'sanity/structure' import {Feedback, useListeningQuery} from 'sanity-plugin-utils' interface TagReference { _id: string _type: string title: string } export const ConceptUseView: UserViewComponent> = ({ document, }: { document: {displayed: {_id?: string}} }) => { const {selectedPerspectiveName} = usePerspective() const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter() const schema = useSchema() const refId = getPublishedId((document.displayed as {_id?: string})?._id ?? '') const handleClick = useCallback( (id: string, type: string) => { const childParams = routerPanesState[groupIndex + 1]?.[0].params || {} const {parentRefPath} = childParams handleEditReference({ id, type, parentRefPath: parentRefPath ? stringToPath(parentRefPath) : [``], template: {id}, }) }, [routerPanesState, groupIndex, handleEditReference], ) const {data, loading, error} = useListeningQuery( `*[!(_type in ["skosConcept","skosConceptScheme"]) && references($refId)]{_id,_type,title}`, { params: {refId}, options: { perspective: selectedPerspectiveName ? [selectedPerspectiveName] : 'drafts', }, }, ) if (loading) { return ( Loading usage… ) } if (error) { return ( There was en error fetching use data ) } if (!data?.length) { return ( This concept is not currently in use ) } return ( Resource count (all schemes): {data.length} {data?.map((d: TagReference) => { const schemaType = schema.get(d._type) return ( schemaType && ( ) ) })} ) }