Skip to content

Commit

Permalink
Added download counts
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed May 5, 2024
1 parent e0a6848 commit e9bdad7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
9 changes: 2 additions & 7 deletions src/components/Asset.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import { formatBytes, formatNum } from '../utils';
interface Asset {
name: string;
label: string;
Expand All @@ -13,13 +15,6 @@ interface Props {
}
const { asset, include_downloads = true } = Astro.props;
const formatNum = Intl.NumberFormat('en', { notation: 'compact' }).format;
function formatBytes(bytes: number): string {
const unit = Math.floor(Math.log10(bytes) / 3);
const adjusted = bytes / 10 ** (3 * unit);
return adjusted.toFixed(2) + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][unit];
}
---

<span>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Release.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { parse as parseMarkdown } from 'marked';
import type { Release } from '../repository';
import Asset from './Asset.astro';
import { formatNum } from '../utils';
interface Props {
release: Release;
Expand Down Expand Up @@ -40,7 +41,7 @@ try {
{
release.assets?.length > 0 && (
<div>
<h4>Downloads</h4>
<h4>Downloads ({formatNum(release.assets.reduce((total, { download_count }) => total + download_count, 0))} total)</h4>
<p>
{release.assets?.map((asset: any) => (
<Asset {asset} />
Expand Down
7 changes: 4 additions & 3 deletions src/pages/download.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
import Main from '../layouts/Main.astro';
import Asset from '../components/Asset.astro';
import { repo_url, repositoryAPIRequest, type Release } from '../repository';
import Main from '../layouts/Main.astro';
import { repositoryAPIRequest, type Release } from '../repository';
import { formatNum } from '../utils';
let error: string | Error, release: Release;
Expand All @@ -28,7 +29,7 @@ try {
</span>
) : (
<div>
<h4>Downloads</h4>
<h4>Downloads ({formatNum(release.assets.reduce((total, { download_count }) => total + download_count, 0))} total)</h4>
<p>
{release.assets?.map((asset: any) => (
<Asset {asset} />
Expand Down
4 changes: 2 additions & 2 deletions src/pages/releases/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ try {
}
---

<Main title="Changelog">
<Main title="Releases">
{
error ? (
<span>
<br />
Failed to fetch changelog. See it&nbsp;
Failed to fetch releases. See it&nbsp;
<a href={repo_url + '/releases'} target="_blank">
here
</a>
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ export async function checkAdminAuth(astro: Readonly<AstroGlobal>, minType?: Acc

return account;
}

export const formatNum = Intl.NumberFormat('en', { notation: 'compact' }).format;
export function formatBytes(bytes: number): string {
const unit = Math.floor(Math.log10(bytes) / 3);
const adjusted = bytes / 10 ** (3 * unit);
return adjusted.toFixed(2) + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][unit];
}

0 comments on commit e9bdad7

Please sign in to comment.