242 lines
8.9 KiB
TypeScript
242 lines
8.9 KiB
TypeScript
"use client";
|
|
|
|
import Image from "next/image";
|
|
import Link from "next/link";
|
|
import { useState } from "react";
|
|
import { usePathname } from "next/navigation";
|
|
import type { LoginUser } from "@kri/common";
|
|
import { cn } from "@/lib/utils";
|
|
import { kartMenus } from "@/config/menu.data";
|
|
import type { KartMenuItem } from "@/config/menu.type";
|
|
import { ProjectSelector } from "@/components/project/ProjectSelector";
|
|
import { useProjectTitle } from "@/query/project";
|
|
import { useProjectParamsMigration, useSelectedProject } from "@/hooks/useSelectedProject";
|
|
import { projectPagePath } from "@/lib/report-navigation";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { useMutation } from "@tanstack/react-query";
|
|
import { nextApi } from "@/lib/http-client";
|
|
import { apiError } from "@kri/common/http";
|
|
import { isExternalUser, isShareLink, REPORT_MEMBER_ROLE } from "@/types/share";
|
|
|
|
interface AppHeaderProps {
|
|
user: LoginUser;
|
|
}
|
|
|
|
/** Legacy KART Header.js: fixed GNB (64px) + title bar (80px). */
|
|
export function AppHeader({ user }: AppHeaderProps) {
|
|
const pathname = usePathname();
|
|
const { auth } = useAuth();
|
|
const external = isExternalUser(user);
|
|
const { project } = useSelectedProject();
|
|
useProjectParamsMigration();
|
|
const title = useProjectTitle(project);
|
|
const [mobileOpen, setMobileOpen] = useState(false);
|
|
const menus = project && !(external && auth.external?.pageOnly)
|
|
? kartMenus.filter((menu) => !menu.visible || menu.visible(user))
|
|
: [];
|
|
const projectTitle = project ? title : "Project Overview TOP 6";
|
|
const canCopyUrl = !!project && user.userRole < REPORT_MEMBER_ROLE;
|
|
const copy = useMutation({
|
|
mutationFn: async (mode: "P" | "U") => {
|
|
if (!project) return;
|
|
const url = new URL(window.location.href);
|
|
url.searchParams.delete("projectType");
|
|
url.searchParams.delete("pkey");
|
|
const link = await nextApi.post<unknown>(`/share/${project.projectType}/${encodeURIComponent(project.pkey)}`, {
|
|
realUrl: pathname + url.search + url.hash, mode,
|
|
});
|
|
if (!isShareLink(link)) throw new Error("공유 링크 응답을 확인해 주세요.");
|
|
await navigator.clipboard.writeText(link.url);
|
|
},
|
|
onSuccess: () => window.alert("주소가 복사되었습니다."),
|
|
onError: (error) => window.alert(apiError(error, { fallbackMessage: "주소를 복사하지 못했습니다." }).message),
|
|
});
|
|
|
|
return (
|
|
<header className="sticky top-0 z-40">
|
|
<nav className="bg-kart-bg px-[1.0416vw]">
|
|
<div className="flex h-16 items-center justify-between">
|
|
{external ? <h1><KartLogo /></h1> : <Link href="/" replace><h1><KartLogo /></h1></Link>}
|
|
<button
|
|
type="button"
|
|
aria-label="메뉴 열기"
|
|
aria-expanded={mobileOpen}
|
|
className="min-[1301px]:hidden"
|
|
onClick={() => setMobileOpen((open) => !open)}
|
|
>
|
|
<Image
|
|
src="/images/kart/bx_menu.svg"
|
|
alt=""
|
|
width={24}
|
|
height={24}
|
|
unoptimized
|
|
/>
|
|
</button>
|
|
<div className="hidden h-full items-center gap-[2.604vw] min-[1301px]:flex">
|
|
{pathname === "/" ? (
|
|
<p className="text-[15px] font-bold">
|
|
안녕하세요! {user.userName}님
|
|
</p>
|
|
) : null}
|
|
{project ? (
|
|
<ul className="flex h-full items-center">
|
|
{menus.map((menu) => (
|
|
<li key={menu.id} className="relative">
|
|
<GnbLink menu={menu} active={pathname === menu.path} />
|
|
</li>
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
{!external ? <Link
|
|
href="/logout"
|
|
replace
|
|
className="inline-flex h-7 w-[113px] items-center justify-center rounded-[52px] bg-kart-green text-sm leading-[17px] font-bold text-white"
|
|
>
|
|
Logout
|
|
</Link> : null}
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
{mobileOpen ? (
|
|
<>
|
|
<ul className="fixed top-16 right-0 z-50 w-full bg-kart-bg px-[3.076vw] py-[4.102vw] text-right min-[1301px]:hidden">
|
|
{menus.map((menu) => (
|
|
<li key={menu.id} className="mb-3">
|
|
{menu.ready && project ? (
|
|
<Link
|
|
href={projectPagePath(menu.path)}
|
|
className={cn(
|
|
"text-lg font-bold text-[#9e9e9e] transition-colors hover:text-kart-green",
|
|
pathname === menu.path && "text-kart-green",
|
|
)}
|
|
onClick={() => setMobileOpen(false)}
|
|
>
|
|
{menu.label}
|
|
</Link>
|
|
) : (
|
|
<span
|
|
aria-disabled="true"
|
|
title="준비 중"
|
|
className="text-lg font-bold text-[#9e9e9e]"
|
|
>
|
|
{menu.label}
|
|
</span>
|
|
)}
|
|
</li>
|
|
))}
|
|
{!external ? <li className="mb-3">
|
|
<Link
|
|
href="/logout"
|
|
replace
|
|
className="text-lg font-bold text-[#9e9e9e] hover:text-kart-green"
|
|
>
|
|
Logout
|
|
</Link>
|
|
</li> : null}
|
|
</ul>
|
|
<div
|
|
aria-hidden="true"
|
|
className="fixed inset-x-0 top-16 bottom-0 z-30 bg-black/20 min-[1301px]:hidden"
|
|
onClick={() => setMobileOpen(false)}
|
|
/>
|
|
</>
|
|
) : null}
|
|
<div className="relative flex h-20 flex-col items-start border-y border-kart-line bg-white px-[1.0416vw] pt-2 max-[1300px]:h-[82px]">
|
|
<div className="mt-[2.5px] w-[400px] max-[1300px]:w-full">
|
|
{external ? <div className="h-8" /> : <ProjectSelector />}
|
|
</div>
|
|
<div className="mt-[2.5px] w-full min-w-0 max-[1300px]:mt-1.5">
|
|
{/* One line; a long title ends with "..." and shows in full on hover. */}
|
|
<h2
|
|
title={projectTitle || undefined}
|
|
className="truncate text-xl leading-[1.28571] font-bold text-kart-title max-[1300px]:text-lg"
|
|
>
|
|
{projectTitle}
|
|
</h2>
|
|
</div>
|
|
{canCopyUrl ? (
|
|
<div className="absolute top-[23px] right-5 flex max-[1300px]:hidden">
|
|
{(
|
|
[
|
|
["Page Copy", "ic_copy_page.svg"],
|
|
["URL Copy", "ic_copy_url.svg"],
|
|
] as const
|
|
).map(([label, icon]) => (
|
|
<button
|
|
key={label}
|
|
type="button"
|
|
disabled={copy.isPending}
|
|
onClick={() => copy.mutate(label === "Page Copy" ? "P" : "U")}
|
|
className="ml-[15px] inline-block h-7 cursor-pointer rounded-[52px] bg-kart-bg bg-size-[18px_18px] bg-position-[left_12px_center] bg-no-repeat py-1.5 pr-[14px] pl-[34px] text-sm leading-[15px] font-bold text-kart-green"
|
|
style={{ backgroundImage: `url(/images/kart/${icon})` }}
|
|
>
|
|
{label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
</header>
|
|
);
|
|
}
|
|
|
|
function KartLogo() {
|
|
return <Image src="/images/kart/logo_kart.svg" alt="KART" width={135} height={32}
|
|
className="mt-2 h-auto w-[135px]" unoptimized priority />;
|
|
}
|
|
|
|
function GnbLink({ menu, active }: { menu: KartMenuItem; active: boolean }) {
|
|
const content = (
|
|
<>
|
|
<span
|
|
aria-hidden="true"
|
|
className="absolute top-[34%] -left-[17px] size-5 bg-contain bg-left bg-no-repeat"
|
|
style={{
|
|
backgroundImage: `url(/images/kart/ic_gnb${menu.icon}_20${active ? "-1" : ""}.png)`,
|
|
}}
|
|
/>
|
|
<span
|
|
aria-hidden="true"
|
|
className="absolute top-[34%] -left-[17px] hidden size-5 bg-contain bg-left bg-no-repeat group-hover:block"
|
|
style={{
|
|
backgroundImage: `url(/images/kart/ic_gnb${menu.icon}_20-1.png)`,
|
|
}}
|
|
/>
|
|
{menu.label}
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn(
|
|
"absolute right-0 bottom-[14px] -left-[17px] h-1 bg-kart-green",
|
|
active ? "block" : "hidden group-hover:block",
|
|
)}
|
|
/>
|
|
</>
|
|
);
|
|
const className = cn(
|
|
"group relative ml-[3.125vw] block h-16 pl-2 text-center text-sm leading-[64px] font-bold text-[#444] transition-colors",
|
|
active && "text-kart-green",
|
|
);
|
|
if (!menu.ready)
|
|
return (
|
|
<span aria-disabled="true" title="준비 중" className={className}>
|
|
{menu.label}
|
|
<span
|
|
aria-hidden="true"
|
|
className="absolute top-[34%] -left-[17px] size-5 bg-contain bg-left bg-no-repeat"
|
|
style={{
|
|
backgroundImage: `url(/images/kart/ic_gnb${menu.icon}_20.png)`,
|
|
}}
|
|
/>
|
|
</span>
|
|
);
|
|
return (
|
|
<Link
|
|
href={projectPagePath(menu.path)}
|
|
aria-current={active ? "page" : undefined}
|
|
className={cn(className, "hover:text-kart-green")}
|
|
>
|
|
{content}
|
|
</Link>
|
|
);
|
|
}
|