|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Carlos Pérez Ramil |
| 3 | + * |
| 4 | + * This file is part of the GaliGuessr project and is licensed under the GNU GPL v3.0. |
| 5 | + * See LICENSE file in the root directory of this project or at <https://www.gnu.org/licenses/gpl-3.0>. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { FeatureCollection } from "geojson"; |
| 9 | +import { useCallback, useState, type ChangeEvent } from "react"; |
| 10 | +import { useTranslation } from "react-i18next"; |
| 11 | +import { useNavigate } from "react-router-dom"; |
| 12 | +import { Mode } from "../enums"; |
| 13 | + |
| 14 | +export default function SelectFile() { |
| 15 | + const [fileContent, setFileContent] = useState<FeatureCollection | null>(null); |
| 16 | + const [error, setError] = useState<string | null>(null); |
| 17 | + const navigate = useNavigate(); |
| 18 | + const { t } = useTranslation(); |
| 19 | + |
| 20 | + const handleFileChange = useCallback((event: ChangeEvent<HTMLInputElement>) => { |
| 21 | + setError(null); |
| 22 | + const file = event.target.files?.[0]; |
| 23 | + if (!file) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const reader = new FileReader(); |
| 28 | + reader.onload = (e) => { |
| 29 | + try { |
| 30 | + const content = e.target?.result as string; |
| 31 | + const featureCollection = JSON.parse(content) as FeatureCollection; |
| 32 | + featureCollection.features.forEach((feature, index) => feature.id = index); |
| 33 | + setFileContent(featureCollection); |
| 34 | + } catch (err) { |
| 35 | + setError("Failed to parse JSON file"); |
| 36 | + console.error("Error parsing JSON:", err); |
| 37 | + } |
| 38 | + }; |
| 39 | + reader.onerror = () => { |
| 40 | + setError("Error reading file"); |
| 41 | + }; |
| 42 | + reader.readAsText(file); |
| 43 | + |
| 44 | + //TODO: Validate datasets are well formed geojsons with id and name fields |
| 45 | + |
| 46 | + }, []); |
| 47 | + |
| 48 | + const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { |
| 49 | + event.preventDefault(); |
| 50 | + const selectedMode = event.currentTarget.elements.namedItem("mode") as HTMLInputElement; |
| 51 | + void navigate(`/play?mode=${selectedMode.value}`, { state: { fileContent } }); |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <div className="flex flex-col items-center p-10"> |
| 56 | + <form |
| 57 | + className="flex flex-col items-center gap-4" |
| 58 | + onSubmit={handleSubmit} |
| 59 | + > |
| 60 | + <label><b>Select a valid GeoJSON File</b></label> |
| 61 | + <input |
| 62 | + type="file" |
| 63 | + accept=".json,.geojson" |
| 64 | + onChange={handleFileChange} |
| 65 | + /> |
| 66 | + <select id="mode" name="mode"> |
| 67 | + {Object.values(Mode) |
| 68 | + .map((mode, index: number) => ( |
| 69 | + <option |
| 70 | + value={mode} |
| 71 | + key={index}> |
| 72 | + {t("modes." + mode, { lng: 'en' })} |
| 73 | + </option> |
| 74 | + )) |
| 75 | + } |
| 76 | + </select> |
| 77 | + <button |
| 78 | + className="border border-solid border-black p-2" |
| 79 | + type="submit" |
| 80 | + > |
| 81 | + Play |
| 82 | + </button> |
| 83 | + </form> |
| 84 | + |
| 85 | + {error && <div style={{ color: "red" }}>{error}</div>} |
| 86 | + |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
0 commit comments