upload-modal.tsx325 lines, 9.9 KB
| 1 | import { |
| 2 | Dialog, |
| 3 | DialogContent, |
| 4 | DialogDescription, |
| 5 | DialogHeader, |
| 6 | DialogTitle, |
| 7 | DialogTrigger, |
| 8 | } from "@/components/ui/dialog"; |
| 9 | import { api } from "@/lib/api"; |
| 10 | import { useContractStore } from "@/store/zustand"; |
| 11 | import { useMutation } from "@tanstack/react-query"; |
| 12 | import { useCallback, useState } from "react"; |
| 13 | import { useDropzone } from "react-dropzone"; |
| 14 | import { AnimatePresence, motion } from "framer-motion"; |
| 15 | import { Brain, FileText, Loader2, Sparkles, Trash } from "lucide-react"; |
| 16 | import { cn } from "@/lib/utils"; |
| 17 | import { Button } from "../ui/button"; |
| 18 | import { Alert, AlertDescription, AlertTitle } from "../ui/alert"; |
| 19 | import { useRouter } from "next/navigation"; |
| 20 | |
| 21 | interface IUploadModalProps { |
| 22 | isOpen: boolean; |
| 23 | onClose: () => void; |
| 24 | onUploadComplete: () => void; |
| 25 | } |
| 26 | |
| 27 | export function UploadModal({ |
| 28 | isOpen, |
| 29 | onClose, |
| 30 | onUploadComplete, |
| 31 | }: IUploadModalProps) { |
| 32 | const { setAnalysisResults } = useContractStore(); |
| 33 | const router = useRouter(); |
| 34 | |
| 35 | const [detectedType, setDetectedType] = useState<string | null>(null); |
| 36 | const [error, setError] = useState<string | null>(null); |
| 37 | const [files, setFiles] = useState<File[]>([]); |
| 38 | const [step, setStep] = useState< |
| 39 | "upload" | "detecting" | "confirm" | "processing" | "done" |
| 40 | >("upload"); |
| 41 | |
| 42 | const { mutate: detectContractType } = useMutation({ |
| 43 | mutationFn: async (file: File) => { |
| 44 | const formData = new FormData(); |
| 45 | formData.append("contract", file); |
| 46 | |
| 47 | const response = await api.post<{ detectedType: string }>( |
| 48 | `/contracts/detect-type`, |
| 49 | formData, |
| 50 | { |
| 51 | headers: { |
| 52 | "Content-Type": "multipart/form-data", |
| 53 | }, |
| 54 | } |
| 55 | ); |
| 56 | |
| 57 | return response.data.detectedType; |
| 58 | }, |
| 59 | |
| 60 | onSuccess: (data: string) => { |
| 61 | setDetectedType(data); |
| 62 | setStep("confirm"); |
| 63 | }, |
| 64 | onError: (error) => { |
| 65 | console.error(error); |
| 66 | setError("Failed to detect contract type"); |
| 67 | setStep("upload"); |
| 68 | }, |
| 69 | }); |
| 70 | |
| 71 | const { mutate: uploadFile, isPending: isProcessing } = useMutation({ |
| 72 | mutationFn: async ({ |
| 73 | file, |
| 74 | contractType, |
| 75 | }: { |
| 76 | file: File; |
| 77 | contractType: string; |
| 78 | }) => { |
| 79 | const formData = new FormData(); |
| 80 | formData.append("contract", file); |
| 81 | formData.append("contractType", contractType); |
| 82 | |
| 83 | const response = await api.post(`/contracts/analyze`, formData, { |
| 84 | headers: { |
| 85 | "Content-Type": "multipart/form-data", |
| 86 | }, |
| 87 | }); |
| 88 | |
| 89 | console.log(response.data); |
| 90 | return response.data; |
| 91 | }, |
| 92 | onSuccess: (data) => { |
| 93 | setAnalysisResults(data); |
| 94 | setStep("done"); |
| 95 | onUploadComplete(); |
| 96 | }, |
| 97 | onError: (error) => { |
| 98 | console.error(error); |
| 99 | setError("Failed to upload contract"); |
| 100 | setStep("upload"); |
| 101 | }, |
| 102 | }); |
| 103 | |
| 104 | const onDrop = useCallback((acceptedFiles: File[]) => { |
| 105 | if (acceptedFiles.length > 0) { |
| 106 | setFiles(acceptedFiles); |
| 107 | setError(null); |
| 108 | setStep("upload"); |
| 109 | } else { |
| 110 | setError("No file selected"); |
| 111 | } |
| 112 | }, []); |
| 113 | |
| 114 | const { getRootProps, getInputProps, isDragActive } = useDropzone({ |
| 115 | onDrop, |
| 116 | accept: { |
| 117 | "application/pdf": [".pdf"], |
| 118 | }, |
| 119 | maxFiles: 1, |
| 120 | multiple: false, |
| 121 | }); |
| 122 | |
| 123 | const handleFileUpload = () => { |
| 124 | if (files.length > 0) { |
| 125 | setStep("detecting"); |
| 126 | detectContractType(files[0]); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | const handleAnalyzeContract = () => { |
| 131 | if (files.length > 0 && detectedType) { |
| 132 | setStep("processing"); |
| 133 | uploadFile({ file: files[0], contractType: detectedType }); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | const handleClose = () => { |
| 138 | onClose(); |
| 139 | setFiles([]); |
| 140 | setDetectedType(null); |
| 141 | setError(null); |
| 142 | setStep("upload"); |
| 143 | }; |
| 144 | |
| 145 | const renderContent = () => { |
| 146 | switch (step) { |
| 147 | case "upload": { |
| 148 | return ( |
| 149 | <AnimatePresence> |
| 150 | <motion.div> |
| 151 | <div |
| 152 | {...getRootProps()} |
| 153 | className={cn( |
| 154 | "border-2 border-dashed rounded-lg p-8 mt-8 mb-4 text-center transition-colors", |
| 155 | isDragActive |
| 156 | ? "border-primary bg-primary/10" |
| 157 | : "border-gray-300 hover:border-gray-400" |
| 158 | )} |
| 159 | > |
| 160 | <input {...getInputProps()} /> |
| 161 | <motion.div> |
| 162 | <FileText className="mx-auto size-16 text-primary" /> |
| 163 | </motion.div> |
| 164 | <p className="mt-4 text-sm text-gray-600"> |
| 165 | Drag 'n' drop some files here, or click to select |
| 166 | files |
| 167 | </p> |
| 168 | <p className="bg-yellow-500/30 border border-yellow-500 border-dashed text-yellow-700 p-2 rounded mt-2"> |
| 169 | Note: Only PDF files are accepted |
| 170 | </p> |
| 171 | </div> |
| 172 | {files.length > 0 && ( |
| 173 | <div className="mt-4 bg-green-500/30 border border-green-500 border-dashed text-green-700 p-2 rounded flex items-center justify-between"> |
| 174 | <span> |
| 175 | {files[0].name}{" "} |
| 176 | <span className="text-sm text-gray-600"> |
| 177 | ({files[0].size} bytes) |
| 178 | </span> |
| 179 | </span> |
| 180 | <Button |
| 181 | variant={"ghost"} |
| 182 | size={"sm"} |
| 183 | className="hover:bg-green-500" |
| 184 | onClick={() => setFiles([])} |
| 185 | > |
| 186 | <Trash className="size-5 hover:text-green-900" /> |
| 187 | </Button> |
| 188 | </div> |
| 189 | )} |
| 190 | {files.length > 0 && !isProcessing && ( |
| 191 | <Button className="mt-4 w-full mb-4" onClick={handleFileUpload}> |
| 192 | <Sparkles className="mr-2 size-4" /> |
| 193 | Analyze Contract With AI |
| 194 | </Button> |
| 195 | )} |
| 196 | </motion.div> |
| 197 | </AnimatePresence> |
| 198 | ); |
| 199 | } |
| 200 | case "detecting": { |
| 201 | return ( |
| 202 | <AnimatePresence> |
| 203 | <motion.div className="flex flex-col items-center justify-center py-8"> |
| 204 | <Loader2 className="size-16 animate-spin text-primary" /> |
| 205 | <p className="mt-4 text-lg font-semibold"> |
| 206 | Detecting contract type... |
| 207 | </p> |
| 208 | </motion.div> |
| 209 | </AnimatePresence> |
| 210 | ); |
| 211 | } |
| 212 | case "confirm": { |
| 213 | return ( |
| 214 | <AnimatePresence> |
| 215 | <motion.div> |
| 216 | <div className="flex flex-col space-y-4 mb-4"> |
| 217 | <p> |
| 218 | We have detected the following contract type: |
| 219 | <span className="font-semibold"> {detectedType}</span> |
| 220 | </p> |
| 221 | <p>Would you like to analyze this contract with our AI?</p> |
| 222 | </div> |
| 223 | <div className="flex space-x-4"> |
| 224 | <Button onClick={handleAnalyzeContract}> |
| 225 | Yes, I want to analyze it |
| 226 | </Button> |
| 227 | <Button |
| 228 | onClick={() => setStep("upload")} |
| 229 | variant={"outline"} |
| 230 | className="flex-1" |
| 231 | > |
| 232 | No, Try another file |
| 233 | </Button> |
| 234 | </div> |
| 235 | </motion.div> |
| 236 | </AnimatePresence> |
| 237 | ); |
| 238 | } |
| 239 | case "processing": { |
| 240 | return ( |
| 241 | <AnimatePresence> |
| 242 | <motion.div |
| 243 | initial={{ opacity: 0, scale: 0.9 }} |
| 244 | animate={{ opacity: 1, scale: 1 }} |
| 245 | exit={{ opacity: 0, scale: 0.9 }} |
| 246 | className="flex flex-col items-center justify-center py-8" |
| 247 | > |
| 248 | <motion.div |
| 249 | animate={{ |
| 250 | scale: [1, 1.2, 1], |
| 251 | rotate: [0, 360], |
| 252 | }} |
| 253 | transition={{ |
| 254 | duration: 3, |
| 255 | repeat: Infinity, |
| 256 | ease: "easeInOut", |
| 257 | }} |
| 258 | > |
| 259 | <Brain className="size-20 text-primary" /> |
| 260 | </motion.div> |
| 261 | <motion.p |
| 262 | initial={{ opacity: 0, y: 20 }} |
| 263 | animate={{ opacity: 1, y: 0 }} |
| 264 | transition={{ delay: 0.5 }} |
| 265 | className="mt-6 text-lg font-semibold text-gray-700" |
| 266 | > |
| 267 | AI is analyzing your contract... |
| 268 | </motion.p> |
| 269 | <motion.p |
| 270 | initial={{ opacity: 0 }} |
| 271 | animate={{ opacity: 1 }} |
| 272 | transition={{ delay: 1 }} |
| 273 | className="mt-2 text-sm text-gray-700" |
| 274 | > |
| 275 | This may take some time. |
| 276 | </motion.p> |
| 277 | <motion.div |
| 278 | className="w-64 h-2 bg-gray-200 rounded-full mt-6 overflow-hidden" |
| 279 | initial={{ width: 0 }} |
| 280 | animate={{ width: "100%" }} |
| 281 | transition={{ duration: 10, ease: "linear" }} |
| 282 | > |
| 283 | <motion.div |
| 284 | className="h-full bg-primary" |
| 285 | initial={{ width: "0%" }} |
| 286 | animate={{ width: "100%" }} |
| 287 | transition={{ duration: 10, ease: "linear" }} |
| 288 | /> |
| 289 | </motion.div> |
| 290 | </motion.div> |
| 291 | </AnimatePresence> |
| 292 | ); |
| 293 | } |
| 294 | case "done": { |
| 295 | return ( |
| 296 | <AnimatePresence> |
| 297 | <motion.div> |
| 298 | <Alert className="mt-4"> |
| 299 | <AlertTitle>Analysis completed</AlertTitle> |
| 300 | <AlertDescription> |
| 301 | Your contract has been analyzed. you can now view the results |
| 302 | </AlertDescription> |
| 303 | </Alert> |
| 304 | |
| 305 | <motion.div className="mt-6 flex flex-col space-y-3 relative"> |
| 306 | <Button onClick={() => router.push(`/dashboard/results`)}> |
| 307 | View results |
| 308 | </Button> |
| 309 | <Button variant={"outline"} onClick={handleClose}> |
| 310 | Close |
| 311 | </Button> |
| 312 | </motion.div> |
| 313 | </motion.div> |
| 314 | </AnimatePresence> |
| 315 | ); |
| 316 | } |
| 317 | } |
| 318 | }; |
| 319 | |
| 320 | return ( |
| 321 | <Dialog open={isOpen} onOpenChange={handleClose}> |
| 322 | <DialogContent>{renderContent()}</DialogContent> |
| 323 | </Dialog> |
| 324 | ); |
| 325 | } |