-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAccessDatasetMenu.tsx
More file actions
198 lines (184 loc) · 6.82 KB
/
AccessDatasetMenu.tsx
File metadata and controls
198 lines (184 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { useState } from 'react'
import { DropdownButton, DropdownButtonItem, DropdownHeader } from '@iqss/dataverse-design-system'
import { Download as DownloadIcon } from 'react-bootstrap-icons'
import { useTranslation } from 'react-i18next'
import { toast } from 'react-toastify'
import {
CustomTerms,
DatasetLicense,
DatasetPermissions,
DatasetPublishingStatus,
DatasetVersion,
defaultLicense
} from '../../../../dataset/domain/models/Dataset'
import { FileDownloadSize, FileDownloadMode } from '../../../../files/domain/models/FileMetadata'
import { DatasetExploreOptions } from '../DatasetToolsOptions'
import { useAccessRepository } from '@/sections/access/AccessRepositoryContext'
import { DownloadWithTermsAndGuestbookModal } from '@/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/file-options-menu/DownloadWithTermsAndGuestbookModal'
import {
downloadFromSignedUrl,
EMPTY_GUESTBOOK_RESPONSE,
requestSignedDownloadUrlFromAccessApi
} from '@/shared/helpers/DownloadHelper'
// TODO: add compute feature
interface AccessDatasetMenuProps {
datasetNumericId?: number | string
version: DatasetVersion
permissions: DatasetPermissions
hasOneTabularFileAtLeast: boolean
fileDownloadSizes: FileDownloadSize[]
fileStore: string | undefined
persistentId: string
guestbookId?: number
license?: DatasetLicense
customTerms?: CustomTerms
}
export function AccessDatasetMenu({
datasetNumericId,
version,
permissions,
hasOneTabularFileAtLeast,
fileDownloadSizes,
fileStore,
persistentId,
guestbookId,
license,
customTerms
}: AccessDatasetMenuProps) {
const { t } = useTranslation('dataset')
const [showDownloadWithTermsAndGuestbookModal, setShowDownloadWithTermsAndGuestbookModal] =
useState(false)
const [selectedDownloadFormat, setSelectedDownloadFormat] = useState<FileDownloadMode>(
FileDownloadMode.ORIGINAL
)
const isDraft = version.publishingStatus === DatasetPublishingStatus.DRAFT
const bypassTermsGuard = isDraft || permissions.canUpdateDataset
const hasGuestbook = guestbookId !== undefined
const hasNonDefaultLicense = license !== undefined && license.name !== defaultLicense.name
const hasCustomTerms = customTerms !== undefined
const shouldShowModal =
!bypassTermsGuard && (hasGuestbook || hasCustomTerms || hasNonDefaultLicense)
const flesToDownloadSizeIsZero =
fileDownloadSizes.map(({ value }) => value).reduce((acc, curr) => acc + curr, 0) === 0
if (
flesToDownloadSizeIsZero ||
!permissions.canDownloadFiles ||
(version.publishingStatus === DatasetPublishingStatus.DEACCESSIONED &&
!permissions.canUpdateDataset)
) {
return <></>
}
// TODO: remove this when we can handle non-S3 files
if (!fileStore?.startsWith('s3')) {
return <></>
}
const handleDownloadWithGuestbook = (
event: React.MouseEvent<HTMLElement>,
mode: FileDownloadMode
) => {
event.preventDefault()
setSelectedDownloadFormat(mode)
setShowDownloadWithTermsAndGuestbookModal(true)
}
return (
<>
<DropdownButton
id={`access-dataset-menu`}
title={t('datasetActionButtons.accessDataset.title')}
asButtonGroup
variant="primary">
<DropdownHeader className="d-flex align-items-center gap-1">
{t('datasetActionButtons.accessDataset.downloadOptions.header')} <DownloadIcon />
</DropdownHeader>
<DatasetDownloadOptions
datasetNumericId={datasetNumericId}
hasOneTabularFileAtLeast={hasOneTabularFileAtLeast}
fileDownloadSizes={fileDownloadSizes}
requiresTermsOrGuestbook={shouldShowModal}
onDownloadWithGuestbook={handleDownloadWithGuestbook}
/>
<DatasetExploreOptions persistentId={persistentId} />
</DropdownButton>
{shouldShowModal && showDownloadWithTermsAndGuestbookModal && (
<DownloadWithTermsAndGuestbookModal
show={showDownloadWithTermsAndGuestbookModal}
handleClose={() => setShowDownloadWithTermsAndGuestbookModal(false)}
datasetId={datasetNumericId} // TODO: we should allow this to pass persistentId when we have the backend support for guestbook submission with persistentId
datasetPersistentId={persistentId}
guestbookId={guestbookId}
format={selectedDownloadFormat}
datasetLicense={license}
datasetCustomTerms={customTerms}
/>
)}
</>
)
}
interface DatasetDownloadOptionsProps {
datasetNumericId?: number | string
hasOneTabularFileAtLeast: boolean
fileDownloadSizes: FileDownloadSize[]
requiresTermsOrGuestbook: boolean
onDownloadWithGuestbook: (event: React.MouseEvent<HTMLElement>, mode: FileDownloadMode) => void
}
const DatasetDownloadOptions = ({
datasetNumericId,
hasOneTabularFileAtLeast,
fileDownloadSizes,
requiresTermsOrGuestbook,
onDownloadWithGuestbook
}: DatasetDownloadOptionsProps) => {
const { t } = useTranslation('dataset')
const { t: tFiles } = useTranslation('files')
const accessRepository = useAccessRepository()
const handleDirectDownload = (
event: React.MouseEvent<HTMLElement>,
mode: FileDownloadMode
): void => {
if (requiresTermsOrGuestbook) {
onDownloadWithGuestbook(event, mode)
return
}
if (datasetNumericId === undefined) {
return
}
event.preventDefault()
void requestSignedDownloadUrlFromAccessApi({
accessRepository,
datasetId: datasetNumericId,
fileIds: undefined,
guestbookResponse: EMPTY_GUESTBOOK_RESPONSE,
format: mode
})
.then(downloadFromSignedUrl)
.then(() => {
toast.success(tFiles('actions.optionsMenu.guestbookCollectModal.downloadStarted'))
})
.catch(() => {
toast.error(tFiles('actions.optionsMenu.guestbookCollectModal.downloadError'))
})
}
function getFormattedFileSize(mode: FileDownloadMode): string {
const foundSize = fileDownloadSizes.find((size) => size.mode === mode)
return foundSize ? foundSize.toString() : ''
}
return hasOneTabularFileAtLeast ? (
<>
<DropdownButtonItem
onClick={(event) => handleDirectDownload(event, FileDownloadMode.ORIGINAL)}>
{t('datasetActionButtons.accessDataset.downloadOptions.originalZip')} (
{getFormattedFileSize(FileDownloadMode.ORIGINAL)})
</DropdownButtonItem>
<DropdownButtonItem
onClick={(event) => handleDirectDownload(event, FileDownloadMode.ARCHIVAL)}>
{t('datasetActionButtons.accessDataset.downloadOptions.archivalZip')} (
{getFormattedFileSize(FileDownloadMode.ARCHIVAL)})
</DropdownButtonItem>
</>
) : (
<DropdownButtonItem onClick={(event) => handleDirectDownload(event, FileDownloadMode.ORIGINAL)}>
{t('datasetActionButtons.accessDataset.downloadOptions.zip')} (
{getFormattedFileSize(FileDownloadMode.ORIGINAL)})
</DropdownButtonItem>
)
}