diff --git a/src/assets/scss/pages/_dashboard.scss b/src/assets/scss/pages/_dashboard.scss index 8c4e2c2..377374f 100644 --- a/src/assets/scss/pages/_dashboard.scss +++ b/src/assets/scss/pages/_dashboard.scss @@ -201,6 +201,19 @@ color: #4a4a4a; box-shadow: 0 12px 18px rgba(0, 0, 0, 0.08); } +.mlb-rank-list { margin-top: 14px; } +.mlb-rank-row { + display: flex; + align-items: center; + gap: 10px; + padding: 8px 10px; + border-radius: 12px; + background: #fff; + margin-bottom: 8px; +} +.mlb-rank-no { width: 22px; font-weight: 700; color: #6b6b6b; } +.mlb-rank-name { flex: 1; font-weight: 600; color: #1f1f1f; } +.mlb-rank-score { font-weight: 700; } .mlb-bar.rank-1 { height: 128px; background: #f3d34a; } .mlb-bar.rank-2 { height: 92px; background: #d5d8e0; } .mlb-bar.rank-3 { height: 68px; background: #ff8b3d; } diff --git a/src/components/form/RankRolesField.jsx b/src/components/form/RankRolesField.jsx index 2db422f..76734bb 100644 --- a/src/components/form/RankRolesField.jsx +++ b/src/components/form/RankRolesField.jsx @@ -1,4 +1,4 @@ -import { Controller } from 'react-hook-form'; +import { Controller, useWatch } from 'react-hook-form'; import { Button, Form, Table } from 'react-bootstrap'; function patchRow(rows, index, patch) { @@ -8,11 +8,12 @@ function patchRow(rows, index, patch) { } function emptyRow() { - return { title: '', role: '', limit: 10, sort_order: 0, view_mode: 'versus', scope: 'domain' }; + return { title: '', role: '', limit: 10, sort_order: 0, view_mode: 'versus', scope: 'domain', show_as: 'user', highlight_count: 0, highlight_color: '' }; } export default function RankRolesField({ field, control }) { const roles = field.roles || {}; + const isTeam = useWatch({ control, name: 'leaderboard_roles_type' }) === 'team'; return ( Title Role View Mode - Show + {isTeam ? What to show : null} + {isTeam ? Show : null} Limit + Highlight + Color Sort Order @@ -50,6 +54,7 @@ export default function RankRolesField({ field, control }) { rhf.onChange(patchRow(rows, index, { role: e.target.value }))} > @@ -71,15 +76,32 @@ export default function RankRolesField({ field, control }) { - - rhf.onChange(patchRow(rows, index, { scope: e.target.value }))} - > - - - - + {isTeam ? ( + + rhf.onChange(patchRow(rows, index, { + show_as: e.target.value, + ...(e.target.value === 'team' ? { role: '', scope: 'domain' } : {}), + }))} + > + + + + + ) : null} + {isTeam ? ( + + rhf.onChange(patchRow(rows, index, { scope: e.target.value }))} + > + + + + + ) : null} rhf.onChange(patchRow(rows, index, { limit: e.target.value }))} /> + + rhf.onChange(patchRow(rows, index, { highlight_count: e.target.value }))} + /> + + + rhf.onChange(patchRow(rows, index, { highlight_color: e.target.value }))} + /> + ({ ...row, view_mode: ['versus', 'podium', 'list'].includes(row.view_mode) ? row.view_mode : 'versus', - scope: row.scope === 'state_head' ? 'state_head' : 'domain', + show_as: next.leaderboard_roles_type === 'team' && row.show_as === 'team' ? 'team' : 'user', + role: row.show_as === 'team' ? '' : (row.role || ''), + scope: next.leaderboard_roles_type === 'team' && row.show_as !== 'team' && row.scope === 'own_team' ? 'own_team' : 'domain', + highlight_count: Number(row.highlight_count) > 0 ? Number(row.highlight_count) : 0, + highlight_color: /^#[0-9A-Fa-f]{6}$/.test(row.highlight_color || '') ? row.highlight_color : '', })) .sort(bySortOrder); next.metrics = (Array.isArray(next.metrics) ? next.metrics : []) @@ -252,15 +256,21 @@ const leaderboards = { payload.bg_color = Array.isArray(payload.bg_color) ? '' : payload.bg_color || ''; payload.default_interval = payload.default_interval ?? ''; payload.rank_roles = (Array.isArray(payload.rank_roles) ? payload.rank_roles : []) - .filter((row) => row.title || row.role) - .map((row) => ({ - title: row.title || '', - role: row.role || '', - limit: row.limit === '' || row.limit == null ? 10 : Number(row.limit), - sort_order: row.sort_order === '' || row.sort_order == null ? 0 : Number(row.sort_order), - view_mode: ['versus', 'podium', 'list'].includes(row.view_mode) ? row.view_mode : 'versus', - scope: row.scope === 'state_head' ? 'state_head' : 'domain', - })); + .filter((row) => row.title || row.role || row.show_as === 'team') + .map((row) => { + const showAs = leaderboardType === 'team' && row.show_as === 'team' ? 'team' : 'user'; + return { + title: row.title || '', + role: showAs === 'team' ? 'team' : (row.role || ''), + limit: row.limit === '' || row.limit == null ? 10 : Number(row.limit), + sort_order: row.sort_order === '' || row.sort_order == null ? 0 : Number(row.sort_order), + view_mode: ['versus', 'podium', 'list'].includes(row.view_mode) ? row.view_mode : 'versus', + show_as: showAs, + scope: showAs === 'user' && leaderboardType === 'team' && row.scope === 'own_team' ? 'own_team' : 'domain', + highlight_count: Number(row.highlight_count) > 0 ? Number(row.highlight_count) : 0, + highlight_color: /^#[0-9A-Fa-f]{6}$/.test(row.highlight_color || '') ? row.highlight_color : '', + }; + }); payload.metrics = (Array.isArray(payload.metrics) ? payload.metrics : []) .filter((row) => row.metric) .map((row) => ({ diff --git a/src/views/dashboard/widgets/MilestoneTeamLeaderboard.jsx b/src/views/dashboard/widgets/MilestoneTeamLeaderboard.jsx index 4503ca7..38b3e67 100644 --- a/src/views/dashboard/widgets/MilestoneTeamLeaderboard.jsx +++ b/src/views/dashboard/widgets/MilestoneTeamLeaderboard.jsx @@ -5,19 +5,31 @@ import { signedGet, isSuccess } from '@/api/client'; import { asArray, rankRoleId, rankRoleName, rankRolePillLabel, userInitials } from '@/views/dashboard/helpers'; import EmptyChart from '@/views/dashboard/EmptyChart'; -function podiumUsersFromRankRole(rankRole) { - return asArray(rankRole?.users) - .slice(0, 3) - .map((row) => { - const info = row.user || {}; - const name = String(info.name || '').trim(); - return { - name, - initials: userInitials(name), - avatar: info.avatar || '', - score: Number.isFinite(Number(row.score)) ? Number(row.score) : 0, - }; - }); +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 }) { @@ -52,7 +64,7 @@ function Podium({ users }) {
{user.name}
-
{user.score}
+
{user.score}
) : ( <> @@ -67,11 +79,31 @@ function Podium({ users }) { ); } +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 users = active?.podium_users || item.podium_users || []; + const rows = active?.rows || []; + const isList = active?.view_mode === 'list'; const detailsUrl = active?.url || item.details_url || '#'; return ( @@ -88,14 +120,11 @@ function LeaderboardCard({ item }) { -
- -
{pills.length ? (
{pills.map((pill, index) => (
) : null} + {isList ? ( + + ) : ( +
+ +
+ )} ); @@ -115,23 +151,28 @@ export default function MilestoneTeamLeaderboard({ widget }) { useEffect(() => { const load = async () => { - const response = await signedGet('reports', 'roles_leaderboard/list', {}, { timeout: 120000 }); - if (!isSuccess(response)) { - setItems([]); - return; + let leaderboards = []; + try { + const response = await signedGet('reports', 'roles_leaderboard/list', {}, { timeout: 120000 }); + leaderboards = isSuccess(response) ? asArray(response.data) : []; + } catch { + leaderboards = []; } - let leaderboards = response.data; - if (leaderboards?.items) leaderboards = leaderboards.items; - leaderboards = asArray(leaderboards); const mapped = leaderboards.map((board) => { - const rankRoles = asArray(board.rank_roles); - const pills = rankRoles.map((rankRole, index) => { - const limit = Number(rankRole.limit) > 0 ? Number(rankRole.limit) : 10; - const roleId = rankRoleId(rankRole); + 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: rankRolePillLabel(rankRole.title, limit, rankRoleName(rankRole)), - url: `/leaderboard/dashboard?id=${encodeURIComponent(board.id || '')}&role=${encodeURIComponent(roleId)}`, - podium_users: podiumUsersFromRankRole(rankRole), + label, + url: `/leaderboard/dashboard?${params.toString()}`, + view_mode: section.view_mode || '', + rows: rankedRowsFromSection(section), active: index === 0, }; }); @@ -142,7 +183,6 @@ export default function MilestoneTeamLeaderboard({ widget }) { title: board.name || widget.title || 'Leaderboard', interval_label: `${start}${start && end ? ' – ' : ''}${end}`.trim(), details_url: pills[0]?.url || `/leaderboard/dashboard?id=${encodeURIComponent(board.id || '')}`, - podium_users: pills[0]?.podium_users || [], pills, }; }); diff --git a/src/views/leaderboard/Dashboard.jsx b/src/views/leaderboard/Dashboard.jsx index a2cd894..b0ea863 100644 --- a/src/views/leaderboard/Dashboard.jsx +++ b/src/views/leaderboard/Dashboard.jsx @@ -56,7 +56,9 @@ function rowsWithMetrics(rows, readInfo) { function buildPills(leaderboardId, selectedRoleId, rankRoles, groups, period) { let roles = asArray(rankRoles); if (!roles.length) { - roles = asArray(groups).map((group) => group?.role || group); + roles = asArray(groups) + .map((group) => group?.role) + .filter((role) => role && typeof role === 'object'); } roles = [...roles].sort((a, b) => Number(a?.sort_order || 0) - Number(b?.sort_order || 0)); return roles.map((rankRole, index) => { @@ -83,6 +85,15 @@ function withTeamPill(pills, leaderboardId, period, roleId, hasTeams) { ]; } +function highlightFrom(group) { + const count = Number(group?.highlight_count ?? group?.role?.highlight_count); + const color = String(group?.highlight_color ?? group?.role?.highlight_color ?? '').trim(); + return { + highlightCount: Number.isFinite(count) && count > 0 ? Math.floor(count) : 0, + highlightColor: /^#[0-9A-Fa-f]{6}$/.test(color) ? color : '', + }; +} + function periodLabelFrom(view) { const firstDay = view.firstDay || ''; const lastDay = view.lastDay || ''; @@ -98,9 +109,11 @@ function shapeView({ id, roleId, period, dashboard, item }) { const title = view.name || item?.name || 'Leaderboard'; const periodLabel = periodLabelFrom(view); const hasTeams = isTeamView(view.team_view) || teamRows.length; - const pills = withTeamPill(buildPills(id, roleId, item?.rank_roles, groups, period), id, period, roleId, hasTeams); + const rolePills = buildPills(id, roleId, item?.rank_roles, groups, period); + const configured = rolePills.length > 0; + const pills = configured ? rolePills : withTeamPill(rolePills, id, period, roleId, hasTeams); - if (hasTeams && !roleId) { + if (hasTeams && !roleId && !configured) { const shaped = rowsWithMetrics(teamRows, (row) => { const team = row?.team || {}; const name = String(team.name || '').trim(); @@ -117,15 +130,18 @@ function shapeView({ id, roleId, period, dashboard, item }) { metrics: shaped.metrics, pills, selectedRole: '', + ...highlightFrom(groups.find((group) => asArray(group?.teams).length) || null), }; } const selected = groups.find((group) => { - const currentRoleId = rankRoleId(group?.role || group); + const role = group?.role; + if (!role || typeof role !== 'object') return false; + const currentRoleId = rankRoleId(role); return !roleId || currentRoleId === roleId; }) || null; - const selectedRole = selected ? rankRoleId(selected.role || selected) : roleId; + const selectedRole = selected ? rankRoleId(selected.role) : roleId; if (!selected) { return { @@ -139,6 +155,30 @@ function shapeView({ id, roleId, period, dashboard, item }) { metrics: [], pills, selectedRole, + highlightCount: 0, + highlightColor: '', + }; + } + + const selectedTeamRows = asArray(selected?.teams); + if (selectedTeamRows.length || selected?.role?.show_as === 'team') { + const shaped = rowsWithMetrics(selectedTeamRows, (row) => { + const team = row?.team || {}; + const name = String(team.name || '').trim(); + return { name: name || 'Team', avatar: team.icon || '' }; + }); + return { + error: selectedTeamRows.length ? '' : 'No data found for this role.', + isTeam: true, + title, + periodLabel, + periods, + selectedPeriod, + users: shaped.users, + metrics: shaped.metrics, + pills, + selectedRole, + ...highlightFrom(selected), }; } @@ -159,6 +199,7 @@ function shapeView({ id, roleId, period, dashboard, item }) { metrics: shaped.metrics, pills, selectedRole, + ...highlightFrom(selected), }; } @@ -325,8 +366,10 @@ export default function LeaderboardDashboard() { - {users.map((user) => ( - + {users.map((user, index) => { + const highlighted = view?.highlightColor && index < Number(view?.highlightCount || 0); + return ( + {user.rank}
@@ -355,7 +398,8 @@ export default function LeaderboardDashboard() { {user.metric_values?.[metric.id] ?? '—'} ))} - ))} + ); + })}