|
| 1 | +import { useState, useRef } from 'react' |
1 | 2 | import { ThumbsdownIcon, ThumbsupIcon } from '@primer/octicons-react' |
2 | 3 | import { useTranslation } from 'components/hooks/useTranslation' |
| 4 | +import { Link } from 'components/Link' |
| 5 | +import { sendEvent } from '../javascripts/events' |
| 6 | + |
| 7 | +enum ViewState { |
| 8 | + START = 'START', |
| 9 | + YES = 'YES', |
| 10 | + NO = 'NO', |
| 11 | + END = 'END', |
| 12 | +} |
3 | 13 |
|
4 | 14 | export const Survey = () => { |
5 | 15 | const { t } = useTranslation('survey') |
| 16 | + const [state, setState] = useState<ViewState>(ViewState.START) |
| 17 | + const formRef = useRef<HTMLFormElement>(null) |
| 18 | + |
| 19 | + function vote(state: ViewState) { |
| 20 | + return (evt: React.ChangeEvent<HTMLInputElement>) => { |
| 21 | + trackEvent(getFormData()) |
| 22 | + setState(state) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + function submit(evt: React.FormEvent) { |
| 27 | + evt.preventDefault() |
| 28 | + trackEvent(getFormData()) |
| 29 | + setState(ViewState.END) |
| 30 | + } |
| 31 | + |
| 32 | + function getFormData() { |
| 33 | + if (!formRef.current) return |
| 34 | + return new FormData(formRef.current) |
| 35 | + } |
6 | 36 |
|
7 | 37 | return ( |
8 | | - <form className="js-survey f5"> |
9 | | - <h2 data-help-start data-help-yes data-help-no className="mb-1 f4"> |
| 38 | + <form className="f5" onSubmit={submit} ref={formRef}> |
| 39 | + <h2 className="mb-1 f4"> |
10 | 40 | {t`able_to_find`} |
| 41 | + |
| 42 | + <Link |
| 43 | + className="f6 text-normal ml-3 color-text-link" |
| 44 | + href="/github/site-policy/github-privacy-statement" |
| 45 | + target="_blank" |
| 46 | + > |
| 47 | + {t`privacy_policy`} |
| 48 | + </Link> |
11 | 49 | </h2> |
12 | | - <p className="f6"> |
13 | | - <a href="/github/site-policy/github-privacy-statement">Privacy policy</a> |
14 | | - </p> |
15 | | - <p className="radio-group" data-help-start data-help-yes data-help-no> |
16 | | - <input |
17 | | - hidden |
18 | | - id="survey-yes" |
19 | | - type="radio" |
20 | | - name="survey-vote" |
21 | | - value="Yes" |
22 | | - aria-label={t('yes')} |
23 | | - /> |
24 | | - <label className="btn x-radio-label mr-1" htmlFor="survey-yes"> |
25 | | - <ThumbsupIcon size={24} className="color-text-tertiary" /> |
26 | | - </label> |
27 | | - <input |
28 | | - hidden |
29 | | - id="survey-no" |
30 | | - type="radio" |
31 | | - name="survey-vote" |
32 | | - value="No" |
33 | | - aria-label={t`no`} |
34 | | - /> |
35 | | - <label className="btn x-radio-label" htmlFor="survey-no"> |
36 | | - <ThumbsdownIcon size={24} className="color-text-tertiary" /> |
37 | | - </label> |
38 | | - </p> |
39 | | - <p className="color-text-secondary f6" hidden data-help-yes> |
40 | | - {t('yes_feedback')} |
41 | | - </p> |
42 | | - <p className="color-text-secondary f6" hidden data-help-no> |
43 | | - {t('no_feedback')} |
44 | | - </p> |
| 50 | + |
| 51 | + {/* Honeypot: token isn't a real field */} |
45 | 52 | <input type="text" className="d-none" name="survey-token" aria-hidden="true" /> |
46 | | - <p hidden data-help-no> |
47 | | - <label className="d-block mb-1 f6" htmlFor="survey-comment"> |
48 | | - <span>{t('comment_label')}</span> |
49 | | - <span className="text-normal color-text-tertiary float-right ml-1">{t('optional')}</span> |
50 | | - </label> |
51 | | - <textarea |
52 | | - className="form-control input-sm width-full" |
53 | | - name="survey-comment" |
54 | | - id="survey-comment" |
55 | | - ></textarea> |
56 | | - </p> |
57 | | - <p> |
58 | | - <label className="d-block mb-1 f6" htmlFor="survey-email" hidden data-help-no> |
59 | | - {t('email_label')} |
60 | | - <span className="text-normal color-text-tertiary float-right ml-1">{t('optional')}</span> |
61 | | - </label> |
62 | | - <input |
63 | | - type="email" |
64 | | - className="form-control input-sm width-full" |
65 | | - name="survey-email" |
66 | | - id="survey-email" |
67 | | - placeholder={t('email_placeholder')} |
68 | | - hidden |
69 | | - data-help-yes |
70 | | - data-help-no |
71 | | - /> |
72 | | - </p> |
73 | | - <p className="text-right" hidden data-help-yes data-help-no> |
74 | | - <button type="submit" className="btn btn-sm"> |
75 | | - {t('send')} |
76 | | - </button> |
77 | | - </p> |
78 | | - <p className="color-text-secondary f6" hidden data-help-end> |
79 | | - {t('feedback')} |
80 | | - </p> |
| 53 | + |
| 54 | + {state !== ViewState.END && ( |
| 55 | + <p className="radio-group"> |
| 56 | + <input |
| 57 | + id="survey-yes" |
| 58 | + type="radio" |
| 59 | + name="survey-vote" |
| 60 | + value="Y" |
| 61 | + aria-label={t`yes`} |
| 62 | + hidden |
| 63 | + onChange={vote(ViewState.YES)} |
| 64 | + defaultChecked={state === ViewState.YES} |
| 65 | + /> |
| 66 | + <label className="btn x-radio-label mr-1" htmlFor="survey-yes"> |
| 67 | + <ThumbsupIcon size={24} className="color-text-tertiary" /> |
| 68 | + </label> |
| 69 | + <input |
| 70 | + id="survey-no" |
| 71 | + type="radio" |
| 72 | + name="survey-vote" |
| 73 | + value="N" |
| 74 | + aria-label={t`no`} |
| 75 | + hidden |
| 76 | + onChange={vote(ViewState.NO)} |
| 77 | + defaultChecked={state === ViewState.NO} |
| 78 | + /> |
| 79 | + <label className="btn x-radio-label" htmlFor="survey-no"> |
| 80 | + <ThumbsdownIcon size={24} className="color-text-tertiary" /> |
| 81 | + </label> |
| 82 | + </p> |
| 83 | + )} |
| 84 | + |
| 85 | + {[ViewState.YES, ViewState.NO].includes(state) && ( |
| 86 | + <> |
| 87 | + <p className="color-text-secondary f6"> |
| 88 | + {state === ViewState.YES && t`yes_feedback`} |
| 89 | + {state === ViewState.NO && t`no_feedback`} |
| 90 | + </p> |
| 91 | + <p className="mb-3"> |
| 92 | + <label className="d-block mb-1 f6" htmlFor="survey-comment"> |
| 93 | + <span> |
| 94 | + {state === ViewState.YES && t`comment_yes_label`} |
| 95 | + {state === ViewState.NO && t`comment_no_label`} |
| 96 | + </span> |
| 97 | + <span className="text-normal color-text-tertiary float-right ml-1"> |
| 98 | + {t`optional`} |
| 99 | + </span> |
| 100 | + </label> |
| 101 | + <textarea |
| 102 | + className="form-control input-sm width-full" |
| 103 | + name="survey-comment" |
| 104 | + id="survey-comment" |
| 105 | + ></textarea> |
| 106 | + </p> |
| 107 | + <p> |
| 108 | + <label className="d-block mb-1 f6" htmlFor="survey-email"> |
| 109 | + {t`email_label`} |
| 110 | + <span className="text-normal color-text-tertiary float-right ml-1"> |
| 111 | + {t`optional`} |
| 112 | + </span> |
| 113 | + </label> |
| 114 | + <input |
| 115 | + type="email" |
| 116 | + className="form-control input-sm width-full" |
| 117 | + name="survey-email" |
| 118 | + id="survey-email" |
| 119 | + placeholder={t`email_placeholder`} |
| 120 | + /> |
| 121 | + <span className="f6 color-text-secondary">{t`not_support`}</span> |
| 122 | + </p> |
| 123 | + <p className="text-right"> |
| 124 | + <button type="submit" className="btn btn-sm"> |
| 125 | + {t`send`} |
| 126 | + </button> |
| 127 | + </p> |
| 128 | + </> |
| 129 | + )} |
| 130 | + |
| 131 | + {state === ViewState.END && <p className="color-text-secondary f6">{t`feedback`}</p>} |
81 | 132 | </form> |
82 | 133 | ) |
83 | 134 | } |
| 135 | + |
| 136 | +function trackEvent(formData: FormData | undefined) { |
| 137 | + if (!formData) return |
| 138 | + // Nota bene: convert empty strings to undefined |
| 139 | + return sendEvent({ |
| 140 | + type: 'survey', |
| 141 | + survey_token: formData.get('survey-token') || undefined, // Honeypot |
| 142 | + survey_vote: formData.get('survey-vote') === 'Y', |
| 143 | + survey_comment: formData.get('survey-comment') || undefined, |
| 144 | + survey_email: formData.get('survey-email') || undefined, |
| 145 | + }) |
| 146 | +} |
0 commit comments