018a0ef096b550191a953f545460d25b4f275d6d
[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 export 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.username = props.data.username;
65         const cyberDavStr = "dav" + cyberDav.toString().slice(4);
66
67         const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
68
69         const sp = props.data.token.split("/");
70         let tokenUuid;
71         let tokenSecret;
72         if (sp.length === 3 && sp[0] === "v2" && sp[1].slice(0, 5) === props.data.localCluster) {
73             tokenUuid = sp[1];
74             tokenSecret = sp[2];
75         } else {
76             tokenUuid = props.data.token.replace(/\//g, "_");
77             tokenSecret = tokenUuid;
78         }
79
80         const supportsWebdav = (props.data.uuid.indexOf("-4zz18-") === 5);
81
82         let activeTab = props.data.activeTab;
83         if (!supportsWebdav) {
84             activeTab = 2;
85         }
86
87         return <Dialog
88             open={props.open}
89             maxWidth="md"
90             onClose={props.closeDialog}
91             style={{ alignSelf: 'stretch' }}>
92             <CardHeader
93                 title={`Open as Network Folder or S3 Bucket`} />
94             <div className={props.classes.details} >
95                 <Tabs value={activeTab} onChange={props.data.setActiveTab}>
96                     {supportsWebdav && <Tab value={0} key="cyberduck" label="Cyberduck/Mountain Duck or Gnome Files" />}
97                     {supportsWebdav && <Tab value={1} key="windows" label="Windows or MacOS" />}
98                     <Tab value={2} key="s3" label="S3 bucket" />
99                 </Tabs>
100
101                 <TabPanel index={1} value={activeTab}>
102                     <h2>Settings</h2>
103
104                     <DetailsAttribute
105                         label='Internet address'
106                         value={<a href={winDav.toString()} target="_blank">{winDav.toString()}</a>}
107                         copyValue={winDav.toString()} />
108
109                     <DetailsAttribute
110                         label='Username'
111                         value={props.data.username}
112                         copyValue={props.data.username} />
113
114                     <DetailsAttribute
115                         label='Password'
116                         value={props.data.token}
117                         copyValue={props.data.token} />
118
119                     <h3>Windows</h3>
120                     <ol>
121                         <li>Open File Explorer</li>
122                         <li>Click on "This PC", then go to Computer &rarr; Add a Network Location</li>
123                         <li>Click Next, then choose "Add a custom network location", then click Next</li>
124                     </ol>
125
126                     <h3>MacOS</h3>
127                     <ol>
128                         <li>Open Finder</li>
129                         <li>Click Go &rarr; Connect to server</li>
130                     </ol>
131                 </TabPanel>
132
133                 <TabPanel index={0} value={activeTab}>
134                     <DetailsAttribute
135                         label='Server'
136                         value={<a href={cyberDavStr} target="_blank">{cyberDavStr}</a>}
137                         copyValue={cyberDavStr} />
138
139                     <DetailsAttribute
140                         label='Username'
141                         value={props.data.username}
142                         copyValue={props.data.username} />
143
144                     <DetailsAttribute
145                         label='Password'
146                         value={props.data.token}
147                         copyValue={props.data.token} />
148
149                     <h3>Gnome</h3>
150                     <ol>
151                         <li>Open Files</li>
152                         <li>Select +Other Locations</li>
153                         <li>Connect to Server &rarr; Enter server address</li>
154                     </ol>
155
156                 </TabPanel>
157
158                 <TabPanel index={2} value={activeTab}>
159                     <DetailsAttribute
160                         label='Endpoint'
161                         value={s3endpoint.host}
162                         copyValue={s3endpoint.host} />
163
164                     <DetailsAttribute
165                         label='Bucket'
166                         value={props.data.uuid}
167                         copyValue={props.data.uuid} />
168
169                     <DetailsAttribute
170                         label='Access Key'
171                         value={tokenUuid}
172                         copyValue={tokenUuid} />
173
174                     <DetailsAttribute
175                         label='Secret Key'
176                         value={tokenSecret}
177                         copyValue={tokenSecret} />
178
179                 </TabPanel>
180
181             </div>
182             <DialogActions>
183                 <Button
184                     variant='text'
185                     color='primary'
186                     onClick={props.closeDialog}>
187                     Close
188                 </Button>
189             </DialogActions>
190
191         </Dialog >;
192     }
193 );