2022-12-14 18:35:27 +00:00
|
|
|
import { useUpdateTheme } from "../pages/_app";
|
2022-12-15 20:23:54 +00:00
|
|
|
import { Fragment, useContext, useState } from 'react';
|
|
|
|
import { ThemeContext, DefaultTheme } from "styled-components";
|
|
|
|
import { darkTheme, amoledTheme, lightTheme, amoled2Theme } from './themes';
|
|
|
|
import { ThemeDropDown, ThemeDropDownButton, ThemeDropDownOption, ThemeDropDownOptions } from "./styles/themedropdown";
|
|
|
|
|
|
|
|
const themes = [
|
|
|
|
lightTheme,
|
|
|
|
darkTheme,
|
|
|
|
amoledTheme,
|
|
|
|
amoled2Theme,
|
|
|
|
]
|
2022-12-14 18:35:27 +00:00
|
|
|
|
|
|
|
const StyleSelector = () => {
|
|
|
|
const updateTheme = useUpdateTheme();
|
|
|
|
const currentTheme = useContext(ThemeContext);
|
2022-12-15 20:23:54 +00:00
|
|
|
const [selectedTheme, setSelectedTheme] = useState(themes[currentTheme.themeId]);
|
2022-12-14 18:35:27 +00:00
|
|
|
|
2022-12-15 20:23:54 +00:00
|
|
|
const updateThemeWithStorage = (newTheme: DefaultTheme) => {
|
2022-12-14 18:35:27 +00:00
|
|
|
|
2022-12-15 20:23:54 +00:00
|
|
|
if (newTheme.themeId === lightTheme.themeId) {
|
|
|
|
updateLightTheme(newTheme);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setSelectedTheme(newTheme);
|
|
|
|
localStorage.setItem("theme", newTheme.themeId.toString());
|
|
|
|
updateTheme(newTheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateLightTheme = (newTheme: DefaultTheme) => {
|
|
|
|
if (confirm("Really switch to Light Mode?")) {
|
|
|
|
setSelectedTheme(newTheme);
|
|
|
|
localStorage.setItem("theme", newTheme.themeId.toString());
|
|
|
|
updateTheme(newTheme)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
}
|
2022-12-14 18:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 20:23:54 +00:00
|
|
|
const [test, setTest] = useState(false);
|
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
return (
|
2022-12-15 20:23:54 +00:00
|
|
|
|
|
|
|
<ThemeDropDown>
|
|
|
|
<ThemeDropDownButton onClick={() => setTest(test => !test)}>{selectedTheme.themeName}</ThemeDropDownButton>
|
|
|
|
<ThemeDropDownOptions id="themesDropdown" show={test}>
|
|
|
|
{themes.map((theme) => (
|
|
|
|
<ThemeDropDownOption active={theme === selectedTheme} key={theme.themeId} onClick={() => updateThemeWithStorage(theme)}>
|
|
|
|
{theme.themeName}
|
|
|
|
</ThemeDropDownOption>
|
|
|
|
))}
|
|
|
|
</ThemeDropDownOptions>
|
|
|
|
</ThemeDropDown>
|
2022-12-14 18:35:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:23:54 +00:00
|
|
|
{/* <button onClick={() => updateLightTheme(lightTheme)}>Light Style</button>
|
|
|
|
<button onClick={() => updateThemeWithStorage(darkTheme)}>Dark Style</button>
|
|
|
|
<button onClick={() => updateThemeWithStorage(amoledTheme)}>Amoled Style</button>
|
|
|
|
<button onClick={() => updateThemeWithStorage(amoled2Theme)}>Amoled 2 Style</button> */}
|
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
export function getTheme(themeId: number): DefaultTheme {
|
|
|
|
let theme: DefaultTheme;
|
|
|
|
|
2022-12-15 20:23:54 +00:00
|
|
|
switch (themeId) {
|
|
|
|
case 0:
|
2022-12-14 18:35:27 +00:00
|
|
|
theme = lightTheme;
|
|
|
|
break;
|
2022-12-15 20:23:54 +00:00
|
|
|
case 1:
|
2022-12-14 18:35:27 +00:00
|
|
|
theme = darkTheme;
|
|
|
|
break;
|
2022-12-15 20:23:54 +00:00
|
|
|
case 2:
|
2022-12-14 18:35:27 +00:00
|
|
|
theme = amoledTheme;
|
|
|
|
break;
|
2022-12-15 20:23:54 +00:00
|
|
|
case 3:
|
|
|
|
theme = amoled2Theme;
|
|
|
|
break;
|
2022-12-14 18:35:27 +00:00
|
|
|
default:
|
|
|
|
theme = darkTheme
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default StyleSelector;
|