22141: Add details for more object types
[arvados.git] / services / workbench2 / src / views-components / sharing-dialog / sharing-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { compose, Dispatch } from 'redux';
6 import { connect } from 'react-redux';
7 import { RootState } from 'store/store';
8 import { formValueSelector } from 'redux-form'
9 import {
10     connectSharingDialog,
11     saveSharingDialogChanges,
12     connectSharingDialogProgress,
13     SharingDialogData,
14     createSharingToken,
15     initializeManagementForm
16 } from 'store/sharing-dialog/sharing-dialog-actions';
17 import { WithDialogProps } from 'store/dialog/with-dialog';
18 import {
19     SharingDialogComponent,
20     SharingDialogDataProps,
21     SharingDialogActionProps
22 } from './sharing-dialog-component';
23 import {
24     getSharingPublicAccessFormData,
25     hasChanges,
26     SHARING_DIALOG_NAME,
27     SHARING_MANAGEMENT_FORM_NAME,
28     VisibilityLevel
29 } from 'store/sharing-dialog/sharing-dialog-types';
30 import { WithProgressStateProps } from 'store/progress-indicator/with-progress';
31 import { getDialog } from 'store/dialog/dialog-reducer';
32 import { filterResources } from 'store/resources/resources';
33 import { ApiClientAuthorization } from 'models/api-client-authorization';
34 import { ResourceKind } from 'models/resource';
35
36 type Props = WithDialogProps<string> & WithProgressStateProps;
37
38 const sharingManagementFormSelector = formValueSelector(SHARING_MANAGEMENT_FORM_NAME);
39
40 const mapStateToProps = (state: RootState, { working, ...props }: Props): SharingDialogDataProps => {
41     const dialog = getDialog<SharingDialogData>(state.dialog, SHARING_DIALOG_NAME);
42     const sharedResourceUuid = dialog?.data.resourceUuid || '';
43     const sharingURLsDisabled = state.auth.config.clusterConfig.Workbench.DisableSharingURLsUI;
44     return ({
45         ...props,
46         permissions: sharingManagementFormSelector(state, 'permissions'),
47         saveEnabled: hasChanges(state),
48         loading: working,
49         sharedResourceUuid,
50         sharingURLsDisabled,
51         sharingURLsNr: !sharingURLsDisabled
52             ? (filterResources((resource: ApiClientAuthorization) =>
53                 resource.kind === ResourceKind.API_CLIENT_AUTHORIZATION &&
54                 resource.scopes.includes(`GET /arvados/v1/collections/${sharedResourceUuid}`) &&
55                 resource.scopes.includes(`GET /arvados/v1/collections/${sharedResourceUuid}/`) &&
56                 resource.scopes.includes('GET /arvados/v1/keep_services/accessible')
57             )(state.resources) as ApiClientAuthorization[]).length
58             : 0,
59         privateAccess: getSharingPublicAccessFormData(state)?.visibility === VisibilityLevel.PRIVATE,
60     })
61 };
62
63 const mapDispatchToProps = (dispatch: Dispatch, { ...props }: Props): SharingDialogActionProps => ({
64     ...props,
65     onClose: props.closeDialog,
66     onSave: () => {
67         setTimeout(() => dispatch<any>(saveSharingDialogChanges), 0);
68     },
69     onCreateSharingToken: (d: Date) => () => {
70         dispatch<any>(createSharingToken(d));
71     },
72     refreshPermissions: () => {
73         dispatch<any>(initializeManagementForm);
74     }
75 });
76
77 export const SharingDialog = compose(
78     connectSharingDialog,
79     connectSharingDialogProgress,
80     connect(mapStateToProps, mapDispatchToProps)
81 )(SharingDialogComponent);