2023-11-01 11:41:29 +00:00
|
|
|
import React, { FC, Dispatch, SetStateAction, useEffect, useState } from "react";
|
2023-04-20 08:11:24 +00:00
|
|
|
import { Command } from "cmdk";
|
|
|
|
import { useTheme } from "next-themes";
|
2023-10-16 14:57:22 +00:00
|
|
|
import { Settings } from "lucide-react";
|
2023-08-16 12:56:36 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-11-01 11:41:29 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-08-16 12:56:36 +00:00
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-11-02 11:41:33 +00:00
|
|
|
// constants
|
|
|
|
import { THEME_OPTIONS } from "constants/themes";
|
2023-04-20 08:11:24 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
setIsPaletteOpen: Dispatch<SetStateAction<boolean>>;
|
|
|
|
};
|
|
|
|
|
2023-11-01 11:41:29 +00:00
|
|
|
export const ChangeInterfaceTheme: FC<Props> = observer((props) => {
|
|
|
|
const { setIsPaletteOpen } = props;
|
|
|
|
// store
|
|
|
|
const { user: userStore } = useMobxStore();
|
|
|
|
// states
|
2023-04-20 08:11:24 +00:00
|
|
|
const [mounted, setMounted] = useState(false);
|
2023-11-01 11:41:29 +00:00
|
|
|
// hooks
|
2023-04-20 08:11:24 +00:00
|
|
|
const { setTheme } = useTheme();
|
2023-11-01 11:41:29 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-08-03 09:31:31 +00:00
|
|
|
|
|
|
|
const updateUserTheme = (newTheme: string) => {
|
|
|
|
setTheme(newTheme);
|
2023-11-01 11:41:29 +00:00
|
|
|
return userStore.updateCurrentUserTheme(newTheme).catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
title: "Failed to save user theme settings!",
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
});
|
2023-08-03 09:31:31 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-10-17 07:16:38 +00:00
|
|
|
{THEME_OPTIONS.filter((t) => t.value !== "custom").map((theme) => (
|
2023-04-20 08:11:24 +00:00
|
|
|
<Command.Item
|
|
|
|
key={theme.value}
|
|
|
|
onSelect={() => {
|
2023-08-03 09:31:31 +00:00
|
|
|
updateUserTheme(theme.value);
|
2023-04-20 08:11:24 +00:00
|
|
|
setIsPaletteOpen(false);
|
|
|
|
}}
|
|
|
|
className="focus:outline-none"
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="flex items-center gap-2 text-custom-text-200">
|
2023-10-16 14:57:22 +00:00
|
|
|
<Settings className="h-4 w-4 text-custom-text-200" />
|
2023-04-20 08:11:24 +00:00
|
|
|
{theme.label}
|
|
|
|
</div>
|
|
|
|
</Command.Item>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
2023-08-16 12:56:36 +00:00
|
|
|
});
|