1+ "use client"
2+
3+ import { useState , useEffect } from 'react'
4+ import { useAnnotation } from '@/components/pdf-container/plugin-annotation-2'
5+ import { useEntityState } from '@/hooks/use-entity-state'
6+ import { EntityAnnotation , EntityType } from '@/lib/entity-types'
7+ import { Dialog , DialogContent , DialogHeader , DialogTitle } from '@/components/shadcn-ui/dialog'
8+ import { Button } from '@/components/shadcn-ui/button'
9+ import { Badge } from '@/components/shadcn-ui/badge'
10+
11+ interface EntitySelectionDialogProps {
12+ isOpen : boolean
13+ onClose : ( ) => void
14+ annotationId : string
15+ annotationText : string
16+ }
17+
18+ export function EntitySelectionDialog ( {
19+ isOpen,
20+ onClose,
21+ annotationId,
22+ annotationText
23+ } : EntitySelectionDialogProps ) {
24+ const { entityState, linkAnnotationToEntity, unlinkAnnotation } = useEntityState ( )
25+
26+ const currentLink = entityState . entityAnnotations . find (
27+ ( ea : EntityAnnotation ) => ea . annotationId === annotationId
28+ )
29+
30+ const handleEntitySelect = ( entityTypeId : string ) => {
31+ linkAnnotationToEntity ( entityTypeId , annotationId )
32+ onClose ( )
33+ }
34+
35+ const handleUnlink = ( ) => {
36+ if ( currentLink ) {
37+ unlinkAnnotation ( annotationId )
38+ }
39+ onClose ( )
40+ }
41+
42+ return (
43+ < Dialog open = { isOpen } onOpenChange = { onClose } >
44+ < DialogContent className = "max-w-md" >
45+ < DialogHeader >
46+ < DialogTitle > Assign Entity Type</ DialogTitle >
47+ </ DialogHeader >
48+
49+ < div className = "space-y-4" >
50+ < div >
51+ < p className = "text-sm font-medium text-gray-700" > Selected Text:</ p >
52+ < p className = "text-sm bg-gray-100 p-2 rounded mt-1" > { annotationText } </ p >
53+ </ div >
54+
55+ { currentLink && (
56+ < div >
57+ < p className = "text-sm font-medium text-gray-700" > Currently assigned to:</ p >
58+ < Badge className = "mt-1" >
59+ { entityState . entityTypes . find ( ( et : EntityType ) => et . id === currentLink . entityTypeId ) ?. name }
60+ </ Badge >
61+ </ div >
62+ ) }
63+
64+ < div >
65+ < p className = "text-sm font-medium text-gray-700 mb-2" > Assign to entity type:</ p >
66+ < div className = "space-y-2" >
67+ { entityState . entityTypes . map ( ( entityType : EntityType ) => (
68+ < Button
69+ key = { entityType . id }
70+ variant = { currentLink ?. entityTypeId === entityType . id ? "default" : "outline" }
71+ className = "w-full justify-start"
72+ onClick = { ( ) => handleEntitySelect ( entityType . id ) }
73+ >
74+ < div className = "flex items-center gap-2" >
75+ < div
76+ className = "w-4 h-4 rounded"
77+ style = { { backgroundColor : entityType . color } }
78+ />
79+ < span > { entityType . name } </ span >
80+ { entityType . required && (
81+ < Badge variant = "destructive" className = "text-xs ml-auto" > Required</ Badge >
82+ ) }
83+ </ div >
84+ </ Button >
85+ ) ) }
86+ </ div >
87+ </ div >
88+
89+ { currentLink && (
90+ < div className = "pt-2 border-t" >
91+ < Button
92+ variant = "destructive"
93+ size = "sm"
94+ onClick = { handleUnlink }
95+ className = "w-full"
96+ >
97+ Remove Entity Assignment
98+ </ Button >
99+ </ div >
100+ ) }
101+ </ div >
102+ </ DialogContent >
103+ </ Dialog >
104+ )
105+ }
106+
107+ interface EntitySelectionHandlerProps {
108+ children : ( props : {
109+ openEntitySelection : ( annotationId : string , annotationText : string ) => void
110+ } ) => React . ReactNode
111+ }
112+
113+ export function EntitySelectionHandler ( { children } : EntitySelectionHandlerProps ) {
114+ const [ dialogState , setDialogState ] = useState < {
115+ isOpen : boolean
116+ annotationId : string
117+ annotationText : string
118+ } > ( {
119+ isOpen : false ,
120+ annotationId : '' ,
121+ annotationText : '' ,
122+ } )
123+
124+ const openEntitySelection = ( annotationId : string , annotationText : string ) => {
125+ setDialogState ( {
126+ isOpen : true ,
127+ annotationId,
128+ annotationText,
129+ } )
130+ }
131+
132+ const closeDialog = ( ) => {
133+ setDialogState ( {
134+ isOpen : false ,
135+ annotationId : '' ,
136+ annotationText : '' ,
137+ } )
138+ }
139+
140+ return (
141+ < >
142+ { children ( { openEntitySelection } ) }
143+ < EntitySelectionDialog
144+ isOpen = { dialogState . isOpen }
145+ onClose = { closeDialog }
146+ annotationId = { dialogState . annotationId }
147+ annotationText = { dialogState . annotationText }
148+ />
149+ </ >
150+ )
151+ }
0 commit comments