forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDmarcByDomainPage.js
More file actions
250 lines (232 loc) · 6.89 KB
/
Copy pathDmarcByDomainPage.js
File metadata and controls
250 lines (232 loc) · 6.89 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import React, { useState } from 'react'
import { useUserState } from './UserState'
import {
PAGINATED_DMARC_REPORT_SUMMARY_TABLE as FORWARD,
REVERSE_PAGINATED_DMARC_REPORT_SUMMARY_TABLE as BACKWARD,
} from './graphql/queries'
import { Box, Heading, Select, Stack, Text } from '@chakra-ui/core'
import DmarcReportTable from './DmarcReportTable'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { months } from './months'
import { ErrorBoundary } from 'react-error-boundary'
import { ErrorFallbackMessage } from './ErrorFallbackMessage'
import { LoadingMessage } from './LoadingMessage'
import { usePaginatedCollection } from './usePaginatedCollection'
export default function DmarcByDomainPage() {
const { currentUser } = useUserState()
const { i18n } = useLingui()
const currentDate = new Date()
const [selectedPeriod, setSelectedPeriod] = useState('LAST30DAYS')
const [selectedYear, setSelectedYear] = useState(
currentDate.getFullYear().toString(),
)
const [selectedDate, setSelectedDate] = useState(
`LAST30DAYS, ${currentDate.getFullYear()}`,
)
const [selectedTableDisplayLimit, setSelectedTableDisplayLimit] = useState(10)
const {
loading,
error,
nodes,
next,
previous,
hasNextPage,
hasPreviousPage,
} = usePaginatedCollection({
fetchForward: FORWARD,
fetchBackward: BACKWARD,
fetchHeaders: { authorization: currentUser.jwt },
recordsPerPage: selectedTableDisplayLimit,
variables: {
month: selectedPeriod,
year: selectedYear,
},
relayRoot: 'findMyDomains',
})
if (error) return <ErrorFallbackMessage error={error} />
// DMARC Summary Table setup
let tableDisplay
// Display loading message
if (loading) {
tableDisplay = (
<LoadingMessage>
<Trans>Domains Table</Trans>
</LoadingMessage>
)
}
// If not loading and data exists, show table
else if (nodes.length > 0) {
const formattedData = []
nodes.forEach((node) => {
const domain = node.domain
const percentages = { ...node.dmarcSummaryByPeriod.categoryPercentages }
Object.entries(percentages).forEach(([key, value]) => {
if (typeof value === 'number') percentages[key] = Math.round(value)
})
formattedData.push({ domain, ...percentages })
})
// Initial sorting category for detail tables
const initialSort = [{ id: 'totalMessages', desc: true }]
const [
domain,
totalMessages,
fullPassPercentage,
passSpfOnlyPercentage,
passDkimOnlyPercentage,
failPercentage,
] = [
{
Header: i18n._(t`Domain`),
accessor: 'domain',
},
{
Header: i18n._(t`Total Messages`),
accessor: 'totalMessages',
Cell: ({ value }) => value.toLocaleString(i18n.locale),
style: { textAlign: 'right' },
},
{
Header: i18n._(t`Full Pass %`),
accessor: 'fullPassPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
},
{
Header: i18n._(t`Fail DKIM %`),
accessor: 'passSpfOnlyPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
},
{
Header: i18n._(t`Fail SPF %`),
accessor: 'passDkimOnlyPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
},
{
Header: i18n._(t`Full Fail %`),
accessor: 'failPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
},
]
const percentageColumns = [
{
Header: i18n._(t`DMARC Messages`),
hidden: true,
columns: [
domain,
totalMessages,
fullPassPercentage,
passDkimOnlyPercentage,
passSpfOnlyPercentage,
failPercentage,
],
},
]
const displayLimitOptions = [5, 10, 20, 50, 100]
const paginationConfig = {
previous: previous,
hasPreviousPage: hasPreviousPage,
next: next,
hasNextPage: hasNextPage,
displayLimitOptions: displayLimitOptions,
}
tableDisplay = (
<DmarcReportTable
data={formattedData}
columns={percentageColumns}
title={i18n._(t`Pass/Fail Ratios by Domain`)}
initialSort={initialSort}
mb="10px"
hideTitleButton={true}
linkColumns={[{ column: 'domain', isExternal: false }]}
prependLink="domains/"
appendLink={`/dmarc-report/${selectedPeriod}/${selectedYear}`}
frontendPagination={false}
paginationConfig={paginationConfig}
selectedDisplayLimit={selectedTableDisplayLimit}
setSelectedDisplayLimit={setSelectedTableDisplayLimit}
/>
)
}
// Display error if exists
else if (error) {
tableDisplay = <ErrorFallbackMessage error={error} />
}
// If not loading, no error, and no data, no data exists. Show message
else {
tableDisplay = (
<Heading as="h3" size="lg">
* <Trans>No data for the DMARC summary table</Trans> *
</Heading>
)
}
const handleChange = (e) => {
setSelectedDate(e.target.value)
const [newPeriod, newYear] = e.target.value.split(', ')
setSelectedPeriod(newPeriod)
setSelectedYear(newYear)
}
const options = [
<option
key="LAST30DAYS"
value={`LAST30DAYS, ${currentDate.getFullYear().toString()}`}
>
{t`Last 30 Days`}
</option>,
]
// add dmarc date selection options
for (let i = currentDate.getMonth(), j = 13; j > 0; i--, j--) {
// handle previous year
if (i < 0) {
const value = `${months[months.length + i].toUpperCase()}, ${
currentDate.getFullYear() - 1
}`
const translatedValue = `${months[months.length + i].toUpperCase()}, ${
currentDate.getFullYear() - 1
}`
options.push(
<option key={value} value={value}>
{translatedValue}
</option>,
)
}
// handle current year
else {
const value = `${months[i].toUpperCase()}, ${currentDate.getFullYear()}`
const translatedValue = `${months[
i
].toUpperCase()}, ${currentDate.getFullYear()}`
options.push(
<option key={value} value={value}>
{translatedValue}
</option>,
)
}
}
return (
<Box width="100%">
<Heading as="h1" textAlign="center" size="lg" mb="4px">
<Trans>DMARC Messages</Trans>
</Heading>
<Stack isInline align="center" mb="4px">
<Text fontWeight="bold" textAlign="center">
<Trans>Showing data for period: </Trans>
</Text>
<Select
width="fit-content"
onChange={(e) => handleChange(e)}
value={selectedDate}
>
{options}
</Select>
</Stack>
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
{tableDisplay}
</ErrorBoundary>
</Box>
)
}
DmarcByDomainPage.propTypes = {}