projectRemoteConfig.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {
  5. addErrorMessage,
  6. addLoadingMessage,
  7. addSuccessMessage,
  8. } from 'sentry/actionCreators/indicator';
  9. import {openRemoteConfigCreateFeatureModal} from 'sentry/actionCreators/modal';
  10. import Feature from 'sentry/components/acl/feature';
  11. import Alert from 'sentry/components/alert';
  12. import {Button} from 'sentry/components/button';
  13. import {openConfirmModal} from 'sentry/components/confirm';
  14. import {Flex} from 'sentry/components/container/flex';
  15. import EmptyMessage from 'sentry/components/emptyMessage';
  16. import TextArea from 'sentry/components/forms/controls/textarea';
  17. import FieldGroup from 'sentry/components/forms/fieldGroup';
  18. import FieldControl from 'sentry/components/forms/fieldGroup/fieldControl';
  19. import * as Layout from 'sentry/components/layouts/thirds';
  20. import NoProjectMessage from 'sentry/components/noProjectMessage';
  21. import Panel from 'sentry/components/panels/panel';
  22. import PanelBody from 'sentry/components/panels/panelBody';
  23. import PanelHeader from 'sentry/components/panels/panelHeader';
  24. import {PanelTable} from 'sentry/components/panels/panelTable';
  25. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  26. import {Slider} from 'sentry/components/slider';
  27. import SplitDiff from 'sentry/components/splitDiff';
  28. import TextCopyInput from 'sentry/components/textCopyInput';
  29. import {IconSubtract} from 'sentry/icons';
  30. import {t, tct} from 'sentry/locale';
  31. import {space} from 'sentry/styles/space';
  32. import type {Organization} from 'sentry/types/organization';
  33. import type {Project} from 'sentry/types/project';
  34. import type {RemoteConfigFeature, RemoteConfigOptions} from 'sentry/types/remoteConfig';
  35. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  36. import useRemoteConfigSettings from 'sentry/views/settings/project/remoteConfig/useRemoteConfigSettings';
  37. type RouteParams = {
  38. projectId: string;
  39. };
  40. type Props = RouteComponentProps<RouteParams, {}> & {
  41. organization: Organization;
  42. project: Project;
  43. };
  44. export default function RemoteConfigContainer(props: Props) {
  45. return (
  46. <Feature
  47. features="remote-config"
  48. organization={props.organization}
  49. renderDisabled={NoAccess}
  50. >
  51. <NoProjectMessage organization={props.organization}>
  52. <ProjectRemoteConfig {...props} />
  53. </NoProjectMessage>
  54. </Feature>
  55. );
  56. }
  57. function NoAccess() {
  58. return (
  59. <Layout.Page withPadding>
  60. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  61. </Layout.Page>
  62. );
  63. }
  64. function ProjectRemoteConfig({organization, project, params: {projectId}}: Props) {
  65. const {result, staged, dispatch, handleSave, handleDelete} = useRemoteConfigSettings({
  66. organization,
  67. projectId,
  68. });
  69. const disabled = result.isLoading || result.isFetching;
  70. const addFeatureButton = (
  71. <Flex justify="flex-end">
  72. <Button
  73. size="xs"
  74. onClick={() => {
  75. openRemoteConfigCreateFeatureModal({
  76. createFeature: (key: string, value: string) =>
  77. dispatch({type: 'addFeature', key, value}),
  78. isValid: key =>
  79. !staged.data.features.map(feature => feature.key).includes(key),
  80. });
  81. }}
  82. >
  83. {t('Add Feature')}
  84. </Button>
  85. </Flex>
  86. );
  87. return (
  88. <SentryDocumentTitle title={t('Remote Config')} projectSlug={project.slug}>
  89. <SettingsPageHeader title={t('Remote Config')} />
  90. <Panel>
  91. <PanelHeader>{t('Settings')}</PanelHeader>
  92. <PanelBody>
  93. <OptionsPanelContent
  94. disabled={disabled}
  95. dispatch={dispatch}
  96. options={staged.data.options}
  97. />
  98. </PanelBody>
  99. </Panel>
  100. <FeaturesPanelTable
  101. isLoading={result.isLoading}
  102. headers={[t('Feature Key'), t('Feature Value'), addFeatureButton]}
  103. >
  104. <FeaturesPanelContent
  105. disabled={disabled}
  106. dispatch={dispatch}
  107. features={staged.data.features || []}
  108. />
  109. </FeaturesPanelTable>
  110. <PanelTable headers={['Current Config', 'Proposed Config']}>
  111. <PreviewPanelContent result={result} staged={staged} />
  112. </PanelTable>
  113. <SaveRow
  114. result={result}
  115. staged={staged}
  116. handleSave={handleSave}
  117. dispatch={dispatch}
  118. />
  119. <Panel>
  120. <PanelHeader>{t('Danger Zone')}</PanelHeader>
  121. <PanelBody>
  122. <DangerZonePanelContent
  123. onDelete={handleDelete}
  124. project={project}
  125. result={result}
  126. />
  127. </PanelBody>
  128. </Panel>
  129. </SentryDocumentTitle>
  130. );
  131. }
  132. function OptionsPanelContent({
  133. disabled,
  134. dispatch,
  135. options,
  136. }: {
  137. disabled: boolean;
  138. dispatch: ReturnType<typeof useRemoteConfigSettings>['dispatch'];
  139. options: RemoteConfigOptions;
  140. }) {
  141. return (
  142. <Fragment>
  143. <FieldGroup
  144. id="sample_rate"
  145. label={t('Sample Rate')}
  146. help={t(
  147. 'Configures the sample rate for error events, in the range of 0.0 to 1.0. The default is 1.0, which means that 100% of error events will be sent. If set to 0.1, only 10% of error events will be sent. Events are picked randomly.'
  148. )}
  149. >
  150. <FieldControl>
  151. <Slider
  152. id="sample_rate"
  153. aria-describedby="sample_rate_help"
  154. aria-label={t('Sample Rate')}
  155. disabled={disabled}
  156. onChangeEnd={value => {
  157. dispatch({
  158. type: 'updateOption',
  159. key: 'sample_rate',
  160. value: value as number,
  161. });
  162. }}
  163. showThumbLabels
  164. min={0}
  165. max={1}
  166. step={0.01}
  167. value={options.sample_rate}
  168. />
  169. </FieldControl>
  170. </FieldGroup>
  171. <FieldGroup
  172. id="traces_sample_rate"
  173. label={t('Traces Sample Rate')}
  174. help={t(
  175. 'A number between 0 and 1, controlling the percentage chance a given transaction will be sent to Sentry. (0 represents 0% while 1 represents 100%.) Applies equally to all transactions created in the app. Either this or traces_sampler must be defined to enable tracing.'
  176. )}
  177. >
  178. <FieldControl>
  179. <Slider
  180. id="traces_sample_rate"
  181. aria-describedby="traces_sample_rate_help"
  182. aria-label={t('Traces Sample Rate')}
  183. disabled={disabled}
  184. onChangeEnd={value => {
  185. dispatch({
  186. type: 'updateOption',
  187. key: 'traces_sample_rate',
  188. value: value as number,
  189. });
  190. }}
  191. showThumbLabels
  192. min={0}
  193. max={1}
  194. step={0.01}
  195. value={options.traces_sample_rate}
  196. />
  197. </FieldControl>
  198. </FieldGroup>
  199. </Fragment>
  200. );
  201. }
  202. function FeaturesPanelContent({
  203. disabled,
  204. dispatch,
  205. features,
  206. }: {
  207. disabled: boolean;
  208. dispatch: ReturnType<typeof useRemoteConfigSettings>['dispatch'];
  209. features: RemoteConfigFeature[];
  210. }) {
  211. if (!features.length) {
  212. return (
  213. <Fragment>
  214. <ColSpanner>
  215. <EmptyMessage>{t('No features defined')}</EmptyMessage>
  216. </ColSpanner>
  217. <div style={{padding: 0}} />
  218. <div style={{padding: 0}} />
  219. </Fragment>
  220. );
  221. }
  222. return features.map(feature => {
  223. return [
  224. <div key={`key[${feature.key}]`}>
  225. <TextCopyInput size="sm">{feature.key}</TextCopyInput>
  226. </div>,
  227. <div key={`value[${feature.key}]`}>
  228. <TextArea
  229. name={`feature[${feature.key}]`}
  230. defaultValue={feature.value}
  231. rows={2}
  232. onChange={event => {
  233. const value = event.currentTarget.value;
  234. dispatch({type: 'updateFeature', key: feature.key, value});
  235. }}
  236. />
  237. </div>,
  238. <Flex justify="flex-end" key={`remove[${feature.key}]`}>
  239. <Button
  240. size="xs"
  241. disabled={disabled}
  242. icon={<IconSubtract isCircled />}
  243. aria-label={t('Remove')}
  244. onClick={() => {
  245. dispatch({type: 'removeFeature', key: feature.key});
  246. }}
  247. >
  248. {t('Remove')}
  249. </Button>
  250. </Flex>,
  251. ];
  252. });
  253. }
  254. function PreviewPanelContent({
  255. result,
  256. staged,
  257. }: {
  258. result: ReturnType<typeof useRemoteConfigSettings>['result'];
  259. staged: ReturnType<typeof useRemoteConfigSettings>['staged'];
  260. }) {
  261. const baseJSON = JSON.stringify(result.data, null, '\t');
  262. const targetJSON = JSON.stringify(staged, null, '\t');
  263. return (
  264. <Fragment>
  265. <ColSpanner>
  266. <SplitDiff key="diff" type="words" base={baseJSON} target={targetJSON} />
  267. </ColSpanner>
  268. <div style={{padding: 0}} />
  269. </Fragment>
  270. );
  271. }
  272. function SaveRow({
  273. dispatch,
  274. handleSave,
  275. result,
  276. staged,
  277. }: {
  278. dispatch: ReturnType<typeof useRemoteConfigSettings>['dispatch'];
  279. handleSave: ReturnType<typeof useRemoteConfigSettings>['handleSave'];
  280. result: ReturnType<typeof useRemoteConfigSettings>['result'];
  281. staged: ReturnType<typeof useRemoteConfigSettings>['staged'];
  282. }) {
  283. const baseJSON = JSON.stringify(result.data, null, '\t');
  284. const targetJSON = JSON.stringify(staged, null, '\t');
  285. const isDisabled = !result.data || baseJSON === targetJSON;
  286. const handleRevert = result.data
  287. ? () => dispatch({type: 'revertStaged', data: result.data})
  288. : () => {};
  289. return (
  290. <ButtonFlex gap={space(2)}>
  291. <Button
  292. size="md"
  293. priority="primary"
  294. onClick={() => {
  295. addLoadingMessage(t('Saving remote config...'));
  296. handleSave(
  297. () => addSuccessMessage(t('Remote config saved')),
  298. () => addErrorMessage(t('Unable to save remote config'))
  299. );
  300. }}
  301. disabled={isDisabled}
  302. >
  303. {t('Save Changes')}
  304. </Button>
  305. <Button size="md" onClick={handleRevert} disabled={isDisabled}>
  306. {t('Revert Changes')}
  307. </Button>
  308. </ButtonFlex>
  309. );
  310. }
  311. function DangerZonePanelContent({
  312. onDelete,
  313. project,
  314. result,
  315. }: {
  316. onDelete: ReturnType<typeof useRemoteConfigSettings>['handleDelete'];
  317. project: Project;
  318. result: ReturnType<typeof useRemoteConfigSettings>['result'];
  319. }) {
  320. const disabled = result.isError;
  321. return (
  322. <FieldGroup
  323. label={t('Delete Remote Config')}
  324. help={tct(
  325. 'If you want to start over, you can delete the remote config for [projectName].',
  326. {projectName: <strong>{project.slug}</strong>}
  327. )}
  328. >
  329. <FieldControl>
  330. <div>
  331. <Button
  332. priority="danger"
  333. disabled={disabled}
  334. onClick={() => {
  335. openConfirmModal({
  336. header: t('Delete Remote Config'),
  337. message: t(
  338. 'Are you sure you want to delete the remote config for %s back to defaults? This cannot be undone.',
  339. project.slug
  340. ),
  341. onConfirm: () => {
  342. addLoadingMessage(t('Deleting remote config...'));
  343. onDelete(
  344. () => addSuccessMessage(t('Remote config deleted')),
  345. () => addErrorMessage(t('Unable to delete remote config'))
  346. );
  347. },
  348. });
  349. }}
  350. >
  351. {t('Delete Config')}
  352. </Button>
  353. </div>
  354. </FieldControl>
  355. </FieldGroup>
  356. );
  357. }
  358. const ButtonFlex = styled(Flex)`
  359. margin-bottom: ${space(4)};
  360. `;
  361. const FeaturesPanelTable = styled(PanelTable)`
  362. grid-template-columns: 1fr 2fr max-content;
  363. `;
  364. const ColSpanner = styled('div')`
  365. display: flex;
  366. flex-flow: column;
  367. grid-column-end: -1;
  368. grid-column-start: 1;
  369. `;