Skip to content

Commit

Permalink
feat: add timeout setting for datepicker (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
luohoufu authored Dec 5, 2024
1 parent d002f94 commit 4823339
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 11 deletions.
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"numeral": "^2.0.6",
"nzh": "^1.0.3",
"omit.js": "^1.0.0",
"path-to-regexp": "^2.4.0",
"path-to-regexp": "3.3.0",
"pathfinding": "^0.4.18",
"prop-types": "^15.5.10",
"qs": "^6.5.2",
Expand Down
60 changes: 57 additions & 3 deletions web/src/common/src/DatePicker/TimeSetting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ const timeIntervals = [
{ label: 'Year', value: 'y' },
];

const timeOuts = [
{ label: 'Second', value: 's' },
{ label: 'Minute', value: 'm' },
];

const TimeSetting = props => {
const { currentLocales, timeFields = [], showTimeField, showTimeInterval, onTimeSettingChange, onCancel } = props;
const { currentLocales, timeFields = [], showTimeField, showTimeInterval, showTimeout, onTimeSettingChange, onCancel } = props;

const [isAuto, setIsAuto] = useState(!props.timeInterval)
const [timeField, setTimeField] = useState(props.timeField);
const [timeInterval, setTimeInterval] = useState(props.timeInterval);
const [timeout, setTimeout] = useState(props.timeout);
const timeIntervalCache = useRef('');

const handleApply = () => {
onTimeSettingChange({ timeField, timeInterval });
onTimeSettingChange({ timeField, timeInterval, timeout });
onCancel()
};

Expand All @@ -36,6 +42,20 @@ const TimeSetting = props => {
}
}, [timeInterval])

const timeoutObject = useMemo(() => {
if (!timeout) {
return {
value: 120,
unit: 's',
}
}
const value = parseInt(timeout);
return {
value,
unit: timeout.replace(`${value}`, ''),
}
}, [timeout])

return (
<div className={styles.timeSetting}>
<div className={styles.title}>{currentLocales[`datepicker.time_setting`]}</div>
Expand Down Expand Up @@ -98,11 +118,45 @@ const TimeSetting = props => {
</div>
</div>
)}
{showTimeout && (
<div className={styles.formItem}>
<div className={styles.label}>
{currentLocales[`datepicker.time_setting.timeout`]}
</div>
<div className={styles.form}>
{
timeoutObject && (
<>
<InputNumber
min={60}
value={timeoutObject.value}
style={{ width: '100%' }}
step={10}
precision={0}
onChange={(value) => {
if (Number.isInteger(value)) {
setTimeout(`${value}${timeoutObject.unit}`)
}
}}
/>
<Select value={timeoutObject.unit} onChange={(value) => setTimeout(`${timeoutObject.value}${value}`)} style={{ width: '100%' }}>
{timeOuts.map((item) => (
<Select.Option key={item.value} value={item.value}>
{currentLocales[`datepicker.time_setting.time_interval.${item.value}`]}
</Select.Option>
))}
</Select>
</>
)
}
</div>
</div>
)}
<div className={styles.apply}>
<Apply currentLocales={currentLocales} onApply={handleApply} onCancel={onCancel} />
</div>
</div>
);
};

