-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathactiveStatementsView.tsx
206 lines (189 loc) · 5.98 KB
/
activeStatementsView.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
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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
import React, { useEffect, useState } from "react";
import classNames from "classnames/bind";
import { useHistory } from "react-router-dom";
import {
ISortedTablePagination,
Loading,
PageConfig,
PageConfigItem,
Search,
SortSetting,
} from "src";
import { ActiveStatement, ActiveStatementFilters } from "src/activeExecutions";
import { Filter } from "src/queryFilter";
import SQLActivityError from "src/sqlActivity/errorComponent";
import {
ACTIVE_STATEMENT_SEARCH_PARAM,
getAppsFromActiveStatements,
filterActiveStatements,
} from "../activeExecutions/activeStatementUtils";
import {
calculateActiveFilters,
getFullFiltersAsStringRecord,
} from "../queryFilter/filter";
import styles from "./statementsPage.module.scss";
import { ActiveStatementsSection } from "../activeExecutions/activeStatementsSection";
import { inactiveFiltersState } from "../queryFilter/filter";
import { queryByName, syncHistory } from "src/util/query";
import { getTableSortFromURL } from "../sortedtable/getTableSortFromURL";
import { getActiveStatementFiltersFromURL } from "src/queryFilter/utils";
const cx = classNames.bind(styles);
export type ActiveStatementsViewDispatchProps = {
onColumnsSelect: (columns: string[]) => void;
onFiltersChange: (filters: ActiveStatementFilters) => void;
onSortChange: (ss: SortSetting) => void;
refreshSessions: () => void;
};
export type ActiveStatementsViewStateProps = {
selectedColumns: string[];
statements: ActiveStatement[];
sortSetting: SortSetting;
fetchError: Error | null;
filters: ActiveStatementFilters;
};
export type ActiveStatementsViewProps = ActiveStatementsViewStateProps &
ActiveStatementsViewDispatchProps;
export const ActiveStatementsView: React.FC<ActiveStatementsViewProps> = ({
onColumnsSelect,
refreshSessions,
onFiltersChange,
onSortChange,
selectedColumns,
sortSetting,
statements,
fetchError,
filters,
}: ActiveStatementsViewProps) => {
const [pagination, setPagination] = useState<ISortedTablePagination>({
current: 1,
pageSize: 20,
});
const history = useHistory();
const [search, setSearch] = useState<string>(
queryByName(history.location, ACTIVE_STATEMENT_SEARCH_PARAM),
);
useEffect(() => {
// Refresh every 10 seconds.
refreshSessions();
const interval = setInterval(refreshSessions, 10 * 1000);
return () => {
clearInterval(interval);
};
}, [refreshSessions]);
useEffect(() => {
// We use this effect to sync settings defined on the URL (sort, filters),
// with the redux store. The only time we do this is, when the user navigates
// to the page directly via the URL and specifies settings in the query string.
// Note that the desired behaviour is currently that the user is unable to
// clear filters via the URL, and must do so with page controls.
const sortSettingURL = getTableSortFromURL(history.location);
const filtersFromURL = getActiveStatementFiltersFromURL(history.location);
if (sortSettingURL) {
onSortChange(sortSettingURL);
}
if (filtersFromURL) {
onFiltersChange(filtersFromURL);
}
}, [history, onSortChange, onFiltersChange]);
useEffect(() => {
// This effect runs when the filters or sort settings received from
// redux changes and syncs the URL params with redux.
syncHistory(
{
ascending: sortSetting.ascending.toString(),
columnTitle: sortSetting.columnTitle,
[ACTIVE_STATEMENT_SEARCH_PARAM]: search,
...getFullFiltersAsStringRecord(filters),
},
history,
true,
);
}, [
history,
filters,
sortSetting.ascending,
sortSetting.columnTitle,
search,
]);
const resetPagination = () => {
setPagination({
current: 1,
pageSize: 20,
});
};
const onSortClick = (ss: SortSetting): void => {
onSortChange(ss);
resetPagination();
};
const onSubmitSearch = (newSearch: string): void => {
if (newSearch === search) return;
setSearch(search);
resetPagination();
};
const onSubmitFilters = (selectedFilters: ActiveStatementFilters) => {
onFiltersChange(selectedFilters);
resetPagination();
};
const clearSearch = () => onSubmitSearch("");
const clearFilters = () => onSubmitFilters({ app: inactiveFiltersState.app });
const apps = getAppsFromActiveStatements(statements);
const countActiveFilters = calculateActiveFilters(filters);
const filteredStatements = filterActiveStatements(
statements,
filters,
search,
);
return (
<div className={cx("root")}>
<PageConfig>
<PageConfigItem>
<Search
onSubmit={onSubmitSearch}
onClear={clearSearch}
defaultValue={search}
/>
</PageConfigItem>
<PageConfigItem>
<Filter
activeFilters={countActiveFilters}
onSubmitFilters={onSubmitFilters}
appNames={apps}
filters={filters}
/>
</PageConfigItem>
</PageConfig>
<div className={cx("table-area")}></div>
<Loading
loading={statements == null}
page="active statements"
error={fetchError}
renderError={() =>
SQLActivityError({
statsType: "statements",
})
}
>
<ActiveStatementsSection
filters={filters}
pagination={pagination}
search={search}
statements={filteredStatements}
selectedColumns={selectedColumns}
sortSetting={sortSetting}
onClearFilters={clearFilters}
onChangeSortSetting={onSortClick}
onColumnsSelect={onColumnsSelect}
/>
</Loading>
</div>
);
};