// MFL player image: a standalone React component (no dependency besides React). // Copy it into your project and pass the player object you got from the MFL API. import { useState, type CSSProperties } from 'react' export const MFL_CDN_URL = 'https://d13e14gtps4iwl.cloudfront.net' export interface PlayerImageSource { id: number // Set on some players (e.g. Arena-generated ones): their photo lives at this path instead. imagePath?: string | null metadata?: { firstName?: string; lastName?: string; overall?: number } } // 960x960 transparent WebP, head and torso. export function playerImageUrl(player: Pick): string { return player.imagePath ? `${MFL_CDN_URL}/${player.imagePath}` : `${MFL_CDN_URL}/players/v2/${player.id}/photo.webp` } const RARITY_COLORS: [minOverall: number, color: string][] = [ [95, '#00ffe9'], // ultimate [85, '#fa53ff'], // legendary [75, '#0077ff'], // rare [65, '#71ff30'], // uncommon [55, '#ecd17f'], // limited [0, '#bebebe'], // common ] export function rarityColor(overall: number): string { return RARITY_COLORS.find(([min]) => overall >= min)?.[1] ?? '#bebebe' } export interface PlayerImageProps { player: PlayerImageSource size?: number // 'portrait': the whole photo. 'avatar': zoomed on the face, in a circle. variant?: 'portrait' | 'avatar' // Background gradient in the player's rarity colour (needs metadata.overall). withRarity?: boolean className?: string style?: CSSProperties } export function PlayerImage({ player, size = 96, variant = 'portrait', withRarity = true, className, style }: PlayerImageProps) { const [failed, setFailed] = useState(false) const name = [player.metadata?.firstName, player.metadata?.lastName].filter(Boolean).join(' ') const overall = player.metadata?.overall const isAvatar = variant === 'avatar' const box: CSSProperties = { position: 'relative', width: size, height: size, overflow: 'hidden', borderRadius: isAvatar ? '50%' : 8, background: withRarity && overall !== undefined ? `linear-gradient(to bottom, #1e1e21, ${rarityColor(overall)})` : '#1e1e21', flexShrink: 0, ...style, } if (failed) { const initials = (player.metadata?.firstName?.[0] ?? '') + (player.metadata?.lastName?.[0] ?? '') return (
{initials || '?'}
) } return (
{/* eslint-disable-next-line @next/next/no-img-element */} {name setFailed(true)} style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'contain', transformOrigin: 'top', transform: isAvatar ? 'scale(2.2) translateY(2%)' : undefined, }} />
) } export default PlayerImage