useSystemSelectorOptions.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type {ReactNode} from 'react';
  2. import styled from '@emotion/styled';
  3. import FeatureBadge from 'sentry/components/badge/featureBadge';
  4. import type {SelectOption} from 'sentry/components/compactSelect';
  5. import {space} from 'sentry/styles/space';
  6. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  8. import {useSpanMetrics} from 'sentry/views/insights/common/queries/useDiscover';
  9. import {
  10. DATABASE_SYSTEM_TO_LABEL,
  11. SupportedDatabaseSystem,
  12. } from 'sentry/views/insights/database/utils/constants';
  13. import {SpanMetricsField} from 'sentry/views/insights/types';
  14. export function useSystemSelectorOptions() {
  15. const [selectedSystem, setSelectedSystem] = useLocalStorageState<string>(
  16. 'insights-db-system-selector',
  17. ''
  18. );
  19. const {data, isPending, isError} = useSpanMetrics(
  20. {
  21. search: MutableSearch.fromQueryObject({'span.op': 'db'}),
  22. fields: [SpanMetricsField.SPAN_SYSTEM, 'count()'],
  23. sorts: [{field: 'count()', kind: 'desc'}],
  24. },
  25. 'api.starfish.database-system-selector'
  26. );
  27. const options: SelectOption<string>[] = [];
  28. data.forEach(entry => {
  29. const system = entry['span.system'];
  30. if (system) {
  31. let label: ReactNode = '';
  32. const textValue =
  33. system in DATABASE_SYSTEM_TO_LABEL ? DATABASE_SYSTEM_TO_LABEL[system] : system;
  34. const supportedSystemSet: Set<string> = new Set(
  35. Object.values(SupportedDatabaseSystem)
  36. );
  37. if (system === SupportedDatabaseSystem.MONGODB) {
  38. label = (
  39. <LabelContainer>
  40. {textValue}
  41. <StyledFeatureBadge type={'new'} />
  42. </LabelContainer>
  43. );
  44. } else {
  45. label = textValue;
  46. }
  47. if (supportedSystemSet.has(system)) {
  48. options.push({value: system, label, textValue});
  49. }
  50. }
  51. });
  52. // Edge case: Invalid DB system was retrieved from localStorage
  53. if (!options.find(option => selectedSystem === option.value) && options.length > 0) {
  54. setSelectedSystem(options[0].value);
  55. }
  56. // Edge case: No current system is saved in localStorage
  57. if (!selectedSystem && options.length > 0) {
  58. setSelectedSystem(options[0].value);
  59. }
  60. return {selectedSystem, setSelectedSystem, options, isLoading: isPending, isError};
  61. }
  62. const StyledFeatureBadge = styled(FeatureBadge)`
  63. margin-left: ${space(1)};
  64. `;
  65. const LabelContainer = styled('div')`
  66. display: flex;
  67. align-items: center;
  68. `;