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 ( ); } 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 ( 0} label={spellName} emptyLabel="빈 스펠 슬롯" icon={summonerSpellIconUrl(manifest, spellId)} className={className} imageClassName={imageClassName} slotClassName={slotClassName} > ); } 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 ( ); } 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 ( ); } function TooltipIcon({ active, label, emptyLabel, icon, className, imageClassName, slotClassName, children, }: TooltipIconProps) { const anchorRef = useRef(null); const tooltipId = useId(); const [open, setOpen] = useState(false); return ( setOpen(false)} onFocus={() => active && setOpen(true)} onMouseEnter={() => active && setOpen(true)} onMouseLeave={() => setOpen(false)} > {active && open ? ( {children} ) : null} ); } function TooltipPortal({ id, anchorRef, children, }: { id: string; anchorRef: RefObject; children: ReactNode; }) { const [position, setPosition] = useState(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( , 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 (
{spell?.name ?? `스펠 ${spellId}`}
{descriptionHtml ? (
) : (
스펠 상세 정보를 불러오는 중입니다.
)}
); } function RuneTooltipContent({ runeId, manifest, }: { runeId: number | undefined; manifest: DdragonManifest | null | undefined; }) { const rune = runeDetails(manifest, runeId); const descriptionHtml = rune?.longDesc ? safeDdragonDescriptionHtml(rune.longDesc) : ""; return (
{rune?.name ?? (runeId ? `룬 ${runeId}` : "룬")}
{rune?.shortDesc ? (
{rune.shortDesc}
) : null} {descriptionHtml ? (
) : (
룬 상세 정보를 불러오는 중입니다.
)}
); } function RuneStyleTooltipContent({ styleId, manifest, }: { styleId: number | undefined; manifest: DdragonManifest | null | undefined; }) { const style = runeStyleDetails(manifest, styleId); return (
{style?.name ?? (styleId ? `룬 ${styleId}` : "룬")}
룬 스타일
); } function ItemTooltipContent({ itemId, manifest, }: { itemId: number; manifest: DdragonManifest | null | undefined; }) { const item = itemDetails(manifest, itemId); const descriptionHtml = item?.description ? safeDdragonDescriptionHtml(item.description) : ""; return (
{item?.name ?? `아이템 ${itemId}`}
{item?.plaintext ? (
{item.plaintext}
) : null} {descriptionHtml ? (
) : (
아이템 상세 정보를 불러오는 중입니다.
)} {item?.gold.total ? (
가격: {item.gold.total}{" "} ({item.gold.base || item.gold.total})
) : null}
); } function clamp(value: number, min: number, max: number) { if (max < min) return min; return Math.min(Math.max(value, min), max); }