bigNumber.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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';
  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. // Big number widgets only have one query
  27. const value = data.data[0][0].totals;
  28. const lastMetaEntry = data.meta[0][1];
  29. const metaUnit =
  30. (lastMetaEntry && 'unit' in lastMetaEntry && lastMetaEntry.unit) || 'none';
  31. return formatMetricUsingUnit(value, metaUnit);
  32. }
  33. const BigNumberWrapper = styled('div')`
  34. height: 100%;
  35. width: 100%;
  36. overflow: hidden;
  37. `;
  38. export const BigNumber = styled('div')`
  39. line-height: 1;
  40. display: inline-flex;
  41. flex: 1;
  42. width: 100%;
  43. min-height: 0;
  44. font-size: 32px;
  45. color: ${p => p.theme.headingColor};
  46. padding: ${space(1)} ${space(3)} ${space(3)} ${space(3)};
  47. * {
  48. text-align: left !important;
  49. }
  50. `;