useSystemSelectorOptions.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type {SelectOption} from 'sentry/components/compactSelect';
  2. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  3. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  4. import {useSpanMetrics} from 'sentry/views/insights/common/queries/useDiscover';
  5. import {
  6. DATABASE_SYSTEM_TO_LABEL,
  7. SupportedDatabaseSystem,
  8. } from 'sentry/views/insights/database/utils/constants';
  9. import {SpanMetricsField} from 'sentry/views/insights/types';
  10. export function useSystemSelectorOptions() {
  11. const [selectedSystem, setSelectedSystem] = useLocalStorageState<string | undefined>(
  12. 'insights-db-system-selector',
  13. undefined
  14. );
  15. const {data, isPending, isError} = useSpanMetrics(
  16. {
  17. search: MutableSearch.fromQueryObject({'span.op': 'db'}),
  18. fields: [SpanMetricsField.SPAN_SYSTEM, 'count()'],
  19. sorts: [{field: 'count()', kind: 'desc'}],
  20. },
  21. 'api.starfish.database-system-selector'
  22. );
  23. const options: Array<SelectOption<string>> = [];
  24. data.forEach(entry => {
  25. const system = entry['span.system'];
  26. if (system) {
  27. const textValue =
  28. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  29. system in DATABASE_SYSTEM_TO_LABEL ? DATABASE_SYSTEM_TO_LABEL[system] : system;
  30. const supportedSystemSet: Set<string> = new Set(
  31. Object.values(SupportedDatabaseSystem)
  32. );
  33. if (supportedSystemSet.has(system)) {
  34. options.push({value: system, label: textValue, textValue});
  35. }
  36. }
  37. });
  38. // Edge case: Invalid DB system was retrieved from localStorage
  39. if (!options.find(option => selectedSystem === option.value) && options.length > 0) {
  40. setSelectedSystem(options[0]!.value);
  41. }
  42. // Edge case: No current system is saved in localStorage
  43. if (!selectedSystem && options.length > 0) {
  44. setSelectedSystem(options[0]!.value);
  45. }
  46. return {selectedSystem, setSelectedSystem, options, isLoading: isPending, isError};
  47. }