|
| 1 | +import React, { useState, useRef, ChangeEvent, DragEvent } from "react"; |
| 2 | +import { useNavigate } from "react-router-dom"; |
| 3 | +import { usePromptContext } from "../../contexts/PromptContext"; |
| 4 | +import { setPrompts } from "../../utils/message"; |
| 5 | +import { HiArrowLeft, HiUpload, HiDownload } from "react-icons/hi"; |
| 6 | + |
| 7 | +const DataPage: React.FC = () => { |
| 8 | + const { prompts } = usePromptContext(); |
| 9 | + const navigate = useNavigate(); |
| 10 | + const fileRef = useRef<HTMLInputElement>(null); |
| 11 | + const [loading, setLoading] = useState(false); |
| 12 | + const [uploadStatus, setUploadStatus] = useState<{ |
| 13 | + message: string; |
| 14 | + success: boolean; |
| 15 | + } | null>(null); |
| 16 | + |
| 17 | + const handleBack = () => { |
| 18 | + navigate(-1); |
| 19 | + }; |
| 20 | + |
| 21 | + const handleDownload = () => { |
| 22 | + const blob = new Blob([JSON.stringify(prompts, null, 2)], { |
| 23 | + type: "application/json", |
| 24 | + }); |
| 25 | + const url = URL.createObjectURL(blob); |
| 26 | + const link = document.createElement("a"); |
| 27 | + link.href = url; |
| 28 | + link.download = "prompts.json"; |
| 29 | + link.click(); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleUpload = (e: ChangeEvent<HTMLInputElement>) => { |
| 33 | + setLoading(true); |
| 34 | + if (e.target) { |
| 35 | + const file = e.target.files?.[0]; |
| 36 | + if (file) { |
| 37 | + const reader = new FileReader(); |
| 38 | + reader.onload = (e) => { |
| 39 | + if (e.target) { |
| 40 | + try { |
| 41 | + const newPrompts = JSON.parse(e.target.result as string); |
| 42 | + console.log(newPrompts); |
| 43 | + setPrompts(newPrompts); |
| 44 | + setUploadStatus({ message: "Upload successful!", success: true }); |
| 45 | + } catch (error) { |
| 46 | + setUploadStatus({ |
| 47 | + message: "Invalid JSON. Upload failed!", |
| 48 | + success: false, |
| 49 | + }); |
| 50 | + } |
| 51 | + setLoading(false); |
| 52 | + } |
| 53 | + }; |
| 54 | + reader.readAsText(file); |
| 55 | + } else { |
| 56 | + setLoading(false); |
| 57 | + setUploadStatus({ |
| 58 | + message: "No file selected. Upload failed!", |
| 59 | + success: false, |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + const handleDrop = (e: DragEvent<HTMLDivElement>) => { |
| 66 | + e.preventDefault(); |
| 67 | + handleUpload({ |
| 68 | + target: { files: e.dataTransfer.files }, |
| 69 | + } as ChangeEvent<HTMLInputElement>); |
| 70 | + }; |
| 71 | + |
| 72 | + return ( |
| 73 | + <div className="w-full"> |
| 74 | + <div className="flex flex-row items-center justify-start border-b border-zinc-700 p-2"> |
| 75 | + <button |
| 76 | + className="rounded p-2 text-blue-500 hover:bg-zinc-900 hover:text-blue-400" |
| 77 | + onClick={handleBack} |
| 78 | + > |
| 79 | + <HiArrowLeft size={18} /> |
| 80 | + </button> |
| 81 | + <h2 className="text-base text-white">Edit Data File</h2> |
| 82 | + </div> |
| 83 | + <div className="flex flex-col space-y-2 p-2"> |
| 84 | + <div className="p-2 text-zinc-500"> |
| 85 | + You can download your prompts as a JSON file to edit them in a text |
| 86 | + editor of your choice and then upload them back to prompster. |
| 87 | + </div> |
| 88 | + <button |
| 89 | + className="flex w-full flex-row items-center justify-center space-x-4 rounded p-2 text-blue-500 hover:bg-zinc-900 hover:text-blue-400" |
| 90 | + onClick={handleDownload} |
| 91 | + > |
| 92 | + <HiDownload size={18} /> |
| 93 | + Download Prompts |
| 94 | + </button> |
| 95 | + <div |
| 96 | + onDrop={handleDrop} |
| 97 | + onDragOver={(e) => e.preventDefault()} |
| 98 | + className="flex h-[200px] cursor-pointer flex-col items-center justify-center rounded bg-zinc-900 text-blue-500 hover:text-blue-400 p-4" |
| 99 | + onClick={() => fileRef.current?.click()} |
| 100 | + > |
| 101 | + <input |
| 102 | + ref={fileRef} |
| 103 | + type="file" |
| 104 | + accept=".json" |
| 105 | + hidden |
| 106 | + onChange={handleUpload} |
| 107 | + /> |
| 108 | + <div className="flex flex-row items-center justify-center space-x-4"> |
| 109 | + <HiUpload size={18} /> |
| 110 | + Upload Prompts |
| 111 | + </div> |
| 112 | + <div className="p-2 text-zinc-500 text-center"> |
| 113 | + Warning: Uploading a file will overwrite your current prompts. |
| 114 | + </div> |
| 115 | + {loading ? ( |
| 116 | + <div className="p-2 text-zinc-500"> |
| 117 | + {loading ? "Uploading..." : uploadStatus?.message} |
| 118 | + </div> |
| 119 | + ) : ( |
| 120 | + <div |
| 121 | + className={`p-2 ${ |
| 122 | + uploadStatus?.success ? "text-green-500" : "text-red-500" |
| 123 | + }`} |
| 124 | + > |
| 125 | + {uploadStatus?.message} |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +export default DataPage; |
0 commit comments