metricListItemDetails.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import {Fragment, startTransition, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {navigateTo} from 'sentry/actionCreators/navigation';
  4. import {Button, LinkButton} from 'sentry/components/button';
  5. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import {IconSettings, IconWarning} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {MetricMeta, MRI, Project} from 'sentry/types';
  11. import {getReadableMetricType} from 'sentry/utils/metrics/formatters';
  12. import {formatMRI, parseMRI} from 'sentry/utils/metrics/mri';
  13. import {
  14. getMetricsTagsQueryKey,
  15. useMetricsTags,
  16. } from 'sentry/utils/metrics/useMetricsTags';
  17. import {useQueryClient} from 'sentry/utils/queryClient';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import useRouter from 'sentry/utils/useRouter';
  20. const MAX_PROJECTS_TO_SHOW = 3;
  21. const MAX_TAGS_TO_SHOW = 5;
  22. const STANDARD_TAGS = ['release', 'environment', 'transaction', 'project'];
  23. export function MetricListItemDetails({
  24. metric,
  25. selectedProjects,
  26. onTagClick,
  27. }: {
  28. metric: MetricMeta;
  29. onTagClick: (mri: MRI, tag: string) => void;
  30. selectedProjects: Project[];
  31. }) {
  32. const router = useRouter();
  33. const organization = useOrganization();
  34. const queryClient = useQueryClient();
  35. const isCustomMetric = parseMRI(metric.mri)?.useCase === 'custom';
  36. const [showAllTags, setShowAllTags] = useState(false);
  37. const [showAllProjects, setShowAllProjects] = useState(false);
  38. const projectIds = useMemo(
  39. () => selectedProjects.map(project => parseInt(project.id, 10)),
  40. [selectedProjects]
  41. );
  42. const [isQueryEnabled, setIsQueryEnabled] = useState(() => {
  43. // We only wnat to disable the query if there is no data in the cache
  44. const queryKey = getMetricsTagsQueryKey(organization, metric.mri, {
  45. projects: projectIds,
  46. });
  47. const data = queryClient.getQueryData(queryKey);
  48. return !!data;
  49. });
  50. const {data: tagsData = [], isLoading: tagsIsLoading} = useMetricsTags(
  51. // TODO: improve useMetricsTag interface
  52. isQueryEnabled ? metric.mri : undefined,
  53. {
  54. projects: projectIds,
  55. }
  56. );
  57. useEffect(() => {
  58. // Start querying tags after a short delay to avoid querying
  59. // for every metric if a user quickly hovers over them
  60. const timeout = setTimeout(() => {
  61. startTransition(() => setIsQueryEnabled(true));
  62. }, 200);
  63. return () => clearTimeout(timeout);
  64. }, []);
  65. const metricProjects = selectedProjects.filter(project =>
  66. metric.projectIds.includes(parseInt(project.id, 10))
  67. );
  68. const truncatedProjects = showAllProjects
  69. ? metricProjects
  70. : metricProjects.slice(0, MAX_PROJECTS_TO_SHOW);
  71. // Display custom tags first, then sort alphabetically
  72. const sortedTags = useMemo(
  73. () =>
  74. tagsData.toSorted((a, b) => {
  75. const aIsStandard = STANDARD_TAGS.includes(a.key);
  76. const bIsStandard = STANDARD_TAGS.includes(b.key);
  77. if (aIsStandard && !bIsStandard) {
  78. return 1;
  79. }
  80. if (!aIsStandard && bIsStandard) {
  81. return -1;
  82. }
  83. return a.key.localeCompare(b.key);
  84. }),
  85. [tagsData]
  86. );
  87. const truncatedTags = showAllTags ? sortedTags : sortedTags.slice(0, MAX_TAGS_TO_SHOW);
  88. const firstMetricProject = metricProjects[0];
  89. return (
  90. <DetailsWrapper>
  91. <Header>
  92. <MetricName>
  93. {/* Add zero width spaces at delimiter characters for nice word breaks */}
  94. {formatMRI(metric.mri).replaceAll(/([\.\/-_])/g, '\u200b$1')}
  95. {!isCustomMetric && (
  96. <SamplingWarning>
  97. <IconWarning color="yellow400" size="xs" />
  98. {t('Prone to client-side sampling')}
  99. </SamplingWarning>
  100. )}
  101. </MetricName>
  102. {isCustomMetric &&
  103. (firstMetricProject ? (
  104. <LinkButton
  105. size="xs"
  106. to={`/settings/projects/${firstMetricProject.slug}/metrics/${encodeURIComponent(metric.mri)}`}
  107. aria-label={t('Open metric settings')}
  108. icon={<IconSettings />}
  109. borderless
  110. />
  111. ) : (
  112. // TODO: figure out when we can end up in this case
  113. <Button
  114. size="xs"
  115. onClick={() =>
  116. navigateTo(
  117. `/settings/projects/:projectId/metrics/${encodeURIComponent(metric.mri)}`,
  118. router
  119. )
  120. }
  121. aria-label={t('Open metric settings')}
  122. icon={<IconSettings />}
  123. borderless
  124. />
  125. ))}
  126. </Header>
  127. <DetailsGrid>
  128. <DetailsLabel>{t('Project')}</DetailsLabel>
  129. <DetailsValue>
  130. {truncatedProjects.map(project => (
  131. <ProjectBadge project={project} key={project.slug} avatarSize={12} />
  132. ))}
  133. {metricProjects.length > MAX_PROJECTS_TO_SHOW && !showAllProjects && (
  134. <Button priority="link" onClick={() => setShowAllProjects(true)}>
  135. {t('+%d more', metricProjects.length - MAX_PROJECTS_TO_SHOW)}
  136. </Button>
  137. )}
  138. </DetailsValue>
  139. <DetailsLabel>{t('Type')}</DetailsLabel>
  140. <DetailsValue>{getReadableMetricType(metric.type)}</DetailsValue>
  141. <DetailsLabel>{t('Unit')}</DetailsLabel>
  142. <DetailsValue>{metric.unit}</DetailsValue>
  143. <DetailsLabel>{t('Tags')}</DetailsLabel>
  144. <DetailsValue>
  145. {tagsIsLoading || !isQueryEnabled ? (
  146. <StyledLoadingIndicator mini size={12} />
  147. ) : truncatedTags.length === 0 ? (
  148. t('(None)')
  149. ) : (
  150. <Fragment>
  151. {truncatedTags.map((tag, index) => {
  152. const shouldAddDelimiter = index < truncatedTags.length - 1;
  153. return (
  154. <Fragment key={tag.key}>
  155. <TagWrapper>
  156. <Button
  157. priority="link"
  158. onClick={() => onTagClick(metric.mri, tag.key)}
  159. >
  160. {tag.key}
  161. </Button>
  162. {/* Make the comma stick to the Button when the text wraps to the next line */}
  163. {shouldAddDelimiter ? ',' : null}
  164. </TagWrapper>
  165. {shouldAddDelimiter ? ' ' : null}
  166. </Fragment>
  167. );
  168. })}
  169. <br />
  170. {tagsData.length > MAX_TAGS_TO_SHOW && !showAllTags && (
  171. <Button priority="link" onClick={() => setShowAllTags(true)}>
  172. {t('+%d more', tagsData.length - MAX_TAGS_TO_SHOW)}
  173. </Button>
  174. )}
  175. </Fragment>
  176. )}
  177. </DetailsValue>
  178. </DetailsGrid>
  179. </DetailsWrapper>
  180. );
  181. }
  182. const DetailsWrapper = styled('div')`
  183. width: 300px;
  184. line-height: 1.4;
  185. `;
  186. const Header = styled('div')`
  187. display: grid;
  188. grid-template-columns: 1fr max-content;
  189. align-items: center;
  190. gap: ${space(0.5)};
  191. padding: ${space(0.75)} ${space(0.25)} ${space(0.75)} ${space(1.5)};
  192. `;
  193. const MetricName = styled('div')`
  194. word-break: break-word;
  195. `;
  196. const DetailsGrid = styled('div')`
  197. display: grid;
  198. grid-template-columns: max-content 1fr;
  199. & > div:nth-child(4n + 1),
  200. & > div:nth-child(4n + 2) {
  201. background-color: ${p => p.theme.backgroundSecondary};
  202. }
  203. `;
  204. const StyledLoadingIndicator = styled(LoadingIndicator)`
  205. && {
  206. margin: ${space(0.75)} 0 0;
  207. height: 12px;
  208. width: 12px;
  209. }
  210. `;
  211. const DetailsLabel = styled('div')`
  212. color: ${p => p.theme.subText};
  213. padding: ${space(0.75)} ${space(1)} ${space(0.75)} ${space(1.5)};
  214. border-top-left-radius: ${p => p.theme.borderRadius};
  215. border-bottom-left-radius: ${p => p.theme.borderRadius};
  216. `;
  217. const DetailsValue = styled('div')`
  218. white-space: pre-wrap;
  219. padding: ${space(0.75)} ${space(1.5)} ${space(0.75)} ${space(1)};
  220. border-top-right-radius: ${p => p.theme.borderRadius};
  221. border-bottom-right-radius: ${p => p.theme.borderRadius};
  222. min-width: 0;
  223. `;
  224. const TagWrapper = styled('span')`
  225. white-space: nowrap;
  226. `;
  227. const SamplingWarning = styled('div')`
  228. display: grid;
  229. gap: ${space(0.25)};
  230. grid-template-columns: max-content 1fr;
  231. font-size: ${p => p.theme.fontSizeSmall};
  232. color: ${p => p.theme.yellow400};
  233. align-items: center;
  234. `;