Skip to content

Commit 8e22543

Browse files
committed
Add converter to website
1 parent dccc82c commit 8e22543

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

src/app/converter.tsx

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,90 @@
1+
import React, { useState } from "react";
2+
3+
import init, { convert } from "libresplit-converter";
4+
15
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+
250
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>
588
</div>
689
);
790
}

src/components/libresplit/AppNav.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function AppNav() {
2626

2727
<NavigationMenuItem>
2828
<NavigationMenuLink asChild>
29-
<a href="https://converter.libresplit.org/">Converter</a>
29+
<Link to="/converter">Converter</Link>
3030
</NavigationMenuLink>
3131
</NavigationMenuItem>
3232
</NavigationMenuList>

0 commit comments

Comments
 (0)