sidebar.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import {Component, Fragment} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import isEqual from 'lodash/isEqual';
  6. import isObject from 'lodash/isObject';
  7. import keyBy from 'lodash/keyBy';
  8. import pickBy from 'lodash/pickBy';
  9. import {Client} from 'sentry/api';
  10. import Feature from 'sentry/components/acl/feature';
  11. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  12. import ErrorBoundary from 'sentry/components/errorBoundary';
  13. import AssignedTo from 'sentry/components/group/assignedTo';
  14. import ExternalIssueList from 'sentry/components/group/externalIssuesList';
  15. import OwnedBy from 'sentry/components/group/ownedBy';
  16. import GroupParticipants from 'sentry/components/group/participants';
  17. import GroupReleaseStats from 'sentry/components/group/releaseStats';
  18. import SuggestedOwners from 'sentry/components/group/suggestedOwners/suggestedOwners';
  19. import SuspectReleases from 'sentry/components/group/suspectReleases';
  20. import GroupTagDistributionMeter from 'sentry/components/group/tagDistributionMeter';
  21. import LoadingError from 'sentry/components/loadingError';
  22. import Placeholder from 'sentry/components/placeholder';
  23. import * as SidebarSection from 'sentry/components/sidebarSection';
  24. import {t} from 'sentry/locale';
  25. import space from 'sentry/styles/space';
  26. import {
  27. CurrentRelease,
  28. Environment,
  29. Group,
  30. Organization,
  31. Project,
  32. TagWithTopValues,
  33. } from 'sentry/types';
  34. import {Event} from 'sentry/types/event';
  35. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  36. import {getUtcDateString} from 'sentry/utils/dates';
  37. import withApi from 'sentry/utils/withApi';
  38. type Props = WithRouterProps & {
  39. api: Client;
  40. environments: Environment[];
  41. group: Group;
  42. organization: Organization;
  43. project: Project;
  44. event?: Event;
  45. };
  46. type State = {
  47. environments: Environment[];
  48. participants: Group['participants'];
  49. allEnvironmentsGroupData?: Group;
  50. currentRelease?: CurrentRelease;
  51. error?: boolean;
  52. tagsWithTopValues?: Record<string, TagWithTopValues>;
  53. };
  54. class BaseGroupSidebar extends Component<Props, State> {
  55. state: State = {
  56. participants: [],
  57. environments: this.props.environments,
  58. };
  59. componentDidMount() {
  60. this.fetchAllEnvironmentsGroupData();
  61. this.fetchCurrentRelease();
  62. this.fetchParticipants();
  63. this.fetchTagData();
  64. }
  65. UNSAFE_componentWillReceiveProps(nextProps: Props) {
  66. if (!isEqual(nextProps.environments, this.props.environments)) {
  67. this.setState({environments: nextProps.environments}, this.fetchTagData);
  68. }
  69. }
  70. trackAssign: React.ComponentProps<typeof AssignedTo>['onAssign'] = () => {
  71. const {group, project, organization, location} = this.props;
  72. const {alert_date, alert_rule_id, alert_type} = location.query;
  73. trackAdvancedAnalyticsEvent('issue_details.action_clicked', {
  74. organization,
  75. project_id: parseInt(project.id, 10),
  76. group_id: parseInt(group.id, 10),
  77. issue_category: group.issueCategory,
  78. action_type: 'assign',
  79. alert_date:
  80. typeof alert_date === 'string' ? getUtcDateString(Number(alert_date)) : undefined,
  81. alert_rule_id: typeof alert_rule_id === 'string' ? alert_rule_id : undefined,
  82. alert_type: typeof alert_type === 'string' ? alert_type : undefined,
  83. });
  84. };
  85. async fetchAllEnvironmentsGroupData() {
  86. const {group, api} = this.props;
  87. // Fetch group data for all environments since the one passed in props is filtered for the selected environment
  88. // The charts rely on having all environment data as well as the data for the selected env
  89. try {
  90. const query = {collapse: 'release'};
  91. const allEnvironmentsGroupData = await api.requestPromise(`/issues/${group.id}/`, {
  92. query,
  93. });
  94. // eslint-disable-next-line react/no-did-mount-set-state
  95. this.setState({allEnvironmentsGroupData});
  96. } catch {
  97. // eslint-disable-next-line react/no-did-mount-set-state
  98. this.setState({error: true});
  99. }
  100. }
  101. async fetchCurrentRelease() {
  102. const {group, api} = this.props;
  103. try {
  104. const {currentRelease} = await api.requestPromise(
  105. `/issues/${group.id}/current-release/`
  106. );
  107. this.setState({currentRelease});
  108. } catch {
  109. this.setState({error: true});
  110. }
  111. }
  112. async fetchParticipants() {
  113. const {group, api} = this.props;
  114. try {
  115. const participants = await api.requestPromise(`/issues/${group.id}/participants/`);
  116. this.setState({
  117. participants,
  118. error: false,
  119. });
  120. return participants;
  121. } catch {
  122. this.setState({
  123. error: true,
  124. });
  125. return [];
  126. }
  127. }
  128. async fetchTagData() {
  129. const {api, group} = this.props;
  130. try {
  131. // Fetch the top values for the current group's top tags.
  132. const data = await api.requestPromise(`/issues/${group.id}/tags/`, {
  133. query: pickBy({
  134. key: group.tags.map(tag => tag.key),
  135. environment: this.state.environments.map(env => env.name),
  136. }),
  137. });
  138. this.setState({tagsWithTopValues: keyBy(data, 'key')});
  139. } catch {
  140. this.setState({
  141. tagsWithTopValues: {},
  142. error: true,
  143. });
  144. }
  145. }
  146. renderPluginIssue() {
  147. const issues: React.ReactNode[] = [];
  148. (this.props.group.pluginIssues || []).forEach(plugin => {
  149. const issue = plugin.issue;
  150. // # TODO(dcramer): remove plugin.title check in Sentry 8.22+
  151. if (issue) {
  152. issues.push(
  153. <Fragment key={plugin.slug}>
  154. <span>{`${plugin.shortName || plugin.name || plugin.title}: `}</span>
  155. <a href={issue.url}>{isObject(issue.label) ? issue.label.id : issue.label}</a>
  156. </Fragment>
  157. );
  158. }
  159. });
  160. if (!issues.length) {
  161. return null;
  162. }
  163. return (
  164. <SidebarSection.Wrap>
  165. <SidebarSection.Title>{t('External Issues')}</SidebarSection.Title>
  166. <SidebarSection.Content>
  167. <ExternalIssues>{issues}</ExternalIssues>
  168. </SidebarSection.Content>
  169. </SidebarSection.Wrap>
  170. );
  171. }
  172. renderParticipantData() {
  173. const {error, participants = []} = this.state;
  174. if (error) {
  175. return (
  176. <LoadingError
  177. message={t('There was an error while trying to load participants.')}
  178. />
  179. );
  180. }
  181. return participants.length !== 0 && <GroupParticipants participants={participants} />;
  182. }
  183. render() {
  184. const {event, group, organization, project, environments} = this.props;
  185. const {allEnvironmentsGroupData, currentRelease, tagsWithTopValues} = this.state;
  186. const projectId = project.slug;
  187. return (
  188. <Container>
  189. {!organization.features.includes('issue-actions-v2') && (
  190. <PageFiltersContainer>
  191. <EnvironmentPageFilter alignDropdown="right" />
  192. </PageFiltersContainer>
  193. )}
  194. <Feature organization={organization} features={['issue-details-owners']}>
  195. <OwnedBy group={group} project={project} organization={organization} />
  196. <AssignedTo group={group} projectId={project.id} onAssign={this.trackAssign} />
  197. </Feature>
  198. {event && <SuggestedOwners project={project} group={group} event={event} />}
  199. <GroupReleaseStats
  200. organization={organization}
  201. project={project}
  202. environments={environments}
  203. allEnvironments={allEnvironmentsGroupData}
  204. group={group}
  205. currentRelease={currentRelease}
  206. />
  207. <Feature organization={organization} features={['active-release-monitor-alpha']}>
  208. <SuspectReleases group={group} />
  209. </Feature>
  210. {event && (
  211. <ErrorBoundary mini>
  212. <ExternalIssueList project={project} group={group} event={event} />
  213. </ErrorBoundary>
  214. )}
  215. {this.renderPluginIssue()}
  216. <SidebarSection.Wrap>
  217. <SidebarSection.Title>{t('Tag Summary')}</SidebarSection.Title>
  218. <SidebarSection.Content>
  219. {!tagsWithTopValues ? (
  220. <TagPlaceholders>
  221. <Placeholder height="40px" />
  222. <Placeholder height="40px" />
  223. <Placeholder height="40px" />
  224. <Placeholder height="40px" />
  225. </TagPlaceholders>
  226. ) : (
  227. group.tags.map(tag => {
  228. const tagWithTopValues = tagsWithTopValues[tag.key];
  229. const topValues = tagWithTopValues ? tagWithTopValues.topValues : [];
  230. const topValuesTotal = tagWithTopValues
  231. ? tagWithTopValues.totalValues
  232. : 0;
  233. return (
  234. <GroupTagDistributionMeter
  235. key={tag.key}
  236. tag={tag.key}
  237. totalValues={topValuesTotal}
  238. topValues={topValues}
  239. name={tag.name}
  240. organization={organization}
  241. projectId={projectId}
  242. group={group}
  243. />
  244. );
  245. })
  246. )}
  247. {group.tags.length === 0 && (
  248. <p data-test-id="no-tags">
  249. {environments.length
  250. ? t('No tags found in the selected environments')
  251. : t('No tags found')}
  252. </p>
  253. )}
  254. </SidebarSection.Content>
  255. </SidebarSection.Wrap>
  256. {this.renderParticipantData()}
  257. </Container>
  258. );
  259. }
  260. }
  261. const PageFiltersContainer = styled('div')`
  262. margin-bottom: ${space(2)};
  263. `;
  264. const Container = styled('div')`
  265. font-size: ${p => p.theme.fontSizeMedium};
  266. `;
  267. const TagPlaceholders = styled('div')`
  268. display: grid;
  269. gap: ${space(1)};
  270. grid-auto-flow: row;
  271. `;
  272. const ExternalIssues = styled('div')`
  273. display: grid;
  274. grid-template-columns: auto max-content;
  275. gap: ${space(2)};
  276. `;
  277. const GroupSidebar = withApi(withRouter(BaseGroupSidebar));
  278. export default GroupSidebar;