import { useEffect, useState } from 'react'; import { loadSet, saveSet } from '../../storage.js'; import { BY_CODE, CONTINENTS, continentOf, countryName } from './laender.js'; // Besuchte Orte: Kontinent → Land (ausklappbar) → Städte; auf-/zugeklappt wird pro Browser gemerkt const OPEN_KEY = 'p42.reisenOffen'; const CLOSED_KEY = 'p42.reisenKontinenteZu'; const loadOpen = () => loadSet(OPEN_KEY); export default function VisitedList({ visited, cities, manual, selected, onSelect }) { const [open, setOpen] = useState(loadOpen); const save = (next) => { setOpen(next); saveSet(OPEN_KEY, next); }; const toggle = (code) => { const next = new Set(open); if (next.has(code)) next.delete(code); else next.add(code); save(next); }; // Kontinente: standardmäßig offen, zugeklappte werden gemerkt const [closed, setClosed] = useState(() => loadSet(CLOSED_KEY)); const saveClosed = (next) => { setClosed(next); saveSet(CLOSED_KEY, next); }; const toggleContinent = (name) => { const next = new Set(closed); if (next.has(name)) next.delete(name); else next.add(name); saveClosed(next); }; // Wird eine Stadt (z. B. auf der Karte) gewählt, ihr Land aufklappen const selectedCountry = selected?.type === 'city' ? cities.find((c) => c.id === selected.key)?.country : null; useEffect(() => { if (selectedCountry && !open.has(selectedCountry)) save(new Set(open).add(selectedCountry)); // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedCountry]); // Ein gewähltes Land oder eine gewählte Stadt macht ihren Kontinent sichtbar const selectedCode = selected?.type === 'country' ? selected.key : selectedCountry; const selectedContinent = selectedCode ? continentOf(selectedCode) : null; useEffect(() => { if (selectedContinent && closed.has(selectedContinent)) { const next = new Set(closed); next.delete(selectedContinent); saveClosed(next); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedContinent, selected]); if (!visited.length) { return
Noch keine Orte. Füge eine Stadt hinzu oder klick ein Land auf der Karte an und markiere es.
; } const groups = [...CONTINENTS, 'Sonstige'] .map((name) => ({ name, countries: visited.filter((code) => continentOf(code) === name) })) .filter((g) => g.countries.length); return (