"use client"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { HomeSmall } from "components/Icons/HomeSmall"; import { ActionButton } from "./ActionButton"; import { useIdentityData } from "components/IdentityProvider"; import { ReaderUnreadSmall } from "components/Icons/ReaderSmall"; import { NotificationsReadSmall, NotificationsUnreadSmall, } from "components/Icons/NotificationSmall"; import { SpeedyLink } from "components/SpeedyLink"; import { WriterSmall } from "components/Icons/WriterSmall"; export type navPages = | "home" | "reader" | "pub" | "notifications" | "looseleafs" | "tag" | "profile" | "discover"; function useIsActive(href: string) { let pathname = usePathname(); return pathname === href || pathname.startsWith(href + "/"); } export const WRITER_PATHS = ["/home", "/looseleafs", "/notifications"] as const; export function useIsOnWriterPage() { let pathname = usePathname(); return WRITER_PATHS.some((p) => pathname.startsWith(p)); } export const HomeButton = (props: { className?: string }) => { let current = useIsActive("/home"); return ( } label="Home" active={current} className={`w-full! ${props.className}`} /> ); }; export const WriterButton = () => { let current = useIsOnWriterPage(); return ( } label="Write" active={current} /> ); }; export const ReaderButton = (props: { subs: boolean }) => { let current = useIsActive("/reader"); return ( } label="Read" active={current} /> ); }; export function NotificationButton() { let { identity } = useIdentityData(); let unreads = identity?.notifications[0]?.count; let pathname = usePathname(); let searchParams = useSearchParams(); let router = useRouter(); let isOnPage = pathname === "/notifications" || pathname.startsWith("/notifications/"); let isOpen = searchParams.get("notifications") === "open"; let active = isOnPage || isOpen; function handleClick() { if (isOnPage || isOpen) return; let params = new URLSearchParams(searchParams.toString()); params.set("notifications", "open"); router.push(`${pathname}?${params.toString()}`, { scroll: false }); } return ( ) : ( ) } label={ unreads ? ( Notifications {unreads} ) : ( "Notifications" ) } active={active} className={unreads ? "text-accent-contrast! font-bold w-full!" : "w-full"} /> ); }