use hook for background

This commit is contained in:
incognitotgt 2024-04-15 16:22:16 -04:00
parent 217d3a40db
commit 7117e67e0a
No known key found for this signature in database
GPG key ID: D229D19A500C60B0
4 changed files with 54 additions and 45 deletions

View file

@ -21,13 +21,17 @@ type ThemeProviderProps = {
type ThemeProviderState = { type ThemeProviderState = {
themes: Theme[]; themes: Theme[];
theme: Theme; theme: Theme;
background: string;
setTheme: (theme: Theme) => void; setTheme: (theme: Theme) => void;
setBackground: (background: string) => void;
}; };
const initialState: ThemeProviderState = { const initialState: ThemeProviderState = {
themes: themes, themes: themes,
theme: "main", theme: "main",
setTheme: () => null background: "",
setTheme: () => null,
setBackground: () => null
}; };
const ThemeProviderContext = createContext<ThemeProviderState>(initialState); const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
@ -35,9 +39,13 @@ const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
export function ThemeProvider({ children, ...props }: ThemeProviderProps) { export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
const defaultTheme = "main"; const defaultTheme = "main";
const storageKey = "theme"; const storageKey = "theme";
const bgKey = "background";
const [theme, setTheme] = useState<Theme>( const [theme, setTheme] = useState<Theme>(
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme () => (localStorage.getItem(storageKey) as Theme) || defaultTheme
); );
const [background, setBackground] = useState<string>(() =>
localStorage.getItem(bgKey)
);
useEffect(() => { useEffect(() => {
const root = window.document.documentElement; const root = window.document.documentElement;
@ -45,16 +53,20 @@ export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
themes.forEach((theme) => { themes.forEach((theme) => {
root.classList.remove(theme); root.classList.remove(theme);
}); });
root.classList.add(theme); root.classList.add(theme);
}, [theme, themes]); }, [theme, themes]);
const value = { const value = {
theme, theme,
themes, themes,
background,
setTheme: (theme: Theme) => { setTheme: (theme: Theme) => {
localStorage.setItem(storageKey, theme); localStorage.setItem(storageKey, theme);
setTheme(theme); setTheme(theme);
},
setBackground: (background: string) => {
localStorage.setItem(bgKey, background);
setBackground(background);
} }
}; };

View file

@ -6,11 +6,10 @@ import Particles, { initParticlesEngine } from "@tsparticles/react";
import { loadSlim } from "@tsparticles/slim"; import { loadSlim } from "@tsparticles/slim";
import { useEffect, useState } from "preact/compat"; import { useEffect, useState } from "preact/compat";
import { ThemeProvider } from "./components/ThemeProvider"; import { ThemeProvider, useTheme } from "./components/ThemeProvider";
const Routes = lazy(() => import("./routes")); const Routes = lazy(() => import("./routes"));
const theme = localStorage.getItem("theme") || "main";
const particlesUrl = localStorage.getItem("particles") || "none"; const particlesUrl = localStorage.getItem("particles") || "none";
export default function App() { export default function App() {
@ -30,42 +29,44 @@ export default function App() {
setInit(true); setInit(true);
}); });
}, []); }, []);
const { background } = useTheme();
const particlesLoaded = (container) => { const particlesLoaded = (container) => {
console.log(container); console.log(container);
}; };
const bgImage: string | undefined = localStorage.getItem("background");
return ( return (
<ThemeProvider> <div class="h-screen w-screen">
<div class="h-screen w-screen"> {window.location.origin === "https://nebulaproxy.io" && <Meta />}
{window.location.origin === "https://nebulaproxy.io" && <Meta />} {/* {window.location.origin === "http://localhost:8080" && <Meta />} */}
{/* {window.location.origin === "http://localhost:8080" && <Meta />} */} <Suspense fallback={<LoadSuspense />}>
<Suspense fallback={<LoadSuspense />}> <div className="absolute z-20 h-full w-full">
<div className="absolute z-20 h-full w-full"> <Routes />
<Routes /> </div>
</div> <div
<div className="z-10 h-full w-full bg-primary"
className="z-10 h-full w-full bg-primary" style={{
style={{ backgroundImage: `url(${background})`,
backgroundImage: bgImage ? `url(${bgImage})` : "none", backgroundSize: "contain",
backgroundSize: "contain", backgroundRepeat: "repeat",
backgroundRepeat: "repeat", backgroundPosition: "center"
backgroundPosition: "center" }}
}} >
> {init && particlesUrl !== "none" && (
{init && particlesUrl !== "none" && ( <Particles
<Particles id="tsparticles"
id="tsparticles" url={particlesUrl}
url={particlesUrl} particlesLoaded={particlesLoaded}
particlesLoaded={particlesLoaded} class="bg-primary"
class="bg-primary" />
/> )}
)} </div>
</div> </Suspense>
</Suspense> </div>
</div>
</ThemeProvider>
); );
} }
render(<App />, document.getElementById("app")); render(
<ThemeProvider>
<App />
</ThemeProvider>,
document.getElementById("app")
);

