eventsGeoRequest.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {useEffect, useState} from 'react';
  2. import {Client} from 'sentry/api';
  3. import {DateString, OrganizationSummary} from 'sentry/types';
  4. import {getUtcDateString} from 'sentry/utils/dates';
  5. import {TableData, TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  6. import EventView from 'sentry/utils/discover/eventView';
  7. import {doDiscoverQuery} from 'sentry/utils/discover/genericDiscoverQuery';
  8. interface ChildrenRenderProps {
  9. errored: boolean;
  10. loading: boolean;
  11. reloading: boolean;
  12. tableData?: TableDataWithTitle[];
  13. }
  14. export interface EventsGeoRequestProps {
  15. api: Client;
  16. children: (props: ChildrenRenderProps) => React.ReactElement;
  17. end: DateString;
  18. environments: string[];
  19. organization: OrganizationSummary;
  20. projects: number[];
  21. query: string;
  22. start: DateString;
  23. yAxis: string | string[];
  24. orderby?: string;
  25. period?: string | null;
  26. referrer?: string;
  27. }
  28. const EventsGeoRequest = ({
  29. api,
  30. organization,
  31. yAxis,
  32. query,
  33. orderby,
  34. projects,
  35. period,
  36. start,
  37. end,
  38. environments,
  39. referrer,
  40. children,
  41. }: EventsGeoRequestProps) => {
  42. const eventView = EventView.fromSavedQuery({
  43. id: undefined,
  44. name: '',
  45. version: 2,
  46. fields: Array.isArray(yAxis) ? yAxis : [yAxis],
  47. query,
  48. orderby: orderby ?? '',
  49. projects,
  50. range: period ?? '',
  51. start: start ? getUtcDateString(start) : undefined,
  52. end: end ? getUtcDateString(end) : undefined,
  53. environment: environments,
  54. });
  55. const [results, setResults] = useState(undefined as ChildrenRenderProps['tableData']);
  56. const [reloading, setReloading] = useState(false);
  57. const [errored, setErrored] = useState(false);
  58. useEffect(() => {
  59. let mounted = true;
  60. setErrored(false);
  61. if (results) {
  62. setReloading(true);
  63. }
  64. doDiscoverQuery<TableData>(api, `/organizations/${organization.slug}/events-geo/`, {
  65. ...eventView.generateQueryStringObject(),
  66. referrer,
  67. })
  68. .then(discoverQueryResults => {
  69. if (mounted) {
  70. setResults([discoverQueryResults[0]] as TableDataWithTitle[]);
  71. setReloading(false);
  72. }
  73. })
  74. .catch(() => {
  75. if (mounted) {
  76. setErrored(true);
  77. setReloading(false);
  78. }
  79. });
  80. return () => {
  81. // Prevent setState leaking on unmounted component
  82. mounted = false;
  83. };
  84. }, [query, yAxis, start, end, period, environments, projects, api]);
  85. return children({
  86. errored,
  87. loading: !results && !errored,
  88. reloading,
  89. tableData: results,
  90. });
  91. };
  92. export default EventsGeoRequest;