import "./RootTabs.css"; import { useState, ReactNode, useEffect } from "react"; import { useLocation, useNavigate } from "react-router-dom"; import { Menu, MenuItem } from "@szhsin/react-menu"; import { FormattedMessage } from "react-intl"; import useLogin from "Hooks/useLogin"; import Icon from "Icons/Icon"; export type RootTab = | "following" | "conversations" | "trending-notes" | "trending-people" | "suggested" | "tags" | "global"; export function RootTabs({ base }: { base?: string }) { const navigate = useNavigate(); const location = useLocation(); const { publicKey: pubKey, tags } = useLogin(); const [rootType, setRootType] = useState("following"); const menuItems = [ { tab: "following", path: `${base}/notes`, show: Boolean(pubKey), element: ( <> ), }, { tab: "trending-notes", path: `${base}/trending/notes`, show: true, element: ( <> ), }, { tab: "conversations", path: `${base}/conversations`, show: Boolean(pubKey), element: ( <> ), }, { tab: "trending-people", path: `${base}/trending/people`, show: true, element: ( <> ), }, { tab: "suggested", path: `${base}/suggested`, show: Boolean(pubKey), element: ( <> ), }, { tab: "global", path: `${base}/global`, show: true, element: ( <> ), }, ] as Array<{ tab: RootTab; path: string; show: boolean; element: ReactNode; }>; useEffect(() => { const currentTab = menuItems.find(a => a.path === location.pathname)?.tab; if (currentTab) { setRootType(currentTab); } }, [location]); function currentMenuItem() { if (location.pathname.startsWith(`${base}/t/`)) { return ( <> {location.pathname.split("/").slice(-1)} ); } return menuItems.find(a => a.tab === rootType)?.element; } return (
{currentMenuItem()} } align="center" menuClassName={() => "ctx-menu"}>
{menuItems .filter(a => a.show) .map(a => ( { navigate(a.path); }}> {a.element} ))} {tags.item.map(v => ( { navigate(`${base}/t/${v}`); }}> {v} ))}
); }