407 lines
10 KiB
TypeScript
407 lines
10 KiB
TypeScript
import type { CSSProperties, ReactNode, RefObject } from "react";
|
|
import { useEffect, useId, useRef, useState } from "react";
|
|
import { createPortal } from "react-dom";
|
|
import type { DdragonManifest } from "../api/types";
|
|
import {
|
|
itemDetails,
|
|
itemDisplayName,
|
|
itemIconUrl,
|
|
runeDetails,
|
|
runeIconUrl,
|
|
runeStyleDetails,
|
|
runeStyleIconUrl,
|
|
safeDdragonDescriptionHtml,
|
|
summonerSpellDetails,
|
|
summonerSpellIconUrl,
|
|
} from "../lib/ddragon";
|
|
|
|
type TooltipPosition = {
|
|
left: number;
|
|
top: number;
|
|
width: number;
|
|
placement: "top" | "bottom";
|
|
};
|
|
|
|
type ItemIconProps = {
|
|
itemId: number;
|
|
manifest: DdragonManifest | null | undefined;
|
|
className?: string;
|
|
imageClassName?: string;
|
|
trinket?: boolean;
|
|
};
|
|
|
|
type TooltipIconProps = {
|
|
active: boolean;
|
|
label: string;
|
|
emptyLabel: string;
|
|
icon: string | null;
|
|
className: string;
|
|
imageClassName: string;
|
|
slotClassName: string;
|
|
children: ReactNode;
|
|
};
|
|
|
|
type ManifestIconProps = {
|
|
manifest: DdragonManifest | null | undefined;
|
|
className?: string;
|
|
imageClassName?: string;
|
|
slotClassName?: string;
|
|
};
|
|
|
|
const VIEWPORT_MARGIN = 8;
|
|
const TOOLTIP_GAP = 8;
|
|
const TOOLTIP_WIDTH = 320;
|
|
|
|
export function ItemIcon({
|
|
itemId,
|
|
manifest,
|
|
className = "h-6 w-6",
|
|
imageClassName = "scale-[1.18]",
|
|
trinket = false,
|
|
}: ItemIconProps) {
|
|
const icon = itemIconUrl(manifest, itemId);
|
|
const hasItem = itemId > 0;
|
|
const itemName = itemDisplayName(manifest, itemId) ?? `아이템 ${itemId}`;
|
|
const slotTone = trinket
|
|
? "border-primary/25 bg-base-content/[0.04]"
|
|
: "border-base-content/5 bg-base-content/8";
|
|
|
|
return (
|
|
<TooltipIcon
|
|
active={hasItem}
|
|
label={itemName}
|
|
emptyLabel="빈 아이템 슬롯"
|
|
icon={icon}
|
|
className={className}
|
|
imageClassName={imageClassName}
|
|
slotClassName={`border ${slotTone}`}
|
|
>
|
|
<ItemTooltipContent itemId={itemId} manifest={manifest} />
|
|
</TooltipIcon>
|
|
);
|
|
}
|
|
|
|
export function SummonerSpellIcon({
|
|
spellId,
|
|
manifest,
|
|
className = "h-5 w-5",
|
|
imageClassName = "",
|
|
slotClassName = "border border-base-content/5 bg-base-content/10",
|
|
}: ManifestIconProps & { spellId: number }) {
|
|
const spell = summonerSpellDetails(manifest, spellId);
|
|
const spellName = spell?.name ?? `스펠 ${spellId}`;
|
|
|
|
return (
|
|
<TooltipIcon
|
|
active={spellId > 0}
|
|
label={spellName}
|
|
emptyLabel="빈 스펠 슬롯"
|
|
icon={summonerSpellIconUrl(manifest, spellId)}
|
|
className={className}
|
|
imageClassName={imageClassName}
|
|
slotClassName={slotClassName}
|
|
>
|
|
<SummonerSpellTooltipContent spellId={spellId} manifest={manifest} />
|
|
</TooltipIcon>
|
|
);
|
|
}
|
|
|
|
export function RuneIcon({
|
|
runeId,
|
|
manifest,
|
|
className = "h-5 w-5",
|
|
imageClassName = "",
|
|
slotClassName = "border border-base-content/5 bg-base-content/10",
|
|
}: ManifestIconProps & { runeId: number | undefined }) {
|
|
const rune = runeDetails(manifest, runeId);
|
|
const runeName = rune?.name ?? (runeId ? `룬 ${runeId}` : "룬");
|
|
|
|
return (
|
|
<TooltipIcon
|
|
active={Boolean(runeId)}
|
|
label={runeName}
|
|
emptyLabel="빈 룬 슬롯"
|
|
icon={runeIconUrl(manifest, runeId)}
|
|
className={className}
|
|
imageClassName={imageClassName}
|
|
slotClassName={slotClassName}
|
|
>
|
|
<RuneTooltipContent runeId={runeId} manifest={manifest} />
|
|
</TooltipIcon>
|
|
);
|
|
}
|
|
|
|
export function RuneStyleIcon({
|
|
styleId,
|
|
manifest,
|
|
className = "h-5 w-5",
|
|
imageClassName = "",
|
|
slotClassName = "border border-base-content/5 bg-base-content/10",
|
|
}: ManifestIconProps & { styleId: number | undefined }) {
|
|
const style = runeStyleDetails(manifest, styleId);
|
|
const styleName = style?.name ?? (styleId ? `룬 ${styleId}` : "룬");
|
|
|
|
return (
|
|
<TooltipIcon
|
|
active={Boolean(styleId)}
|
|
label={styleName}
|
|
emptyLabel="빈 룬 슬롯"
|
|
icon={runeStyleIconUrl(manifest, styleId)}
|
|
className={className}
|
|
imageClassName={imageClassName}
|
|
slotClassName={slotClassName}
|
|
>
|
|
<RuneStyleTooltipContent styleId={styleId} manifest={manifest} />
|
|
</TooltipIcon>
|
|
);
|
|
}
|
|
|
|
function TooltipIcon({
|
|
active,
|
|
label,
|
|
emptyLabel,
|
|
icon,
|
|
className,
|
|
imageClassName,
|
|
slotClassName,
|
|
children,
|
|
}: TooltipIconProps) {
|
|
const anchorRef = useRef<HTMLSpanElement>(null);
|
|
const tooltipId = useId();
|
|
const [open, setOpen] = useState(false);
|
|
|
|
return (
|
|
<span
|
|
ref={anchorRef}
|
|
className={`relative inline-flex shrink-0 ${className}`}
|
|
role={active ? "img" : undefined}
|
|
tabIndex={active ? 0 : undefined}
|
|
aria-label={active ? label : emptyLabel}
|
|
aria-describedby={active && open ? tooltipId : undefined}
|
|
onBlur={() => setOpen(false)}
|
|
onFocus={() => active && setOpen(true)}
|
|
onMouseEnter={() => active && setOpen(true)}
|
|
onMouseLeave={() => setOpen(false)}
|
|
>
|
|
<span
|
|
className={`block h-full w-full overflow-hidden ${slotClassName}`}
|
|
aria-hidden="true"
|
|
>
|
|
{icon ? (
|
|
<img
|
|
src={icon}
|
|
alt=""
|
|
className={`h-full w-full object-cover ${imageClassName}`}
|
|
loading="lazy"
|
|
/>
|
|
) : null}
|
|
</span>
|
|
{active && open ? (
|
|
<TooltipPortal id={tooltipId} anchorRef={anchorRef}>
|
|
{children}
|
|
</TooltipPortal>
|
|
) : null}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function TooltipPortal({
|
|
id,
|
|
anchorRef,
|
|
children,
|
|
}: {
|
|
id: string;
|
|
anchorRef: RefObject<HTMLElement | null>;
|
|
children: ReactNode;
|
|
}) {
|
|
const [position, setPosition] = useState<TooltipPosition | null>(null);
|
|
|
|
useEffect(() => {
|
|
function updatePosition() {
|
|
const anchor = anchorRef.current;
|
|
if (!anchor) return;
|
|
|
|
const rect = anchor.getBoundingClientRect();
|
|
const width = Math.min(
|
|
TOOLTIP_WIDTH,
|
|
window.innerWidth - VIEWPORT_MARGIN * 2,
|
|
);
|
|
const left = clamp(
|
|
rect.left + rect.width / 2 - width / 2,
|
|
VIEWPORT_MARGIN,
|
|
window.innerWidth - width - VIEWPORT_MARGIN,
|
|
);
|
|
const placement = rect.top > 260 ? "top" : "bottom";
|
|
setPosition({
|
|
left,
|
|
top:
|
|
placement === "top"
|
|
? rect.top - TOOLTIP_GAP
|
|
: rect.bottom + TOOLTIP_GAP,
|
|
width,
|
|
placement,
|
|
});
|
|
}
|
|
|
|
updatePosition();
|
|
window.addEventListener("resize", updatePosition);
|
|
window.addEventListener("scroll", updatePosition, true);
|
|
return () => {
|
|
window.removeEventListener("resize", updatePosition);
|
|
window.removeEventListener("scroll", updatePosition, true);
|
|
};
|
|
}, [anchorRef]);
|
|
|
|
if (typeof document === "undefined" || !position) return null;
|
|
|
|
const style: CSSProperties = {
|
|
left: position.left,
|
|
top: position.top,
|
|
width: position.width,
|
|
transform:
|
|
position.placement === "top" ? "translateY(-100%)" : undefined,
|
|
};
|
|
|
|
return createPortal(
|
|
<div
|
|
id={id}
|
|
role="tooltip"
|
|
style={style}
|
|
className="pointer-events-none fixed z-[1000] border border-base-content/20 bg-black px-3 py-3 text-xs leading-relaxed text-white shadow-2xl"
|
|
>
|
|
{children}
|
|
</div>,
|
|
document.body,
|
|
);
|
|
}
|
|
|
|
function SummonerSpellTooltipContent({
|
|
spellId,
|
|
manifest,
|
|
}: {
|
|
spellId: number;
|
|
manifest: DdragonManifest | null | undefined;
|
|
}) {
|
|
const spell = summonerSpellDetails(manifest, spellId);
|
|
const descriptionHtml = spell?.description
|
|
? safeDdragonDescriptionHtml(spell.description)
|
|
: "";
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="font-black text-[#f0d878]">
|
|
{spell?.name ?? `스펠 ${spellId}`}
|
|
</div>
|
|
{descriptionHtml ? (
|
|
<div
|
|
className="text-white/80 [&_br]:block [&_br]:content-['']"
|
|
dangerouslySetInnerHTML={{ __html: descriptionHtml }}
|
|
/>
|
|
) : (
|
|
<div className="text-white/55">
|
|
스펠 상세 정보를 불러오는 중입니다.
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function RuneTooltipContent({
|
|
runeId,
|
|
manifest,
|
|
}: {
|
|
runeId: number | undefined;
|
|
manifest: DdragonManifest | null | undefined;
|
|
}) {
|
|
const rune = runeDetails(manifest, runeId);
|
|
const descriptionHtml = rune?.longDesc
|
|
? safeDdragonDescriptionHtml(rune.longDesc)
|
|
: "";
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="font-black text-[#f0d878]">
|
|
{rune?.name ?? (runeId ? `룬 ${runeId}` : "룬")}
|
|
</div>
|
|
{rune?.shortDesc ? (
|
|
<div className="font-bold text-white">{rune.shortDesc}</div>
|
|
) : null}
|
|
{descriptionHtml ? (
|
|
<div
|
|
className="text-white/80 [&_br]:block [&_br]:content-['']"
|
|
dangerouslySetInnerHTML={{ __html: descriptionHtml }}
|
|
/>
|
|
) : (
|
|
<div className="text-white/55">룬 상세 정보를 불러오는 중입니다.</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function RuneStyleTooltipContent({
|
|
styleId,
|
|
manifest,
|
|
}: {
|
|
styleId: number | undefined;
|
|
manifest: DdragonManifest | null | undefined;
|
|
}) {
|
|
const style = runeStyleDetails(manifest, styleId);
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="font-black text-[#f0d878]">
|
|
{style?.name ?? (styleId ? `룬 ${styleId}` : "룬")}
|
|
</div>
|
|
<div className="text-white/55">룬 스타일</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function ItemTooltipContent({
|
|
itemId,
|
|
manifest,
|
|
}: {
|
|
itemId: number;
|
|
manifest: DdragonManifest | null | undefined;
|
|
}) {
|
|
const item = itemDetails(manifest, itemId);
|
|
const descriptionHtml = item?.description
|
|
? safeDdragonDescriptionHtml(item.description)
|
|
: "";
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="font-black text-[#f0d878]">
|
|
{item?.name ?? `아이템 ${itemId}`}
|
|
</div>
|
|
{item?.plaintext ? (
|
|
<div className="font-bold text-white">{item.plaintext}</div>
|
|
) : null}
|
|
{descriptionHtml ? (
|
|
<div
|
|
className="space-y-1 text-white/80 [&_br]:block [&_br]:content-['']"
|
|
dangerouslySetInnerHTML={{ __html: descriptionHtml }}
|
|
/>
|
|
) : (
|
|
<div className="text-white/55">
|
|
아이템 상세 정보를 불러오는 중입니다.
|
|
</div>
|
|
)}
|
|
{item?.gold.total ? (
|
|
<div className="border-t border-white/10 pt-2 font-bold text-white">
|
|
가격:
|
|
<span className="text-[#f0d878]">{item.gold.total}</span>{" "}
|
|
<span className="text-[#f0d878]">
|
|
({item.gold.base || item.gold.total})
|
|
</span>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function clamp(value: number, min: number, max: number) {
|
|
if (max < min) return min;
|
|
return Math.min(Math.max(value, min), max);
|
|
}
|