import { useState, useEffect } from "preact/hooks"; import { FaAngleDown } from "react-icons/fa"; interface Option { id: string; label: string; // Translations CAN be passed } const Dropdown = ({ storageKey, options, refresh }: { storageKey: string; options: Option[]; refresh: boolean; }) => { const [isOpen, setIsOpen] = useState(false); const [choice, setChoice] = useState(() => { return localStorage.getItem(storageKey) || options[0]?.id || ""; }); // update on localstorage change useEffect(() => { setChoice(localStorage.getItem(storageKey) || options[0]?.id || ""); }, [storageKey, options]); return (
setIsOpen(!isOpen)} >
{options.find((o) => o.id === choice)?.label}
{isOpen && (
{options.map((option, index) => (
{ setIsOpen(false); setChoice(option.id); localStorage.setItem(storageKey, option.id); if (refresh === true) { window.location.reload(); } }} > {option.label}
))}
)}
); }; export default Dropdown;