index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import sentryPattern from 'sentry-images/pattern/sentry-pattern.png';
  4. import {Alert} from 'sentry/components/alert';
  5. import ApiForm from 'sentry/components/forms/apiForm';
  6. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  7. import {t} from 'sentry/locale';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import {space} from 'sentry/styles/space';
  10. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  11. import type {Field} from '../options';
  12. import {getForm, getOptionDefault, getOptionField} from '../options';
  13. export type InstallWizardProps = DeprecatedAsyncView['props'] & {
  14. onConfigured: () => void;
  15. };
  16. export type InstallWizardOptions = Record<
  17. string,
  18. {
  19. field: Field;
  20. value?: unknown;
  21. }
  22. >;
  23. type State = DeprecatedAsyncView['state'] & {
  24. data: null | InstallWizardOptions;
  25. };
  26. export default class InstallWizard extends DeprecatedAsyncView<
  27. InstallWizardProps,
  28. State
  29. > {
  30. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  31. return [['data', '/internal/options/?query=is:required']];
  32. }
  33. renderFormFields() {
  34. const options = this.state.data!;
  35. let missingOptions = new Set(
  36. Object.keys(options).filter(option => !options[option].field.isSet)
  37. );
  38. // This is to handle the initial installation case.
  39. // Even if all options are filled out, we want to prompt to confirm
  40. // them. This is a bit of a hack because we're assuming that
  41. // the backend only spit back all filled out options for
  42. // this case.
  43. if (missingOptions.size === 0) {
  44. missingOptions = new Set(Object.keys(options));
  45. }
  46. // A mapping of option name to Field object
  47. const fields = {};
  48. for (const key of missingOptions) {
  49. const option = options[key];
  50. if (option.field.disabled) {
  51. continue;
  52. }
  53. fields[key] = getOptionField(key, option.field);
  54. }
  55. return getForm(fields);
  56. }
  57. getInitialData() {
  58. const options = this.state.data!;
  59. const data = {};
  60. Object.keys(options).forEach(optionName => {
  61. const option = options[optionName];
  62. if (option.field.disabled) {
  63. return;
  64. }
  65. // TODO(dcramer): we need to rethink this logic as doing multiple "is this value actually set"
  66. // is problematic
  67. // all values to their server-defaults (as client-side defaults don't really work)
  68. const displayValue = option.value || getOptionDefault(optionName);
  69. if (
  70. // XXX(dcramer): we need the user to explicitly choose beacon.anonymous
  71. // vs using an implied default so effectively this is binding
  72. optionName !== 'beacon.anonymous' &&
  73. // XXX(byk): if we don't have a set value but have a default value filled
  74. // instead, from the client, set it on the data so it is sent to the server
  75. !option.field.isSet &&
  76. displayValue !== undefined
  77. ) {
  78. data[optionName] = displayValue;
  79. }
  80. });
  81. return data;
  82. }
  83. getTitle() {
  84. return t('Setup Sentry');
  85. }
  86. render() {
  87. const version = ConfigStore.get('version');
  88. return (
  89. <SentryDocumentTitle noSuffix title={this.getTitle()}>
  90. <Wrapper>
  91. <Pattern />
  92. <SetupWizard>
  93. <Heading>
  94. <span>{t('Welcome to Sentry')}</span>
  95. <Version>{version.current}</Version>
  96. </Heading>
  97. {this.state.loading
  98. ? this.renderLoading()
  99. : this.state.error
  100. ? this.renderError()
  101. : this.renderBody()}
  102. </SetupWizard>
  103. </Wrapper>
  104. </SentryDocumentTitle>
  105. );
  106. }
  107. renderError() {
  108. return (
  109. <Alert type="error" showIcon>
  110. {t(
  111. 'We were unable to load the required configuration from the Sentry server. Please take a look at the service logs.'
  112. )}
  113. </Alert>
  114. );
  115. }
  116. renderBody() {
  117. return (
  118. <ApiForm
  119. apiMethod="PUT"
  120. apiEndpoint={this.getEndpoints()[0][1]}
  121. submitLabel={t('Continue')}
  122. initialData={this.getInitialData()}
  123. onSubmitSuccess={this.props.onConfigured}
  124. >
  125. <p>{t('Complete setup by filling out the required configuration.')}</p>
  126. {this.renderFormFields()}
  127. </ApiForm>
  128. );
  129. }
  130. }
  131. const Wrapper = styled('div')`
  132. display: flex;
  133. justify-content: center;
  134. `;
  135. const fixedStyle = css`
  136. position: fixed;
  137. top: 0;
  138. right: 0;
  139. bottom: 0;
  140. left: 0;
  141. `;
  142. const Pattern = styled('div')`
  143. z-index: -1;
  144. &::before {
  145. ${fixedStyle}
  146. content: '';
  147. background-image: linear-gradient(
  148. to right,
  149. ${p => p.theme.purple200} 0%,
  150. ${p => p.theme.purple300} 100%
  151. );
  152. background-repeat: repeat-y;
  153. }
  154. &::after {
  155. ${fixedStyle}
  156. content: '';
  157. background: url(${sentryPattern});
  158. background-size: 400px;
  159. opacity: 0.8;
  160. }
  161. `;
  162. const Heading = styled('h1')`
  163. display: grid;
  164. gap: ${space(1)};
  165. justify-content: space-between;
  166. grid-auto-flow: column;
  167. line-height: 36px;
  168. `;
  169. const Version = styled('small')`
  170. font-size: ${p => p.theme.fontSizeExtraLarge};
  171. line-height: inherit;
  172. `;
  173. const SetupWizard = styled('div')`
  174. background: ${p => p.theme.background};
  175. border-radius: ${p => p.theme.borderRadius};
  176. box-shadow: ${p => p.theme.dropShadowHeavy};
  177. margin-top: 40px;
  178. padding: 40px 40px 20px;
  179. width: 600px;
  180. z-index: ${p => p.theme.zIndex.initial};
  181. `;