18972: Records the last refresh click on localStorage for others to act on.
[arvados-workbench2.git] / src / components / refresh-button / refresh-button.tsx
1
2 // Copyright (C) The Arvados Authors. All rights reserved.
3 //
4 // SPDX-License-Identifier: AGPL-3.0
5
6 import React from 'react';
7 import classNames from 'classnames';
8 import { withRouter, RouteComponentProps } from 'react-router';
9 import { StyleRulesCallback, Button, WithStyles, withStyles } from "@material-ui/core";
10 import { ReRunProcessIcon } from 'components/icon/icon';
11
12 type CssRules = 'button' | 'buttonRight';
13
14 const styles: StyleRulesCallback<CssRules> = theme => ({
15     button: {
16         boxShadow: 'none',
17         padding: '2px 10px 2px 5px',
18         fontSize: '0.75rem'
19     },
20     buttonRight: {
21         marginLeft: 'auto',
22     },
23 });
24
25 interface RefreshButtonProps {
26     onClick?: () => void;
27 }
28
29 export const LAST_REFRESH_TIMESTAMP = 'lastRefreshTimestamp';
30
31 export const RefreshButton = ({ history, classes, onClick }: RouteComponentProps & WithStyles<CssRules> & RefreshButtonProps) =>
32     <Button
33         color="primary"
34         size="small"
35         variant="contained"
36         onClick={() => {
37             // Notify interested parties that the refresh button was clicked.
38             const now = (new Date()).getTime();
39             localStorage.setItem(LAST_REFRESH_TIMESTAMP, now.toString());
40             history.replace(window.location.pathname);
41             if (onClick) {
42                 onClick();
43             }
44         }}
45         className={classNames(classes.buttonRight, classes.button)}>
46         <ReRunProcessIcon />
47         Refresh
48     </Button>;
49
50 export default withStyles(styles)(withRouter(RefreshButton));