16622: Show connection info for projects as well.
[arvados-workbench2.git] / src / views-components / webdav-s3-dialog / webdav-s3-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from "react";
6 import { Dialog, DialogActions, Button, StyleRulesCallback, WithStyles, withStyles, CardHeader, Tab, Tabs } from '@material-ui/core';
7 import { withDialog } from "~/store/dialog/with-dialog";
8 import { COLLECTION_WEBDAV_S3_DIALOG_NAME, WebDavS3InfoDialogData } from '~/store/collections/collection-info-actions';
9 import { WithDialogProps } from '~/store/dialog/with-dialog';
10 import { compose } from 'redux';
11 import { DetailsAttribute } from "~/components/details-attribute/details-attribute";
12
13 type CssRules = 'details';
14
15 const styles: StyleRulesCallback<CssRules> = theme => ({
16     details: {
17         marginLeft: theme.spacing.unit * 3,
18         marginRight: theme.spacing.unit * 3,
19     }
20 });
21
22 interface TabPanelData {
23     children: React.ReactElement<any>[];
24     value: number;
25     index: number;
26 }
27
28 function TabPanel(props: TabPanelData) {
29     const { children, value, index } = props;
30
31     return (
32         <div
33             role="tabpanel"
34             hidden={value !== index}
35             id={`simple-tabpanel-${index}`}
36             aria-labelledby={`simple-tab-${index}`}
37         >
38             {value === index && children}
39         </div>
40     );
41 }
42
43 export const WebDavS3InfoDialog = compose(
44     withDialog(COLLECTION_WEBDAV_S3_DIALOG_NAME),
45     withStyles(styles),
46 )(
47     (props: WithDialogProps<WebDavS3InfoDialogData> & WithStyles<CssRules>) => {
48         if (!props.data.downloadUrl) { return null; }
49
50         let winDav;
51         let cyberDav;
52
53         if (props.data.collectionsUrl.indexOf("*") > -1) {
54             const withuuid = props.data.collectionsUrl.replace("*", props.data.uuid);
55             winDav = new URL(withuuid);
56             cyberDav = new URL(withuuid);
57         } else {
58             winDav = new URL(props.data.downloadUrl);
59             cyberDav = new URL(props.data.downloadUrl);
60             winDav.pathname = `/by_id/${props.data.uuid}/`;
61             cyberDav.pathname = `/by_id/${props.data.uuid}/`;
62         }
63
64         cyberDav.protocol = { "http:": "dav:", "https:": "davs:" }[cyberDav.protocol];
65
66         const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
67
68         const sp = props.data.token.split("/");
69         let tokenUuid;
70         let tokenSecret;
71         if (sp.length === 3 && sp[0] === "v2" && props.data.homeCluster === props.data.localCluster) {
72             tokenUuid = sp[1];
73             tokenSecret = sp[2];
74         } else {
75             tokenUuid = props.data.token.replace(/\//g, "_");
76             tokenSecret = tokenUuid;
77         }
78
79         const supportsWebdav = (props.data.uuid.indexOf("-4zz18-") === 5);
80
81         let activeTab = props.data.activeTab;
82         if (!supportsWebdav) {
83             activeTab = 2;
84         }
85
86         return <Dialog
87             open={props.open}
88             maxWidth="md"
89             onClose={props.closeDialog}
90             style={{ alignSelf: 'stretch' }}>
91             <CardHeader
92                 title={`Open as Network Folder or S3 Bucket`} />
93             <div className={props.classes.details} >
94                 <Tabs value={activeTab} onChange={props.data.setActiveTab}>
95                     {supportsWebdav && <Tab value={0} key="cyberduck" label="Cyberduck/Mountain Duck or Gnome Files" />}
96                     {supportsWebdav && <Tab value={1} key="windows" label="Windows or MacOS" />}
97                     <Tab value={2} key="s3" label="S3 bucket" />
98                 </Tabs>
99
100                 <TabPanel index={0} value={activeTab}>
101                     <h2>Settings</h2>
102
103                     <DetailsAttribute
104                         label='Internet address'
105                         value={<a href={winDav.toString()}>{winDav.toString()}</a>}
106                         copyValue={winDav.toString()} />
107
108                     <DetailsAttribute
109                         label='Username'
110                         value={props.data.username}
111                         copyValue={props.data.username} />
112
113                     <DetailsAttribute
114                         label='Password'
115                         value={props.data.token}
116                         copyValue={props.data.token} />
117
118                     <h3>Windows</h3>
119                     <ol>
120                         <li>Open File Explorer</li>
121                         <li>Click on "This PC", then go to Computer &rarr; Add a Network Location</li>
122                         <li>Click Next, then choose "Add a custom network location", then click Next</li>
123                     </ol>
124
125                     <h3>MacOS</h3>
126                     <ol>
127                         <li>Open Finder</li>
128                         <li>Click Go &rarr; Connect to server</li>
129                     </ol>
130                 </TabPanel>
131
132                 <TabPanel index={1} value={activeTab}>
133                     <DetailsAttribute
134                         label='Server'
135                         value={<a href={cyberDav.toString()}>{cyberDav.toString()}</a>}
136                         copyValue={cyberDav.toString()} />
137
138                     <DetailsAttribute
139                         label='Username'
140                         value={props.data.username}
141                         copyValue={props.data.username} />
142
143                     <DetailsAttribute
144                         label='Password'
145                         value={props.data.token}
146                         copyValue={props.data.token} />
147
148                     <h3>Gnome</h3>
149                     <ol>
150                         <li>Open Files</li>
151                         <li>Select +Other Locations</li>
152                         <li>Connect to Server &rarr; Enter server address</li>
153                     </ol>
154
155                 </TabPanel>
156
157                 <TabPanel index={2} value={activeTab}>
158                     <DetailsAttribute
159                         label='Endpoint'
160                         value={s3endpoint.host}
161                         copyValue={s3endpoint.host} />
162
163                     <DetailsAttribute
164                         label='Bucket'
165                         value={props.data.uuid}
166                         copyValue={props.data.uuid} />
167
168                     <DetailsAttribute
169                         label='Access Key'
170                         value={tokenUuid}
171                         copyValue={tokenUuid} />
172
173                     <DetailsAttribute
174                         label='Secret Key'
175                         value={tokenSecret}
176                         copyValue={tokenSecret} />
177
178                 </TabPanel>
179
180             </div>
181             <DialogActions>
182                 <Button
183                     variant='text'
184                     color='primary'
185                     onClick={props.closeDialog}>
186                     Close
187                 </Button>
188             </DialogActions>
189
190         </Dialog >;
191     }
192 );