bigNumber.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {space} from 'sentry/styles/space';
  4. import type {MetricsQueryApiResponse} from 'sentry/types/metrics';
  5. import {formatMetricUsingUnit} from 'sentry/utils/metrics/formatters';
  6. import {LoadingScreen} from 'sentry/views/dashboards/widgetCard/widgetCardChartContainer';
  7. interface MetricBigNumberContainerProps {
  8. isLoading: boolean;
  9. timeseriesData?: MetricsQueryApiResponse;
  10. }
  11. export function MetricBigNumberContainer({
  12. timeseriesData,
  13. isLoading,
  14. }: MetricBigNumberContainerProps) {
  15. const bigNumberData = useMemo(() => {
  16. return timeseriesData ? getBigNumberData(timeseriesData) : undefined;
  17. }, [timeseriesData]);
  18. return (
  19. <BigNumberWrapper>
  20. <LoadingScreen loading={isLoading} />
  21. <BigNumber>{bigNumberData}</BigNumber>
  22. </BigNumberWrapper>
  23. );
  24. }
  25. export function getBigNumberData(data: MetricsQueryApiResponse): string {
  26. try {
  27. // Big number widgets only have one query
  28. const value = data.data[0][0].totals;
  29. const lastMetaEntry = data.meta[0][1];
  30. const metaUnit =
  31. (lastMetaEntry && 'unit' in lastMetaEntry && lastMetaEntry.unit) || 'none';
  32. return formatMetricUsingUnit(value, metaUnit);
  33. } catch (e) {
  34. // TODO(metrics): handle this when adding support for bing number equations
  35. return '-';
  36. }
  37. }
  38. const BigNumberWrapper = styled('div')`
  39. height: 100%;
  40. width: 100%;
  41. overflow: hidden;
  42. `;
  43. export const BigNumber = styled('div')`
  44. line-height: 1;
  45. display: inline-flex;
  46. flex: 1;
  47. width: 100%;
  48. min-height: 0;
  49. font-size: 32px;
  50. color: ${p => p.theme.headingColor};
  51. padding: ${space(1)} ${space(3)} ${space(3)} ${space(3)};
  52. * {
  53. text-align: left !important;
  54. }
  55. `;