|
| 1 | +import {useTelemetry} from '@sanity/telemetry/react' |
| 2 | +import {Box, Card, DialogProvider, Flex, Stack, Text, TextInput, useToast} from '@sanity/ui' |
| 3 | +import {useId, useMemo, useState} from 'react' |
| 4 | +import {useObservable} from 'react-rx' |
| 5 | +import {catchError, map, type Observable, of, startWith} from 'rxjs' |
| 6 | +import {type Role, useClient, useProjectId, useTranslation, useZIndex} from 'sanity' |
| 7 | +import {styled} from 'styled-components' |
| 8 | + |
| 9 | +import {Dialog} from '../../../ui-components' |
| 10 | +import {structureLocaleNamespace} from '../../i18n' |
| 11 | +import {AskToEditRequestSent} from './__telemetry__/RequestPermissionDialog.telemetry' |
| 12 | +import {type AccessRequest} from './useRoleRequestsStatus' |
| 13 | + |
| 14 | +const MAX_NOTE_LENGTH = 150 |
| 15 | + |
| 16 | +/** @internal */ |
| 17 | +export const DialogBody = styled(Box)` |
| 18 | + box-sizing: border-box; |
| 19 | +` |
| 20 | + |
| 21 | +/** @internal */ |
| 22 | +export const LoadingContainer = styled(Flex).attrs({ |
| 23 | + align: 'center', |
| 24 | + direction: 'column', |
| 25 | + justify: 'center', |
| 26 | +})` |
| 27 | + height: 110px; |
| 28 | +` |
| 29 | + |
| 30 | +/** @internal */ |
| 31 | +export interface RequestPermissionDialogProps { |
| 32 | + onClose?: () => void |
| 33 | + onRequestSubmitted?: () => void |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * A confirmation dialog used to prevent unwanted document deletes. Loads all |
| 38 | + * the referencing internal and cross-data references prior to showing the |
| 39 | + * delete button. |
| 40 | + * |
| 41 | + * @internal |
| 42 | + */ |
| 43 | +export function RequestPermissionDialog({ |
| 44 | + onClose, |
| 45 | + onRequestSubmitted, |
| 46 | +}: RequestPermissionDialogProps) { |
| 47 | + const {t} = useTranslation(structureLocaleNamespace) |
| 48 | + const telemtry = useTelemetry() |
| 49 | + const dialogId = `request-permissions-${useId()}` |
| 50 | + const projectId = useProjectId() |
| 51 | + const client = useClient({apiVersion: '2024-09-26'}) |
| 52 | + const toast = useToast() |
| 53 | + const zOffset = useZIndex() |
| 54 | + |
| 55 | + const [isSubmitting, setIsSubmitting] = useState(false) |
| 56 | + |
| 57 | + const [note, setNote] = useState('') |
| 58 | + const [noteLength, setNoteLength] = useState<number>(0) |
| 59 | + |
| 60 | + const [msgError, setMsgError] = useState<string | undefined>() |
| 61 | + const [hasTooManyRequests, setHasTooManyRequests] = useState<boolean>(false) |
| 62 | + const [hasBeenDenied, setHasBeenDenied] = useState<boolean>(false) |
| 63 | + |
| 64 | + const requestedRole$: Observable<'administrator' | 'editor'> = useMemo(() => { |
| 65 | + const adminRole = 'administrator' as const |
| 66 | + if (!projectId || !client) return of(adminRole) |
| 67 | + return client.observable |
| 68 | + .request<(Role & {appliesToUsers?: boolean})[]>({url: `/projects/${projectId}/roles`}) |
| 69 | + .pipe( |
| 70 | + map((roles) => { |
| 71 | + const hasEditor = roles |
| 72 | + .filter((role) => role?.appliesToUsers) |
| 73 | + .find((role) => role.name === 'editor') |
| 74 | + return hasEditor ? 'editor' : adminRole |
| 75 | + }), |
| 76 | + startWith(adminRole), |
| 77 | + catchError(() => of(adminRole)), |
| 78 | + ) |
| 79 | + }, [projectId, client]) |
| 80 | + |
| 81 | + const requestedRole = useObservable(requestedRole$) |
| 82 | + |
| 83 | + const onSubmit = () => { |
| 84 | + setIsSubmitting(true) |
| 85 | + client |
| 86 | + .request<AccessRequest | null>({ |
| 87 | + url: `/access/project/${projectId}/requests`, |
| 88 | + method: 'post', |
| 89 | + body: {note, requestUrl: window?.location.href, requestedRole, type: 'role'}, |
| 90 | + }) |
| 91 | + .then((request) => { |
| 92 | + if (request) { |
| 93 | + if (onRequestSubmitted) onRequestSubmitted() |
| 94 | + telemtry.log(AskToEditRequestSent) |
| 95 | + toast.push({title: 'Edit access requested'}) |
| 96 | + } |
| 97 | + }) |
| 98 | + .catch((err) => { |
| 99 | + const statusCode = err?.response?.statusCode |
| 100 | + const errMessage = err?.response?.body?.message |
| 101 | + if (statusCode === 429) { |
| 102 | + // User is over their cross-project request limit |
| 103 | + setHasTooManyRequests(true) |
| 104 | + setMsgError(errMessage) |
| 105 | + } |
| 106 | + if (statusCode === 409) { |
| 107 | + // If we get a 409, user has been denied on this project or has a valid pending request |
| 108 | + // valid pending request should be handled by GET request above |
| 109 | + setHasBeenDenied(true) |
| 110 | + setMsgError(errMessage) |
| 111 | + } else { |
| 112 | + toast.push({ |
| 113 | + title: 'There was a problem submitting your request.', |
| 114 | + status: 'error', |
| 115 | + }) |
| 116 | + } |
| 117 | + }) |
| 118 | + .finally(() => { |
| 119 | + setIsSubmitting(false) |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + return ( |
| 124 | + <DialogProvider position={'fixed'} zOffset={zOffset.fullscreen}> |
| 125 | + <Dialog |
| 126 | + width={1} |
| 127 | + id={dialogId} |
| 128 | + header={t('request-permission-dialog.header.text')} |
| 129 | + footer={{ |
| 130 | + cancelButton: { |
| 131 | + onClick: onClose, |
| 132 | + text: t('confirm-dialog.cancel-button.fallback-text'), |
| 133 | + }, |
| 134 | + confirmButton: { |
| 135 | + onClick: onSubmit, |
| 136 | + loading: isSubmitting, |
| 137 | + disabled: hasTooManyRequests || hasBeenDenied, |
| 138 | + text: t('request-permission-dialog.confirm-button.text'), |
| 139 | + tone: 'primary', |
| 140 | + type: 'submit', |
| 141 | + }, |
| 142 | + }} |
| 143 | + onClose={onClose} |
| 144 | + onClickOutside={onClose} |
| 145 | + > |
| 146 | + <DialogBody> |
| 147 | + <Stack space={4}> |
| 148 | + <Text>{t('request-permission-dialog.description.text')}</Text> |
| 149 | + {hasTooManyRequests || hasBeenDenied ? ( |
| 150 | + <Card tone={'caution'} padding={3} radius={2} shadow={1}> |
| 151 | + <Text size={1}> |
| 152 | + {hasTooManyRequests && ( |
| 153 | + <>{msgError ?? t('request-permission-dialog.warning.limit-reached.text')}</> |
| 154 | + )} |
| 155 | + {hasBeenDenied && ( |
| 156 | + <>{msgError ?? t('request-permission-dialog.warning.denied.text')}</> |
| 157 | + )} |
| 158 | + </Text> |
| 159 | + </Card> |
| 160 | + ) : ( |
| 161 | + <Stack space={3} paddingBottom={0}> |
| 162 | + <TextInput |
| 163 | + placeholder={t('request-permission-dialog.note-input.placeholder.text')} |
| 164 | + disabled={isSubmitting} |
| 165 | + onKeyDown={(e) => { |
| 166 | + if (e.key === 'Enter') onSubmit() |
| 167 | + }} |
| 168 | + maxLength={MAX_NOTE_LENGTH} |
| 169 | + value={note} |
| 170 | + onChange={(e) => { |
| 171 | + setNote(e.currentTarget.value) |
| 172 | + setNoteLength(e.currentTarget.value.length) |
| 173 | + }} |
| 174 | + /> |
| 175 | + |
| 176 | + <Text align="right" muted size={1}>{`${noteLength}/${MAX_NOTE_LENGTH}`}</Text> |
| 177 | + </Stack> |
| 178 | + )} |
| 179 | + </Stack> |
| 180 | + </DialogBody> |
| 181 | + </Dialog> |
| 182 | + </DialogProvider> |
| 183 | + ) |
| 184 | +} |
0 commit comments