1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
19 } from '@material-ui/core';
24 } from '@material-ui/core/styles';
25 import { DialogActions } from 'components/dialog-actions/dialog-actions';
26 import { SharingURLsContent } from './sharing-urls';
28 extractUuidObjectType,
30 } from 'models/resource';
31 import { SharingInvitationForm } from './sharing-invitation-form';
32 import { SharingManagementForm } from './sharing-management-form';
36 MuiPickersUtilsProvider,
38 } from 'material-ui-pickers';
39 import DateFnsUtils from "@date-io/date-fns";
40 import moment from 'moment';
41 import { SharingPublicAccessForm } from './sharing-public-access-form';
43 export interface SharingDialogDataProps {
47 sharedResourceUuid: string;
48 sharingURLsNr: number;
49 privateAccess: boolean;
50 sharingURLsDisabled: boolean;
52 export interface SharingDialogActionProps {
55 onCreateSharingToken: (d: Date | undefined) => () => void;
56 refreshPermissions: () => void;
58 enum SharingDialogTab {
62 export type SharingDialogComponentProps = SharingDialogDataProps & SharingDialogActionProps;
64 export default (props: SharingDialogComponentProps) => {
65 const { open, loading, saveEnabled, sharedResourceUuid,
66 sharingURLsNr, privateAccess, sharingURLsDisabled,
67 onClose, onSave, onCreateSharingToken, refreshPermissions } = props;
68 const showTabs = !sharingURLsDisabled && extractUuidObjectType(sharedResourceUuid) === ResourceObjectType.COLLECTION;
69 const [tabNr, setTabNr] = React.useState<number>(SharingDialogTab.PERMISSIONS);
70 const [expDate, setExpDate] = React.useState<Date>();
71 const [withExpiration, setWithExpiration] = React.useState<boolean>(false);
73 // Sets up the dialog depending on the resource type
74 if (!showTabs && tabNr !== SharingDialogTab.PERMISSIONS) {
75 setTabNr(SharingDialogTab.PERMISSIONS);
78 React.useEffect(() => {
79 if (!withExpiration) {
80 setExpDate(undefined);
82 setExpDate(moment().add(2, 'hour').minutes(0).seconds(0).toDate());
87 {...{ open, onClose }}
88 className="sharing-dialog"
91 disableBackdropClick={saveEnabled}
92 disableEscapeKeyDown={saveEnabled}>
98 onChange={(_, tb) => {
99 if (tb === SharingDialogTab.PERMISSIONS) {
100 refreshPermissions();
104 <Tab label="With users/groups" />
105 <Tab label={`Sharing URLs ${sharingURLsNr > 0 ? '('+sharingURLsNr+')' : ''}`} disabled={saveEnabled} />
109 { tabNr === SharingDialogTab.PERMISSIONS &&
110 <Grid container direction='column' spacing={24}>
112 <SharingPublicAccessForm />
115 <SharingManagementForm />
119 { tabNr === SharingDialogTab.URLS &&
120 <SharingURLsContent uuid={sharedResourceUuid} />
124 <Grid container spacing={8}>
125 { tabNr === SharingDialogTab.PERMISSIONS &&
127 <SharingInvitationForm />
130 { tabNr === SharingDialogTab.URLS && withExpiration && <>
131 <Grid item container direction='row' md={12}>
132 <MuiPickersUtilsProvider utils={DateFnsUtils}>
133 <BasePicker autoOk value={expDate} onChange={setExpDate}>
134 {({ date, handleChange }) => (<>
136 <Calendar date={date} minDate={new Date()} maxDate={undefined}
137 onChange={handleChange} />
140 <TimePickerView type="hours" date={date} ampm={false}
141 onMinutesChange={() => {}}
142 onSecondsChange={() => {}}
143 onHourChange={handleChange}
148 </MuiPickersUtilsProvider>
151 <Typography variant='caption' align='center'>
152 Maximum expiration date may be limited by the cluster configuration.
157 { tabNr === SharingDialogTab.PERMISSIONS && !sharingURLsDisabled &&
158 privateAccess && sharingURLsNr > 0 &&
160 <Typography variant='caption' align='center' color='error'>
161 Although there aren't specific permissions set, this is publicly accessible via Sharing URL(s).
166 { tabNr === SharingDialogTab.URLS && <>
167 <Grid item><FormControlLabel
168 control={<Checkbox color="primary" checked={withExpiration}
169 onChange={(e) => setWithExpiration(e.target.checked)} />}
170 label="With expiration" />
173 <Button variant="contained" color="primary"
174 disabled={expDate !== undefined && expDate <= new Date()}
175 onClick={onCreateSharingToken(expDate)}>
181 { tabNr === SharingDialogTab.PERMISSIONS &&
183 <Button onClick={onSave} variant="contained" color="primary"
184 disabled={!saveEnabled}>
190 <Button onClick={() => {
192 setWithExpiration(false);
200 loading && <LoadingIndicator />
205 const loadingIndicatorStyles: StyleRulesCallback<'root'> = theme => ({
207 position: 'absolute',
213 alignItems: 'center',
214 justifyContent: 'center',
215 backgroundColor: 'rgba(255, 255, 255, 0.8)',
219 const LoadingIndicator = withStyles(loadingIndicatorStyles)(
220 (props: WithStyles<'root'>) =>
221 <Paper classes={props.classes}>