1
- import { useState } from 'react'
1
+ import { useRef , useState } from 'react'
2
2
import { useTranslation } from 'react-i18next'
3
3
import {
4
4
Box ,
@@ -18,7 +18,7 @@ import {
18
18
IconButton ,
19
19
Stack ,
20
20
} from '@mui/material'
21
- import { Link as RouterLink } from 'react-router-dom'
21
+ import { redirect , Link as RouterLink } from 'react-router-dom'
22
22
import useStatistics from '../hooks/useStatistics'
23
23
import { Statistic } from '../types'
24
24
import programme from '../locales/programme.json'
@@ -27,6 +27,8 @@ import useCurrentUser from '../hooks/useCurrentUser'
27
27
import * as xlsx from 'xlsx'
28
28
import { BlueButton , GrayButton } from './ChatV2/generics/Buttons'
29
29
import CloudDownloadIcon from '@mui/icons-material/CloudDownload' ;
30
+ import { ChromeReaderModeRounded , DownloadSharp } from '@mui/icons-material'
31
+ import { BrowserClient } from '@sentry/browser'
30
32
31
33
const Statistics = ( ) => {
32
34
const [ from , setFrom ] = useState ( 1 )
@@ -36,7 +38,8 @@ const Statistics = () => {
36
38
const { t, i18n } = useTranslation ( )
37
39
const { language } = i18n
38
40
const { user, isLoading : isUserLoading } = useCurrentUser ( )
39
-
41
+ const dataDownloadLink = useRef < HTMLAnchorElement | null > ( null )
42
+
40
43
if ( ! isSuccess || isUserLoading ) return null
41
44
42
45
const namesOf = ( codes : string [ ] ) => {
@@ -69,6 +72,23 @@ const Statistics = () => {
69
72
xlsx . writeFile ( book , 'statistics.xlsx' )
70
73
}
71
74
75
+
76
+
77
+ const exportToCSV = ( jsonData : any ) => {
78
+ //const book = xlsx.utils.book_new()
79
+ const sheet = xlsx . utils . json_to_sheet ( jsonData )
80
+ const csv = xlsx . utils . sheet_to_csv ( sheet , { } )
81
+
82
+ const blob = new Blob ( [ csv ] , { type : 'text/csv' } )
83
+ const url = URL . createObjectURL ( blob )
84
+ if ( dataDownloadLink ?. current ) {
85
+ dataDownloadLink . current . href = url
86
+ dataDownloadLink . current . download = 'data.csv'
87
+ dataDownloadLink . current . click ( )
88
+ }
89
+ }
90
+
91
+
72
92
const handleXLSX = ( ) => {
73
93
const mangledStatistics = statsToShow . map ( ( chat ) => {
74
94
return (
@@ -82,7 +102,7 @@ const Statistics = () => {
82
102
PromptCount : chat . promptCount
83
103
84
104
} ) } )
85
- exportToExcel ( mangledStatistics )
105
+ exportToCSV ( mangledStatistics )
86
106
}
87
107
88
108
return (
@@ -121,7 +141,8 @@ const Statistics = () => {
121
141
) ) }
122
142
</ Select >
123
143
</ div >
124
- < IconButton onClick = { ( ) => { handleXLSX ( ) } } sx = { { marginLeft : 'auto' } } > < CloudDownloadIcon fontSize = "large" /> </ IconButton >
144
+ < a ref = { dataDownloadLink } style = { { display : 'none' } } />
145
+ < IconButton onClick = { ( ) => { handleXLSX ( ) } } sx = { { marginLeft : 'auto' } } > < CloudDownloadIcon fontSize = "large" /> </ IconButton >
125
146
</ Stack >
126
147
127
148
< TableContainer component = { Paper } >
0 commit comments