|
| 1 | +import React, {FormEvent, useEffect, useRef} from 'react'; |
| 2 | +import {css} from '@emotion/react'; |
| 3 | +import styled from '@emotion/styled'; |
| 4 | + |
| 5 | +import {useFocusTrap} from '../hooks/useFocusTrap'; |
| 6 | +import {useShortcut} from '../hooks/useShortcut'; |
| 7 | + |
| 8 | +const Dialog = styled.dialog` |
| 9 | + background-color: rgba(0, 0, 0, 0.05); |
| 10 | + border: none; |
| 11 | + position: fixed; |
| 12 | + inset: 0; |
| 13 | + z-index: 10000; |
| 14 | + width: 100vw; |
| 15 | + height: 100vh; |
| 16 | + display: flex; |
| 17 | + align-items: center; |
| 18 | + justify-content: center; |
| 19 | + opacity: 1; |
| 20 | + transition: opacity 0.2s ease-in-out; |
| 21 | + &:not([open]) { |
| 22 | + opacity: 0; |
| 23 | + pointer-events: none; |
| 24 | + visibility: hidden; |
| 25 | + } |
| 26 | +`; |
| 27 | + |
| 28 | +const Content = styled.div` |
| 29 | + border-radius: 4px; |
| 30 | + background-color: #fff; |
| 31 | + width: 500px; |
| 32 | + max-width: 100%; |
| 33 | + max-height: calc(100% - 64px); |
| 34 | + display: flex; |
| 35 | + flex-direction: column; |
| 36 | + box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05), 0 4px 16px rgba(0, 0, 0, 0.2); |
| 37 | + transition: transform 0.2s ease-in-out; |
| 38 | + transform: translate(0, 0) scale(1); |
| 39 | + dialog:not([open]) & { |
| 40 | + transform: translate(0, -16px) scale(0.98); |
| 41 | + } |
| 42 | +`; |
| 43 | + |
| 44 | +const Header = styled.h2` |
| 45 | + font-size: 20px; |
| 46 | + font-weight: 600; |
| 47 | + border-bottom: 1px solid #ccc; |
| 48 | + padding: 20px 24px; |
| 49 | + margin: 0px; |
| 50 | +`; |
| 51 | + |
| 52 | +const Form = styled.form` |
| 53 | + display: flex; |
| 54 | + overflow: auto; |
| 55 | + flex-direction: column; |
| 56 | + gap: 16px; |
| 57 | + padding: 24px; |
| 58 | +`; |
| 59 | + |
| 60 | +const Label = styled.label` |
| 61 | + display: flex; |
| 62 | + flex-direction: column; |
| 63 | + gap: 4px; |
| 64 | + margin: 0px; |
| 65 | +`; |
| 66 | + |
| 67 | +const inputStyles = css` |
| 68 | + border: 1px solid #ccc; |
| 69 | + border-radius: 4px; |
| 70 | + font-size: 14px; |
| 71 | + padding: 6px 8px; |
| 72 | + &:focus { |
| 73 | + outline: 1px solid rgba(108, 95, 199, 1); |
| 74 | + border-color: rgba(108, 95, 199, 1); |
| 75 | + } |
| 76 | +`; |
| 77 | + |
| 78 | +const Input = styled.input` |
| 79 | + ${inputStyles} |
| 80 | +`; |
| 81 | + |
| 82 | +const TextArea = styled.textarea` |
| 83 | + ${inputStyles} |
| 84 | + min-height: 64px; |
| 85 | + resize: vertical; |
| 86 | +`; |
| 87 | + |
| 88 | +const ModalFooter = styled.div` |
| 89 | + display: flex; |
| 90 | + justify-content: flex-end; |
| 91 | + gap: 8px; |
| 92 | + margin-top: 8px; |
| 93 | +`; |
| 94 | + |
| 95 | +const buttonStyles = css` |
| 96 | + border: 1px solid #ccc; |
| 97 | + border-radius: 4px; |
| 98 | + cursor: pointer; |
| 99 | + font-size: 14px; |
| 100 | + font-weight: 600; |
| 101 | + padding: 6px 16px; |
| 102 | +`; |
| 103 | + |
| 104 | +const SubmitButton = styled.button` |
| 105 | + ${buttonStyles} |
| 106 | + background-color: rgba(108, 95, 199, 1); |
| 107 | + color: #fff; |
| 108 | + &:hover { |
| 109 | + background-color: rgba(88, 74, 192, 1); |
| 110 | + } |
| 111 | + &:focus-visible { |
| 112 | + outline: 1px solid rgba(108, 95, 199, 1); |
| 113 | + background-color: rgba(88, 74, 192, 1); |
| 114 | + } |
| 115 | +`; |
| 116 | + |
| 117 | +const CancelButton = styled.button` |
| 118 | + ${buttonStyles} |
| 119 | + background-color: #fff; |
| 120 | + color: #231c3d; |
| 121 | + font-weight: 500; |
| 122 | + &:hover { |
| 123 | + background-color: #eee; |
| 124 | + } |
| 125 | + &:focus-visible { |
| 126 | + outline: 1px solid rgba(108, 95, 199, 1); |
| 127 | + background-color: #eee; |
| 128 | + } |
| 129 | +`; |
| 130 | + |
| 131 | +const FlexColumns = styled.div` |
| 132 | + display: flex; |
| 133 | + flex-direction: row; |
| 134 | + gap: 16px; |
| 135 | + & > * { |
| 136 | + flex: 1; |
| 137 | + } |
| 138 | +`; |
| 139 | + |
| 140 | +interface FeedbackModalProps { |
| 141 | + onClose: () => void; |
| 142 | + onSubmit: (data: {comment: string; email: string; name: string}) => void; |
| 143 | + open: boolean; |
| 144 | +} |
| 145 | + |
| 146 | +function stopPropagation(e: React.MouseEvent) { |
| 147 | + e.stopPropagation(); |
| 148 | +} |
| 149 | + |
| 150 | +const retrieveStringValue = (formData: FormData, key: string) => { |
| 151 | + const value = formData.get(key); |
| 152 | + if (typeof value === 'string') { |
| 153 | + return value.trim(); |
| 154 | + } |
| 155 | + return ''; |
| 156 | +}; |
| 157 | + |
| 158 | +export function FeedbackModal({open, onClose, onSubmit}: FeedbackModalProps) { |
| 159 | + const dialogRef = useRef<HTMLDialogElement>(null); |
| 160 | + const formRef = useRef<HTMLFormElement>(null); |
| 161 | + |
| 162 | + useFocusTrap(dialogRef, open); |
| 163 | + useShortcut('Escape', onClose); |
| 164 | + |
| 165 | + // Reset on close |
| 166 | + useEffect(() => { |
| 167 | + let timeoutId: ReturnType<typeof setTimeout> | undefined; |
| 168 | + |
| 169 | + if (!open) { |
| 170 | + timeoutId = setTimeout(() => { |
| 171 | + formRef.current.reset(); |
| 172 | + }, 200); |
| 173 | + } |
| 174 | + return () => { |
| 175 | + if (timeoutId) { |
| 176 | + clearTimeout(timeoutId); |
| 177 | + } |
| 178 | + }; |
| 179 | + }, [open]); |
| 180 | + |
| 181 | + const handleSubmit = (e: FormEvent<HTMLFormElement>) => { |
| 182 | + e.preventDefault(); |
| 183 | + const formData = new FormData(e.target as HTMLFormElement); |
| 184 | + onSubmit({ |
| 185 | + name: retrieveStringValue(formData, 'name'), |
| 186 | + email: retrieveStringValue(formData, 'email'), |
| 187 | + comment: retrieveStringValue(formData, 'comment'), |
| 188 | + }); |
| 189 | + }; |
| 190 | + |
| 191 | + const user = window.Sentry?.getCurrentHub().getScope()?.getUser(); |
| 192 | + |
| 193 | + return ( |
| 194 | + <Dialog id="feedbackModal" open={open} ref={dialogRef} onClick={onClose}> |
| 195 | + <Content onClick={stopPropagation}> |
| 196 | + <Header>Got any Feedback?</Header> |
| 197 | + <Form ref={formRef} onSubmit={handleSubmit}> |
| 198 | + <FlexColumns> |
| 199 | + <Label htmlFor="sentry-feedback-name"> |
| 200 | + Your Name |
| 201 | + <Input |
| 202 | + type="text" |
| 203 | + id="sentry-feedback-name" |
| 204 | + name="name" |
| 205 | + placeholder="Anonymous" |
| 206 | + defaultValue={user?.username} |
| 207 | + /> |
| 208 | + </Label> |
| 209 | + <Label htmlFor="sentry-feedback-email"> |
| 210 | + Your Email |
| 211 | + <Input |
| 212 | + type="text" |
| 213 | + id="sentry-feedback-email" |
| 214 | + name="email" |
| 215 | + placeholder="you@test.com" |
| 216 | + defaultValue={user?.email} |
| 217 | + /> |
| 218 | + </Label> |
| 219 | + </FlexColumns> |
| 220 | + <Label htmlFor="sentry-feedback-comment"> |
| 221 | + Comment |
| 222 | + <TextArea |
| 223 | + onKeyDown={event => { |
| 224 | + if (event.key === 'Enter' && event.ctrlKey) { |
| 225 | + formRef.current.requestSubmit(); |
| 226 | + } |
| 227 | + }} |
| 228 | + id="sentry-feedback-comment" |
| 229 | + name="comment" |
| 230 | + placeholder="Explain what bothers you" |
| 231 | + /> |
| 232 | + </Label> |
| 233 | + <ModalFooter> |
| 234 | + <CancelButton type="button" onClick={onClose}> |
| 235 | + Cancel |
| 236 | + </CancelButton> |
| 237 | + <SubmitButton type="submit">Submit</SubmitButton> |
| 238 | + </ModalFooter> |
| 239 | + </Form> |
| 240 | + </Content> |
| 241 | + </Dialog> |
| 242 | + ); |
| 243 | +} |
0 commit comments