import { Trans } from "@lingui/react/macro"
import {
Box,
Divider,
Group,
type MantineColorScheme,
Menu,
SegmentedControl,
type SegmentedControlItem,
Slider,
useMantineColorScheme,
} from "@mantine/core"
import { showNotification } from "@mantine/notifications"
import dayjs from "dayjs"
import { type ReactNode, useEffect, useState } from "react"
import {
TbChartLine,
TbHeartFilled,
TbHelp,
TbLayoutList,
TbList,
TbListDetails,
TbMoon,
TbNotes,
TbPower,
TbSettings,
TbSun,
TbSunMoon,
TbUsers,
TbWorldDownload,
} from "react-icons/tb"
import { throttle } from "throttle-debounce"
import { client } from "@/app/client"
import { redirectToAbout, redirectToAdminUsers, redirectToDonate, redirectToMetrics, redirectToSettings } from "@/app/redirect/thunks"
import { useAppDispatch, useAppSelector } from "@/app/store"
import type { ViewMode } from "@/app/types"
import { setFontSizePercentage, setViewMode } from "@/app/user/slice"
import { reloadProfile } from "@/app/user/thunks"
import { useNow } from "@/hooks/useNow"
interface ProfileMenuProps {
control: React.ReactElement
}
const ProfileMenuControlItem = ({ icon, label }: { icon: ReactNode; label: ReactNode }) => {
return (
{icon}
{label}
)
}
const iconSize = 16
interface ColorSchemeControlItem extends SegmentedControlItem {
value: MantineColorScheme
}
const colorSchemeData: ColorSchemeControlItem[] = [
{
value: "light",
label: } label={Light} />,
},
{
value: "dark",
label: } label={Dark} />,
},
{
value: "auto",
label: } label={System} />,
},
]
interface ViewModeControlItem extends SegmentedControlItem {
value: ViewMode
}
const viewModeData: ViewModeControlItem[] = [
{
value: "title",
label: } label={Compact} />,
},
{
value: "cozy",
label: } label={Cozy} />,
},
{
value: "detailed",
label: } label={Detailed} />,
},
{
value: "expanded",
label: } label={Expanded} />,
},
]
export function ProfileMenu(props: Readonly) {
const [opened, setOpened] = useState(false)
// close profile menu on scroll
useEffect(() => {
const listener = throttle(100, () => setOpened(false))
window.addEventListener("scroll", listener)
return () => window.removeEventListener("scroll", listener)
}, [])
const now = useNow()
const profile = useAppSelector(state => state.user.profile)
const admin = useAppSelector(state => state.user.profile?.admin)
const viewMode = useAppSelector(state => state.user.localSettings.viewMode)
const forceRefreshCooldownDuration = useAppSelector(state => state.server.serverInfos?.forceRefreshCooldownDuration)
const fontSizePercentage = useAppSelector(state => state.user.localSettings.fontSizePercentage)
const dispatch = useAppDispatch()
const { colorScheme, setColorScheme } = useMantineColorScheme()
const nextAvailableForceRefresh = profile?.lastForceRefresh
? profile.lastForceRefresh + (forceRefreshCooldownDuration ?? 0)
: now.getTime()
const forceRefreshEnabled = nextAvailableForceRefresh <= now.getTime()
const logout = () => {
window.location.href = "logout"
}
return (
)
}