export default TimeSetting;
export default TimeSetting;
4 changes: 4 additions & 0 deletions web/src/common/src/DatePicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ const DatePicker = (props) => {
timeFields = [],
showTimeInterval = false,
timeInterval,
showTimeout = false,
timeout,
autoFitLoading = false,
timeZone = "Asia/Shanghai",
commonlyUsedRanges = DEFAULT_COMMONLY_USED_RANGES,
Expand Down Expand Up @@ -234,6 +236,8 @@ const DatePicker = (props) => {
timeFields={timeFields}
showTimeInterval={showTimeInterval}
timeInterval={timeInterval}
showTimeout={showTimeout}
timeout={timeout}
autoFitLoading={autoFitLoading}
timeZone={timeZone}
commonlyUsedRanges={commonlyUsedRanges}
Expand Down
4 changes: 3 additions & 1 deletion web/src/common/src/DatePicker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
| timeFields | 时间字段列表 | string[] | [] | 1.0.0 |
| showTimeInterval | 是否显示时间间隔 | boolean | false | 1.0.0 |
| timeInterval | 时间间隔 | string | - | 1.0.0 |
| onTimeSettingChange | 时间配置变更的回调 | ({timeField: string, timeInterval: string}) => void | - | 1.0.0 |
| showTimeout | 是否显示超时时间 | boolean | false | 1.0.0 |
| timeout | 超时时间 | string | 120s | 1.0.0 |
| onTimeSettingChange | 时间配置变更的回调 | ({timeField: string, timeInterval: string, timeout: string}) => void | - | 1.0.0 |
| autoFitLoading | 自动适配时间载入状态 | boolean | false | 1.0.0 |
| onAutoFit | 自动适配时间的回调 | () => void | - | 1.0.0 |
| timeZone | 时区 | string | 'Asia/Shanghai' | 1.0.0 |
Expand Down
1 change: 1 addition & 0 deletions web/src/common/src/DatePicker/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default {
"datepicker.time_setting.time_interval.w": "Week",
"datepicker.time_setting.time_interval.M": "Month",
"datepicker.time_setting.time_interval.y": "Year",
"datepicker.time_setting.timeout": "Timeout",
"datepicker.time_zone": "Time zone",
"datepicker.time_zone.current": "Current time zone",
"datepicker.refresh_every": "Refresh every",
Expand Down
1 change: 1 addition & 0 deletions web/src/common/src/DatePicker/locales/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default {
"datepicker.time_setting.time_interval.w": "周",
"datepicker.time_setting.time_interval.M": "月",
"datepicker.time_setting.time_interval.y": "年",
"datepicker.time_setting.timeout": "超时时间",
"datepicker.time_zone": "时区",
"datepicker.time_zone.current": "当前时区",
"datepicker.refresh_every": "刷新间隔",
Expand Down
2 changes: 2 additions & 0 deletions web/src/components/ListView/components/DatePicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default (props) => {
timeFields: timeFields,
showTimeInterval: true,
timeInterval: "15s",
showTimeout: true,
timeout: "120s",
});

const [currentTimeZone, setCurrentTimeZone] = useState(timeZone);
Expand Down
28 changes: 22 additions & 6 deletions web/src/components/Overview/Monitor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ const formatTimeInterval = (timeInterval) => {
return timeInterval
}

const formatTimeout = (timeout) => {
if (!timeout) return timeout
const value = parseInt(timeout)
if (!value) return undefined
if (!Number.isInteger(value)) return undefined
const unit = timeout.replace(`${value}`, '');
if (!['s', 'm'].includes(unit)) return undefined
return timeout
}

const Monitor = (props) => {
const {
formatState,
Expand All @@ -51,6 +61,7 @@ const Monitor = (props) => {
timeFormatter: formatter.dates(1),
},
timeInterval: formatTimeInterval(param?.timeInterval),
timeout: formatTimeout(param?.timeout),
param: param,
})
);
Expand All @@ -59,10 +70,10 @@ const Monitor = (props) => {
const [timeZone, setTimeZone] = useState(() => getTimezone());

useEffect(() => {
setParam({ ...param, timeRange: state.timeRange, timeInterval: state.timeInterval });
}, [state.timeRange, state.timeInterval]);
setParam({ ...param, timeRange: state.timeRange, timeInterval: state.timeInterval, timeout: state.timeout });
}, [state.timeRange, state.timeInterval, state.timeout]);

const handleTimeChange = useCallback(({ start, end, timeInterval }) => {
const handleTimeChange = useCallback(({ start, end, timeInterval, timeout }) => {
const bounds = calculateBounds({
from: start,
to: end,
Expand All @@ -78,7 +89,8 @@ const Monitor = (props) => {
max: end,
timeFormatter: formatter.dates(intDay),
},
timeInterval: timeInterval || state.timeInterval
timeInterval: timeInterval || state.timeInterval,
timeout: timeout || state.timeout
});
setSpinning(true);
}, [state])
Expand Down Expand Up @@ -113,10 +125,13 @@ const Monitor = (props) => {
showTimeSetting={true}
showTimeInterval={true}
timeInterval={state.timeInterval}
showTimeout={true}
timeout={state.timeout}
onTimeSettingChange={(timeSetting) => {
setState({
...state,
timeInterval: timeSetting.timeInterval
timeInterval: timeSetting.timeInterval,
timeout: timeSetting.timeout
});
}}
timeZone={timeZone}
Expand All @@ -129,7 +144,7 @@ const Monitor = (props) => {
icon={"reload"}
type="primary"
onClick={() => {
handleTimeChange({ start: state.timeRange.min, end: state.timeRange.max, timeInterval: state.timeInterval})
handleTimeChange({ start: state.timeRange.min, end: state.timeRange.max, timeInterval: state.timeInterval, timeout: state.timeout})
}}
>
{formatMessage({ id: "form.button.refresh"})}
Expand Down Expand Up @@ -168,6 +183,7 @@ const Monitor = (props) => {
setSpinning={setSpinning}
{...extraParams}
bucketSize={state.timeInterval}
timeout={state.timeout}
/>
)
) : null}
Expand Down

0 comments on commit 4823339

Please sign in to comment.