From d6bc32817a90de85a1c3c9572192ed5dedd1d275 Mon Sep 17 00:00:00 2001 From: Praveen K B Date: Tue, 26 Nov 2024 08:34:37 +0530 Subject: [PATCH] fix: handle null userRoles in stream metadata fetching logic --- src/hooks/useGetStreamMetadata.ts | 62 ++++++++++++++++--------------- src/pages/Home/index.tsx | 4 +- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/hooks/useGetStreamMetadata.ts b/src/hooks/useGetStreamMetadata.ts index 0cdc9712..d2ab4cc0 100644 --- a/src/hooks/useGetStreamMetadata.ts +++ b/src/hooks/useGetStreamMetadata.ts @@ -20,37 +20,41 @@ export const useGetStreamMetadata = () => { const [metaData, setMetadata] = useState(null); const [userRoles] = useAppStore((store) => store.userRoles); - const getStreamMetadata = useCallback(async (streams: string[]) => { - setLoading(true); - try { - // stats - const allStatsReqs = streams.map((stream) => getLogStreamStats(stream)); - const allStatsRes = await Promise.all(allStatsReqs); + const getStreamMetadata = useCallback( + async (streams: string[]) => { + if (!userRoles) return; + setLoading(true); + try { + // stats + const allStatsReqs = streams.map((stream) => getLogStreamStats(stream)); + const allStatsRes = await Promise.all(allStatsReqs); - // retention - const streamsWithSettingsAccess = _.filter(streams, (stream) => - _.includes(getStreamsSepcificAccess(userRoles, stream), 'StreamSettings'), - ); - const allretentionReqs = streamsWithSettingsAccess.map((stream) => getLogStreamRetention(stream)); - const allretentionRes = await Promise.all(allretentionReqs); + // retention + const streamsWithSettingsAccess = _.filter(streams, (stream) => + _.includes(getStreamsSepcificAccess(userRoles, stream), 'StreamSettings'), + ); + const allretentionReqs = streamsWithSettingsAccess.map((stream) => getLogStreamRetention(stream)); + const allretentionRes = await Promise.all(allretentionReqs); - const metadata = streams.reduce((acc, stream, index) => { - return { - ...acc, - [stream]: { stats: allStatsRes[index]?.data || {}, retention: allretentionRes[index]?.data || [] }, - }; - }, {}); - setMetadata(metadata); - } catch { - setError(true); - setMetadata(null); - notifyError({ - message: 'Unable to fetch stream data', - }); - } finally { - setLoading(false); - } - }, []); + const metadata = streams.reduce((acc, stream, index) => { + return { + ...acc, + [stream]: { stats: allStatsRes[index]?.data || {}, retention: allretentionRes[index]?.data || [] }, + }; + }, {}); + setMetadata(metadata); + } catch { + setError(true); + setMetadata(null); + notifyError({ + message: 'Unable to fetch stream data', + }); + } finally { + setLoading(false); + } + }, + [userRoles], + ); return { isLoading, diff --git a/src/pages/Home/index.tsx b/src/pages/Home/index.tsx index b46c3e3a..a4b22d33 100644 --- a/src/pages/Home/index.tsx +++ b/src/pages/Home/index.tsx @@ -88,9 +88,9 @@ const Home: FC = () => { const [searchTerm, setSearchTerm] = useState(''); useEffect(() => { - if (!Array.isArray(userSpecificStreams) || userSpecificStreams.length === 0) return; + if (!Array.isArray(userSpecificStreams) || userSpecificStreams.length === 0 || !userRoles) return; getStreamMetadata(userSpecificStreams.map((stream) => stream.name)); - }, [userSpecificStreams]); + }, [userSpecificStreams, userRoles]); const filteredMetaData = useMemo(() => { if (!searchTerm || !metaData) return metaData || {};