|
| 1 | +import {Fragment} from 'react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | +import invariant from 'invariant'; |
| 4 | + |
| 5 | +import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator'; |
| 6 | +import {openConfirmModal} from 'sentry/components/confirm'; |
| 7 | +import {UserAvatar} from 'sentry/components/core/avatar/userAvatar'; |
| 8 | +import {Button} from 'sentry/components/core/button'; |
| 9 | +import {Flex} from 'sentry/components/core/layout/flex'; |
| 10 | +import {Link} from 'sentry/components/core/link'; |
| 11 | +import {Tooltip} from 'sentry/components/core/tooltip'; |
| 12 | +import Duration from 'sentry/components/duration/duration'; |
| 13 | +import ErrorBoundary from 'sentry/components/errorBoundary'; |
| 14 | +import {KeyValueData} from 'sentry/components/keyValueData'; |
| 15 | +import {SimpleTable} from 'sentry/components/tables/simpleTable'; |
| 16 | +import TimeSince from 'sentry/components/timeSince'; |
| 17 | +import {IconCalendar, IconDelete} from 'sentry/icons'; |
| 18 | +import {t, tct, tn} from 'sentry/locale'; |
| 19 | +import {space} from 'sentry/styles/space'; |
| 20 | +import type {Project} from 'sentry/types/project'; |
| 21 | +import {getShortEventId} from 'sentry/utils/events'; |
| 22 | +import type {QueryKeyEndpointOptions} from 'sentry/utils/queryClient'; |
| 23 | +import {decodeList} from 'sentry/utils/queryString'; |
| 24 | +import useDeleteReplays, { |
| 25 | + type ReplayBulkDeletePayload, |
| 26 | +} from 'sentry/utils/replays/hooks/useDeleteReplays'; |
| 27 | +import useLocationQuery from 'sentry/utils/url/useLocationQuery'; |
| 28 | +import useProjectFromId from 'sentry/utils/useProjectFromId'; |
| 29 | +import type {ReplayListRecord} from 'sentry/views/replays/types'; |
| 30 | + |
| 31 | +interface Props { |
| 32 | + queryOptions: QueryKeyEndpointOptions | undefined; |
| 33 | + replays: ReplayListRecord[]; |
| 34 | + selectedIds: 'all' | string[]; |
| 35 | +} |
| 36 | + |
| 37 | +export default function DeleteReplays({selectedIds, replays, queryOptions}: Props) { |
| 38 | + const {project: projectIds} = useLocationQuery({ |
| 39 | + fields: { |
| 40 | + project: decodeList, |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + const project = useProjectFromId({ |
| 45 | + project_id: projectIds?.length === 1 ? projectIds[0] : undefined, |
| 46 | + }); |
| 47 | + |
| 48 | + const {bulkDelete, canDelete, queryOptionsToPayload} = useDeleteReplays({ |
| 49 | + projectSlug: project?.slug ?? '', |
| 50 | + }); |
| 51 | + const deletePayload = queryOptionsToPayload(selectedIds, queryOptions ?? {}); |
| 52 | + |
| 53 | + const settingsPath = `/settings/projects/${project?.slug}/replays/?replaySettingsTab=bulk-delete`; |
| 54 | + |
| 55 | + return ( |
| 56 | + <Tooltip |
| 57 | + disabled={canDelete} |
| 58 | + title={t('Select a single project from the dropdown to delete replays')} |
| 59 | + > |
| 60 | + <Button |
| 61 | + disabled={!canDelete} |
| 62 | + icon={<IconDelete />} |
| 63 | + onClick={() => |
| 64 | + openConfirmModal({ |
| 65 | + bypass: selectedIds !== 'all' && selectedIds.length === 1, |
| 66 | + renderMessage: _props => ( |
| 67 | + <Fragment> |
| 68 | + {selectedIds === 'all' ? ( |
| 69 | + <ReplayQueryPreview deletePayload={deletePayload} project={project!} /> |
| 70 | + ) : ( |
| 71 | + <ErrorBoundary mini> |
| 72 | + <ReplayPreviewTable |
| 73 | + replays={replays} |
| 74 | + selectedIds={selectedIds} |
| 75 | + project={project!} |
| 76 | + /> |
| 77 | + </ErrorBoundary> |
| 78 | + )} |
| 79 | + </Fragment> |
| 80 | + ), |
| 81 | + renderConfirmButton: ({defaultOnClick}) => ( |
| 82 | + <Button onClick={defaultOnClick} priority="danger"> |
| 83 | + {t('Delete')} |
| 84 | + </Button> |
| 85 | + ), |
| 86 | + onConfirm: () => { |
| 87 | + bulkDelete([deletePayload], { |
| 88 | + onSuccess: () => |
| 89 | + addSuccessMessage( |
| 90 | + tct('Replays are being deleted. [link:View progress]', { |
| 91 | + settings: <LinkWithUnderline to={settingsPath} />, |
| 92 | + }) |
| 93 | + ), |
| 94 | + onError: () => |
| 95 | + addErrorMessage( |
| 96 | + tn( |
| 97 | + 'Failed to delete replay', |
| 98 | + 'Failed to delete replays', |
| 99 | + selectedIds === 'all' ? Number.MAX_SAFE_INTEGER : selectedIds.length |
| 100 | + ) |
| 101 | + ), |
| 102 | + onSettled: () => {}, |
| 103 | + }); |
| 104 | + }, |
| 105 | + }) |
| 106 | + } |
| 107 | + size="xs" |
| 108 | + > |
| 109 | + {t('Delete')} |
| 110 | + </Button> |
| 111 | + </Tooltip> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +function ReplayQueryPreview({ |
| 116 | + deletePayload, |
| 117 | + project, |
| 118 | +}: { |
| 119 | + deletePayload: ReplayBulkDeletePayload; |
| 120 | + project: Project; |
| 121 | +}) { |
| 122 | + const contentItems = Object.entries(deletePayload).map(([key, value]) => ({ |
| 123 | + item: { |
| 124 | + key, |
| 125 | + subject: key, |
| 126 | + value, |
| 127 | + }, |
| 128 | + })); |
| 129 | + return ( |
| 130 | + <Fragment> |
| 131 | + <Title project={project}> |
| 132 | + {t('Replays matching the following query will be deleted')} |
| 133 | + </Title> |
| 134 | + <KeyValueData.Card contentItems={contentItems} /> |
| 135 | + </Fragment> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +function ReplayPreviewTable({ |
| 140 | + project, |
| 141 | + replays, |
| 142 | + selectedIds, |
| 143 | +}: { |
| 144 | + project: Project; |
| 145 | + replays: ReplayListRecord[]; |
| 146 | + selectedIds: string[]; |
| 147 | +}) { |
| 148 | + return ( |
| 149 | + <Fragment> |
| 150 | + <Title project={project}> |
| 151 | + {tn( |
| 152 | + 'The following %s replay will be deleted', |
| 153 | + 'The following %s replays will be deleted', |
| 154 | + selectedIds.length |
| 155 | + )} |
| 156 | + </Title> |
| 157 | + <SimpleTableWithTwoColumns> |
| 158 | + <SimpleTable.Header> |
| 159 | + <SimpleTable.HeaderCell>{t('Replay')}</SimpleTable.HeaderCell> |
| 160 | + <SimpleTable.HeaderCell>{t('Duration')}</SimpleTable.HeaderCell> |
| 161 | + </SimpleTable.Header> |
| 162 | + {selectedIds.map(id => { |
| 163 | + const replay = replays.find(r => r.id === id) as ReplayListRecord; |
| 164 | + if (replay.is_archived) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + invariant( |
| 168 | + replay.duration && replay.started_at, |
| 169 | + 'For TypeScript: replay.duration and replay.started_at are implied because replay.is_archived is false' |
| 170 | + ); |
| 171 | + |
| 172 | + return ( |
| 173 | + <SimpleTable.Row key={id}> |
| 174 | + <SimpleTable.RowCell> |
| 175 | + <Flex key="session" align="center" gap={space(1)}> |
| 176 | + <UserAvatar |
| 177 | + user={{ |
| 178 | + username: replay.user?.display_name || '', |
| 179 | + email: replay.user?.email || '', |
| 180 | + id: replay.user?.id || '', |
| 181 | + ip_address: replay.user?.ip || '', |
| 182 | + name: replay.user?.username || '', |
| 183 | + }} |
| 184 | + size={24} |
| 185 | + /> |
| 186 | + <SubText> |
| 187 | + <Flex gap={space(0.5)} align="flex-start"> |
| 188 | + <DisplayName> |
| 189 | + {replay.user.display_name || t('Anonymous User')} |
| 190 | + </DisplayName> |
| 191 | + </Flex> |
| 192 | + <Flex gap={space(0.5)}> |
| 193 | + {getShortEventId(replay.id)} |
| 194 | + <Flex gap={space(0.5)}> |
| 195 | + <IconCalendar color="gray300" size="xs" /> |
| 196 | + <TimeSince date={replay.started_at} /> |
| 197 | + </Flex> |
| 198 | + </Flex> |
| 199 | + </SubText> |
| 200 | + </Flex> |
| 201 | + </SimpleTable.RowCell> |
| 202 | + <SimpleTable.RowCell justify="flex-end"> |
| 203 | + <Duration |
| 204 | + duration={[replay.duration.asMilliseconds() ?? 0, 'ms']} |
| 205 | + precision="sec" |
| 206 | + /> |
| 207 | + </SimpleTable.RowCell> |
| 208 | + </SimpleTable.Row> |
| 209 | + ); |
| 210 | + })} |
| 211 | + </SimpleTableWithTwoColumns> |
| 212 | + </Fragment> |
| 213 | + ); |
| 214 | +} |
| 215 | + |
| 216 | +function Title({children, project}: {children: React.ReactNode; project: Project}) { |
| 217 | + const settingsPath = `/settings/projects/${project.slug}/replays/?replaySettingsTab=bulk-delete`; |
| 218 | + return ( |
| 219 | + <Fragment> |
| 220 | + <p> |
| 221 | + <strong>{children}</strong> |
| 222 | + </p> |
| 223 | + <p> |
| 224 | + {tct('You can track the progress in [link].', { |
| 225 | + link: ( |
| 226 | + <LinkWithUnderline |
| 227 | + to={settingsPath} |
| 228 | + >{`Settings > ${project.slug} > Replays`}</LinkWithUnderline> |
| 229 | + ), |
| 230 | + })} |
| 231 | + </p> |
| 232 | + </Fragment> |
| 233 | + ); |
| 234 | +} |
| 235 | + |
| 236 | +const SimpleTableWithTwoColumns = styled(SimpleTable)` |
| 237 | + grid-template-columns: 1fr max-content; |
| 238 | +`; |
| 239 | + |
| 240 | +const SubText = styled('div')` |
| 241 | + font-size: 0.875em; |
| 242 | + line-height: normal; |
| 243 | + color: ${p => p.theme.subText}; |
| 244 | + ${p => p.theme.overflowEllipsis}; |
| 245 | + display: flex; |
| 246 | + flex-direction: column; |
| 247 | + gap: ${space(0.25)}; |
| 248 | + align-items: flex-start; |
| 249 | +`; |
| 250 | + |
| 251 | +const DisplayName = styled('span')` |
| 252 | + color: ${p => p.theme.textColor}; |
| 253 | + font-size: ${p => p.theme.fontSize.md}; |
| 254 | + font-weight: ${p => p.theme.fontWeight.bold}; |
| 255 | + line-height: normal; |
| 256 | + ${p => p.theme.overflowEllipsis}; |
| 257 | +`; |
| 258 | + |
| 259 | +const LinkWithUnderline = styled(Link)` |
| 260 | + cursor: pointer; |
| 261 | + &:hover { |
| 262 | + text-decoration: underline; |
| 263 | + } |
| 264 | +`; |
0 commit comments