import { useEffect, useState } from 'react'; import { Link } from 'react-router'; import { Spinner } from 'react-bootstrap'; import { signedGet, isSuccess } from '@/api/client'; import { asArray, rankRoleId, rankRoleName, rankRolePillLabel, userInitials } from '@/views/dashboard/helpers'; import EmptyChart from '@/views/dashboard/EmptyChart'; function sectionsFor(board) { const roleRanks = asArray(board?.role_ranks); if (roleRanks.length) return roleRanks; return asArray(board?.rank_roles); } function rankedRowsFromSection(section) { const people = asArray(section?.users); const teams = asArray(section?.teams); const source = people.length ? people : teams.length ? teams : (section?.team ? [section] : []); const highlightCount = Number(section?.highlight_count) > 0 ? Number(section.highlight_count) : 0; const color = /^#[0-9A-Fa-f]{6}$/.test(section?.highlight_color || '') ? section.highlight_color : ''; return source.map((row, index) => { const info = row.user || row.team || {}; const name = String(info.name || '').trim(); return { rank: row.rank ?? index + 1, name, initials: userInitials(name), avatar: info.avatar || info.icon || '', score: Number.isFinite(Number(row.score)) ? Number(row.score) : 0, highlight: Boolean(color) && index < highlightCount, color, }; }); } function Podium({ users }) { const slots = [users[1] || null, users[0] || null, users[2] || null]; const ranks = [2, 1, 3]; return (
{slots.map((user, index) => { const rank = ranks[index]; return (
{user ? ( <>
{rank === 1 ? ( ) : null}
{user.avatar && !String(user.avatar).includes('ui-avatars.com') ? ( ) : ( user.initials )}
{rank}
{user.name}
{user.score}
) : ( <>
-
-
)}
); })}
); } function RankList({ rows }) { if (!rows.length) return

No data

; return (
{rows.map((row) => (
{row.rank} {row.name || '-'} {row.score}
))}
); } function LeaderboardCard({ item }) { const [activeIndex, setActiveIndex] = useState(0); const pills = item.pills || []; const active = pills[activeIndex] || pills[0]; const rows = active?.rows || []; const isList = active?.view_mode === 'list'; const detailsUrl = active?.url || item.details_url || '#'; return (

{item.title}

{item.interval_label || '—'}

View details
{pills.length ? (
{pills.map((pill, index) => ( ))}
) : null} {isList ? ( ) : (
)}
); } export default function MilestoneTeamLeaderboard({ widget }) { const [items, setItems] = useState(null); useEffect(() => { const load = async () => { let leaderboards = []; try { const response = await signedGet('reports', 'roles_leaderboard/list', {}, { timeout: 120000 }); leaderboards = isSuccess(response) ? asArray(response.data) : []; } catch { leaderboards = []; } const mapped = leaderboards.map((board) => { const sections = sectionsFor(board); const pills = sections.map((section, index) => { const limit = Number(section.limit) > 0 ? Number(section.limit) : 10; const roleId = section.show_as === 'team' ? '' : rankRoleId(section); const params = new URLSearchParams({ id: board.id || '' }); if (roleId) params.set('role', roleId); const label = section.show_as === 'team' ? (String(section.title || '').trim() || 'Teams') : rankRolePillLabel(section.title, limit, rankRoleName(section)); return { label, url: `/leaderboard/dashboard?${params.toString()}`, view_mode: section.view_mode || '', rows: rankedRowsFromSection(section), active: index === 0, }; }); const start = board.validity_start || ''; const end = board.validity_end || ''; return { id: board.id, title: board.name || widget.title || 'Leaderboard', interval_label: `${start}${start && end ? ' – ' : ''}${end}`.trim(), details_url: pills[0]?.url || `/leaderboard/dashboard?id=${encodeURIComponent(board.id || '')}`, pills, }; }); setItems(mapped); }; load(); }, [widget.title]); return (

{widget.title || 'Leaderboard'}

{items === null ? (

Loading leaderboards...

) : items.length === 0 ? ( ) : (
{items.map((item) => ( ))}
)}
); }