Files

121 lines
4.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { getOverLayObj } from "@/components/lib/func/mapUtil";
import { Card } from "@common/business_components/ui/chart/lib/card";
import { useEffect, useRef, useState } from "react";
import WrOverLayTable from "./WrOverLayTable/WrOverLayTable";
import WrOverLayRuse from "./WrOverLayRuse/WrOverLayRuse";
import Draggable from "react-draggable";
export default function WrOverLay({
title,
data,
code,
}: {
title: string;
data: any;
code: any;
}) {
const [newOverLayData, setNewOverLayData] = useState<Record<string, string>>(
{}
);
const [typeTitle, setTypeTitle] = useState<string>("");
const [pos, setPos] = useState<{ x: number; y: number }>({ x: 0, y: 0 });
const [open, setOpen] = useState(true);
const bubbleLabel = title?.[0] ?? "•";
const dragRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
if (title !== "하수재이용시설" && data) {
const overRayData = getOverLayObj(title, data, code);
setTypeTitle(overRayData?.title ?? "");
setNewOverLayData(overRayData.newObj ?? {});
}
}, [title, data]);
return (
<>
{open ? (
<Draggable
position={pos}
nodeRef={dragRef}
onDrag={(e, data) => setPos({ x: data.x, y: data.y })}
>
<div ref={dragRef}>
<Card className="relative w-[320px] overflow-hidden rounded-xl border border-border bg-card p-0 text-card-foreground shadow-md">
{/* 헤더: 제목 바 + 우측 정렬 버튼 (절대배치 제거, 타이틀 중앙 정렬 유지) */}
<div className="bg-primary py-2 text-xl font-semibold text-primary-foreground">
<div className="grid grid-cols-[auto_1fr_auto] items-center gap-2 px-2">
{/* 좌측 스페이서: 우측 버튼과 동일한 너비를 차지하도록 동일 버튼을 투명하게 복제 */}
<div className="opacity-0 pointer-events-none">
<div className="flex gap-2">
<button
onClick={() => setPos({ x: 0, y: 0 })}
className="bg-secondary p-2 text-secondary-foreground shadow transition hover:bg-accent hover:text-accent-foreground hover:shadow-md"
title="원위치"
>
↺
</button>
<button
onClick={() => setOpen(false)}
aria-label="축소"
title="축소"
className="bg-secondary p-2 text-secondary-foreground shadow transition hover:bg-accent hover:text-accent-foreground hover:shadow-md"
>
–
</button>
</div>
</div>
{/* 중앙 타이틀 */}
<div className="text-2xl text-center truncate">
{title === "하수재이용시설" ? title: typeTitle + "(" + title + ")"}
</div>
{/* 우측 실제 버튼 영역 */}
<div className="flex gap-2 justify-end">
<button
onClick={() => setPos({ x: 0, y: 0 })}
className="bg-secondary p-2 text-secondary-foreground shadow transition hover:bg-accent hover:text-accent-foreground hover:shadow-md"
title="원위치"
>
↺
</button>
<button
onClick={() => setOpen(false)}
aria-label="축소"
title="축소"
className="bg-secondary p-2 text-secondary-foreground shadow transition hover:bg-accent hover:text-accent-foreground hover:shadow-md"
>
–
</button>
</div>
</div>
</div>
{/* 본문 */}
{title === "하수재이용시설" ? (
<WrOverLayRuse overLayData={data ?? []} />
) : (
<WrOverLayTable overLayData={newOverLayData} />
)}
</Card>
</div>
</Draggable>
) : (
<button
onClick={() => setOpen(true)}
aria-label="확장"
title={title || "정보 보기"}
className="flex h-[30px] w-[30px] items-center justify-center border border-border bg-popover font-bold text-popover-foreground shadow"
>
{bubbleLabel}
</button>
)}
</>
);
}