-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathRefreshControls.tsx
More file actions
168 lines (148 loc) · 4.85 KB
/
RefreshControls.tsx
File metadata and controls
168 lines (148 loc) · 4.85 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
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import React, { useCallback } from 'react';
import { styled } from 'styled-components';
import { ProgressAnimation, Icon, Spinner, HoverForHelp } from 'components/common/index';
import { Button, DropdownButton, MenuItem, ButtonGroup } from 'components/bootstrap';
import ReadableDuration from 'components/common/ReadableDuration';
import useAutoRefresh from 'views/hooks/useAutoRefresh';
import { durationToMS } from 'util/DateTime';
const FlexibleButtonGroup = styled(ButtonGroup)`
display: flex;
justify-content: flex-end;
position: relative;
> .btn-group {
.btn:first-child {
max-width: 100%;
}
}
`;
const ButtonLabel = ({ children }: React.PropsWithChildren) => {
const { refreshConfig } = useAutoRefresh();
if (!refreshConfig?.enabled) {
return <>Not updating{children}</>;
}
return (
<>
Every <ReadableDuration duration={refreshConfig.interval} />
{children}
</>
);
};
type Props = React.PropsWithChildren<{
disable: boolean;
intervalOptions: Array<[string, string]>;
isLoadingMinimumInterval: boolean;
minimumRefreshInterval: string;
defaultInterval: string;
onSelectInterval?: (interval: string) => void;
onToggle?: (enabled: boolean) => void;
onEnable?: () => void;
onDisable?: () => void;
humanName: string;
}>;
const RefreshControls = ({
children = null,
humanName,
disable,
onToggle = null,
onEnable = null,
onDisable = null,
intervalOptions,
onSelectInterval = null,
isLoadingMinimumInterval,
minimumRefreshInterval,
defaultInterval,
}: Props) => {
const { refreshConfig, startAutoRefresh, stopAutoRefresh, animationId } = useAutoRefresh();
const selectInterval = useCallback(
(interval: string) => {
startAutoRefresh(durationToMS(interval));
if (typeof onSelectInterval === 'function') {
onSelectInterval(interval);
}
},
[onSelectInterval, startAutoRefresh],
);
const toggleEnable = useCallback(() => {
if (!defaultInterval && !refreshConfig?.interval) {
return;
}
if (typeof onToggle === 'function') {
onToggle(!refreshConfig?.enabled);
}
if (refreshConfig?.enabled) {
stopAutoRefresh();
if (typeof onDisable === 'function') {
onDisable();
}
} else {
startAutoRefresh(refreshConfig?.interval ?? durationToMS(defaultInterval));
if (typeof onEnable === 'function') {
onEnable();
}
}
}, [
defaultInterval,
onDisable,
onEnable,
onToggle,
refreshConfig?.enabled,
refreshConfig?.interval,
startAutoRefresh,
stopAutoRefresh,
]);
return (
<FlexibleButtonGroup aria-label={`Refresh ${humanName} Controls`}>
{refreshConfig?.enabled && animationId && (
<ProgressAnimation
key={`${refreshConfig.interval}-${animationId}`}
$animationDuration={refreshConfig.interval}
$increase={false}
/>
)}
<Button
onClick={toggleEnable}
title={refreshConfig?.enabled ? 'Pause Refresh' : 'Start Refresh'}
disabled={disable || isLoadingMinimumInterval || !defaultInterval}>
<Icon name={refreshConfig?.enabled ? 'pause' : 'update'} />
</Button>
<DropdownButton title={<ButtonLabel>{children}</ButtonLabel>} disabled={disable} id="refresh-options-dropdown">
{isLoadingMinimumInterval && <Spinner />}
{!isLoadingMinimumInterval &&
intervalOptions.map(([interval, label]) => {
const isBelowMinimum = durationToMS(interval) < durationToMS(minimumRefreshInterval);
return (
<MenuItem
key={`RefreshControls-${label}`}
onClick={() => selectInterval(interval)}
disabled={isBelowMinimum}>
{label}
{isBelowMinimum && (
<HoverForHelp displayLeftMargin>
Interval of <ReadableDuration duration={interval} /> ({interval}) is below configured minimum
interval of <ReadableDuration duration={minimumRefreshInterval} /> ({minimumRefreshInterval}).
</HoverForHelp>
)}
</MenuItem>
);
})}
</DropdownButton>
</FlexibleButtonGroup>
);
};
export default RefreshControls;