feat: add item tooltips to match history
This commit is contained in:
1 parent
38c2d1f1b4
commit
68afaf0cb8
8 files changed
+477
-49
No files matched your search
@@ -495,6 +495,18 @@ export interface DdragonRuneStyleEntry extends DdragonRuneEntry {
|
||||
slots: Array<{ runes: DdragonRuneEntry[] }>;
|
||||
}
|
||||
|
||||
export interface DdragonItemEntry {
|
||||
name: string;
|
||||
description: string;
|
||||
plaintext: string;
|
||||
gold: {
|
||||
base: number;
|
||||
total: number;
|
||||
sell: number;
|
||||
purchasable: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface DdragonManifest {
|
||||
version: string;
|
||||
cdn: string;
|
||||
@@ -502,6 +514,7 @@ export interface DdragonManifest {
|
||||
champions: Record<string, DdragonChampionEntry>;
|
||||
summonerSpells: Record<string, DdragonSummonerSpellEntry>;
|
||||
runesReforged: DdragonRuneStyleEntry[];
|
||||
items: Record<string, DdragonItemEntry>;
|
||||
fetchedAt: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
import type { CSSProperties, 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,
|
||||
safeDdragonDescriptionHtml,
|
||||
} 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;
|
||||
};
|
||||
|
||||
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 anchorRef = useRef<HTMLSpanElement>(null);
|
||||
const tooltipId = useId();
|
||||
const [open, setOpen] = useState(false);
|
||||
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 (
|
||||
<span
|
||||
ref={anchorRef}
|
||||
className={`relative inline-flex shrink-0 ${className}`}
|
||||
role={hasItem ? "img" : undefined}
|
||||
tabIndex={hasItem ? 0 : undefined}
|
||||
aria-label={hasItem ? itemName : "빈 아이템 슬롯"}
|
||||
aria-describedby={hasItem && open ? tooltipId : undefined}
|
||||
onBlur={() => setOpen(false)}
|
||||
onFocus={() => hasItem && setOpen(true)}
|
||||
onMouseEnter={() => hasItem && setOpen(true)}
|
||||
onMouseLeave={() => setOpen(false)}
|
||||
>
|
||||
<span
|
||||
className={`block h-full w-full overflow-hidden border ${slotTone}`}
|
||||
aria-hidden="true"
|
||||
>
|
||||
{icon ? (
|
||||
<img
|
||||
src={icon}
|
||||
alt=""
|
||||
className={`h-full w-full object-cover ${imageClassName}`}
|
||||
loading="lazy"
|
||||
/>
|
||||
) : null}
|
||||
</span>
|
||||
{hasItem && open ? (
|
||||
<ItemTooltipPortal
|
||||
id={tooltipId}
|
||||
anchorRef={anchorRef}
|
||||
itemId={itemId}
|
||||
manifest={manifest}
|
||||
/>
|
||||
) : null}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function ItemTooltipPortal({
|
||||
id,
|
||||
anchorRef,
|
||||
itemId,
|
||||
manifest,
|
||||
}: {
|
||||
id: string;
|
||||
anchorRef: RefObject<HTMLElement | null>;
|
||||
itemId: number;
|
||||
manifest: DdragonManifest | null | undefined;
|
||||
}) {
|
||||
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"
|
||||
>
|
||||
<ItemTooltipContent itemId={itemId} manifest={manifest} />
|
||||
</div>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -2,6 +2,25 @@ import type { DdragonManifest } from '../api/types';
|
||||
|
||||
const FALLBACK_VERSION = '15.24.1';
|
||||
const FALLBACK_CDN = 'https://ddragon.leagueoflegends.com/cdn';
|
||||
const DDRAGON_DESCRIPTION_TAG_CLASSES: Record<string, string> = {
|
||||
active: 'text-[#f0d878] font-semibold',
|
||||
attention: 'text-[#f0d878] font-semibold',
|
||||
flavorText: 'text-white/45 italic',
|
||||
healing: 'text-emerald-300 font-semibold',
|
||||
keywordMajor: 'text-[#f0d878] font-semibold',
|
||||
keywordName: 'text-[#f0d878] font-semibold',
|
||||
keywordStealth: 'text-violet-300 font-semibold',
|
||||
mainText: 'text-white/80',
|
||||
ornnBonus: 'text-[#f0d878] font-semibold',
|
||||
passive: 'text-[#f0d878] font-semibold',
|
||||
rarityGeneric: 'text-white/55',
|
||||
rarityLegendary: 'text-[#f0d878] font-semibold',
|
||||
rarityMythic: 'text-violet-300 font-semibold',
|
||||
rules: 'text-white/45',
|
||||
shield: 'text-sky-300 font-semibold',
|
||||
stats: 'text-sky-200',
|
||||
status: 'text-sky-300 font-semibold',
|
||||
};
|
||||
|
||||
function cdn(manifest: DdragonManifest | null | undefined) {
|
||||
return manifest?.cdn ?? FALLBACK_CDN;
|
||||
@@ -42,6 +61,34 @@ export function itemIconUrl(
|
||||
return `${cdn(manifest)}/${version(manifest)}/img/item/${itemId}.png`;
|
||||
}
|
||||
|
||||
export function itemDetails(
|
||||
manifest: DdragonManifest | null | undefined,
|
||||
itemId: number,
|
||||
) {
|
||||
if (!itemId) return null;
|
||||
return manifest?.items?.[String(itemId)] ?? null;
|
||||
}
|
||||
|
||||
export function itemDisplayName(
|
||||
manifest: DdragonManifest | null | undefined,
|
||||
itemId: number,
|
||||
) {
|
||||
return itemDetails(manifest, itemId)?.name ?? null;
|
||||
}
|
||||
|
||||
export function safeDdragonDescriptionHtml(description: string) {
|
||||
const escaped = escapeHtml(description);
|
||||
return escaped
|
||||
.replace(/<br\s*\/?>/gi, '<br />')
|
||||
.replace(/<(\/?)([a-zA-Z][a-zA-Z0-9]*)(?:\s[\s\S]*?)?>/g, (_match, closing, tag) => {
|
||||
const normalizedTag = String(tag);
|
||||
const className = DDRAGON_DESCRIPTION_TAG_CLASSES[normalizedTag];
|
||||
if (!className) return '';
|
||||
return closing ? '</span>' : `<span class="${className}">`;
|
||||
})
|
||||
.replace(/<\/?[a-zA-Z][\s\S]*?>/g, '');
|
||||
}
|
||||
|
||||
export function summonerSpellIconUrl(
|
||||
manifest: DdragonManifest | null | undefined,
|
||||
spellId: number,
|
||||
@@ -76,3 +123,12 @@ export function runeIconUrl(
|
||||
) {
|
||||
return runeKeystoneIconUrl(manifest, runeId);
|
||||
}
|
||||
|
||||
function escapeHtml(value: string) {
|
||||
return value
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import { DEFAULT_COMMUNITY_ID } from "../config";
|
||||
import {
|
||||
championDisplayName,
|
||||
championIconUrl,
|
||||
itemIconUrl,
|
||||
} from "../lib/ddragon";
|
||||
import {
|
||||
sideShortLabel,
|
||||
@@ -14,6 +13,7 @@ import {
|
||||
} from "../lib/league";
|
||||
import { internalMatchGamePath } from "../lib/internal-match-routes";
|
||||
import { LANE_LABELS } from "../lib/riot-history";
|
||||
import { ItemIcon } from "../components/ItemTooltip";
|
||||
|
||||
function formatDateTime(value: string | number) {
|
||||
return new Intl.DateTimeFormat("ko-KR", {
|
||||
@@ -72,19 +72,16 @@ function ItemStrip({
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{[...items, trinket].map((itemId, index) => {
|
||||
const icon = itemIconUrl(manifest, itemId);
|
||||
return (
|
||||
<span
|
||||
key={`${itemId}-${index}`}
|
||||
className="w-5 h-5 bg-base-content/10 border border-base-content/5 overflow-hidden"
|
||||
>
|
||||
{icon ? (
|
||||
<img src={icon} alt="" className="w-full h-full object-cover" />
|
||||
) : null}
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
{[...items, trinket].map((itemId, index) => (
|
||||
<ItemIcon
|
||||
key={`${itemId}-${index}`}
|
||||
itemId={itemId}
|
||||
manifest={manifest}
|
||||
trinket={index === items.length}
|
||||
className="h-5 w-5"
|
||||
imageClassName=""
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import { DEFAULT_COMMUNITY_ID } from "../config";
|
||||
import {
|
||||
championDisplayName,
|
||||
championIconUrl,
|
||||
itemIconUrl,
|
||||
runeIconUrl,
|
||||
runeKeystoneIconUrl,
|
||||
runeStyleIconUrl,
|
||||
@@ -33,6 +32,7 @@ import {
|
||||
internalMatchGamePath,
|
||||
} from "../lib/internal-match-routes";
|
||||
import { LANE_LABELS } from "../lib/riot-history";
|
||||
import { ItemIcon } from "../components/ItemTooltip";
|
||||
|
||||
type MatchGameDetail = ApiInternalMatchDetailResponse["games"][number];
|
||||
type MatchGameTeam = MatchGameDetail["teams"][number];
|
||||
@@ -382,19 +382,16 @@ function ItemStrip({
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-7 gap-0.5">
|
||||
{itemIds.map((itemId, index) => {
|
||||
const icon = itemIconUrl(manifest, itemId);
|
||||
return (
|
||||
<span
|
||||
key={`${itemId}-${index}`}
|
||||
className="h-8 w-8 overflow-hidden border border-base-content/5 bg-base-content/10"
|
||||
>
|
||||
{icon ? (
|
||||
<img src={icon} alt="" className="h-full w-full object-cover" />
|
||||
) : null}
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
{itemIds.map((itemId, index) => (
|
||||
<ItemIcon
|
||||
key={`${itemId}-${index}`}
|
||||
itemId={itemId}
|
||||
manifest={manifest}
|
||||
trinket={index === 6}
|
||||
className="h-8 w-8"
|
||||
imageClassName=""
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ import { ApiError } from "../lib/api";
|
||||
import {
|
||||
championDisplayName,
|
||||
championIconUrl,
|
||||
itemIconUrl,
|
||||
profileIconUrl,
|
||||
runeKeystoneIconUrl,
|
||||
runeStyleIconUrl,
|
||||
@@ -43,6 +42,7 @@ import {
|
||||
RANK_FILTER_TABS,
|
||||
} from "../lib/riot-history";
|
||||
import { ResponsiveTierLabel } from "../components/RankBadge";
|
||||
import { ItemIcon } from "../components/ItemTooltip";
|
||||
// recharts가 무거워 메인 번들에서 분리한다 (전적 페이지에서만 로드).
|
||||
const TierHistoryChart = lazy(() => import("../components/TierHistoryChart"));
|
||||
|
||||
@@ -344,24 +344,16 @@ function ItemGrid({
|
||||
|
||||
return (
|
||||
<div className={`grid gap-1 ${layoutClass}`}>
|
||||
{slots.map((itemId, index) => {
|
||||
const url = itemIconUrl(manifest, itemId);
|
||||
return (
|
||||
<div
|
||||
key={`${itemKey}-item-${index}`}
|
||||
className="w-5 h-5 bg-base-content/8 overflow-hidden border border-base-content/5"
|
||||
>
|
||||
{url ? (
|
||||
<img
|
||||
src={url}
|
||||
alt=""
|
||||
className="w-full h-full object-cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{slots.map((itemId, index) => (
|
||||
<ItemIcon
|
||||
key={`${itemKey}-item-${index}`}
|
||||
itemId={itemId}
|
||||
manifest={manifest}
|
||||
trinket={index === 6}
|
||||
className="h-5 w-5"
|
||||
imageClassName=""
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user