Skip to content

Commit

Permalink
fix: handle null userRoles in stream metadata fetching logic (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
praveen5959 authored Nov 27, 2024
1 parent 7b3c654 commit 0f41f2d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
62 changes: 33 additions & 29 deletions src/hooks/useGetStreamMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,41 @@ export const useGetStreamMetadata = () => {
const [metaData, setMetadata] = useState<MetaData | null>(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,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down

0 comments on commit 0f41f2d

Please sign in to comment.