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
393 lines (369 loc) · 10.8 KB
/
DmarcByDomainPage.js
File metadata and controls
393 lines (369 loc) · 10.8 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import React, { useCallback, useMemo, useState } from 'react'
import { PAGINATED_DMARC_REPORT_SUMMARY_TABLE as FORWARD } from './graphql/queries'
import {
Box,
Divider,
Flex,
Heading,
Icon,
Input,
InputGroup,
InputLeftElement,
Link,
Select,
Spinner,
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 { usePaginatedCollection } from './usePaginatedCollection'
import { RelayPaginationControls } from './RelayPaginationControls'
import { toConstantCase } from './helpers/toConstantCase'
import { useDebouncedFunction } from './useDebouncedFunction'
import { Link as RouteLink } from 'react-router-dom'
import { InfoButton, InfoBox, InfoPanel } from './InfoPanel'
export default function DmarcByDomainPage() {
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 displayLimitOptions = [5, 10, 20, 50, 100]
const [orderBy, setOrderBy] = useState({
field: 'TOTAL_MESSAGES',
direction: 'DESC',
})
const [searchTerm, setSearchTerm] = useState('')
const [debouncedSearchTerm, setDebouncedSearchTerm] = useState('')
const [infoState, changeInfoState] = React.useState({
isHidden: true,
})
const {
loading,
isLoadingMore,
error,
nodes,
next,
previous,
resetToFirstPage,
hasNextPage,
hasPreviousPage,
} = usePaginatedCollection({
fetchForward: FORWARD,
recordsPerPage: selectedTableDisplayLimit,
variables: {
month: selectedPeriod,
year: selectedYear,
search: debouncedSearchTerm,
orderBy: orderBy,
},
relayRoot: 'findMyDmarcSummaries',
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
})
const memoizedSetDebouncedSearchTermCallback = useCallback(() => {
setDebouncedSearchTerm(searchTerm)
}, [searchTerm])
useDebouncedFunction(memoizedSetDebouncedSearchTermCallback, 500)
const updateOrderBy = useCallback(
(sortBy) => {
let newOrderBy = {
field: 'TOTAL_MESSAGES',
direction: 'DESC',
}
if (sortBy.length) {
newOrderBy = {}
newOrderBy.field = toConstantCase(sortBy[0].id)
newOrderBy.direction = sortBy[0].desc === true ? 'DESC' : 'ASC'
}
resetToFirstPage()
setOrderBy(newOrderBy)
},
[resetToFirstPage],
)
const formattedData = useMemo(() => {
const curData = []
nodes.forEach((node) => {
const domain = node.domain.domain
const percentages = { ...node.categoryPercentages }
Object.entries(percentages).forEach(([key, value]) => {
if (typeof value === 'number') percentages[key] = Math.round(value)
})
curData.push({ domain, ...percentages })
})
return curData
}, [nodes])
const [
domain,
totalMessages,
fullPassPercentage,
passSpfOnlyPercentage,
passDkimOnlyPercentage,
failPercentage,
] = [
{
Header: i18n._(t`Domain`),
accessor: 'domain',
// eslint-disable-next-line react/prop-types
Cell: function CellValueWithLink({ value }) {
return (
<Link
as={RouteLink}
to={`domains/${value}/dmarc-report/LAST30DAYS/${new Date().getFullYear()}`}
isExternal={false}
>
{`${value} `} <Icon name="link" />
</Link>
)
},
sortDescFirst: true,
},
{
Header: i18n._(t`Total Messages`),
accessor: 'totalMessages',
Cell: ({ value }) => value.toLocaleString(i18n.locale),
style: { textAlign: 'right' },
sortDescFirst: true,
},
{
Header: i18n._(t`Full Pass %`),
accessor: 'fullPassPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
sortDescFirst: true,
},
{
Header: i18n._(t`Fail DKIM %`),
accessor: 'passSpfOnlyPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
sortDescFirst: true,
},
{
Header: i18n._(t`Fail SPF %`),
accessor: 'passDkimOnlyPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
sortDescFirst: true,
},
{
Header: i18n._(t`Full Fail %`),
accessor: 'failPercentage',
Cell: ({ value }) => `${value}%`,
style: { textAlign: 'right' },
sortDescFirst: true,
},
]
const percentageColumns = useMemo(
() => [
{
Header: i18n._(t`DMARC Summaries`),
hidden: true,
columns: [
domain,
totalMessages,
fullPassPercentage,
passDkimOnlyPercentage,
passSpfOnlyPercentage,
failPercentage,
],
},
],
[
domain,
totalMessages,
fullPassPercentage,
passSpfOnlyPercentage,
passDkimOnlyPercentage,
failPercentage,
],
)
// DMARC Summary Table setup
let tableDisplay
// Initial sorting category for detail tables
const initialSort = [{ id: 'totalMessages', desc: true }]
// Display error if exists
if (error) {
tableDisplay = <ErrorFallbackMessage error={error} />
} else
tableDisplay = (
<DmarcReportTable
data={formattedData}
columns={percentageColumns}
title={i18n._(t`Pass/Fail Ratios by Domain`)}
initialSort={initialSort}
mb="10px"
hideTitleButton={true}
frontendPagination={false}
selectedDisplayLimit={selectedTableDisplayLimit}
manualSort={true}
manualFilters={true}
onSort={updateOrderBy}
/>
)
const handleChange = (e) => {
setSelectedDate(e.target.value)
const [newPeriod, newYear] = e.target.value.split(', ')
setSelectedPeriod(newPeriod)
setSelectedYear(newYear)
resetToFirstPage()
}
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%" px="2">
<Stack direction="row" mb="4">
<Heading as="h1" textAlign="left">
<Trans>DMARC Summaries</Trans>
</Heading>
<Box ml="auto" />
<InfoButton label="Glossary" state={infoState} changeState={changeInfoState} />
</Stack>
<InfoPanel
state={infoState}
>
<InfoBox
title='Domain'
info='The domain address.'
/>
<InfoBox
title='Total Messages'
info='Shows the total number of emails that have been sent by this domain during the selected time range.'
/>
<InfoBox
title='Full Pass %'
info='Shows the percentage of emails from the domain that have passed both SPF and DKIM requirments.'
/>
<InfoBox
title='Fail SPF %'
info='Shows the percentage of emails from the domain that fail SPF requirments, but pass DKIM requirments.'
/>
<InfoBox
title='Fail DKIM %'
info='Shows the percentage of emails from the domain that fail DKIM requirments, but pass SPF requirments.'
/>
<InfoBox
title='Full Fail %'
info='Shows the percentage of emails from the domain that fail both SPF and DKIM requirments.'
/>
<Divider borderColor="gray.500" />
<Trans>
A more detaild breakdown of each domain can be found by clicking on its address in the first column.
</Trans>
</InfoPanel>
<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>
<Flex
direction={{ base: 'column', md: 'row' }}
alignItems={{ base: 'stretch', md: 'center' }}
mb={{ base: '8px', md: '8px' }}
>
<InputGroup
w={{ base: '100%', md: '50%' }}
mb={{ base: '8px', md: '0' }}
>
<InputLeftElement>
<Icon name="search" color="gray.300" />
</InputLeftElement>
<Input
type="text"
placeholder={t`Search for a domain`}
onChange={(e) => {
setSearchTerm(e.target.value)
resetToFirstPage()
}}
/>
</InputGroup>
{loading && (
<Stack
isInline
justifyContent="center"
w={{ base: '100%', md: '50%' }}
>
<Text fontWeight="bold" ml={{ md: 'auto' }} mr="1.5em">
<Trans>Loading Data...</Trans>
</Text>
<Spinner
size="md"
speed="0.6s"
color="primary"
emptyColor="accent"
thickness="0.175em"
/>
</Stack>
)}
</Flex>
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
{tableDisplay}
</ErrorBoundary>
<RelayPaginationControls
onlyPagination={false}
selectedDisplayLimit={selectedTableDisplayLimit}
setSelectedDisplayLimit={setSelectedTableDisplayLimit}
displayLimitOptions={displayLimitOptions}
resetToFirstPage={resetToFirstPage}
hasNextPage={hasNextPage}
hasPreviousPage={hasPreviousPage}
next={next}
previous={previous}
isLoadingMore={isLoadingMore}
/>
</Box>
)
}
DmarcByDomainPage.propTypes = {}