issuesReplayCountProvider.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {ReactNode, useMemo} from 'react';
  2. import first from 'lodash/first';
  3. import ReplayCountContext from 'sentry/components/replays/replayCountContext';
  4. import useReplaysCount from 'sentry/components/replays/useReplaysCount';
  5. import GroupStore from 'sentry/stores/groupStore';
  6. import type {Group} from 'sentry/types';
  7. import projectSupportsReplay from 'sentry/utils/replays/projectSupportsReplay';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. /**
  10. * Component to make it easier to query for replay counts against a list of groups
  11. * that exist across many projects.
  12. *
  13. * Later on when you want to read the fetched data:
  14. * ```
  15. * import ReplayCountContext from 'sentry/components/replays/replayCountContext';
  16. * const count = useContext(ReplayCountContext)[groupId];
  17. * ```
  18. */
  19. export default function IssuesReplayCountProvider({
  20. children,
  21. groupIds,
  22. }: {
  23. children: ReactNode;
  24. groupIds: string[];
  25. }) {
  26. const organization = useOrganization();
  27. // Only ask for the groupIds where the project supports replay.
  28. // For projects that don't support replay the count will always be zero.
  29. const groups = useMemo(
  30. () =>
  31. groupIds
  32. .map(id => GroupStore.get(id) as Group)
  33. .filter(Boolean)
  34. .filter(group => projectSupportsReplay(group.project)),
  35. [groupIds]
  36. );
  37. // Any project that supports replay will do here.
  38. // Project is used to signal if we should/should not do the query at all.
  39. const project = first(groups)?.project;
  40. const counts = useReplaysCount({
  41. groupIds,
  42. organization,
  43. project,
  44. });
  45. return (
  46. <ReplayCountContext.Provider value={counts}>{children}</ReplayCountContext.Provider>
  47. );
  48. }