Files
sentinel/static/app/utils/useSentryAppComponentsStore.tsx
2024-02-15 17:01:50 +00:00

31 lines
856 B
TypeScript

import {useEffect, useMemo, useState} from 'react';
import SentryAppComponentsStore from 'sentry/stores/sentryAppComponentsStore';
import type {SentryAppComponent} from 'sentry/types';
export default function useSentryAppComponentsStore({
componentType,
}: {
componentType: undefined | SentryAppComponent['type'];
}) {
const [components, setComponents] = useState(SentryAppComponentsStore.getAll());
useEffect(() => {
const unsubscribe = SentryAppComponentsStore.listen(
() => setComponents(SentryAppComponentsStore.getAll()),
undefined
);
return unsubscribe as () => void;
}, []);
const filteredComponents = useMemo(() => {
if (componentType) {
return components.filter(item => item.type === componentType);
}
return components;
}, [components, componentType]);
return filteredComponents;
}