-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
CSVExportButton.tsx
71 lines (55 loc) · 1.53 KB
/
CSVExportButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Libraries
import React, {PureComponent} from 'react'
import {connect} from 'react-redux'
import moment from 'moment'
// Components
import {Button, ComponentStatus, IconFont} from '@influxdata/clockface'
// Utils
import {downloadTextFile} from 'src/shared/utils/download'
import {getActiveTimeMachine} from 'src/timeMachine/selectors'
// Types
import {AppState} from 'src/types'
interface StateProps {
files: string[] | null
}
class CSVExportButton extends PureComponent<StateProps, {}> {
public render() {
return (
<Button
titleText={this.titleText}
text="CSV"
icon={IconFont.Download}
onClick={this.handleClick}
status={this.buttonStatus}
/>
)
}
private get buttonStatus(): ComponentStatus {
const {files} = this.props
if (files) {
return ComponentStatus.Default
}
return ComponentStatus.Disabled
}
private get titleText(): string {
const {files} = this.props
if (files) {
return 'Download query results as a .CSV file'
}
return 'Create a query in order to download results as .CSV'
}
private handleClick = () => {
const {files} = this.props
const csv = files.join('\n\n')
const now = moment().format('YYYY-MM-DD-HH-mm')
const filename = `${now} Chronograf Data`
downloadTextFile(csv, filename, '.csv', 'text/csv')
}
}
const mstp = (state: AppState) => {
const {
queryResults: {files},
} = getActiveTimeMachine(state)
return {files}
}
export default connect<StateProps>(mstp)(CSVExportButton)