import { currentMonthlyPeriod } from '@documenso/lib/universal/monthly-period'; import { Progress } from '@documenso/ui/primitives/progress'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@documenso/ui/primitives/select'; import { Trans } from '@lingui/react/macro'; import type { OrganisationClaim, OrganisationMonthlyStat } from '@prisma/client'; import { useState } from 'react'; import { match } from 'ts-pattern'; import { OrganisationUsageResetButton } from './organisation-usage-reset-button'; type OrganisationUsagePanelProps = { organisationId: string; monthlyStats: Pick< OrganisationMonthlyStat, 'period' | 'documentCount' | 'emailCount' | 'apiCount' | 'emailReports' >[]; organisationClaim: OrganisationClaim; }; export const OrganisationUsagePanel = ({ organisationId, monthlyStats, organisationClaim, }: OrganisationUsagePanelProps) => { const [selectedPeriod, setSelectedPeriod] = useState(() => monthlyStats[0]?.period); const selectedStat = monthlyStats.find((stat) => stat.period === selectedPeriod) ?? monthlyStats[0]; // Resetting a counter only affects the current month (the server hardcodes the // current period), so only offer the reset action when viewing the current month. const isCurrentPeriod = selectedStat?.period === currentMonthlyPeriod(); const rows = [ { counter: 'document' as const, label: Documents, used: selectedStat?.documentCount ?? 0, effectiveLimit: organisationClaim.documentQuota, }, { counter: 'email' as const, label: Emails, used: selectedStat?.emailCount ?? 0, effectiveLimit: organisationClaim.emailQuota, }, { counter: 'api' as const, label: API requests, used: selectedStat?.apiCount ?? 0, effectiveLimit: organisationClaim.apiQuota, }, ]; return (

Usage for period: {selectedStat?.period || 'N/A'}

{monthlyStats.length > 0 && ( )}
{rows.map((row) => { const percent = row.effectiveLimit && row.effectiveLimit > 0 ? Math.min(100, Math.round((row.used / row.effectiveLimit) * 100)) : 0; return (
{row.label} {row.used} /{' '} {match(row.effectiveLimit) .with(null, () => Unlimited) .with(0, () => Blocked) .otherwise(String)}
{row.effectiveLimit && row.effectiveLimit > 0 ? : null} {selectedStat && isCurrentPeriod && (
)}
); })}
Reports {selectedStat?.emailReports ?? 0}
); };