kontenhumas-fe/components/landing-page/navbar.tsx

388 lines
14 KiB
TypeScript
Raw Normal View History

2025-09-16 08:29:07 +00:00
"use client";
import { useState, useEffect } from "react";
import { cn, getCookiesDecrypt } from "@/lib/utils";
import Image from "next/image";
import { Menu, ChevronDown } from "lucide-react";
import { Button } from "../ui/button";
import Cookies from "js-cookie";
import { Card } from "../ui/card";
import { Input } from "../ui/input";
import { usePathname, useRouter } from "next/navigation";
import { Link } from "@/i18n/routing";
import { DynamicLogoTenant } from "./dynamic-logo-tenant";
2025-10-31 16:21:05 +00:00
import { useTranslations } from "next-intl";
import LocalSwitcher from "../partials/header/locale-switcher";
2025-09-16 08:29:07 +00:00
export default function Navbar() {
2025-10-31 16:21:05 +00:00
const t = useTranslations("Navbar");
2025-09-16 08:29:07 +00:00
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [isDropdownOpen, setDropdownOpen] = useState(false);
const [showProfileMenu, setShowProfileMenu] = useState(false);
2025-10-05 05:04:09 +00:00
const [isLoggedIn, setIsLoggedIn] = useState(false);
2025-09-16 08:29:07 +00:00
const pathname = usePathname();
const router = useRouter();
2025-09-16 08:29:07 +00:00
2025-10-31 16:21:05 +00:00
const NAV_ITEMS = [
{ label: t("home"), href: "/" },
{ label: t("forYou"), href: "/for-you" },
{ label: t("following"), href: "/auth" },
{ label: t("publication"), href: "/publikasi" },
{ label: t("schedule"), href: "/schedule" },
];
const PUBLIKASI_SUBMENU = [
{ label: "K/L", href: "/in/publication/kl" },
{ label: "BUMN", href: "/in/publication/bumn" },
{ label: t("localGov"), href: "/in/publication/pemerintah-daerah" },
];
// 🔍 Fungsi cek login
2025-10-05 05:04:09 +00:00
const checkLoginStatus = () => {
const roleId = getCookiesDecrypt("urie");
const fullname = Cookies.get("ufne");
return roleId && fullname ? true : false;
};
useEffect(() => {
setIsLoggedIn(checkLoginStatus());
}, []);
2025-09-16 08:29:07 +00:00
const filteredNavItems = isLoggedIn
2025-10-31 16:21:05 +00:00
? NAV_ITEMS.filter((item) => item.label !== t("following"))
2025-09-16 08:29:07 +00:00
: NAV_ITEMS;
2025-10-05 05:04:09 +00:00
// 🚪 Fungsi logout
2025-09-16 08:29:07 +00:00
const handleLogout = () => {
Object.keys(Cookies.get()).forEach((cookieName) => {
2025-10-05 05:04:09 +00:00
Cookies.remove(cookieName, { path: "/" });
Cookies.remove(cookieName, {
path: "",
domain: window.location.hostname,
});
Cookies.remove(cookieName, {
path: "/",
domain: window.location.hostname,
});
2025-09-16 08:29:07 +00:00
});
2025-10-05 05:04:09 +00:00
setIsLoggedIn(false);
2025-09-16 08:29:07 +00:00
setShowProfileMenu(false);
2025-10-05 05:04:09 +00:00
window.location.href = "/";
2025-09-16 08:29:07 +00:00
};
const username = Cookies.get("username");
const fullname = Cookies.get("ufne");
2025-09-16 08:29:07 +00:00
return (
<header className="relative max-w-[1400px] mx-auto flex items-center justify-between px-4 py-3 border-b bg-white z-50">
<div className="flex flex-row items-center justify-between space-x-4 z-10">
2025-10-31 16:21:05 +00:00
<Menu
className="w-6 h-6 cursor-pointer"
onClick={() => setIsSidebarOpen(true)}
/>
<Link href="/" className="relative w-32 h-20">
2025-09-16 08:29:07 +00:00
<Image
2025-09-25 15:46:47 +00:00
src="/assets/logo1.png"
2025-09-16 08:29:07 +00:00
alt="Logo"
fill
className="object-contain"
/>
2025-10-31 16:21:05 +00:00
</Link>
<DynamicLogoTenant />
2025-09-16 08:29:07 +00:00
</div>
{/* 🌐 NAV MENU */}
2025-09-16 08:29:07 +00:00
<nav className="absolute left-1/2 -translate-x-1/2 hidden md:flex space-x-3 lg:space-x-8 text-sm font-medium">
{filteredNavItems.map((item) => {
const isActive = pathname === item.href;
// 🔹 Pengecekan khusus untuk "Untuk Anda"
const handleClick = (e: React.MouseEvent) => {
2025-10-31 16:21:05 +00:00
if (item.label === t("forYou")) {
e.preventDefault();
if (!checkLoginStatus()) {
router.push("/auth");
} else {
2025-10-19 03:53:16 +00:00
router.push("/in/for-you");
}
}
};
2025-09-16 08:29:07 +00:00
return (
<div key={item.label} className="relative">
2025-10-31 16:21:05 +00:00
{item.label === t("publication") ? (
2025-09-16 08:29:07 +00:00
<>
<button
onClick={() => setDropdownOpen(!isDropdownOpen)}
className={cn(
"relative text-gray-500 hover:text-black transition-colors",
isDropdownOpen ||
pathname.startsWith("/public/publication")
? "text-black"
: ""
)}
>
{item.label}
<span
className={cn(
2025-09-25 15:46:47 +00:00
"absolute -bottom-1 left-1/2 -translate-x-1/2 w-6 h-[3px] bg-red-800 rounded transition-all",
2025-09-16 08:29:07 +00:00
isDropdownOpen ||
pathname.startsWith("/public/publication")
? "opacity-100"
: "opacity-0"
)}
/>
</button>
{isDropdownOpen && (
<div className="absolute top-full mt-2 w-48 bg-white border rounded shadow z-50">
{PUBLIKASI_SUBMENU.map((sub) => (
<Link
key={sub.label}
href={sub.href}
className="block px-4 py-2 text-sm hover:bg-gray-100 text-gray-700"
>
{sub.label}
</Link>
))}
</div>
)}
</>
) : (
<Link
href={item.href}
onClick={handleClick}
2025-09-16 08:29:07 +00:00
className={cn(
"relative text-gray-500 hover:text-black transition-colors",
isActive && "text-black"
)}
>
{item.label}
{isActive && (
2025-09-25 15:46:47 +00:00
<span className="absolute -bottom-1 left-1/2 -translate-x-1/2 w-6 h-[3px] bg-red-800 rounded" />
2025-09-16 08:29:07 +00:00
)}
</Link>
)}
</div>
);
})}
</nav>
{/* 🔹 PROFILE / LOGIN SECTION */}
2025-09-16 08:29:07 +00:00
<nav className="hidden md:flex items-center gap-3 z-10 relative">
{!isLoggedIn ? (
<>
<Link href="/auth/register">
2025-09-25 15:46:47 +00:00
<Button className="bg-transparent border text-black hover:bg-red-600 hover:text-white">
2025-10-31 16:21:05 +00:00
{t("register")}{" "}
2025-09-16 08:29:07 +00:00
</Button>
</Link>
<Link href="/auth">
2025-10-31 16:21:05 +00:00
<Button className="bg-red-700 text-white">{t("login")}</Button>
2025-09-16 08:29:07 +00:00
</Link>
</>
) : (
<div className="relative">
<button
onClick={() => setShowProfileMenu((prev) => !prev)}
2026-01-07 16:03:38 +00:00
className="flex items-center gap-2 border-2 py-1 px-3 rounded-lg hover:bg-gray-50 cursor-pointer"
2025-09-16 08:29:07 +00:00
>
<div className="w-9 h-9 rounded-full overflow-hidden border">
<Image
2025-10-05 05:04:09 +00:00
src="/avatar-profile.png"
alt={username || "User avatar"}
2025-09-16 08:29:07 +00:00
width={36}
height={36}
className="object-cover"
/>
</div>
<span className="text-sm font-medium text-gray-800">
{fullname}
2025-09-16 08:29:07 +00:00
</span>
<ChevronDown className="w-4 h-4 text-gray-600" />
</button>
{showProfileMenu && (
2025-12-11 04:45:26 +00:00
<div className="absolute right-0 mt-2 w-40 bg-white border rounded shadow z-50 ">
<Link
href="/admin/dashboard"
className="flex flex-row items-center text-left px-4 py-2 hover:bg-gray-100 text-gray-700"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
viewBox="0 0 24 24"
>
<path
fill="currentColor"
d="M14 21a1 1 0 0 1-1-1v-8a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v8a1 1 0 0 1-1 1zM4 13a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v8a1 1 0 0 1-1 1zm5-2V5H5v6zM4 21a1 1 0 0 1-1-1v-4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v4a1 1 0 0 1-1 1zm1-2h4v-2H5zm10 0h4v-6h-4zM13 4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v4a1 1 0 0 1-1 1h-6a1 1 0 0 1-1-1zm2 1v2h4V5z"
/>
</svg>
&nbsp;Dashboard
</Link>
2025-09-16 08:29:07 +00:00
<button
onClick={handleLogout}
2025-12-11 04:45:26 +00:00
className="w-full flex flex-row text-left px-4 py-2 hover:bg-gray-100 text-gray-700 cursor-pointer"
2025-09-16 08:29:07 +00:00
>
2025-12-11 04:45:26 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
viewBox="0 0 24 24"
>
<g
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeWidth="1.5"
>
<path
strokeLinejoin="round"
d="M13.477 21.245H8.34a4.92 4.92 0 0 1-5.136-4.623V7.378A4.92 4.92 0 0 1 8.34 2.755h5.136"
/>
<path strokeMiterlimit="10" d="M20.795 12H7.442" />
<path
strokeLinejoin="round"
d="m16.083 17.136l4.404-4.404a1.04 1.04 0 0 0 0-1.464l-4.404-4.404"
/>
</g>
</svg>
&nbsp;{t("logout")}
2025-09-16 08:29:07 +00:00
</button>
</div>
)}
</div>
)}
</nav>
{/* 📱 SIDEBAR MOBILE */}
2025-09-16 08:29:07 +00:00
{isSidebarOpen && (
<div className="fixed inset-0 z-50 flex">
<div className="w-80 bg-white p-6 space-y-6 shadow-lg relative h-full overflow-y-auto">
2025-09-16 08:29:07 +00:00
<button
onClick={() => setIsSidebarOpen(false)}
className="absolute top-4 right-4 text-gray-600"
>
</button>
<div className="mt-10">
<h3 className="text-[16px] font-bold text-gray-700 mb-2">
2025-10-31 16:21:05 +00:00
{t("language")}
2025-09-16 08:29:07 +00:00
</h3>
2025-10-31 16:21:05 +00:00
{/* button language */}
<div className={`relative text-left border w-fit rounded-lg`}>
<LocalSwitcher />
</div>
{/* <div className="space-y-5 ml-3">
2025-09-16 08:29:07 +00:00
<button className="flex items-center gap-2 text-sm text-gray-800">
<Image src={"/Flag.svg"} width={24} height={24} alt="flag" />
English
</button>
<button className="flex items-center gap-2 text-sm text-gray-800">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 36 36"
>
<path
fill="#dc1f26"
d="M32 5H4a4 4 0 0 0-4 4v9h36V9a4 4 0 0 0-4-4"
/>
<path
fill="#eee"
d="M36 27a4 4 0 0 1-4 4H4a4 4 0 0 1-4-4v-9h36z"
/>
</svg>{" "}
Bahasa Indonesia
</button>
2025-10-31 16:21:05 +00:00
</div> */}
2025-09-16 08:29:07 +00:00
</div>
<div>
<h3 className="text-[16px] font-bold text-gray-700 mb-2">
2025-10-31 16:21:05 +00:00
{t("features")}
2025-09-16 08:29:07 +00:00
</h3>
<div className="space-y-5 ml-3">
{NAV_ITEMS.map((item) => (
<button
2025-09-16 08:29:07 +00:00
key={item.label}
onClick={() => {
2025-10-31 16:21:05 +00:00
if (item.label === t("forYou")) {
if (!checkLoginStatus()) {
router.push("/auth");
} else {
2025-10-19 03:53:16 +00:00
router.push("/for-you");
}
} else {
router.push(item.href);
}
setIsSidebarOpen(false);
}}
className="block text-[15px] text-gray-800 text-left w-full"
2025-09-16 08:29:07 +00:00
>
{item.label}
</button>
2025-09-16 08:29:07 +00:00
))}
</div>
</div>
<div className="space-y-5 text-[16px] font-bold">
<Link href="/about" className="block text-black">
2025-10-31 16:21:05 +00:00
{t("about")}
2025-09-16 08:29:07 +00:00
</Link>
<Link href="/advertising" className="block text-black">
2025-10-31 16:21:05 +00:00
{t("advertising")}
2025-09-16 08:29:07 +00:00
</Link>
<Link href="/contact" className="block text-black">
2025-10-31 16:21:05 +00:00
{t("contact")}
2025-09-16 08:29:07 +00:00
</Link>
2025-09-16 08:29:07 +00:00
{!isLoggedIn ? (
<>
<Link href="/auth" className="block text-lg text-gray-800">
2025-10-31 16:21:05 +00:00
{t("login")}
2025-09-16 08:29:07 +00:00
</Link>
<Link
href="/auth/register"
className="block text-lg text-gray-800"
>
2025-10-31 16:21:05 +00:00
{t("register")}
2025-09-16 08:29:07 +00:00
</Link>
</>
) : (
<button
onClick={handleLogout}
className="block text-left w-full text-lg text-red-600 hover:underline"
>
2025-10-31 16:21:05 +00:00
{t("logout")}
2025-09-16 08:29:07 +00:00
</button>
)}
</div>
2025-09-16 08:29:07 +00:00
<Card className="rounded-none p-4">
2025-10-31 16:21:05 +00:00
<h2 className="text-[#C6A455] text-center text-lg font-semibold mb-2">
{t("subscribeTitle")}
2025-09-16 08:29:07 +00:00
</h2>
2025-10-31 16:21:05 +00:00
<Input type="email" placeholder={t("subscribePlaceholder")} />
2025-12-11 04:45:26 +00:00
<Button className="bg-[#C6A455] mt-2">
{t("subscribeButton")}
</Button>
2025-09-16 08:29:07 +00:00
</Card>
</div>
<div
className="flex-1 bg-black/50"
onClick={() => setIsSidebarOpen(false)}
/>
</div>
)}
</header>
);
}