Skip to content

Commit 2b980a9

Browse files
Download icon now downloads a csv
1 parent ae93ecb commit 2b980a9

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/client/components/Statistics.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react'
1+
import { useRef, useState } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import {
44
Box,
@@ -18,7 +18,7 @@ import {
1818
IconButton,
1919
Stack,
2020
} from '@mui/material'
21-
import { Link as RouterLink } from 'react-router-dom'
21+
import { redirect, Link as RouterLink } from 'react-router-dom'
2222
import useStatistics from '../hooks/useStatistics'
2323
import { Statistic } from '../types'
2424
import programme from '../locales/programme.json'
@@ -27,6 +27,8 @@ import useCurrentUser from '../hooks/useCurrentUser'
2727
import * as xlsx from 'xlsx'
2828
import { BlueButton, GrayButton } from './ChatV2/generics/Buttons'
2929
import CloudDownloadIcon from '@mui/icons-material/CloudDownload';
30+
import { ChromeReaderModeRounded, DownloadSharp } from '@mui/icons-material'
31+
import { BrowserClient } from '@sentry/browser'
3032

3133
const Statistics = () => {
3234
const [from, setFrom] = useState(1)
@@ -36,7 +38,8 @@ const Statistics = () => {
3638
const { t, i18n } = useTranslation()
3739
const { language } = i18n
3840
const { user, isLoading: isUserLoading } = useCurrentUser()
39-
41+
const dataDownloadLink = useRef<HTMLAnchorElement | null>(null)
42+
4043
if (!isSuccess || isUserLoading) return null
4144

4245
const namesOf = (codes: string[]) => {
@@ -69,6 +72,23 @@ const Statistics = () => {
6972
xlsx.writeFile(book, 'statistics.xlsx')
7073
}
7174

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+
7292
const handleXLSX = () => {
7393
const mangledStatistics = statsToShow.map((chat) => {
7494
return (
@@ -82,7 +102,7 @@ const Statistics = () => {
82102
PromptCount: chat.promptCount
83103

84104
})})
85-
exportToExcel(mangledStatistics)
105+
exportToCSV(mangledStatistics)
86106
}
87107

88108
return (
@@ -121,7 +141,8 @@ const Statistics = () => {
121141
))}
122142
</Select>
123143
</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>
125146
</Stack>
126147

127148
<TableContainer component={Paper}>

0 commit comments

Comments
 (0)