View file

@ -1,4 +1,5 @@
import { useRef } from "preact/hooks"; import { useRef } from "preact/hooks";
import { useTheme } from "../../components/ThemeProvider";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
import { tabContentVariant, settingsPageVariant } from "./Variants"; import { tabContentVariant, settingsPageVariant } from "./Variants";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
@ -8,7 +9,7 @@ import ThemeDropdown from "./ThemeDropdown";
function Customization({ id, active }) { function Customization({ id, active }) {
const bgInput = useRef<HTMLInputElement>(null); const bgInput = useRef<HTMLInputElement>(null);
const { t } = useTranslation(); const { t } = useTranslation();
const { background, setBackground } = useTheme();
const particles = [ const particles = [
{ id: "none", label: t("settings.theme.particlesNone") }, { id: "none", label: t("settings.theme.particlesNone") },
{ id: "/crismas.json", label: t("themes.crismas") } { id: "/crismas.json", label: t("themes.crismas") }
@ -55,16 +56,11 @@ function Customization({ id, active }) {
<input <input
ref={bgInput} ref={bgInput}
className="font-roboto h-14 rounded-2xl border border-input-border-color bg-input p-2 text-center text-xl text-input-text placeholder:text-input-text" className="font-roboto h-14 rounded-2xl border border-input-border-color bg-input p-2 text-center text-xl text-input-text placeholder:text-input-text"
defaultValue={localStorage.getItem("background")} defaultValue={background}
placeholder={t("settings.theme.backgroundNone")} placeholder={t("settings.theme.backgroundNone")}
/> />
<button <button
onClick={() => { onClick={() => setBackground(bgInput.current.value)}
if (bgInput) {
localStorage.setItem("background", bgInput.current.value);
window.location.reload();
}
}}
className="font-roboto mt-2 flex h-4 w-36 cursor-pointer flex-row items-center justify-center rounded-xl border border-input-border-color bg-input p-5 text-center text-lg text-input-text" className="font-roboto mt-2 flex h-4 w-36 cursor-pointer flex-row items-center justify-center rounded-xl border border-input-border-color bg-input p-5 text-center text-lg text-input-text"
> >
{t("settings.theme.select")} {t("settings.theme.select")}

View file

@ -19,7 +19,7 @@ export default function Routes() {
"/wisp/"; // @TODO Japan - US ping "/wisp/"; // @TODO Japan - US ping
if ("serviceWorker" in navigator) { if ("serviceWorker" in navigator) {
navigator.serviceWorker.ready.then(async (sw) => { navigator.serviceWorker.ready.then(async () => {
//await registerRemoteListener(sw.active!) //await registerRemoteListener(sw.active!)
setTransport(); setTransport();
}); });