|
| 1 | +"use client" |
| 2 | +import { useState } from "react" |
| 3 | +import { Button } from "@/components/ui/button" |
| 4 | +import { Type, ImageIcon, MousePointer, Check, PenTool, FileText, Loader2 } from "lucide-react" |
| 5 | +import { EmbedPDF, useEmbed } from "@simplepdf/react-embed-pdf" |
| 6 | +import { Switch } from "@/components/ui/switch" |
| 7 | + |
| 8 | +type ToolType = 'TEXT' | 'BOXED_TEXT' | 'CHECKBOX' | 'PICTURE' | 'SIGNATURE' | null; |
| 9 | + |
| 10 | +export default function PDFEditorUI() { |
| 11 | + const [selectedTool, setSelectedTool] = useState<ToolType>(null) |
| 12 | + const [allowDownload, setAllowDownload] = useState<boolean>(false) |
| 13 | + const [isSubmitting, setIsSubmitting] = useState<boolean>(false) |
| 14 | + const [submitError, setSubmitError] = useState<string | null>(null) |
| 15 | + const [toolError, setToolError] = useState<string | null>(null) |
| 16 | + const { embedRef, actions } = useEmbed() |
| 17 | + |
| 18 | + const handleToolSelect = async (tool: ToolType) => { |
| 19 | + setToolError(null) |
| 20 | + const selectedTool = await actions.selectTool(tool) |
| 21 | + if (!selectedTool.success) { |
| 22 | + console.error(selectedTool.error); |
| 23 | + setToolError("Failed to select tool"); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + setSelectedTool(tool); |
| 28 | + } |
| 29 | + |
| 30 | + const handleSubmit = async () => { |
| 31 | + setIsSubmitting(true) |
| 32 | + setSubmitError(null) |
| 33 | + |
| 34 | + const submitResult = await actions.submit({ downloadCopyOnDevice: allowDownload }) |
| 35 | + |
| 36 | + if (!submitResult.success) { |
| 37 | + console.error(submitResult.error); |
| 38 | + setSubmitError("Failed to submit document"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + setIsSubmitting(false) |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="flex h-screen bg-gray-100"> |
| 48 | + {/* Main Content Area */} |
| 49 | + <div className="flex-1 flex flex-col"> |
| 50 | + {/* PDF Viewer */} |
| 51 | + <EmbedPDF |
| 52 | + className="w-100 h-screen" |
| 53 | + ref={embedRef} |
| 54 | + companyIdentifier="headless" |
| 55 | + mode="inline" |
| 56 | + /> |
| 57 | + </div> |
| 58 | + |
| 59 | + {/* Right Sidebar */} |
| 60 | + <div className="w-80 bg-white border-l border-gray-200 flex flex-col"> |
| 61 | + {/* Tool Icons */} |
| 62 | + <div className="p-4 border-b border-gray-200"> |
| 63 | + <div className="flex items-center justify-center"> |
| 64 | + <div className="flex space-x-2"> |
| 65 | + <button |
| 66 | + onClick={() => handleToolSelect(null)} |
| 67 | + className={`p-2 hover:bg-gray-100 rounded border border-gray-200 ${selectedTool === null ? "bg-blue-100 border-blue-500" : ""}`} |
| 68 | + > |
| 69 | + <MousePointer className="h-5 w-5" /> |
| 70 | + </button> |
| 71 | + <button |
| 72 | + onClick={() => handleToolSelect("TEXT")} |
| 73 | + className={`p-2 hover:bg-gray-100 rounded border border-gray-200 ${selectedTool === "TEXT" ? "bg-blue-100 border-blue-500" : ""}`} |
| 74 | + > |
| 75 | + <Type className="h-5 w-5" /> |
| 76 | + </button> |
| 77 | + <button |
| 78 | + onClick={() => handleToolSelect("CHECKBOX")} |
| 79 | + className={`p-2 hover:bg-gray-100 rounded border border-gray-200 ${selectedTool === "CHECKBOX" ? "bg-blue-100 border-blue-500" : ""}`} |
| 80 | + > |
| 81 | + <Check className="h-5 w-5" /> |
| 82 | + </button> |
| 83 | + <button |
| 84 | + onClick={() => handleToolSelect("SIGNATURE")} |
| 85 | + className={`p-2 hover:bg-gray-100 rounded border border-gray-200 ${selectedTool === "SIGNATURE" ? "bg-blue-100 border-blue-500" : ""}`} |
| 86 | + > |
| 87 | + <PenTool className="h-5 w-5" /> |
| 88 | + </button> |
| 89 | + <button |
| 90 | + onClick={() => handleToolSelect("PICTURE")} |
| 91 | + className={`p-2 hover:bg-gray-100 rounded border border-gray-200 ${selectedTool === "PICTURE" ? "bg-blue-100 border-blue-500" : ""}`} |
| 92 | + > |
| 93 | + <ImageIcon className="h-5 w-5" /> |
| 94 | + </button> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + {/* Main Content */} |
| 100 | + <div className="flex-1 p-6 flex flex-col"> |
| 101 | + {/* Tool Error Display */} |
| 102 | + {toolError && ( |
| 103 | + <div className="mb-4 p-3 bg-red-50 border border-red-200 rounded text-red-700 text-sm">{toolError}</div> |
| 104 | + )} |
| 105 | + |
| 106 | + <div className="mt-auto space-y-4"> |
| 107 | + {/* Download Copy Toggle - smaller and above submit */} |
| 108 | + <div className="flex items-center justify-between text-sm"> |
| 109 | + <label htmlFor="allow-download" className="text-gray-600"> |
| 110 | + Download copy |
| 111 | + </label> |
| 112 | + <Switch id="allow-download" checked={allowDownload} onCheckedChange={setAllowDownload} /> |
| 113 | + </div> |
| 114 | + |
| 115 | + {/* Submit Error Display */} |
| 116 | + {submitError && ( |
| 117 | + <div className="p-3 bg-red-50 border border-red-200 rounded text-red-700 text-sm">{submitError}</div> |
| 118 | + )} |
| 119 | + |
| 120 | + {/* Submit Button */} |
| 121 | + <Button |
| 122 | + onClick={handleSubmit} |
| 123 | + disabled={isSubmitting} |
| 124 | + className="w-full bg-blue-500 hover:bg-blue-600 text-white py-2.5 disabled:opacity-50" |
| 125 | + > |
| 126 | + {isSubmitting ? ( |
| 127 | + <> |
| 128 | + <Loader2 className="h-4 w-4 mr-2 animate-spin" /> |
| 129 | + Submitting... |
| 130 | + </> |
| 131 | + ) : ( |
| 132 | + "Submit" |
| 133 | + )} |
| 134 | + </Button> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + |
| 138 | + {/* Footer */} |
| 139 | + <div className="p-6 border-t border-gray-200"> |
| 140 | + <div className="flex items-center justify-center"> |
| 141 | + <div className="flex items-center space-x-2"> |
| 142 | + <span className="font-semibold text-gray-900">Custom Sidebar Demo</span> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ) |
| 149 | +} |
0 commit comments