Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle null userRoles in stream metadata fetching logic #383

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading