import classNames from "classnames";
import { useEffect } from "react";
import { NavLink, useLocation } from "react-router-dom";
import useCurrentUser from "@/hooks/useCurrentUser";
import { useLayoutStore } from "@/store/module";
import { useInboxStore } from "@/store/v1";
import { Inbox_Status } from "@/types/proto/api/v2/inbox_service";
import { useTranslate } from "@/utils/i18n";
import { resolution } from "@/utils/layout";
import Icon from "./Icon";
import UserBanner from "./UserBanner";
interface NavLinkItem {
id: string;
path: string;
title: string;
icon: React.ReactNode;
}
const Header = () => {
const t = useTranslate();
const location = useLocation();
const layoutStore = useLayoutStore();
const user = useCurrentUser();
const inboxStore = useInboxStore();
const showHeader = layoutStore.state.showHeader;
const hasUnreadInbox = inboxStore.inboxes.some((inbox) => inbox.status === Inbox_Status.UNREAD);
useEffect(() => {
if (!user) {
return;
}
inboxStore.fetchInboxes();
// Fetch inboxes every 5 minutes.
const timer = setInterval(async () => {
await inboxStore.fetchInboxes();
}, 1000 * 60 * 5);
return () => {
clearInterval(timer);
};
}, []);
useEffect(() => {
const handleWindowResize = () => {
if (window.innerWidth < resolution.sm) {
layoutStore.setHeaderStatus(false);
} else {
layoutStore.setHeaderStatus(true);
}
};
handleWindowResize();
window.addEventListener("resize", handleWindowResize);
return () => {
window.removeEventListener("resize", handleWindowResize);
};
}, [location]);
const homeNavLink: NavLinkItem = {
id: "header-home",
path: "/",
title: t("common.home"),
icon: