2023-08-03 09:31:31 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2023-05-11 13:10:17 +00:00
|
|
|
|
2023-08-03 09:31:31 +00:00
|
|
|
// next-themes
|
2023-04-20 08:11:24 +00:00
|
|
|
import { useTheme } from "next-themes";
|
2023-08-03 09:31:31 +00:00
|
|
|
// services
|
|
|
|
import userService from "services/user.service";
|
|
|
|
// hooks
|
|
|
|
import useUser from "hooks/use-user";
|
2023-05-11 13:10:17 +00:00
|
|
|
// constants
|
2023-04-20 08:11:24 +00:00
|
|
|
import { THEMES_OBJ } from "constants/themes";
|
2023-05-11 13:10:17 +00:00
|
|
|
// ui
|
2023-04-20 08:11:24 +00:00
|
|
|
import { CustomSelect } from "components/ui";
|
2023-05-11 13:10:17 +00:00
|
|
|
// types
|
2023-08-03 09:31:31 +00:00
|
|
|
import { ICustomTheme } from "types";
|
|
|
|
import { unsetCustomCssVariables } from "helpers/theme.helper";
|
2023-05-11 13:10:17 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-08-03 09:31:31 +00:00
|
|
|
setPreLoadedData: React.Dispatch<React.SetStateAction<ICustomTheme | null>>;
|
2023-05-11 13:10:17 +00:00
|
|
|
customThemeSelectorOptions: boolean;
|
2023-08-03 09:31:31 +00:00
|
|
|
setCustomThemeSelectorOptions: React.Dispatch<React.SetStateAction<boolean>>;
|
2023-05-11 13:10:17 +00:00
|
|
|
};
|
2023-04-20 08:11:24 +00:00
|
|
|
|
2023-05-11 13:10:17 +00:00
|
|
|
export const ThemeSwitch: React.FC<Props> = ({
|
|
|
|
setPreLoadedData,
|
|
|
|
customThemeSelectorOptions,
|
|
|
|
setCustomThemeSelectorOptions,
|
|
|
|
}) => {
|
2023-04-20 08:11:24 +00:00
|
|
|
const [mounted, setMounted] = useState(false);
|
2023-08-03 09:31:31 +00:00
|
|
|
|
2023-04-20 08:11:24 +00:00
|
|
|
const { theme, setTheme } = useTheme();
|
|
|
|
|
2023-08-03 09:31:31 +00:00
|
|
|
const { user, mutateUser } = useUser();
|
|
|
|
|
|
|
|
const updateUserTheme = (newTheme: string) => {
|
|
|
|
if (!user) return;
|
|
|
|
|
|
|
|
setTheme(newTheme);
|
|
|
|
|
|
|
|
mutateUser((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...prevData,
|
|
|
|
theme: {
|
|
|
|
...prevData.theme,
|
|
|
|
theme: newTheme,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
userService.updateUser({
|
|
|
|
theme: {
|
|
|
|
...user.theme,
|
|
|
|
theme: newTheme,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-20 08:11:24 +00:00
|
|
|
// useEffect only runs on the client, so now we can safely show the UI
|
|
|
|
useEffect(() => {
|
|
|
|
setMounted(true);
|
|
|
|
}, []);
|
|
|
|
|
2023-08-03 09:31:31 +00:00
|
|
|
if (!mounted) return null;
|
2023-04-20 08:11:24 +00:00
|
|
|
|
2023-05-11 13:10:17 +00:00
|
|
|
const currentThemeObj = THEMES_OBJ.find((t) => t.value === theme);
|
|
|
|
|
2023-04-20 08:11:24 +00:00
|
|
|
return (
|
2023-08-03 09:31:31 +00:00
|
|
|
<CustomSelect
|
|
|
|
value={theme}
|
|
|
|
label={
|
|
|
|
currentThemeObj ? (
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<div
|
|
|
|
className="border-1 relative flex h-4 w-4 rotate-45 transform items-center justify-center rounded-full border"
|
|
|
|
style={{
|
|
|
|
borderColor: currentThemeObj.icon.border,
|
|
|
|
}}
|
|
|
|
>
|
2023-05-11 13:10:17 +00:00
|
|
|
<div
|
2023-08-03 09:31:31 +00:00
|
|
|
className="h-full w-1/2 rounded-l-full"
|
2023-05-11 13:10:17 +00:00
|
|
|
style={{
|
2023-08-03 09:31:31 +00:00
|
|
|
background: currentThemeObj.icon.color1,
|
2023-05-11 13:10:17 +00:00
|
|
|
}}
|
2023-08-03 09:31:31 +00:00
|
|
|
/>
|
|
|
|
<div
|
|
|
|
className="h-full w-1/2 rounded-r-full border-l"
|
|
|
|
style={{
|
|
|
|
borderLeftColor: currentThemeObj.icon.border,
|
|
|
|
background: currentThemeObj.icon.color2,
|
|
|
|
}}
|
|
|
|
/>
|
2023-05-11 13:10:17 +00:00
|
|
|
</div>
|
2023-08-03 09:31:31 +00:00
|
|
|
{currentThemeObj.label}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
"Select your theme"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onChange={({ value, type }: { value: string; type: string }) => {
|
|
|
|
if (value === "custom") {
|
|
|
|
if (user?.theme.palette) {
|
|
|
|
setPreLoadedData({
|
|
|
|
background: user.theme.background !== "" ? user.theme.background : "#0d101b",
|
|
|
|
text: user.theme.text !== "" ? user.theme.text : "#c5c5c5",
|
|
|
|
primary: user.theme.primary !== "" ? user.theme.primary : "#3f76ff",
|
|
|
|
sidebarBackground:
|
|
|
|
user.theme.sidebarBackground !== "" ? user.theme.sidebarBackground : "#0d101b",
|
|
|
|
sidebarText: user.theme.sidebarText !== "" ? user.theme.sidebarText : "#c5c5c5",
|
|
|
|
darkPalette: false,
|
|
|
|
palette:
|
|
|
|
user.theme.palette !== ",,,,"
|
|
|
|
? user.theme.palette
|
|
|
|
: "#0d101b,#c5c5c5,#3f76ff,#0d101b,#c5c5c5",
|
|
|
|
theme: "custom",
|
|
|
|
});
|
2023-04-20 08:11:24 +00:00
|
|
|
}
|
2023-07-20 11:54:17 +00:00
|
|
|
|
2023-08-03 09:31:31 +00:00
|
|
|
if (!customThemeSelectorOptions) setCustomThemeSelectorOptions(true);
|
|
|
|
} else {
|
|
|
|
if (customThemeSelectorOptions) setCustomThemeSelectorOptions(false);
|
|
|
|
unsetCustomCssVariables();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUserTheme(value);
|
|
|
|
document.documentElement.style.setProperty("--color-scheme", type);
|
|
|
|
}}
|
|
|
|
input
|
|
|
|
width="w-full"
|
|
|
|
position="right"
|
|
|
|
>
|
|
|
|
{THEMES_OBJ.map(({ value, label, type, icon }) => (
|
|
|
|
<CustomSelect.Option key={value} value={{ value, type }}>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<div
|
|
|
|
className="border-1 relative flex h-4 w-4 rotate-45 transform items-center justify-center rounded-full border"
|
|
|
|
style={{
|
|
|
|
borderColor: icon.border,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="h-full w-1/2 rounded-l-full"
|
|
|
|
style={{
|
|
|
|
background: icon.color1,
|
|
|
|
}}
|
|
|
|
/>
|
2023-05-11 13:10:17 +00:00
|
|
|
<div
|
2023-08-03 09:31:31 +00:00
|
|
|
className="h-full w-1/2 rounded-r-full border-l"
|
2023-05-11 13:10:17 +00:00
|
|
|
style={{
|
2023-08-03 09:31:31 +00:00
|
|
|
borderLeftColor: icon.border,
|
|
|
|
background: icon.color2,
|
2023-05-11 13:10:17 +00:00
|
|
|
}}
|
2023-08-03 09:31:31 +00:00
|
|
|
/>
|
2023-05-11 13:10:17 +00:00
|
|
|
</div>
|
2023-08-03 09:31:31 +00:00
|
|
|
{label}
|
|
|
|
</div>
|
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
2023-04-20 08:11:24 +00:00
|
|
|
);
|
|
|
|
};
|