/
ConfirmDeleteDialogBody.tsx
272 lines (255 loc) · 9.21 KB
/
ConfirmDeleteDialogBody.tsx
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import React, {useCallback} from 'react'
import {
WarningOutlineIcon,
DocumentsIcon,
ClipboardIcon,
UnknownIcon,
ChevronDownIcon,
} from '@sanity/icons'
import {useToast, Text, Box, Button, Flex, Label, Card, Stack} from '@sanity/ui'
import CopyToClipboard from 'react-copy-to-clipboard'
import {ReferencePreviewLink} from './ReferencePreviewLink'
import {ReferringDocuments} from './useReferringDocuments'
import {
ReferencesCard,
OtherReferenceCount,
CrossDatasetReferencesDetails,
CrossDatasetReferencesSummary,
TableContainer,
Table,
ChevronWrapper,
DocumentIdFlex,
} from './ConfirmDeleteDialogBody.styles'
import {SanityDefaultPreview, useSchema} from 'sanity'
type DeletionConfirmationDialogBodyProps = Required<ReferringDocuments> & {
documentTitle: React.ReactNode
action: string
onReferenceLinkClick?: () => void
}
/**
* The inner part of the `ConfirmDeleteDialog`. This is ran when both the
* `crossDatasetReferences` and `internalReferences` are loaded.
*/
export function ConfirmDeleteDialogBody({
crossDatasetReferences,
internalReferences,
documentTitle,
totalCount,
action,
datasetNames,
hasUnknownDatasetNames,
onReferenceLinkClick,
}: DeletionConfirmationDialogBodyProps) {
const schema = useSchema()
const toast = useToast()
const renderPreviewItem = useCallback(
(item: any) => {
const type = schema.get(item._type)
if (type) {
return <ReferencePreviewLink type={type} value={item} onClick={onReferenceLinkClick} />
}
return (
// Padding added to match the ReferencePreviewLink styling
<Box padding={2}>
<SanityDefaultPreview
icon={UnknownIcon}
title="Preview Unavailable"
subtitle={`ID: ${item._id}`}
layout="default"
/>
</Box>
)
},
[schema, onReferenceLinkClick]
)
if (internalReferences?.totalCount === 0 && crossDatasetReferences?.totalCount === 0) {
return (
<Text as="p">
Are you sure you want to delete <strong>“{documentTitle}”</strong>?
</Text>
)
}
const documentCount =
crossDatasetReferences.totalCount === 1
? '1 document'
: `${crossDatasetReferences.totalCount.toLocaleString()} documents`
// We do some extra checks to handle cases where you have unavailable dataset
// name(s) due to permissions, both alone and in combination with known datasets
// This normalizes one or more undefined dataset names to the catch-all `unavailable`
const normalizedDatasetNames = [
...datasetNames,
...(hasUnknownDatasetNames ? ['unavailable'] : []),
]
const datasetsCount =
normalizedDatasetNames.length === 1
? 'another dataset'
: `${normalizedDatasetNames.length} datasets`
let datasetNameList = `Dataset${
normalizedDatasetNames.length === 1 ? '' : 's'
}: ${normalizedDatasetNames.join(', ')}`
// We only have one dataset, and it is unavailable due to permissions
if (hasUnknownDatasetNames && normalizedDatasetNames.length === 1) {
datasetNameList = 'Unavailable dataset'
}
return (
<>
<Card padding={3} radius={2} tone="caution" marginBottom={4} flex="none">
<Flex>
<Text aria-hidden="true" size={1}>
<WarningOutlineIcon />
</Text>
<Box flex={1} marginLeft={3}>
<Text size={1}>
{totalCount === 1 ? (
<>1 document refers to “{documentTitle}”</>
) : (
<>
{totalCount.toLocaleString()} documents refer to “{documentTitle}”
</>
)}
</Text>
</Box>
</Flex>
</Card>
<Box flex="none" marginBottom={4}>
<Text>
You may not be able to {action} “{documentTitle}” because the following documents refer to
it:
</Text>
</Box>
<ReferencesCard>
<Flex direction="column" height="fill">
{internalReferences.totalCount > 0 && (
<Stack as="ul" padding={3} space={3} overflow="auto" data-testid="internal-references">
{internalReferences?.references.map((item) => (
<Box as="li" key={item._id}>
{renderPreviewItem(item)}
</Box>
))}
{internalReferences.totalCount > internalReferences.references.length && (
<Box as="li" padding={3}>
<OtherReferenceCount {...internalReferences} />
</Box>
)}
</Stack>
)}
{crossDatasetReferences.totalCount > 0 && (
<CrossDatasetReferencesDetails
data-testid="cross-dataset-references"
style={{
// only add the border if needed
borderTop:
internalReferences.totalCount > 0
? '1px solid var(--card-shadow-outline-color)'
: undefined,
}}
>
<CrossDatasetReferencesSummary>
<Flex padding={4} align="center">
<Box marginRight={4}>
<Text size={3}>
<DocumentsIcon />
</Text>
</Box>
<Flex marginRight={4} direction="column">
<Box marginBottom={2}>
<Text>
{documentCount} in {datasetsCount}
</Text>
</Box>
<Box>
<Text title={datasetNameList} textOverflow="ellipsis" size={1} muted>
{datasetNameList}
</Text>
</Box>
</Flex>
<ChevronWrapper>
<Text muted>
<ChevronDownIcon />
</Text>
</ChevronWrapper>
</Flex>
</CrossDatasetReferencesSummary>
<TableContainer>
<Table>
<thead>
<tr>
<th>
<Label muted size={0}>
Project ID
</Label>
</th>
<th>
<Label muted size={0}>
Dataset
</Label>
</th>
<th>
<Label muted size={0}>
Document ID
</Label>
</th>
</tr>
</thead>
<tbody>
{crossDatasetReferences.references
.filter((reference): reference is Required<typeof reference> => {
return 'projectId' in reference
})
.map(({projectId, datasetName, documentId}, index) => (
// eslint-disable-next-line react/no-array-index-key
<tr key={`${documentId}-${index}`}>
<td>
<Text size={1}>{projectId}</Text>
</td>
<td>
<Text size={1}>{datasetName || 'unavailable'}</Text>
</td>
<td>
<DocumentIdFlex align="center" gap={2} justify="flex-end">
<Text textOverflow="ellipsis" size={1}>
{documentId || 'unavailable'}
</Text>
{documentId && (
<CopyToClipboard
text={documentId}
// eslint-disable-next-line react/jsx-no-bind
onCopy={() => {
// TODO: this isn't visible with the dialog open
toast.push({
title: 'Copied document ID to clipboard!',
status: 'success',
})
}}
>
<Button
title="Copy ID to clipboard"
mode="bleed"
icon={ClipboardIcon}
fontSize={0}
/>
</CopyToClipboard>
)}
</DocumentIdFlex>
</td>
</tr>
))}
</tbody>
</Table>
<Box padding={2}>
<OtherReferenceCount {...crossDatasetReferences} />
</Box>
</TableContainer>
</CrossDatasetReferencesDetails>
)}
</Flex>
</ReferencesCard>
<Box flex="none">
<Text>
If you {action} this document, documents that refer to it will no longer be able to access
it.
</Text>
</Box>
</>
)
}