-
Notifications
You must be signed in to change notification settings - Fork 360
/
HQRTable.tsx
175 lines (163 loc) · 4.85 KB
/
HQRTable.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
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
import Button from "components/buttons/Button";
import EmptyTable from "components/EmptyTable";
import Icon from "components/Icon";
import TableContainer from "components/TableContainer";
import React, { useCallback, useState } from "react";
import { Row } from "react-table";
import {
generateCSVFilename,
generateCSVQueryResults,
} from "utilities/generate_csv";
import FileSaver from "file-saver";
import Spinner from "components/Spinner";
import { HumanTimeDiffWithFleetLaunchCutoff } from "components/HumanTimeDiffWithDateTip";
import generateColumnConfigs from "./HQRTableConfig";
const baseClass = "hqr-table";
export interface IHQRTable {
queryName?: string;
queryDescription?: string;
hostName?: string;
rows: Record<string, string>[];
reportClipped?: boolean;
lastFetched?: string | null; // timestamp
onShowQuery: () => void;
isLoading: boolean;
}
const DEFAULT_CSV_TITLE = "Host-Specific Query Report";
const HQRTable = ({
queryName,
queryDescription,
hostName,
rows,
reportClipped,
lastFetched,
onShowQuery,
isLoading,
}: IHQRTable) => {
const [filteredResults, setFilteredResults] = useState<Row[]>([]);
const columnConfigs = generateColumnConfigs(rows);
const renderTableButtons = useCallback(() => {
const onExportQueryResults = (evt: React.MouseEvent<HTMLButtonElement>) => {
evt.preventDefault();
FileSaver.saveAs(
generateCSVQueryResults(
filteredResults,
generateCSVFilename(
queryName && hostName
? `'${queryName}' query report results for host '${hostName}'`
: DEFAULT_CSV_TITLE
),
columnConfigs,
true
)
);
};
return (
<div className={`${baseClass}__results-cta`}>
<Button
className={`${baseClass}__show-query-btn`}
onClick={onShowQuery}
variant="text-icon"
>
<>
Show query <Icon name="eye" />
</>
</Button>
<Button
className={`${baseClass}__export-btn`}
onClick={onExportQueryResults}
variant="text-icon"
>
<>
Export results
<Icon name="download" color="core-fleet-blue" />
</>
</Button>
</div>
);
}, [onShowQuery, filteredResults, queryName, hostName, columnConfigs]);
const renderEmptyState = useCallback(() => {
// rows.length === 0
if (!lastFetched) {
// collecting results
return (
<EmptyTable
className={`${baseClass}__collecting-results`}
graphicName="collecting-results"
header="Collecting results..."
info={`Fleet is collecting query results from ${hostName}. Check back later.`}
/>
);
}
if (reportClipped) {
return (
<EmptyTable
className={`${baseClass}__report-clipped`}
graphicName="empty-software"
header="Report clipped"
info="This query has paused reporting in Fleet, and no results were saved for this host."
/>
);
}
return (
// nothing to report
<EmptyTable
className={`${baseClass}__nothing-to-report`}
graphicName="empty-software"
header="Nothing to report"
info={`This query has run on ${hostName}, but returned no data for this host.`}
/>
);
}, [lastFetched, hostName, reportClipped]);
const renderCount = useCallback(() => {
const count = filteredResults.length;
return (
<div className={`${baseClass}__results-count-and-last-fetched`}>
<span>{`${count} result${count === 1 ? "" : "s"}`}</span>
<span className="last-fetched">
Last fetched{" "}
<HumanTimeDiffWithFleetLaunchCutoff timeString={lastFetched ?? ""} />
</span>
</div>
);
}, [filteredResults.length, lastFetched]);
const renderTableInfo = useCallback(
() => (
<div className={`${baseClass}__query-info`}>
<h2>{queryName}</h2>
<h3>{queryDescription}</h3>
</div>
),
[queryDescription, queryName]
);
if (isLoading) {
return <Spinner />;
}
return (
<div className={`${baseClass} section`}>
{renderTableInfo()}
{rows.length === 0 ? (
renderEmptyState()
) : (
<TableContainer
isLoading={isLoading}
columnConfigs={columnConfigs}
data={rows}
renderCount={renderCount}
isClientSidePagination
isClientSideFilter
isMultiColumnFilter
showMarkAllPages={false}
isAllPagesSelected={false}
resultsTitle="results"
customControl={renderTableButtons}
setExportRows={setFilteredResults}
emptyComponent={() => null}
defaultSortHeader={columnConfigs[0].id}
defaultSortDirection="asc"
/>
)}
</div>
);
};
export default HQRTable;