samplingFeedback.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import {Fragment, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import CheckboxFancy from 'sentry/components/checkboxFancy/checkboxFancy';
  4. import {FeatureFeedback} from 'sentry/components/featureFeedback';
  5. import {TextField} from 'sentry/components/forms';
  6. import Textarea from 'sentry/components/forms/controls/textarea';
  7. import Field from 'sentry/components/forms/field';
  8. import {t} from 'sentry/locale';
  9. import ConfigStore from 'sentry/stores/configStore';
  10. import space from 'sentry/styles/space';
  11. enum TracingCapturingPriorities {
  12. TRANSACTION_NAME = 'transaction_name',
  13. CUSTOM_TAG = 'custom_tag',
  14. OTHER = 'other',
  15. }
  16. type Option = {
  17. checked: boolean;
  18. title: string;
  19. value: string | number;
  20. };
  21. type InitialData = {
  22. opinionAboutFeature: string;
  23. tracingCapturingOtherPriority: string;
  24. tracingCapturingPriorities: Option[];
  25. };
  26. const initialData: InitialData = {
  27. opinionAboutFeature: '',
  28. tracingCapturingPriorities: [
  29. {
  30. title: t('By transaction name'),
  31. value: TracingCapturingPriorities.TRANSACTION_NAME,
  32. checked: false,
  33. },
  34. {
  35. title: t('By custom tag'),
  36. value: TracingCapturingPriorities.CUSTOM_TAG,
  37. checked: false,
  38. },
  39. {
  40. title: t('Other'),
  41. value: TracingCapturingPriorities.OTHER,
  42. checked: false,
  43. },
  44. ],
  45. tracingCapturingOtherPriority: '',
  46. };
  47. function MultipleCheckboxField({
  48. options,
  49. onChange,
  50. otherTextField,
  51. }: {
  52. onChange: (options: Option[]) => void;
  53. options: Option[];
  54. otherTextField: React.ReactNode;
  55. }) {
  56. const handleClick = useCallback(
  57. (newOption: Option) => {
  58. const newOptions = options.map(option => {
  59. if (option.value === newOption.value) {
  60. return {
  61. ...option,
  62. checked: !option.checked,
  63. };
  64. }
  65. return option;
  66. });
  67. onChange(newOptions);
  68. },
  69. [onChange, options]
  70. );
  71. return (
  72. <Fragment>
  73. {options.map(option => {
  74. if (option.value === 'other') {
  75. return (
  76. <CheckboxOtherOptionWrapper
  77. key={option.value}
  78. onClick={() => handleClick(option)}
  79. >
  80. <CheckboxFancy isChecked={option.checked} />
  81. {option.title}
  82. {otherTextField}
  83. </CheckboxOtherOptionWrapper>
  84. );
  85. }
  86. return (
  87. <CheckboxOption key={option.value} onClick={() => handleClick(option)}>
  88. <CheckboxFancy isChecked={option.checked} />
  89. {option.title}
  90. </CheckboxOption>
  91. );
  92. })}
  93. </Fragment>
  94. );
  95. }
  96. export function SamplingFeedback() {
  97. const feedbackMessage = `Dynamic Sampling feedback by ${ConfigStore.get('user').email}`;
  98. return (
  99. <FeatureFeedback
  100. featureName="dynamic-sampling"
  101. initialData={initialData}
  102. buttonProps={{
  103. priority: 'primary',
  104. size: 'sm',
  105. }}
  106. >
  107. {({Header, Body, Footer, state, onFieldChange}) => {
  108. return (
  109. <Fragment>
  110. <Header>{t('Submit Feedback')}</Header>
  111. <Body showSelfHostedMessage={false}>
  112. <Field
  113. label={<Label>{t('What do you think about this feature?')}</Label>}
  114. stacked
  115. inline={false}
  116. flexibleControlStateSize
  117. >
  118. <Textarea
  119. name="opinion-about-feedback"
  120. value={state.opinionAboutFeature}
  121. rows={5}
  122. autosize
  123. onChange={event =>
  124. onFieldChange('opinionAboutFeature', event.target.value)
  125. }
  126. />
  127. </Field>
  128. <Field
  129. label={
  130. <Label>
  131. {t(
  132. 'What other priorities would you like Sentry to apply for trace capturing?'
  133. )}
  134. </Label>
  135. }
  136. stacked
  137. inline={false}
  138. flexibleControlStateSize
  139. >
  140. <MultipleCheckboxField
  141. options={state.tracingCapturingPriorities}
  142. onChange={newTracingCapturingPriorities => {
  143. if (
  144. newTracingCapturingPriorities.some(
  145. newTracingCapturingPriority =>
  146. newTracingCapturingPriority.value ===
  147. TracingCapturingPriorities.OTHER &&
  148. newTracingCapturingPriority.checked === false
  149. )
  150. ) {
  151. onFieldChange('tracingCapturingOtherPriority', '');
  152. }
  153. onFieldChange(
  154. 'tracingCapturingPriorities',
  155. newTracingCapturingPriorities
  156. );
  157. }}
  158. otherTextField={
  159. <OtherTextField
  160. inline={false}
  161. name="tracingCapturingOtherPriority"
  162. flexibleControlStateSize
  163. stacked
  164. disabled={state.tracingCapturingPriorities.some(
  165. tracingCapturingPriority =>
  166. tracingCapturingPriority.value ===
  167. TracingCapturingPriorities.OTHER &&
  168. tracingCapturingPriority.checked === false
  169. )}
  170. onClick={event => event.stopPropagation()}
  171. value={state.tracingCapturingOtherPriority}
  172. onChange={value =>
  173. onFieldChange('tracingCapturingOtherPriority', value)
  174. }
  175. placeholder={t('Please let us know which other priority')}
  176. />
  177. }
  178. />
  179. </Field>
  180. </Body>
  181. <Footer
  182. primaryDisabledReason={
  183. !state.opinionAboutFeature.trim() &&
  184. state.tracingCapturingPriorities.every(
  185. tracingCapturingPriority => tracingCapturingPriority.checked === false
  186. )
  187. ? t('Please answer at least one question')
  188. : undefined
  189. }
  190. submitEventData={{
  191. contexts: {
  192. feedback: {
  193. opinionAboutFeature: state.opinionAboutFeature || null,
  194. tracingCapturingPriorities:
  195. state.tracingCapturingPriorities
  196. .filter(
  197. tracingCapturingPriority => tracingCapturingPriority.checked
  198. )
  199. .map(tracingCapturingPriority => tracingCapturingPriority.title)
  200. .join(', ') || null,
  201. tracingCapturingOtherPriority:
  202. state.tracingCapturingOtherPriority || null,
  203. },
  204. },
  205. message: state.opinionAboutFeature.trim()
  206. ? `${feedbackMessage} - ${state.opinionAboutFeature}`
  207. : feedbackMessage,
  208. }}
  209. />
  210. </Fragment>
  211. );
  212. }}
  213. </FeatureFeedback>
  214. );
  215. }
  216. const Label = styled('strong')`
  217. margin-bottom: ${space(1)};
  218. display: inline-block;
  219. `;
  220. const CheckboxOption = styled('div')`
  221. cursor: pointer;
  222. display: grid;
  223. grid-template-columns: max-content 1fr;
  224. gap: ${space(1)};
  225. align-items: center;
  226. :not(:last-child) {
  227. margin-bottom: ${space(1)};
  228. }
  229. `;
  230. const CheckboxOtherOptionWrapper = styled(CheckboxOption)`
  231. grid-template-columns: max-content max-content 1fr;
  232. @media (max-width: ${p => p.theme.breakpoints.small}) {
  233. grid-template-columns: max-content 1fr;
  234. }
  235. `;
  236. const OtherTextField = styled(TextField)`
  237. @media (max-width: ${p => p.theme.breakpoints.small}) {
  238. grid-column: 1/-1;
  239. }
  240. && {
  241. input {
  242. ${p => p.disabled && 'cursor: pointer;'}
  243. }
  244. }
  245. `;