Skip to content

Commit 0f41f2d

Browse files
authored
fix: handle null userRoles in stream metadata fetching logic (#383)
1 parent 7b3c654 commit 0f41f2d

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

src/hooks/useGetStreamMetadata.ts

+33-29
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,41 @@ export const useGetStreamMetadata = () => {
2020
const [metaData, setMetadata] = useState<MetaData | null>(null);
2121
const [userRoles] = useAppStore((store) => store.userRoles);
2222

23-
const getStreamMetadata = useCallback(async (streams: string[]) => {
24-
setLoading(true);
25-
try {
26-
// stats
27-
const allStatsReqs = streams.map((stream) => getLogStreamStats(stream));
28-
const allStatsRes = await Promise.all(allStatsReqs);
23+
const getStreamMetadata = useCallback(
24+
async (streams: string[]) => {
25+
if (!userRoles) return;
26+
setLoading(true);
27+
try {
28+
// stats
29+
const allStatsReqs = streams.map((stream) => getLogStreamStats(stream));
30+
const allStatsRes = await Promise.all(allStatsReqs);
2931

30-
// retention
31-
const streamsWithSettingsAccess = _.filter(streams, (stream) =>
32-
_.includes(getStreamsSepcificAccess(userRoles, stream), 'StreamSettings'),
33-
);
34-
const allretentionReqs = streamsWithSettingsAccess.map((stream) => getLogStreamRetention(stream));
35-
const allretentionRes = await Promise.all(allretentionReqs);
32+
// retention
33+
const streamsWithSettingsAccess = _.filter(streams, (stream) =>
34+
_.includes(getStreamsSepcificAccess(userRoles, stream), 'StreamSettings'),
35+
);
36+
const allretentionReqs = streamsWithSettingsAccess.map((stream) => getLogStreamRetention(stream));
37+
const allretentionRes = await Promise.all(allretentionReqs);
3638

37-
const metadata = streams.reduce((acc, stream, index) => {
38-
return {
39-
...acc,
40-
[stream]: { stats: allStatsRes[index]?.data || {}, retention: allretentionRes[index]?.data || [] },
41-
};
42-
}, {});
43-
setMetadata(metadata);
44-
} catch {
45-
setError(true);
46-
setMetadata(null);
47-
notifyError({
48-
message: 'Unable to fetch stream data',
49-
});
50-
} finally {
51-
setLoading(false);
52-
}
53-
}, []);
39+
const metadata = streams.reduce((acc, stream, index) => {
40+
return {
41+
...acc,
42+
[stream]: { stats: allStatsRes[index]?.data || {}, retention: allretentionRes[index]?.data || [] },
43+
};
44+
}, {});
45+
setMetadata(metadata);
46+
} catch {
47+
setError(true);
48+
setMetadata(null);
49+
notifyError({
50+
message: 'Unable to fetch stream data',
51+
});
52+
} finally {
53+
setLoading(false);
54+
}
55+
},
56+
[userRoles],
57+
);
5458

5559
return {
5660
isLoading,

src/pages/Home/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ const Home: FC = () => {
8888
const [searchTerm, setSearchTerm] = useState('');
8989

9090
useEffect(() => {
91-
if (!Array.isArray(userSpecificStreams) || userSpecificStreams.length === 0) return;
91+
if (!Array.isArray(userSpecificStreams) || userSpecificStreams.length === 0 || !userRoles) return;
9292
getStreamMetadata(userSpecificStreams.map((stream) => stream.name));
93-
}, [userSpecificStreams]);
93+
}, [userSpecificStreams, userRoles]);
9494

9595
const filteredMetaData = useMemo(() => {
9696
if (!searchTerm || !metaData) return metaData || {};

0 commit comments

Comments
 (0)