()
| 41 | } |
| 42 | |
| 43 | export default function UsagePage() { |
| 44 | const { data: session, status } = useSession() |
| 45 | const params = useParams() ?? {} |
| 46 | const router = useRouter() |
| 47 | const orgSlug = (params.slug as string) ?? '' |
| 48 | |
| 49 | const [usageData, setUsageData] = useState<UsageData | null>(null) |
| 50 | const [usageLoading, setUsageLoading] = useState(true) |
| 51 | const [usageError, setUsageError] = useState<string | null>(null) |
| 52 | |
| 53 | // Use the custom hook for organization data |
| 54 | const { organization, isLoading, error } = useOrganizationData(orgSlug) |
| 55 | |
| 56 | useEffect(() => { |
| 57 | if (organization) { |
| 58 | fetchUsageData() |
| 59 | } |
| 60 | }, [organization]) |
| 61 | |
| 62 | const fetchUsageData = async () => { |
| 63 | if (!organization) return |
| 64 | |
| 65 | try { |
| 66 | setUsageLoading(true) |
| 67 | const response = await fetch(`/api/orgs/${organization.id}/usage`) |
| 68 | |
| 69 | if (response.ok) { |
| 70 | const usage = await response.json() |
| 71 | setUsageData(usage) |
| 72 | } else { |
| 73 | const error = await response.json() |
| 74 | throw new Error(error.error || 'Failed to fetch usage data') |
| 75 | } |
| 76 | } catch (error) { |
| 77 | console.error('Error fetching usage data:', error) |
| 78 | setUsageError( |
| 79 | error instanceof Error ? error.message : 'Failed to load usage data', |
| 80 | ) |
| 81 | } finally { |
| 82 | setUsageLoading(false) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const handleExportUsage = async () => { |
| 87 | if (!organization) return |
| 88 | |
| 89 | try { |
| 90 | const response = await fetch(`/api/orgs/${organization.id}/usage/export`) |
| 91 | |
| 92 | if (!response.ok) { |
| 93 | throw new Error('Failed to export usage data') |
| 94 | } |
| 95 | |
| 96 | const blob = await response.blob() |
| 97 | const url = window.URL.createObjectURL(blob) |
| 98 | const a = document.createElement('a') |
| 99 | a.style.display = 'none' |
| 100 | a.href = url |
nothing calls this directly
no test coverage detected