21846: applied select to context menu open
[arvados.git] / services / workbench2 / src / views-components / token-dialog / token-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import {
7     Dialog,
8     DialogActions,
9     DialogTitle,
10     DialogContent,
11     WithStyles,
12     withStyles,
13     StyleRulesCallback,
14     Button,
15     Typography
16 } from '@material-ui/core';
17 import CopyToClipboard from 'react-copy-to-clipboard';
18 import { ArvadosTheme } from 'common/custom-theme';
19 import { withDialog } from 'store/dialog/with-dialog';
20 import { WithDialogProps } from 'store/dialog/with-dialog';
21 import { connect, DispatchProp } from 'react-redux';
22 import {
23     TokenDialogData,
24     getTokenDialogData,
25     TOKEN_DIALOG_NAME,
26 } from 'store/token-dialog/token-dialog-actions';
27 import { DefaultCodeSnippet } from 'components/default-code-snippet/default-code-snippet';
28 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
29 import { getNewExtraToken } from 'store/auth/auth-action';
30 import { DetailsAttributeComponent } from 'components/details-attribute/details-attribute';
31 import moment from 'moment';
32
33 type CssRules = 'link' | 'paper' | 'button' | 'actionButton' | 'codeBlock';
34
35 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
36     link: {
37         color: theme.palette.primary.main,
38         textDecoration: 'none',
39         margin: '0px 4px'
40     },
41     paper: {
42         padding: theme.spacing.unit,
43         marginBottom: theme.spacing.unit * 2,
44         backgroundColor: theme.palette.grey["200"],
45         border: `1px solid ${theme.palette.grey["300"]}`
46     },
47     button: {
48         fontSize: '0.8125rem',
49         fontWeight: 600
50     },
51     actionButton: {
52         boxShadow: 'none',
53         marginTop: theme.spacing.unit * 2,
54         marginBottom: theme.spacing.unit * 2,
55         marginRight: theme.spacing.unit * 2,
56     },
57     codeBlock: {
58         fontSize: '0.8125rem',
59     },
60 });
61
62 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
63
64 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
65     onCopy = (message: string) => {
66         this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
67             message,
68             hideDuration: 2000,
69             kind: SnackbarKind.SUCCESS
70         }));
71     }
72
73     onGetNewToken = async () => {
74         const newToken = await this.props.dispatch<any>(getNewExtraToken());
75         if (newToken) {
76             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
77                 message: 'New token retrieved',
78                 hideDuration: 2000,
79                 kind: SnackbarKind.SUCCESS
80             }));
81         } else {
82             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
83                 message: 'Creating new tokens is not allowed',
84                 hideDuration: 2000,
85                 kind: SnackbarKind.WARNING
86             }));
87         }
88     }
89
90     getSnippet = ({ apiHost, token }: TokenDialogData) =>
91         `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
92 export ARVADOS_API_TOKEN=${token}
93 export ARVADOS_API_HOST=${apiHost}
94 unset ARVADOS_API_HOST_INSECURE`
95
96     render() {
97         const { classes, open, closeDialog, ...data } = this.props;
98         const tokenExpiration = data.tokenExpiration
99             ? `${data.tokenExpiration.toLocaleString()} (${moment(data.tokenExpiration).fromNow()})`
100             : `This token does not have an expiration date`;
101
102         return <Dialog
103             open={open}
104             onClose={closeDialog}
105             fullWidth={true}
106             maxWidth='md'>
107             <DialogTitle>Get API Token</DialogTitle>
108             <DialogContent>
109                 <Typography paragraph={true}>
110                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
111                     <Typography component='span'>
112                         For more information see
113                         <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' rel="noopener" className={classes.link}>
114                             Getting an API token.
115                         </a>
116                     </Typography>
117                 </Typography>
118
119                 <DetailsAttributeComponent label='API Host' value={data.apiHost} copyValue={data.apiHost} onCopy={this.onCopy} />
120                 <DetailsAttributeComponent label='API Token' value={data.token} copyValue={data.token} onCopy={this.onCopy} />
121                 <DetailsAttributeComponent label='Token expiration' value={tokenExpiration} />
122                 {this.props.canCreateNewTokens && <Button
123                     onClick={() => this.onGetNewToken()}
124                     color="primary"
125                     size="small"
126                     variant="contained"
127                     className={classes.actionButton}
128                 >
129                     GET NEW TOKEN
130                 </Button>}
131
132                 <Typography paragraph={true}>
133                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
134                 </Typography>
135                 <DefaultCodeSnippet className={classes.codeBlock} lines={[this.getSnippet(data)]} />
136                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Shell code block copied')}>
137                     <Button
138                         color="primary"
139                         size="small"
140                         variant="contained"
141                         className={classes.actionButton}
142                     >
143                         Copy link to clipBOARD
144                     </Button>
145                 </CopyToClipboard>
146                 <Typography>
147                     Arvados
148                     <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' rel="noopener" className={classes.link}>virtual machines</a>
149                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
150                 </Typography>
151             </DialogContent>
152             <DialogActions>
153                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
154             </DialogActions>
155         </Dialog>;
156     }
157 }
158
159 export const TokenDialog =
160     withStyles(styles)(
161         connect(getTokenDialogData)(
162             withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));