|
| 1 | +import React, { useState } from "react"; |
| 2 | + |
| 3 | +import init, { convert } from "libresplit-converter"; |
| 4 | + |
1 | 5 | export function Converter() { |
| 6 | + const [selectedFile, setSelectedFile] = useState<File | null>(null); |
| 7 | + const [result, setResult] = useState<string | null>(null); |
| 8 | + |
| 9 | + const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 10 | + const file = event.target.files?.[0] || null; |
| 11 | + setSelectedFile(file); |
| 12 | + }; |
| 13 | + |
| 14 | + const handleSubmit = async () => { |
| 15 | + if (!selectedFile) { |
| 16 | + alert("Please select a file before submitting!"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + const text = await selectedFile.text(); |
| 22 | + |
| 23 | + await init(); |
| 24 | + |
| 25 | + const converted = convert(text); |
| 26 | + |
| 27 | + setResult(converted); |
| 28 | + } catch (error) { |
| 29 | + console.error("Error processing file: ", error); |
| 30 | + alert("Failed to process file. See console for details."); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const handleDownload = () => { |
| 35 | + if (!result || !selectedFile) return; |
| 36 | + |
| 37 | + const fileName = selectedFile.name.replace(/\.[^/.]+$/, ".json"); |
| 38 | + |
| 39 | + const blob = new Blob([result], { type: "application/json" }); |
| 40 | + const url = URL.createObjectURL(blob); |
| 41 | + |
| 42 | + const link = document.createElement("a"); |
| 43 | + link.href = url; |
| 44 | + link.download = fileName; |
| 45 | + link.click(); |
| 46 | + |
| 47 | + URL.revokeObjectURL(url); |
| 48 | + }; |
| 49 | + |
2 | 50 | return ( |
3 | | - <div className="flex h-screen w-screen items-center justify-center"> |
4 | | - <p className="text-gray-500">Placeholder page for converter.</p> |
| 51 | + <div className="flex min-h-screen items-center justify-center bg-linear-to-tr from-gray-700 to-sky-900 p-6"> |
| 52 | + <div className="w-full max-w-lg space-y-6 rounded-lg bg-gray-800 p-6 shadow-lg"> |
| 53 | + <h1 className="text-center text-2xl font-bold text-white"> |
| 54 | + LibreSplit Converter |
| 55 | + </h1> |
| 56 | + <div className="space-y-4"> |
| 57 | + <input |
| 58 | + type="file" |
| 59 | + accept=".lss" |
| 60 | + onChange={handleFileChange} |
| 61 | + className="block w-full rounded-md border border-white px-3 py-2 text-white focus:ring focus:ring-indigo-500 focus:outline-none" |
| 62 | + /> |
| 63 | + <button |
| 64 | + onClick={handleSubmit} |
| 65 | + disabled={!selectedFile} |
| 66 | + className={ |
| 67 | + '${selectedFile ? "bg-indigo-600 hover:bg-indigo-700" : "bg-gray-400 cursor-not-allowed"} w-full rounded-md px-4 py-2 font-semibold text-white' |
| 68 | + } |
| 69 | + > |
| 70 | + Convert |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + {result && ( |
| 74 | + <div className="space-y-4"> |
| 75 | + <p className="text-center font-medium text-green-600"> |
| 76 | + Conversion successful! Click the button below to download your |
| 77 | + LibreSplit file. |
| 78 | + </p> |
| 79 | + <button |
| 80 | + onClick={handleDownload} |
| 81 | + className="w-full rounded-md bg-green-600 px-4 py-2 font-semibold text-white hover:bg-green-700" |
| 82 | + > |
| 83 | + Download |
| 84 | + </button> |
| 85 | + </div> |
| 86 | + )} |
| 87 | + </div> |
5 | 88 | </div> |
6 | 89 | ); |
7 | 90 | } |
0 commit comments