2023-07-10 06:55:32 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { useTheme } from "next-themes";
|
|
|
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
// ui
|
|
|
|
import { PrimaryButton } from "components/ui";
|
|
|
|
import { ColorPickerInput } from "components/core";
|
|
|
|
// types
|
|
|
|
import { ICustomTheme } from "types";
|
2023-08-10 07:33:42 +00:00
|
|
|
// mobx react lite
|
|
|
|
import { observer } from "mobx-react-lite";
|
2023-08-08 07:20:27 +00:00
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-07-10 06:55:32 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
preLoadedData?: Partial<ICustomTheme> | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: ICustomTheme = {
|
2023-07-18 09:50:05 +00:00
|
|
|
background: "#0d101b",
|
|
|
|
text: "#c5c5c5",
|
|
|
|
primary: "#3f76ff",
|
|
|
|
sidebarBackground: "#0d101b",
|
|
|
|
sidebarText: "#c5c5c5",
|
2023-07-10 06:55:32 +00:00
|
|
|
darkPalette: false,
|
|
|
|
palette: "",
|
2023-08-03 09:31:31 +00:00
|
|
|
theme: "custom",
|
2023-07-10 06:55:32 +00:00
|
|
|
};
|
|
|
|
|
2023-08-10 07:33:42 +00:00
|
|
|
export const CustomThemeSelector: React.FC<Props> = observer(({ preLoadedData }) => {
|
2023-08-08 07:20:27 +00:00
|
|
|
const store: any = useMobxStore();
|
2023-08-10 07:33:42 +00:00
|
|
|
const { setTheme } = useTheme();
|
2023-08-08 07:20:27 +00:00
|
|
|
|
2023-07-10 06:55:32 +00:00
|
|
|
const [darkPalette, setDarkPalette] = useState(false);
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
watch,
|
|
|
|
setValue,
|
|
|
|
reset,
|
|
|
|
} = useForm<ICustomTheme>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
2023-08-10 07:33:42 +00:00
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...preLoadedData,
|
|
|
|
});
|
|
|
|
}, [preLoadedData, reset]);
|
2023-07-10 06:55:32 +00:00
|
|
|
|
2023-08-10 07:33:42 +00:00
|
|
|
const handleUpdateTheme = async (formData: any) => {
|
2023-07-10 06:55:32 +00:00
|
|
|
const payload: ICustomTheme = {
|
|
|
|
background: formData.background,
|
|
|
|
text: formData.text,
|
|
|
|
primary: formData.primary,
|
|
|
|
sidebarBackground: formData.sidebarBackground,
|
|
|
|
sidebarText: formData.sidebarText,
|
|
|
|
darkPalette: darkPalette,
|
|
|
|
palette: `${formData.background},${formData.text},${formData.primary},${formData.sidebarBackground},${formData.sidebarText}`,
|
2023-08-03 09:31:31 +00:00
|
|
|
theme: "custom",
|
2023-07-10 06:55:32 +00:00
|
|
|
};
|
|
|
|
|
2023-08-10 07:33:42 +00:00
|
|
|
setTheme("custom");
|
2023-07-10 06:55:32 +00:00
|
|
|
|
2023-08-10 07:33:42 +00:00
|
|
|
return store.user
|
|
|
|
.updateCurrentUserSettings({ theme: payload })
|
|
|
|
.then((response: any) => response)
|
|
|
|
.catch((error: any) => error);
|
2023-07-10 06:55:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit(handleUpdateTheme)}>
|
|
|
|
<div className="space-y-5">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-lg font-semibold text-custom-text-100">Customize your theme</h3>
|
2023-07-10 06:55:32 +00:00
|
|
|
<div className="space-y-4">
|
|
|
|
<div className="grid grid-cols-1 gap-x-6 gap-y-4 sm:grid-cols-2 md:grid-cols-3">
|
|
|
|
<div className="flex flex-col items-start gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-left text-sm font-medium text-custom-text-200">
|
2023-07-10 06:55:32 +00:00
|
|
|
Background color
|
|
|
|
</h3>
|
|
|
|
<ColorPickerInput
|
|
|
|
name="background"
|
2023-08-11 13:47:59 +00:00
|
|
|
position="right"
|
2023-07-10 06:55:32 +00:00
|
|
|
error={errors.background}
|
|
|
|
watch={watch}
|
|
|
|
setValue={setValue}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col items-start gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-left text-sm font-medium text-custom-text-200">Text color</h3>
|
2023-07-10 06:55:32 +00:00
|
|
|
<ColorPickerInput
|
|
|
|
name="text"
|
|
|
|
error={errors.text}
|
|
|
|
watch={watch}
|
|
|
|
setValue={setValue}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col items-start gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-left text-sm font-medium text-custom-text-200">
|
2023-07-10 06:55:32 +00:00
|
|
|
Primary(Theme) color
|
|
|
|
</h3>
|
|
|
|
<ColorPickerInput
|
|
|
|
name="primary"
|
|
|
|
error={errors.primary}
|
|
|
|
watch={watch}
|
|
|
|
setValue={setValue}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col items-start gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-left text-sm font-medium text-custom-text-200">
|
2023-07-10 06:55:32 +00:00
|
|
|
Sidebar background color
|
|
|
|
</h3>
|
|
|
|
<ColorPickerInput
|
|
|
|
name="sidebarBackground"
|
2023-08-11 13:47:59 +00:00
|
|
|
position="right"
|
2023-07-10 06:55:32 +00:00
|
|
|
error={errors.sidebarBackground}
|
|
|
|
watch={watch}
|
|
|
|
setValue={setValue}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col items-start gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-left text-sm font-medium text-custom-text-200">
|
2023-07-10 06:55:32 +00:00
|
|
|
Sidebar text color
|
|
|
|
</h3>
|
|
|
|
<ColorPickerInput
|
|
|
|
name="sidebarText"
|
|
|
|
error={errors.sidebarText}
|
|
|
|
watch={watch}
|
|
|
|
setValue={setValue}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
|
|
|
{isSubmitting ? "Creating Theme..." : "Set Theme"}
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
2023-08-10 07:33:42 +00:00
|
|
|
});
|