useUserQuery.tsx 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {useCallback} from 'react';
  2. import type {Location} from 'history';
  3. import {decodeQuery} from 'sentry/utils/discover/eventView';
  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 function useUserQuery(): [string, (newQuery: string) => void] {
  11. const location = useLocation();
  12. const navigate = useNavigate();
  13. const options = {location, navigate};
  14. return useUserQueryImpl(options);
  15. }
  16. function useUserQueryImpl({
  17. location,
  18. navigate,
  19. }: Options): [string, (newQuery: string) => void] {
  20. const userQuery = decodeQuery(location);
  21. const setUserQuery = useCallback(
  22. (newQuery: string) => {
  23. navigate({
  24. ...location,
  25. query: {
  26. ...location.query,
  27. query: newQuery,
  28. },
  29. });
  30. },
  31. [location, navigate]
  32. );
  33. return [userQuery, setUserQuery];
  34. }