Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/components/common/CreatorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import CreatorLabeledStatRow from '@/components/common/CreatorLabeledStatRow';
import CreatorBio from '@/components/common/CreatorBio';
import { useTransactionTelemetry } from '@/hooks/useTransactionTelemetry';
import { useNetworkMismatch } from '@/hooks/useNetworkMismatch';
import { formatCompactNumber, formatNumber } from '@/utils/numberFormat.utils';
import { formatCompactNumber, formatNumber, formatFollowerCount } from '@/utils/numberFormat.utils';

interface CreatorCardProps {
creator: Course;
Expand Down Expand Up @@ -185,7 +185,7 @@ const CreatorCard: React.FC<CreatorCardProps> = ({ creator, className }) => {
label="Creator Share Supply"
value={
creator.creatorShareSupply
? `${formatCompactNumber(creator.creatorShareSupply)} shares`
? `${formatFollowerCount(creator.creatorShareSupply)} shares`
: 'Supply pending'
}
className="px-3 py-3"
Expand Down
14 changes: 3 additions & 11 deletions src/components/common/FollowerCountPill.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { Users } from 'lucide-react';
import { cn } from '@/lib/utils';
import { formatFollowerCount } from '@/utils/numberFormat.utils';

interface FollowerCountPillProps {
count?: number | null;
className?: string;
}

function formatCount(count: number): string {
if (count >= 1_000_000) {
return `${(count / 1_000_000).toFixed(1).replace(/\.0$/, '')}M`;
}
if (count >= 1_000) {
return `${(count / 1_000).toFixed(1).replace(/\.0$/, '')}K`;
}
return count.toString();
}

const FollowerCountPill: React.FC<FollowerCountPillProps> = ({
count,
className,
Expand All @@ -30,9 +21,10 @@ const FollowerCountPill: React.FC<FollowerCountPillProps> = ({
'inline-flex items-center gap-1 rounded-full border border-white/10 bg-white/5 px-2 py-0.5 text-xs font-medium text-white/70',
className
)}
title={count.toString()} // Preserve full value availability
>
<Users className="size-3 text-amber-500/70" />
{formatCount(count)}
{formatFollowerCount(count)}
</span>
);
};
Expand Down
26 changes: 25 additions & 1 deletion src/utils/__tests__/numberFormat.utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { formatPercent } from '@/utils/numberFormat.utils';
import { formatPercent, formatFollowerCount } from '@/utils/numberFormat.utils';

describe('formatPercent', () => {
it('renders the value with a trailing percent sign', () => {
Expand Down Expand Up @@ -58,3 +58,27 @@ describe('formatPercent', () => {
).toBe('12.00%');
});
});

describe('formatFollowerCount', () => {
it('formats numbers less than 1000 as is', () => {
expect(formatFollowerCount(0)).toBe('0');
expect(formatFollowerCount(999)).toBe('999');
});

it('formats thousands with K suffix', () => {
expect(formatFollowerCount(1000)).toBe('1K');
expect(formatFollowerCount(1500)).toBe('1.5K');
expect(formatFollowerCount(999999)).toBe('1000K');
});

it('formats millions with M suffix', () => {
expect(formatFollowerCount(1000000)).toBe('1M');
expect(formatFollowerCount(1500000)).toBe('1.5M');
expect(formatFollowerCount(999999999)).toBe('1000M');
});

it('removes trailing .0', () => {
expect(formatFollowerCount(1000)).toBe('1K');
expect(formatFollowerCount(1000000)).toBe('1M');
});
});
10 changes: 10 additions & 0 deletions src/utils/numberFormat.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export function formatCompactNumber(
return formatNumber(value, { ...options, style: 'compact' });
}

export function formatFollowerCount(count: number): string {
if (count >= 1_000_000) {
return `${(count / 1_000_000).toFixed(1).replace(/\.0$/, '')}M`;
}
if (count >= 1_000) {
return `${(count / 1_000).toFixed(1).replace(/\.0$/, '')}K`;
}
return count.toString();
}

export interface FormatPercentOptions {
/** Maximum fractional digits in the rendered value. Defaults to 2. */
maximumFractionDigits?: number;
Expand Down
Loading