Rapport_React/src/views/dashboard/widgets/MilestoneTeamLeaderboard.jsx

220 lines
7.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 (
<div className="mlb-podium-row">
{slots.map((user, index) => {
const rank = ranks[index];
return (
<div key={rank} className={`mlb-col ${rank === 1 ? 'is-first' : ''} ${user ? '' : 'is-empty'}`}>
{user ? (
<>
<div className="mlb-avatar-wrap">
{rank === 1 ? (
<span className="mlb-crown" aria-hidden="true">
<svg width="26" height="18" viewBox="0 0 26 18" fill="none">
<path d="M3 15.5L1 5l6.2 4L13 1l5.8 8L25 5l-2 10.5H3z" fill="#F0C94A" />
<path d="M4 15.5h18v2H4v-2z" fill="#E3B341" />
</svg>
</span>
) : null}
<div className={`mlb-avatar rank-${rank}`}>
{user.avatar && !String(user.avatar).includes('ui-avatars.com') ? (
<img src={user.avatar} alt="" />
) : (
user.initials
)}
</div>
<span className={`mlb-badge rank-${rank}`}>{rank}</span>
</div>
<div className="mlb-name" title={user.name}>
{user.name}
</div>
<div className={`mlb-bar rank-${rank}`} style={user.highlight ? { background: user.color } : undefined}>{user.score}</div>
</>
) : (
<>
<div className="mlb-name">-</div>
<div className={`mlb-bar rank-${rank}`}>-</div>
</>
)}
</div>
);
})}
</div>
);
}
function RankList({ rows }) {
if (!rows.length) return <p className="text-muted mb-0 mt-2">No data</p>;
return (
<div className="mlb-rank-list">
{rows.map((row) => (
<div
key={`${row.rank}-${row.name}`}
className="mlb-rank-row"
style={row.highlight ? { background: row.color } : undefined}
>
<span className="mlb-rank-no">{row.rank}</span>
<span className="mlb-rank-name">{row.name || '-'}</span>
<span className="mlb-rank-score">{row.score}</span>
</div>
))}
</div>
);
}
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 (
<div className="col-6">
<article className="mlb-card">
<div className="mlb-card-head">
<h3 className="mlb-card-title">{item.title}</h3>
<div className="mlb-card-meta">
<p className="mlb-card-date">
<span>{item.interval_label || '—'}</span>
</p>
<Link className="mlb-card-details" to={detailsUrl}>
View details
</Link>
</div>
</div>
{pills.length ? (
<div className="mlb-pills">
{pills.map((pill, index) => (
<button
key={`${pill.label}-${index}`}
type="button"
className={`mlb-pill ${index === activeIndex ? 'is-active' : ''}`}
onClick={() => setActiveIndex(index)}
>
{pill.label}
</button>
))}
</div>
) : null}
{isList ? (
<RankList rows={rows} />
) : (
<div className="mlb-podium">
<Podium users={rows.slice(0, 3)} />
</div>
)}
</article>
</div>
);
}
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 (
<div className="mb-3">
<div className="row">
<div className="col-12">
<div className="page-title-box">
<h4 className="page-title">{widget.title || 'Leaderboard'}</h4>
</div>
</div>
</div>
{items === null ? (
<div className="text-center py-4">
<Spinner animation="border" />
<p className="text-muted mt-2 mb-0">Loading leaderboards...</p>
</div>
) : items.length === 0 ? (
<EmptyChart type="radialBar" label="No data" />
) : (
<div className="row">
{items.map((item) => (
<LeaderboardCard key={item.id} item={item} />
))}
</div>
)}
</div>
);
}