useWasSearchSpaceExhausted.tsx 990 B

1234567891011121314151617181920212223242526272829303132
  1. import {useEffect, useState} from 'react';
  2. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  3. interface Props {
  4. isLoading: boolean;
  5. query: string;
  6. pageLinks?: string;
  7. }
  8. /**
  9. * Keeps track of the passed responses. Remembers if at any point a response
  10. * had no query, and didn't have any subsequent data. This means that at
  11. * some point, a _complete_ response was served. Useful for caches and re-fetch
  12. * behavior where we want to _avoid fetches_ if we know we've loaded the
  13. * entire data set at some point and a cache is full.
  14. */
  15. export function useWasSearchSpaceExhausted({query, isLoading, pageLinks}: Props) {
  16. const [wasSearchSpaceExhausted, setWasSearchSpaceExhausted] = useState<boolean>(false);
  17. useEffect(() => {
  18. if (query === '' && !isLoading) {
  19. const {next} = parseLinkHeader(pageLinks ?? '');
  20. if (!next) {
  21. setWasSearchSpaceExhausted(true);
  22. }
  23. }
  24. }, [query, isLoading, pageLinks]);
  25. return wasSearchSpaceExhausted;
  26. }