'use client'; import { useCallback, useEffect, useState } from 'react'; import { Trash } from 'lucide-react'; import { createPortal } from 'react-dom'; import { Rnd } from 'react-rnd'; import { PDF_VIEWER_PAGE_SELECTOR } from '@documenso/lib/constants/pdf-viewer'; import { cn } from '../../lib/utils'; import { Card, CardContent } from '../card'; import type { TDocumentFlowFormSchema } from './types'; import { FRIENDLY_FIELD_TYPE } from './types'; type Field = TDocumentFlowFormSchema['fields'][0]; export type FieldItemProps = { field: Field; passive?: boolean; disabled?: boolean; minHeight?: number; minWidth?: number; onResize?: (_node: HTMLElement) => void; onMove?: (_node: HTMLElement) => void; onRemove?: () => void; }; export const FieldItem = ({ field, passive, disabled, minHeight: _minHeight, minWidth: _minWidth, onResize, onMove, onRemove, }: FieldItemProps) => { const [active, setActive] = useState(false); const [coords, setCoords] = useState({ pageX: 0, pageY: 0, pageHeight: 0, pageWidth: 0, }); const calculateCoords = useCallback(() => { const $page = document.querySelector( `${PDF_VIEWER_PAGE_SELECTOR}[data-page-number="${field.pageNumber}"]`, ); if (!$page) { return; } const { height, width } = $page.getBoundingClientRect(); const top = $page.getBoundingClientRect().top + window.scrollY; const left = $page.getBoundingClientRect().left + window.scrollX; // X and Y are percentages of the page's height and width const pageX = (field.pageX / 100) * width + left; const pageY = (field.pageY / 100) * height + top; const pageHeight = (field.pageHeight / 100) * height; const pageWidth = (field.pageWidth / 100) * width; setCoords({ pageX: pageX, pageY: pageY, pageHeight: pageHeight, pageWidth: pageWidth, }); }, [field.pageHeight, field.pageNumber, field.pageWidth, field.pageX, field.pageY]); useEffect(() => { calculateCoords(); }, [calculateCoords]); useEffect(() => { const onResize = () => { calculateCoords(); }; window.addEventListener('resize', onResize); return () => { window.removeEventListener('resize', onResize); }; }, [calculateCoords]); return createPortal( setActive(true)} onResizeStart={() => setActive(true)} onResizeStop={(_e, _d, ref) => { setActive(false); onResize?.(ref); }} onDragStop={(_e, d) => { setActive(false); onMove?.(d.node); }} > {!disabled && ( )} {FRIENDLY_FIELD_TYPE[field.type]}

{field.signerEmail}

, document.body, ); };