el.xwx.moe/components/ProfileDropdown.tsx

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-04-19 05:16:11 -05:00
import useLocalSettingsStore from "@/store/localSettings";
import { dropdownTriggerer } from "@/lib/client/utils";
import ProfilePhoto from "./ProfilePhoto";
import useAccountStore from "@/store/account";
import Link from "next/link";
import { signOut } from "next-auth/react";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
2024-04-19 05:16:11 -05:00
export default function ProfileDropdown() {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
2024-04-19 05:16:11 -05:00
const { settings, updateSettings } = useLocalSettingsStore();
const { account } = useAccountStore();
2024-07-03 16:29:33 -05:00
const isAdmin = account.id === Number(process.env.NEXT_PUBLIC_ADMIN || 1);
2024-04-19 05:16:11 -05:00
const handleToggle = () => {
2024-06-09 08:27:16 -05:00
const newTheme = settings.theme === "dark" ? "light" : "dark";
updateSettings({ theme: newTheme });
2024-04-19 05:16:11 -05:00
};
return (
<div className="dropdown dropdown-end">
<div
tabIndex={0}
role="button"
onMouseDown={dropdownTriggerer}
className="btn btn-circle btn-ghost"
>
<ProfilePhoto
src={account.image ? account.image : undefined}
priority={true}
/>
</div>
2024-07-03 16:29:33 -05:00
<ul
2024-07-27 16:17:38 -05:00
className={`dropdown-content z-[1] menu shadow bg-base-200 border border-neutral-content rounded-box ${isAdmin ? "w-48" : "w-40"
} mt-1`}
2024-07-03 16:29:33 -05:00
>
2024-04-19 05:16:11 -05:00
<li>
<Link
href="/settings/account"
onClick={() => (document?.activeElement as HTMLElement)?.blur()}
tabIndex={0}
role="button"
>
2024-06-09 08:27:16 -05:00
{t("settings")}
2024-04-19 05:16:11 -05:00
</Link>
</li>
<li className="block sm:hidden">
<div
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
handleToggle();
}}
tabIndex={0}
role="button"
>
2024-06-09 08:27:16 -05:00
{t("switch_to", {
theme: settings.theme === "light" ? t("dark") : t("light"),
})}
2024-04-19 05:16:11 -05:00
</div>
</li>
2024-07-27 16:17:38 -05:00
{isAdmin && (
2024-07-03 16:29:33 -05:00
<li>
<Link
href="/admin"
onClick={() => (document?.activeElement as HTMLElement)?.blur()}
tabIndex={0}
role="button"
>
{t("server_administration")}
</Link>
</li>
2024-07-27 16:17:38 -05:00
)}
2024-04-19 05:16:11 -05:00
<li>
<div
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
signOut();
}}
tabIndex={0}
role="button"
>
2024-06-09 08:27:16 -05:00
{t("logout")}
2024-04-19 05:16:11 -05:00
</div>
</li>
</ul>
</div>
);
}