eventsGeoRequest.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. type ChildrenArgs = {
  9. errored: boolean;
  10. loading: boolean;
  11. reloading: boolean;
  12. tableData?: TableDataWithTitle[];
  13. };
  14. const EventsGeoRequest = ({
  15. api,
  16. organization,
  17. yAxis,
  18. query,
  19. orderby,
  20. projects,
  21. period,
  22. start,
  23. end,
  24. environments,
  25. referrer,
  26. children,
  27. }: {
  28. api: Client;
  29. organization: OrganizationSummary;
  30. yAxis: string | string[];
  31. query: string;
  32. orderby?: string;
  33. projects: number[];
  34. period?: string;
  35. start: DateString;
  36. end: DateString;
  37. environments: string[];
  38. referrer?: string;
  39. children: (args: ChildrenArgs) => React.ReactElement;
  40. }) => {
  41. const eventView = EventView.fromSavedQuery({
  42. id: undefined,
  43. name: '',
  44. version: 2,
  45. fields: Array.isArray(yAxis) ? yAxis : [yAxis],
  46. query,
  47. orderby: orderby ?? '',
  48. projects,
  49. range: period ?? '',
  50. start: start ? getUtcDateString(start) : undefined,
  51. end: end ? getUtcDateString(end) : undefined,
  52. environment: environments,
  53. });
  54. const [results, setResults] = useState(undefined as ChildrenArgs['tableData']);
  55. const [reloading, setReloading] = useState(false);
  56. const [errored, setErrored] = useState(false);
  57. useEffect(() => {
  58. setErrored(false);
  59. if (results) {
  60. setReloading(true);
  61. }
  62. doDiscoverQuery<TableData>(api, `/organizations/${organization.slug}/events-geo/`, {
  63. ...eventView.generateQueryStringObject(),
  64. referrer,
  65. })
  66. .then(discoverQueryResults => {
  67. setResults([discoverQueryResults[0]] as TableDataWithTitle[]);
  68. setReloading(false);
  69. })
  70. .catch(() => {
  71. setErrored(true);
  72. setReloading(false);
  73. });
  74. }, [query, yAxis, start, end, period, environments, projects]);
  75. return children({
  76. errored,
  77. loading: !results && !errored,
  78. reloading,
  79. tableData: results,
  80. });
  81. };
  82. export default EventsGeoRequest;