useSentryAppComponentsStore.tsx 856 B

123456789101112131415161718192021222324252627282930
  1. import {useEffect, useMemo, useState} from 'react';
  2. import SentryAppComponentsStore from 'sentry/stores/sentryAppComponentsStore';
  3. import type {SentryAppComponent} from 'sentry/types';
  4. export default function useSentryAppComponentsStore({
  5. componentType,
  6. }: {
  7. componentType: undefined | SentryAppComponent['type'];
  8. }) {
  9. const [components, setComponents] = useState(SentryAppComponentsStore.getAll());
  10. useEffect(() => {
  11. const unsubscribe = SentryAppComponentsStore.listen(
  12. () => setComponents(SentryAppComponentsStore.getAll()),
  13. undefined
  14. );
  15. return unsubscribe as () => void;
  16. }, []);
  17. const filteredComponents = useMemo(() => {
  18. if (componentType) {
  19. return components.filter(item => item.type === componentType);
  20. }
  21. return components;
  22. }, [components, componentType]);
  23. return filteredComponents;
  24. }