'use client'; import { useId, useRef, useState, type ReactNode } from 'react'; import { X, ArrowLeft, ArrowRight, Save, Send, Info, Check, CircleAlert, FilePenLine, Paperclip } from 'lucide-react'; import { Dialog, DialogContent, DialogTitle, DialogDescription } from '@/components/ui/dialog'; import { AlertDialog, AlertDialogContent, AlertDialogTitle, AlertDialogDescription, AlertDialogCancel, AlertDialogAction } from '@/components/ui/alert-dialog'; import { emptyFields, missingFields, type Fields, type Demand } from '@/lib/demand'; import type { Attachment } from '@/lib/attachments'; import { appPath } from '@/lib/base-path'; import { AttachmentPicker } from './attachments'; import { TemplateActions, type OpenTemplatePreview } from './template-actions'; import TemplatePreview from './template-preview'; import type { DocumentId } from '@/lib/documents'; import { MAX_ATTACHMENTS } from '@/lib/attachments'; export type SaveInput={action:string;id?:string;revision?:number;fields?:Fields;mode?:string;[key:string]:unknown}; export function DemandForm({initial,onSave,onClose,busy:saving,error,userName,existing}:{initial?:Demand;onSave:(body:SaveInput)=>Promise;onClose:()=>void;busy:boolean;error:string;userName:string;existing:Demand[]}){ const inputPrefix=useId();const backgroundUpload=useRef(null); // Guard the parent synchronously, before Radix registers the preview's layer. const [templateId,setTemplateId]=useState(null);const activePreview=useRef(null);const templateTrigger=useRef(null); const openPreview:OpenTemplatePreview=(id,trigger)=>{activePreview.current=id;templateTrigger.current=trigger;setTemplateId(id);}; function closePreview(){activePreview.current=null;setTemplateId(null);} const [attachments,setAttachments]=useState(()=>initial?.attachments??[]);const [uploading,setUploading]=useState(false);const newUploads=useRef(new Set());const busy=saving||uploading; function releaseUploads(ids:Iterable){for(const id of ids){void fetch(appPath('/api/attachments/'+id),{method:'DELETE',keepalive:true}).catch(()=>{});newUploads.current.delete(id);}} function changeAttachments(next:Attachment[]){const kept=new Set(next.map(file=>file.id));releaseUploads(attachments.filter(file=>newUploads.current.has(file.id)&&!kept.has(file.id)).map(file=>file.id));setAttachments(next);} function closeForm(){releaseUploads([...newUploads.current]);onClose();} const [startingFields]=useState(()=>({... (initial?.fields??{...emptyFields,proposer:userName,owner:userName})})); const [f,setF]=useState(startingFields);const [step,setStep]=useState(0);const [invalid,setInvalid]=useState([]);const [exit,setExit]=useState(false); const closeButton=useRef(null);const returnFocus=useRef(null); const changed=(Object.keys(startingFields) as (keyof Fields)[]).some(key=>f[key]!==startingFields[key])||attachments.map(file=>file.id).join(',')!==(initial?.attachments??[]).map(file=>file.id).join(','); function requestClose(){if(activePreview.current){closePreview();return;}if(busy)return;if(changed){returnFocus.current=document.activeElement instanceof HTMLElement?document.activeElement:closeButton.current;setExit(true)}else closeForm();} const set=(k:keyof Fields,v:string|boolean)=>setF(prev=>({...prev,[k]:v})); const duplicate=existing.find(d=>d.id!==initial?.id&&f.title.trim().length>3&&d.fields.title.includes(f.title.trim())); const field=(k:keyof Fields,label:string,placeholder:string,opts:{large?:boolean;full?:boolean;required?:boolean;type?:string;hint?:string;max?:number;actions?:ReactNode}={})=>{ const id=inputPrefix+'-'+k; return
{opts.actions}
{opts.large?