chore: wip

This commit is contained in:
Ephraim Atta-Duncan
2025-02-25 11:12:15 +00:00
parent 59de996603
commit a12c4a67f1
3 changed files with 94 additions and 3 deletions

View File

@ -0,0 +1,67 @@
'use client';
// delete
import { useState } from 'react';
import { Trans } from '@lingui/macro';
import { Download } from 'lucide-react';
import { Button } from '@documenso/ui/primitives/button';
import { downloadLeaderboardData } from './fetch-leaderboard.actions';
export const DownloadButton = () => {
const [isLoading, setIsLoading] = useState(false);
const handleDownload = async () => {
try {
setIsLoading(true);
const data = await downloadLeaderboardData();
// Create a blob with the data
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create a temporary anchor element
const a = document.createElement('a');
a.href = url;
a.download = `leaderboard-data-${new Date().toISOString().split('T')[0]}.json`;
// Trigger the download
document.body.appendChild(a);
a.click();
// Clean up
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (error) {
console.error('Error downloading data:', error);
} finally {
setIsLoading(false);
}
};
return (
<Button
variant="outline"
size="sm"
onClick={handleDownload}
disabled={isLoading}
className="ml-2"
>
{isLoading ? (
<span className="flex items-center gap-x-2">
<Trans>Downloading...</Trans>
</span>
) : (
<span className="flex items-center gap-x-2">
<Download className="h-4 w-4" />
<Trans>Download</Trans>
</span>
)}
</Button>
);
};

View File

@ -23,3 +23,22 @@ export async function search({ search, page, perPage, sortBy, sortOrder }: Searc
return results; return results;
} }
// delete
export async function downloadLeaderboardData() {
const { user } = await getRequiredServerComponentSession();
if (!isAdmin(user)) {
throw new Error('Unauthorized');
}
const results = await getSigningVolume({
search: '',
page: 1,
perPage: 1000,
sortBy: 'signingVolume',
sortOrder: 'desc',
});
return results.leaderboard;
}

View File

@ -5,6 +5,7 @@ import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-
import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin'; import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin';
import { LeaderboardTable, type SigningVolume } from './data-table-leaderboard'; import { LeaderboardTable, type SigningVolume } from './data-table-leaderboard';
import { DownloadButton } from './download-button';
import { search } from './fetch-leaderboard.actions'; import { search } from './fetch-leaderboard.actions';
type AdminLeaderboardProps = { type AdminLeaderboardProps = {
@ -48,9 +49,13 @@ export default async function Leaderboard({ searchParams = {} }: AdminLeaderboar
return ( return (
<div> <div>
<div className="flex items-center">
<h2 className="text-4xl font-semibold"> <h2 className="text-4xl font-semibold">
<Trans>Signing Volume</Trans> <Trans>Signing Volume</Trans>
</h2> </h2>
{/* TODO: remove */}
<DownloadButton />
</div>
<div className="mt-8"> <div className="mt-8">
<LeaderboardTable <LeaderboardTable
signingVolume={typedSigningVolume} signingVolume={typedSigningVolume}