Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/components/leaderboard/MinerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ReactECharts from 'echarts-for-react';
import { useMinerGithubData, useMinerPRs } from '../../api';
import { CHART_COLORS, RANK_COLORS, STATUS_COLORS } from '../../theme';
import { getGithubAvatarSrc } from '../../utils/ExplorerUtils';
import { formatUsdPerDay } from '../../utils/format';
import { linkResetSx, useLinkBehavior } from '../common/linkBehavior';
import { WatchlistButton } from '../common';
import { type MinerStats, type LeaderboardVariant, FONTS } from './types';
Expand Down Expand Up @@ -62,11 +63,6 @@ const formatScore = (score: number): string =>
})
: '0.00';

/** Miner-wide USD/day, shown as a secondary stat. '—' when unavailable —
* per-repo views carry no earnings (earnings is a miner-wide figure). */
const formatUsdPerDay = (usdPerDay: number | undefined): string =>
usdPerDay ? `$${Math.round(usdPerDay).toLocaleString()}/d` : '—';

/* ═══════════════════════════════════════════════════════════════════ */

export const MinerCard: React.FC<MinerCardProps> = ({
Expand Down Expand Up @@ -347,7 +343,7 @@ export const MinerCard: React.FC<MinerCardProps> = ({
fontFeatureSettings: TABULAR_NUMS,
})}
>
${Math.round(miner.usdPerDay || 0).toLocaleString()}
{formatUsdPerDay(miner.usdPerDay, { includePeriod: false })}
</Typography>
<Typography
sx={(theme) => ({
Expand All @@ -372,7 +368,11 @@ export const MinerCard: React.FC<MinerCardProps> = ({
fontFeatureSettings: TABULAR_NUMS,
})}
>
~${Math.round((miner.usdPerDay || 0) * 30).toLocaleString()}/mo
~$
{((miner.usdPerDay || 0) * 30)
.toFixed(0)
.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
/mo
</Typography>
</>
) : (
Expand Down Expand Up @@ -432,7 +432,9 @@ export const MinerCard: React.FC<MinerCardProps> = ({
scoreLabel={isWatchlist ? 'OSS' : 'Earnings'}
scoreValue={Number(miner.totalScore)}
scoreDisplay={
isWatchlist ? undefined : formatUsdPerDay(miner.usdPerDay)
isWatchlist
? undefined
: formatUsdPerDay(miner.usdPerDay, { includePeriod: true })
}
isEligible={isDiscoveries ? discoveriesEligible : ossEligible}
hideScore={isWatchlist}
Expand Down
3 changes: 2 additions & 1 deletion src/components/leaderboard/MinersList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Avatar, Box, Card, Tooltip, Typography } from '@mui/material';
import { useMinerGithubData, useMinerPRs } from '../../api';
import { CHART_COLORS } from '../../theme';
import { getGithubAvatarSrc, type SortOrder } from '../../utils/ExplorerUtils';
import { formatUsdPerDay } from '../../utils/format';
import { DataTable, type DataTableColumn, WatchlistButton } from '../common';
import { RankIcon } from './RankIcon';
import {
Expand Down Expand Up @@ -146,7 +147,7 @@ export const MinersList: React.FC<MinersListProps> = ({
color: earningsHighlighted ? 'status.merged' : 'text.secondary',
}}
>
${Math.round(miner.usdPerDay || 0).toLocaleString()}
{formatUsdPerDay(miner.usdPerDay, { includePeriod: false })}
</Typography>
);
},
Expand Down
17 changes: 14 additions & 3 deletions src/components/miners/MinerIdentityRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { useClipboardCopy } from '../../hooks/useClipboardCopy';
import { STATUS_COLORS } from '../../theme';
import { getRepositoryOwnerAvatarSrc } from '../../utils/avatar';
import { formatUsdPerDay } from '../../utils/format';
import MinerInsightsCard from './MinerInsightsCard';

type ViewMode = 'prs' | 'issues';
Expand Down Expand Up @@ -434,7 +435,10 @@ const MinerIdentityRail: React.FC<MinerIdentityRailProps> = ({
color: usdPerDay > 0 ? STATUS_COLORS.success : 'text.primary',
}}
>
~${Math.round(usdPerDay).toLocaleString()}
{formatUsdPerDay(usdPerDay, {
includeApprox: true,
includePeriod: false,
})}
</Typography>
<Typography
sx={{
Expand All @@ -453,8 +457,15 @@ const MinerIdentityRail: React.FC<MinerIdentityRailProps> = ({
mt: 0.6,
}}
>
~${Math.round(usdPerDay * 30).toLocaleString()}/mo · ~$
{Math.round(lifetimeUsd).toLocaleString()} lifetime
~$
{((usdPerDay || 0) * 30)
.toFixed(0)
.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
/mo · ~$
{(lifetimeUsd || 0)
.toFixed(0)
.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}{' '}
lifetime
</Typography>
</RailCard>

Expand Down
24 changes: 24 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,27 @@ export const credibilityColor = (cred: number): string => {

export const getLowerText = (value: string | null | undefined): string =>
(value ?? '').toLowerCase();

/**
* Format USD per day value with consistent rounding.
* Uses toFixed(0) for predictable rounding instead of Math.round()
* to avoid inconsistencies between API endpoints.
*
* @param value - The USD per day value
* @param options - Formatting options
* @returns Formatted string like "$124/d" or "—"
*/
export const formatUsdPerDay = (
value: number | undefined,
options?: { includeApprox?: boolean; includePeriod?: boolean },
): string => {
const { includeApprox = false, includePeriod = true } = options ?? {};

if (!value || value <= 0) return '—';

const rounded = Number(value.toFixed(0));
const approx = includeApprox ? '~' : '';
const period = includePeriod ? '/d' : '';

return `${approx}$${rounded.toLocaleString()}${period}`;
};