From c87ab6c6aa601e6d37f958409f6cfb892e835ea8 Mon Sep 17 00:00:00 2001 From: Gitead Date: Tue, 1 Dec 2026 10:26:24 -0500 Subject: [PATCH] feat: Take Screenshot in claim form uploads; Claim Again sends only the form's attachments - Shared screenshot crop dialog (useScreenshotAttach) used by appointment attachments and the claim form's upload zone (allowScreenshot). - Claim Again no longer merges appointment files into the submit or relinks them to the claim, so an attachment removed in the form stays removed. Co-Authored-By: Claude Opus 5.5 --- .../appointments/appointment-attachments.tsx | 151 +----------------- .../src/components/claims/claim-form.tsx | 14 +- .../file-upload/multiple-file-upload-zone.tsx | 66 ++++++-- .../file-upload/screenshot-crop-dialog.tsx | 151 ++++++++++++++++++ 4 files changed, 222 insertions(+), 160 deletions(-) create mode 100644 apps/Frontend/src/components/file-upload/screenshot-crop-dialog.tsx diff --git a/apps/Frontend/src/components/appointments/appointment-attachments.tsx b/apps/Frontend/src/components/appointments/appointment-attachments.tsx index 441da94c..86884ad3 100644 --- a/apps/Frontend/src/components/appointments/appointment-attachments.tsx +++ b/apps/Frontend/src/components/appointments/appointment-attachments.tsx @@ -1,21 +1,10 @@ import { useEffect, useRef, useState } from "react"; -import ReactCrop, { Crop, PixelCrop, centerCrop, makeAspectCrop } from "react-image-crop"; -import "react-image-crop/dist/ReactCrop.css"; import { Paperclip, Image as ImageIcon, Camera, Loader2, Check, RotateCcw, FileText, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; import { apiRequest } from "@/lib/queryClient"; import { toast } from "@/hooks/use-toast"; -import { useScreenCapture } from "@/hooks/use-screen-capture"; import { ServerAttachmentsPickerModal } from "@/components/file-upload/server-attachments-picker-modal"; -import { - Dialog, - DialogContent, - DialogHeader, - DialogTitle, - DialogFooter, -} from "@/components/ui/dialog"; +import { useScreenshotAttach } from "@/components/file-upload/screenshot-crop-dialog"; interface AppointmentAttachmentsProps { appointmentId: number; @@ -51,28 +40,6 @@ async function deleteAppointmentAttachment(appointmentId: number, fileId: number } } -function getCroppedDataUrl(image: HTMLImageElement, crop: PixelCrop): string { - const scaleX = image.naturalWidth / image.width; - const scaleY = image.naturalHeight / image.height; - - const canvas = document.createElement("canvas"); - canvas.width = crop.width * scaleX; - canvas.height = crop.height * scaleY; - const ctx = canvas.getContext("2d"); - ctx?.drawImage( - image, - crop.x * scaleX, - crop.y * scaleY, - crop.width * scaleX, - crop.height * scaleY, - 0, - 0, - canvas.width, - canvas.height - ); - return canvas.toDataURL("image/png"); -} - async function uploadAppointmentAttachment( appointmentId: number, patientId: number, @@ -93,15 +60,6 @@ export function AppointmentAttachments({ appointmentId, patientId }: Appointment const [items, setItems] = useState([]); const [pickerOpen, setPickerOpen] = useState(false); const fileInputRef = useRef(null); - const { capture, isCapturing, countdown } = useScreenCapture(); - - // Crop dialog state, shown after a screenshot is captured - const [screenshotDataUrl, setScreenshotDataUrl] = useState(null); - const [screenshotFileName, setScreenshotFileName] = useState(""); - const [crop, setCrop] = useState(); - const [completedCrop, setCompletedCrop] = useState(); - const imgRef = useRef(null); - useEffect(() => { let cancelled = false; if (appointmentId) { @@ -114,25 +72,6 @@ export function AppointmentAttachments({ appointmentId, patientId }: Appointment }; }, [appointmentId]); - const handleImageLoad = (e: React.SyntheticEvent) => { - imgRef.current = e.currentTarget; - const { width, height } = e.currentTarget; - const fullCrop = centerCrop( - makeAspectCrop({ unit: "%", width: 100 }, width / height, width, height), - width, - height - ); - setCrop(fullCrop); - setCompletedCrop({ unit: "px", x: 0, y: 0, width, height }); - }; - - const closeCropDialog = () => { - setScreenshotDataUrl(null); - setScreenshotFileName(""); - setCrop(undefined); - setCompletedCrop(undefined); - }; - const runUpload = async (file: File) => { if (!patientId) { toast({ title: "Missing patient", description: "Cannot attach files without a patient.", variant: "destructive" }); @@ -190,39 +129,12 @@ export function AppointmentAttachments({ appointmentId, patientId }: Appointment files.forEach((file) => void runUpload(file)); }; - const handleScreenshot = async () => { - try { - const dataUrl = await capture(); - setScreenshotDataUrl(dataUrl); - setScreenshotFileName(`screenshot_${Date.now()}`); - } catch (err: any) { - toast({ - title: "Screenshot failed", - description: err?.message ?? "Screen capture failed.", - variant: "destructive", - }); - } - }; - - const handleRetakeScreenshot = async () => { - closeCropDialog(); - await handleScreenshot(); - }; - - const handleSaveScreenshot = async () => { - if (!screenshotDataUrl) return; - const croppedDataUrl = - completedCrop && imgRef.current - ? getCroppedDataUrl(imgRef.current, completedCrop) - : screenshotDataUrl; - const response = await fetch(croppedDataUrl); - const blob = await response.blob(); - const safeName = - screenshotFileName.trim().replace(/[/\\?%*:|"<>]/g, "-") || `screenshot_${Date.now()}`; - const file = new File([blob], `${safeName}.png`, { type: "image/png" }); - closeCropDialog(); - void runUpload(file); - }; + const { + takeScreenshot: handleScreenshot, + isCapturing, + countdown, + dialog: screenshotDialog, + } = useScreenshotAttach((file) => void runUpload(file)); return (
@@ -319,54 +231,7 @@ export function AppointmentAttachments({ appointmentId, patientId }: Appointment onFilesSelected={handleServerFilesSelected} /> - !open && closeCropDialog()}> - - - Crop screenshot - - {screenshotDataUrl && ( -
- setCrop(percentCrop)} - onComplete={(pixelCrop) => setCompletedCrop(pixelCrop)} - > - Captured screenshot - -

- Drag the corners to crop, or leave as-is to attach the full screenshot. -

-
- -
- setScreenshotFileName(e.target.value)} - placeholder="screenshot" - /> - .png -
-
-
- )} - - - - -
-
+ {screenshotDialog}
); } diff --git a/apps/Frontend/src/components/claims/claim-form.tsx b/apps/Frontend/src/components/claims/claim-form.tsx index b1f78826..92167ac7 100755 --- a/apps/Frontend/src/components/claims/claim-form.tsx +++ b/apps/Frontend/src/components/claims/claim-form.tsx @@ -1064,11 +1064,15 @@ export function ClaimForm({ for (const f of carriedClaimFiles) { byFilename.set(f.filename, { filename: f.filename, mimeType: f.mimeType ?? "", ...(f.filePath ? { filePath: f.filePath } : {}) }); } - for (const f of existingAppointmentFiles) byFilename.set(f.filename, f); + // Claim Again: the form's attachment list is final, so an appointment file the user + // removed from it isn't added back + if (!claimAgainSource) { + for (const f of existingAppointmentFiles) byFilename.set(f.filename, f); + } for (const f of newFiles) byFilename.set(f.filename, f); return Array.from(byFilename.values()); }, - [existingAppointmentFiles, carriedClaimFiles], + [existingAppointmentFiles, carriedClaimFiles, claimAgainSource], ); // MH and CCA send file contents to the portal, so the backend reads these from disk by id const carriedClaimFileIds = carriedClaimFiles @@ -1175,7 +1179,7 @@ export function ClaimForm({ // submission already supplied its own files — otherwise files saved directly on the // appointment (e.g. a screenshot attached via the Schedule editor) would never reach // the insurance upload at all. - skipAppointmentAttachmentMerge: !!autoSubmit && !!uploadedFiles?.length, + skipAppointmentAttachmentMerge: (!!autoSubmit && !!uploadedFiles?.length) || !!claimAgainSource, }); // 5. Close form @@ -1312,7 +1316,7 @@ export function ClaimForm({ insuranceSiteKey: "CCA", claimId: createdClaim.id, carriedClaimFileIds, - skipAppointmentAttachmentMerge: !!autoSubmit && !!uploadedFiles?.length, + skipAppointmentAttachmentMerge: (!!autoSubmit && !!uploadedFiles?.length) || !!claimAgainSource, }); onClose(); @@ -2913,6 +2917,7 @@ export function ClaimForm({ acceptedFileTypes="application/pdf,image/jpeg,image/jpg,image/png,image/webp" maxFiles={10} patientId={patientId} + allowScreenshot /> {form.uploadedFiles.length > 0 && (
    @@ -3551,6 +3556,7 @@ export function ClaimForm({ acceptedFileTypes="application/pdf,image/jpeg,image/jpg,image/png,image/webp" maxFiles={10} patientId={patientId} + allowScreenshot /> {form.uploadedFiles.length > 0 && (
      diff --git a/apps/Frontend/src/components/file-upload/multiple-file-upload-zone.tsx b/apps/Frontend/src/components/file-upload/multiple-file-upload-zone.tsx index eb289a78..122ee351 100755 --- a/apps/Frontend/src/components/file-upload/multiple-file-upload-zone.tsx +++ b/apps/Frontend/src/components/file-upload/multiple-file-upload-zone.tsx @@ -5,11 +5,12 @@ import React, { forwardRef, useImperativeHandle, } from "react"; -import { Upload, X, FilePlus } from "lucide-react"; +import { Upload, X, FilePlus, Camera, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useToast } from "@/hooks/use-toast"; import { cn } from "@/lib/utils"; import { ServerAttachmentsPickerModal } from "@/components/file-upload/server-attachments-picker-modal"; +import { useScreenshotAttach } from "@/components/file-upload/screenshot-crop-dialog"; export type MultipleFileUploadZoneHandle = { getFiles: () => File[]; @@ -29,6 +30,8 @@ interface FileUploadZoneProps { // OPTIONAL: when set, shows a "Choose from Server" option listing this patient's // previously saved Cloud Storage attachments (e.g. Copy Agent screenshots) patientId?: number; + // OPTIONAL: shows a "Take Screenshot" button that captures, crops and adds a PNG + allowScreenshot?: boolean; } export const MultipleFileUploadZone = forwardRef< @@ -44,6 +47,7 @@ export const MultipleFileUploadZone = forwardRef< maxFileSizeMB = 10, // default fallback per-file size (MB) maxFileSizeByType, // optional per-type overrides, e.g. { "application/pdf": 10, "image/*": 2 } patientId, + allowScreenshot = false, }, ref ) => { @@ -307,6 +311,44 @@ export const MultipleFileUploadZone = forwardRef< notify(newFiles); }; + const screenshot = useScreenshotAttach((file) => handleFiles([file])); + + const screenshotButton = allowScreenshot ? ( + + ) : null; + + const serverButton = patientId ? ( + + ) : null; + // expose imperative handle to parent useImperativeHandle( ref, @@ -384,6 +426,12 @@ export const MultipleFileUploadZone = forwardRef< ))}
    + {(serverButton || screenshotButton) && ( +
    + {serverButton} + {screenshotButton} +
    + )} {/* prominent per-type size badges */}
    {parsedAccept.map((t) => { @@ -456,18 +504,8 @@ export const MultipleFileUploadZone = forwardRef< > Choose from This Computer - {patientId ? ( - - ) : null} + {serverButton} + {screenshotButton}
    )} @@ -481,6 +519,8 @@ export const MultipleFileUploadZone = forwardRef< onFilesSelected={(files) => handleFiles(files)} /> ) : null} + + {allowScreenshot ? screenshot.dialog : null} ); } diff --git a/apps/Frontend/src/components/file-upload/screenshot-crop-dialog.tsx b/apps/Frontend/src/components/file-upload/screenshot-crop-dialog.tsx new file mode 100644 index 00000000..8dd6c224 --- /dev/null +++ b/apps/Frontend/src/components/file-upload/screenshot-crop-dialog.tsx @@ -0,0 +1,151 @@ +import { useRef, useState } from "react"; +import ReactCrop, { Crop, PixelCrop, centerCrop, makeAspectCrop } from "react-image-crop"; +import "react-image-crop/dist/ReactCrop.css"; +import { Check, RotateCcw } from "lucide-react"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { toast } from "@/hooks/use-toast"; +import { useScreenCapture } from "@/hooks/use-screen-capture"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogFooter, +} from "@/components/ui/dialog"; + +function getCroppedDataUrl(image: HTMLImageElement, crop: PixelCrop): string { + const scaleX = image.naturalWidth / image.width; + const scaleY = image.naturalHeight / image.height; + + const canvas = document.createElement("canvas"); + canvas.width = crop.width * scaleX; + canvas.height = crop.height * scaleY; + const ctx = canvas.getContext("2d"); + ctx?.drawImage( + image, + crop.x * scaleX, + crop.y * scaleY, + crop.width * scaleX, + crop.height * scaleY, + 0, + 0, + canvas.width, + canvas.height + ); + return canvas.toDataURL("image/png"); +} + +/** + * Take a screenshot, let the user crop and name it, then hand back a PNG File. + * Returns `takeScreenshot` to start a capture, the capture state for the button, + * and `dialog` to render once somewhere in the caller. + */ +export function useScreenshotAttach(onAttach: (file: File) => void) { + const { capture, isCapturing, countdown } = useScreenCapture(); + const [dataUrl, setDataUrl] = useState(null); + const [fileName, setFileName] = useState(""); + const [crop, setCrop] = useState(); + const [completedCrop, setCompletedCrop] = useState(); + const imgRef = useRef(null); + + const close = () => { + setDataUrl(null); + setFileName(""); + setCrop(undefined); + setCompletedCrop(undefined); + }; + + const takeScreenshot = async () => { + try { + const url = await capture(); + setDataUrl(url); + setFileName(`screenshot_${Date.now()}`); + } catch (err: any) { + toast({ + title: "Screenshot failed", + description: err?.message ?? "Screen capture failed.", + variant: "destructive", + }); + } + }; + + const handleImageLoad = (e: React.SyntheticEvent) => { + imgRef.current = e.currentTarget; + const { width, height } = e.currentTarget; + setCrop( + centerCrop(makeAspectCrop({ unit: "%", width: 100 }, width / height, width, height), width, height) + ); + setCompletedCrop({ unit: "px", x: 0, y: 0, width, height }); + }; + + const handleRetake = async () => { + close(); + await takeScreenshot(); + }; + + const handleAttach = async () => { + if (!dataUrl) return; + const croppedDataUrl = + completedCrop && imgRef.current ? getCroppedDataUrl(imgRef.current, completedCrop) : dataUrl; + const blob = await (await fetch(croppedDataUrl)).blob(); + const safeName = fileName.trim().replace(/[/\\?%*:|"<>]/g, "-") || `screenshot_${Date.now()}`; + const file = new File([blob], `${safeName}.png`, { type: "image/png" }); + close(); + onAttach(file); + }; + + const dialog = ( + !open && close()}> + e.stopPropagation()}> + + Crop screenshot + + {dataUrl && ( +
    + setCrop(percentCrop)} + onComplete={(pixelCrop) => setCompletedCrop(pixelCrop)} + > + Captured screenshot + +

    + Drag the corners to crop, or leave as-is to attach the full screenshot. +

    +
    + +
    + setFileName(e.target.value)} + placeholder="screenshot" + /> + .png +
    +
    +
    + )} + + + + +
    +
    + ); + + return { takeScreenshot, isCapturing, countdown, dialog }; +}