import React from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import { TextareaControl } from "../ui/textarea-control"; import { Divider } from '../ui/divider'; import { DismissibleAlert } from '../ui/dismissible-alert'; import { PlusCircle, Trash2 } from 'lucide-react'; import { Heading } from '../ui/heading'; import { Flex, FlexItem, FlexBlock } from '../ui/flex'; import { Spacer } from '../ui/spacer'; import { Annotation } from 'fhir/r4'; import { formatFhirHumanName } from '../../utils/fhirHumanName.tsx'; import { useAuthServiceContext, useFhirServiceContext } from '../../services'; interface FhirAnnotationProps { label: string; context: string; disabled?: boolean; readonly?: boolean; } const FhirAnnotation = ({ label, context, disabled, readonly = false }: FhirAnnotationProps) => { const { control, watch, setValue, getValues } = useFormContext(); const { currentUser } = useAuthServiceContext(); const { createReferenceObject } = useFhirServiceContext(); const spacerMargin = '24px'; const notes = watch(context); const addNote = () => { const notes = getValues(context) ?? []; setValue(context, [...notes, { text: '', time: new Date().toISOString(), authorReference: createReferenceObject(currentUser?.fhir?.resourceType as string, currentUser?.fhir?.id as string, formatFhirHumanName(currentUser?.fhir?.name)) }], { shouldDirty: true }); }; const canAddNote = () => { return true; }; const removeNote = (index: number) => { const notes = getValues(context) ?? []; notes.splice(index, 1); setValue(context, notes, { shouldDirty: true }); }; return ( <> {label} {!readonly && (notes ?? []).length === 0 && ( )} {(notes ?? []).length === 0 && ( <> No note added yet. )} {(notes ?? []).map((note: Annotation, index: number) => ( ( { if (!readonly && !disabled) field.onChange(value); }} readOnly={readonly} disabled={disabled} placeholder={label} rows={6}/> )}/> {/* Hidden field for time */} ( )}/> {/* Hidden field for author */} ( { try { const value = e.target.value ? JSON.parse(e.target.value) : null; field.onChange(value); } catch (err) { console.error('Invalid JSON in author field', err); field.onChange(null); } }} /> )}/> {!readonly && ( )} ))} {!readonly && (notes ?? []).length !== 0 && ( <> )} ); }; export default FhirAnnotation;