-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve handling of undefined rank scenario
- Loading branch information
1 parent
716a01d
commit d248f3b
Showing
2 changed files
with
30 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 29 additions & 28 deletions
57
apps/web/src/lib/components/profiles/overview/PercentileBar.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,52 @@ | ||
<script lang="ts"> | ||
import { formatNumber } from '@shared/functions/formatters/numbers'; | ||
interface Props { | ||
rank: number; | ||
rank: number | undefined; | ||
total: number; | ||
classes?: string; | ||
} | ||
let { rank, total, classes = '' }: Props = $props(); | ||
let percentile = $derived(((total - rank) / total) * 100); | ||
let percentile: number | 'N/A' = $derived(rank !== undefined ? ((total - rank) / total) * 100 : 'N/A'); | ||
const getLabel = (pct: number) => { | ||
if (pct <= 50) { | ||
return '<50% Percentile'; | ||
} else if (pct > 99) { | ||
return 'Top 1%'; | ||
} else { | ||
return `Top ${(100 - pct).toFixed(0)}%`; | ||
} | ||
const getLabel = (pct: number | 'N/A') => { | ||
if (pct === 'N/A') return 'N/A'; | ||
if (pct <= 50) return '<50% Percentile'; | ||
if (pct > 99) return 'Top 1%'; | ||
return `Top ${(100 - pct).toFixed(0)}%`; | ||
}; | ||
let getBarColor = (pct: number) => { | ||
let getBarColor = (pct: number | 'N/A') => { | ||
if (pct === 'N/A') return 'bg-slate-200'; | ||
if (pct >= 90) return 'bg-grantmakers-blue'; | ||
if (pct >= 75) return 'bg-grantmakers-blue opacity-80'; | ||
if (pct >= 50) return 'bg-grantmakers-blue opacity-50'; | ||
return 'bg-grantmakers-green'; | ||
}; | ||
</script> | ||
|
||
<div class="flex w-full flex-1 flex-col items-center gap-2 sm:gap-3 {classes}"> | ||
<div class="flex w-full items-center justify-between gap-4"> | ||
<!-- Percentile --> | ||
<div class="flex shrink flex-col gap-1"> | ||
<span class="text-sm font-bold text-slate-700">{getLabel(percentile)}</span> | ||
<span class="text-xs text-slate-500">Rank</span> | ||
</div> | ||
{#if rank !== undefined} | ||
<div class="flex w-full flex-1 flex-col items-center gap-2 sm:gap-3 {classes}"> | ||
<div class="flex w-full items-center justify-between gap-4"> | ||
<!-- Percentile --> | ||
<div class="flex shrink flex-col gap-1"> | ||
<span class="text-sm font-bold text-slate-700">{getLabel(percentile)}</span> | ||
<span class="text-xs text-slate-500">Rank</span> | ||
</div> | ||
|
||
<!-- Bar --> | ||
<div class="grow"> | ||
<div class="h-2 overflow-hidden rounded-full bg-slate-200"> | ||
<div class="h-full {getBarColor(percentile)} rounded-full transition-all duration-500" style:width="{percentile}%"></div> | ||
<!-- Bar --> | ||
<div class="grow"> | ||
<div class="h-2 overflow-hidden rounded-full bg-slate-200"> | ||
<div class="h-full {getBarColor(percentile)} rounded-full transition-all duration-500" style:width="{percentile}%"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<!-- Rank - hidden on mobile --> | ||
<div class="flex shrink flex-col gap-1 text-right"> | ||
<span class="text-sm font-bold text-slate-700">#{rank.toLocaleString()}</span> | ||
<span class="text-xs text-slate-500">of 150,188</span> | ||
<!-- Rank - hidden on mobile --> | ||
<div class="flex shrink flex-col gap-1 text-right"> | ||
<span class="text-sm font-bold text-slate-700">#{formatNumber(rank)}</span> | ||
<span class="text-xs text-slate-500">of 150,188</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{/if} |