useSampleFields.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {useCallback, useMemo} from 'react';
  2. import type {Location} from 'history';
  3. import {decodeList} from 'sentry/utils/queryString';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import {useNavigate} from 'sentry/utils/useNavigate';
  6. interface Options {
  7. location: Location;
  8. navigate: ReturnType<typeof useNavigate>;
  9. }
  10. export type Field = string;
  11. export function useSampleFields(): [Field[], (fields: Field[]) => void] {
  12. const location = useLocation();
  13. const navigate = useNavigate();
  14. const options = {location, navigate};
  15. return useSampleFieldsImpl(options);
  16. }
  17. function useSampleFieldsImpl({
  18. location,
  19. navigate,
  20. }: Options): [Field[], (fields: Field[]) => void] {
  21. const sampleFields = useMemo(() => {
  22. const fields = decodeList(location.query.field);
  23. if (fields.length) {
  24. return fields;
  25. }
  26. return [
  27. 'span_id',
  28. 'project',
  29. 'span.op',
  30. 'span.description',
  31. 'span.duration',
  32. 'timestamp',
  33. ];
  34. }, [location.query.field]);
  35. const setSampleFields = useCallback(
  36. (fields: Field[]) => {
  37. navigate({
  38. ...location,
  39. query: {
  40. ...location.query,
  41. field: fields,
  42. },
  43. });
  44. },
  45. [location, navigate]
  46. );
  47. return [sampleFields, setSampleFields];
  48. }