From 0f7297aa7021a15b467c410bb43813e404db38eb Mon Sep 17 00:00:00 2001 From: rift <117926989+Riftriot@users.noreply.github.com> Date: Sat, 16 Dec 2023 14:13:04 -0600 Subject: [PATCH] Translation support for dropdown.tsx component --- src/locales/en.json | 4 +++ src/locales/ja.json | 4 +++ src/pages/Home.tsx | 1 - src/pages/Settings/Dropdown.tsx | 44 ++++++++++++++++++++++--------- src/pages/Settings/Misc.tsx | 46 +++++++++++++++++++++++---------- src/pages/Settings/Proxy.tsx | 11 ++++++-- 6 files changed, 81 insertions(+), 29 deletions(-) diff --git a/src/locales/en.json b/src/locales/en.json index e4a48ad..5c88410 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -24,6 +24,10 @@ "tab": "Tab", "custom": "Customization", "misc": "Misc" + }, + "proxy": { + "title": "Proxy", + "automatic": "Automatic" } } } diff --git a/src/locales/ja.json b/src/locales/ja.json index 5793763..7003a4f 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -24,6 +24,10 @@ "tab": "タブ", "custom": "カスタマイズ", "misc": "その他" + }, + "proxy": { + "title": "プロキシ", + "automatic": "自動" } } } diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 674d91e..df0d79b 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -1,7 +1,6 @@ import { useState } from "preact/hooks"; import { useTranslation } from "react-i18next"; import { HeaderRoute } from "../components/HeaderRoute"; - export function Home() { const [isFocused, setIsFocused] = useState(false); const { t } = useTranslation(); diff --git a/src/pages/Settings/Dropdown.tsx b/src/pages/Settings/Dropdown.tsx index 80d9648..6aab6d7 100644 --- a/src/pages/Settings/Dropdown.tsx +++ b/src/pages/Settings/Dropdown.tsx @@ -1,10 +1,29 @@ -import { useState } from "preact/hooks"; +import { useState, useEffect } from "preact/hooks"; -const Dropdown = ({ name, options }: { name: string; options: string[] }) => { +interface Option { + id: string; + label: string; // Translations CAN be passed +} + +const Dropdown = ({ + name, + options, + storageKey +}: { + name: string; + storageKey: string; + options: Option[]; +}) => { const [isOpen, setIsOpen] = useState(false); - const [choice, setChoice] = useState( - localStorage.getItem(name) || options[0] - ); + + 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 (