/
TableSection.js
155 lines (139 loc) · 5.11 KB
/
TableSection.js
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
import { Box, Icon, Margins, Pagination, Select, Skeleton, Table, Tile } from '@rocket.chat/fuselage';
import moment from 'moment';
import React, { useMemo, useState } from 'react';
import { useTranslation } from '../../../../../../client/contexts/TranslationContext';
import { useEndpointData } from '../../../../../../client/hooks/useEndpointData';
import Growth from '../../../../../../client/components/data/Growth';
import { Section } from '../Section';
import { ActionButton } from '../../../../../../client/components/basic/Buttons/ActionButton';
import { saveFile } from '../../../../../../client/lib/saveFile';
const convertDataToCSV = (data) => `// type, name, messagesCount, updatedAt, createdAt
${ data.map(({ createdAt, messagesCount, name, t, updatedAt }) => `${ t }, ${ name }, ${ messagesCount }, ${ updatedAt }, ${ createdAt }`).join('\n') }`;
export function TableSection() {
const t = useTranslation();
const periodOptions = useMemo(() => [
['last 7 days', t('Last_7_days')],
['last 30 days', t('Last_30_days')],
['last 90 days', t('Last_90_days')],
], [t]);
const [periodId, setPeriodId] = useState('last 7 days');
const period = useMemo(() => {
switch (periodId) {
case 'last 7 days':
return {
start: moment().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).subtract(7, 'days'),
end: moment().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).subtract(1),
};
case 'last 30 days':
return {
start: moment().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).subtract(30, 'days'),
end: moment().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).subtract(1),
};
case 'last 90 days':
return {
start: moment().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).subtract(90, 'days'),
end: moment().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).subtract(1),
};
}
}, [periodId]);
const handlePeriodChange = (periodId) => setPeriodId(periodId);
const [current, setCurrent] = useState(0);
const [itemsPerPage, setItemsPerPage] = useState(25);
const params = useMemo(() => ({
start: period.start.toISOString(),
end: period.end.toISOString(),
offset: current,
count: itemsPerPage,
}), [period, current, itemsPerPage]);
const data = useEndpointData('engagement-dashboard/channels/list', params);
const channels = useMemo(() => {
if (!data) {
return;
}
return data.channels.map(({
room: { t, name, usernames, ts, _updatedAt },
messages,
diffFromLastWeek,
}) => ({
t,
name: name || usernames.join(' × '),
createdAt: ts,
updatedAt: _updatedAt,
messagesCount: messages,
messagesVariation: diffFromLastWeek,
}));
}, [data]);
const downloadData = () => {
saveFile(convertDataToCSV(channels), `Channels_start_${ params.start }_end_${ params.end }.csv`);
};
return <Section filter={<><Select options={periodOptions} value={periodId} onChange={handlePeriodChange} /><ActionButton mis='x16' disabled={!channels} onClick={downloadData} aria-label={t('Download_Info')} icon='download'/></>}>
<Box>
{channels && !channels.length && <Tile fontScale='p1' color='info' style={{ textAlign: 'center' }}>
{t('No_data_found')}
</Tile>}
{(!channels || channels.length)
&& <Table>
<Table.Head>
<Table.Row>
<Table.Cell>{'#'}</Table.Cell>
<Table.Cell>{t('Channel')}</Table.Cell>
<Table.Cell>{t('Created')}</Table.Cell>
<Table.Cell>{t('Last_active')}</Table.Cell>
<Table.Cell>{t('Messages_sent')}</Table.Cell>
</Table.Row>
</Table.Head>
<Table.Body>
{channels && channels.map(({ t, name, createdAt, updatedAt, messagesCount, messagesVariation }, i) =>
<Table.Row key={i}>
<Table.Cell>{i + 1}.</Table.Cell>
<Table.Cell>
<Margins inlineEnd='x4'>
{(t === 'd' && <Icon name='at' />)
|| (t === 'c' && <Icon name='lock' />)
|| (t === 'p' && <Icon name='hashtag' />)}
</Margins>
{name}
</Table.Cell>
<Table.Cell>
{moment(createdAt).format('L')}
</Table.Cell>
<Table.Cell>
{moment(updatedAt).format('L')}
</Table.Cell>
<Table.Cell>
{messagesCount} <Growth>{messagesVariation}</Growth>
</Table.Cell>
</Table.Row>)}
{!channels && Array.from({ length: 5 }, (_, i) =>
<Table.Row key={i}>
<Table.Cell>
<Skeleton width='100%' />
</Table.Cell>
<Table.Cell>
<Skeleton width='100%' />
</Table.Cell>
<Table.Cell>
<Skeleton width='100%' />
</Table.Cell>
<Table.Cell>
<Skeleton width='100%' />
</Table.Cell>
<Table.Cell>
<Skeleton width='100%' />
</Table.Cell>
</Table.Row>)}
</Table.Body>
</Table>}
<Pagination
current={current}
itemsPerPage={itemsPerPage}
itemsPerPageLabel={() => t('Items_per_page:')}
showingResultsLabel={({ count, current, itemsPerPage }) =>
t('Showing results %s - %s of %s', current + 1, Math.min(current + itemsPerPage, count), count)}
count={(data && data.total) || 0}
onSetItemsPerPage={setItemsPerPage}
onSetCurrent={setCurrent}
/>
</Box>
</Section>;
}