(format: 'csv' | 'json')
| 100 | } |
| 101 | |
| 102 | const exportData = async (format: 'csv' | 'json') => { |
| 103 | if (!organization) return |
| 104 | |
| 105 | try { |
| 106 | const response = await fetch( |
| 107 | `/api/orgs/${organization.id}/analytics/export?format=${format}`, |
| 108 | ) |
| 109 | |
| 110 | if (!response.ok) { |
| 111 | throw new Error('Failed to export data') |
| 112 | } |
| 113 | |
| 114 | const blob = await response.blob() |
| 115 | const url = window.URL.createObjectURL(blob) |
| 116 | const a = document.createElement('a') |
| 117 | a.href = url |
| 118 | a.download = `org-${organization.id}-analytics.${format}` |
| 119 | document.body.appendChild(a) |
| 120 | a.click() |
| 121 | window.URL.revokeObjectURL(url) |
| 122 | document.body.removeChild(a) |
| 123 | |
| 124 | toast({ |
| 125 | title: 'Success', |
| 126 | description: `Analytics data exported as ${format.toUpperCase()}`, |
| 127 | }) |
| 128 | } catch (error) { |
| 129 | toast({ |
| 130 | title: 'Error', |
| 131 | description: 'Failed to export analytics data', |
| 132 | variant: 'destructive', |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (status === 'loading' || isLoading || analyticsLoading) { |
| 138 | return ( |
no test coverage detected