projectQueries.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import type { QueryClient } from '@tanstack/react-query';
  2. /**
  3. * Every React Query key whose data is derived from which archives belong to a
  4. * project. All are prefixes: `['project']` matches `['project', 42]`, so one
  5. * entry covers every project id currently in the cache.
  6. */
  7. const PROJECT_VIEW_QUERY_KEYS = [
  8. ['projects'],
  9. ['project'],
  10. ['project-archives'],
  11. ['project-timeline'],
  12. ['project-file-progress'],
  13. ] as const;
  14. /**
  15. * Refresh everything a project shows after an archive is deleted, or moved
  16. * into or out of a project.
  17. *
  18. * The default `staleTime` is 60s, so without this a project page visited
  19. * within a minute of the change serves its cached answer and keeps showing a
  20. * print that is no longer there — the user has to reload by hand (#2731).
  21. * Deletes previously invalidated only `['archives']`, and the project-assign
  22. * mutations only `['projects']`, which refreshed the overview cards but never
  23. * the detail page they were most likely looking at.
  24. */
  25. export function invalidateProjectViews(queryClient: QueryClient) {
  26. return Promise.all(
  27. PROJECT_VIEW_QUERY_KEYS.map((queryKey) => queryClient.invalidateQueries({ queryKey: [...queryKey] })),
  28. );
  29. }
  30. /** As above, plus the archive list itself — for mutations that change both. */
  31. export function invalidateArchiveAndProjectViews(queryClient: QueryClient) {
  32. return Promise.all([
  33. queryClient.invalidateQueries({ queryKey: ['archives'] }),
  34. invalidateProjectViews(queryClient),
  35. ]);
  36. }