umami/components/input/RefreshButton.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

import { useState, useEffect, useCallback } from 'react';
2023-02-04 17:59:52 +01:00
import { useIntl } from 'react-intl';
import { Button, Icon, Tooltip } from '../react-basics';
import useStore from 'store/queries';
import { setDateRange } from 'store/websites';
import useDateRange from 'hooks/useDateRange';
2023-01-31 06:44:07 +01:00
import Icons from 'components/icons';
2023-02-04 17:59:52 +01:00
import { labels } from 'components/messages';
function RefreshButton({ websiteId }) {
2023-02-04 17:59:52 +01:00
const { formatMessage } = useIntl();
const [dateRange] = useDateRange(websiteId);
const [loading, setLoading] = useState(false);
const selector = useCallback(state => state[`/websites/${websiteId}/stats`], [websiteId]);
const completed = useStore(selector);
2020-08-31 23:11:30 +02:00
function handleClick() {
2022-03-02 06:13:15 +01:00
if (!loading && dateRange) {
setLoading(true);
2023-01-31 06:44:07 +01:00
if (/^\d+/.test(dateRange.value)) {
2022-03-02 06:13:15 +01:00
setDateRange(websiteId, dateRange.value);
} else {
setDateRange(websiteId, dateRange);
}
2020-08-31 23:11:30 +02:00
}
}
useEffect(() => {
setLoading(false);
}, [completed]);
return (
2023-02-04 17:59:52 +01:00
<Tooltip label={formatMessage(labels.refresh)}>
<Button onClick={handleClick}>
<Icon>
<Icons.Refresh />
</Icon>
</Button>
</Tooltip>
);
}
export default RefreshButton;