From 47847b2545a2b8e832bb584d16eedc74fa815f07 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Sun, 22 Oct 2023 16:24:09 -0300 Subject: [PATCH] Add new hook: 'useTheme' --- src/js/components/ThemeSelect.tsx | 20 +++++++++++--------- src/js/hooks/useTheme.ts | 26 ++++++++++++++++++++++++++ src/js/pages/SurahIndex.tsx | 7 ++++--- src/js/pages/SurahStream.tsx | 5 +++-- 4 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 src/js/hooks/useTheme.ts diff --git a/src/js/components/ThemeSelect.tsx b/src/js/components/ThemeSelect.tsx index 8dd2425..3ea11f4 100644 --- a/src/js/components/ThemeSelect.tsx +++ b/src/js/components/ThemeSelect.tsx @@ -1,6 +1,5 @@ import React from "react"; import { Select, SelectOption } from "components/Select"; -import { set as setCookie } from "es-cookie"; interface Props { setTheme: (theme: string) => void; @@ -8,15 +7,18 @@ interface Props { } export function ThemeSelect({ setTheme, theme }: Props) { - const onThemeChange = (o: SelectOption) => { - setCookie("theme", o.value, { domain: location.host, expires: 365 }); - setTheme(o.value); - }; - return ( - setTheme(o.value)} + > + + ); } diff --git a/src/js/hooks/useTheme.ts b/src/js/hooks/useTheme.ts new file mode 100644 index 0000000..ba34f8c --- /dev/null +++ b/src/js/hooks/useTheme.ts @@ -0,0 +1,26 @@ +import { useEffect, useState } from "react"; +import { get as getCookie, set as setCookie } from "es-cookie"; + +type Theme = "blue" | "green"; +const THEMES: Theme[] = ["blue", "green"]; +const DEFAULT_THEME = "blue"; + +export function useTheme(): [Theme, (t: string) => void] { + const [theme, setTheme] = useState(null); + const cookie = getCookie("theme"); + + useEffect(() => { + const _theme = THEMES.find((theme: Theme) => cookie === theme); + setTheme(_theme || DEFAULT_THEME); + }, []); + + function _setTheme(newTheme: string) { + const matchedTheme = THEMES.find((theme: Theme) => newTheme === theme); + if (matchedTheme) { + setCookie("theme", matchedTheme, { domain: location.host, expires: 365 }); + setTheme(matchedTheme); + } + } + + return [theme, _setTheme]; +} diff --git a/src/js/pages/SurahIndex.tsx b/src/js/pages/SurahIndex.tsx index ba15fb5..a3cc8c9 100644 --- a/src/js/pages/SurahIndex.tsx +++ b/src/js/pages/SurahIndex.tsx @@ -1,8 +1,9 @@ -import React, { useState } from "react"; +import React from "react"; import ReactDOM from "react-dom/client"; import classNames from "classnames"; -import { get as getCookie } from "es-cookie"; + import * as Quran from "lib/Quran"; +import { useTheme } from "hooks/useTheme"; import { SelectOption } from "components/Select"; import { ThemeSelect } from "components/ThemeSelect"; import { LanguageSelect } from "components/LanguageSelect"; @@ -15,7 +16,7 @@ interface Props { } function SurahIndex({ locale, surahs, t }: Props) { - const [theme, setTheme] = useState(getCookie("theme") || "moon"); + const [theme, setTheme] = useTheme(); const onLanguageChange = (o: SelectOption) => { document.location.replace(`/${o.value}/`); }; diff --git a/src/js/pages/SurahStream.tsx b/src/js/pages/SurahStream.tsx index 3ad3e13..bf86dd7 100644 --- a/src/js/pages/SurahStream.tsx +++ b/src/js/pages/SurahStream.tsx @@ -1,7 +1,8 @@ import React, { useState, useEffect } from "react"; import ReactDOM from "react-dom/client"; import classNames from "classnames"; -import { get as getCookie } from "es-cookie"; + +import { useTheme } from "hooks/useTheme"; import { Timer } from "components/Timer"; import { Stream } from "components/Stream"; import { SelectOption } from "components/Select"; @@ -32,7 +33,7 @@ function SurahStream({ node, recitations, locale, paused, t }: Props) { const [soundOn, setSoundOn] = useState(false); const [isStalled, setIsStalled] = useState(false); const [endOfStream, setEndOfStream] = useState(false); - const [theme, setTheme] = useState(getCookie("theme") || "moon"); + const [theme, setTheme] = useTheme(); const [recitation] = useState(recitations[0]); const [surah] = useState( Quran.Surah.fromDOMNode(locale, node, getTimeSlots(recitation)),