From 4a1ea7ea708591d2850300806f408ace8180331c Mon Sep 17 00:00:00 2001 From: Fredrik Burmester Date: Sat, 7 Sep 2024 18:15:12 +0300 Subject: [PATCH] fix: add api type and better undefined handling --- app/(auth)/(tabs)/(home)/index.tsx | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/(auth)/(tabs)/(home)/index.tsx b/app/(auth)/(tabs)/(home)/index.tsx index bb9fbc2a..057664c7 100644 --- a/app/(auth)/(tabs)/(home)/index.tsx +++ b/app/(auth)/(tabs)/(home)/index.tsx @@ -7,6 +7,7 @@ import { MediaListSection } from "@/components/medialists/MediaListSection"; import { apiAtom, userAtom } from "@/providers/JellyfinProvider"; import { useSettings } from "@/utils/atoms/settings"; import { Ionicons } from "@expo/vector-icons"; +import { Api } from "@jellyfin/sdk"; import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models"; import { getItemsApi, @@ -245,22 +246,20 @@ export default function index() { { title: "Suggested Episodes", queryKey: ["suggestedEpisodes", user?.Id], - queryFn: async () =>{ + queryFn: async () => { try { - const userId = user?.Id; - if (!userId) return []; - - const suggestions = await getSuggestions(api, userId); - const nextUpPromises = suggestions.map(series => getNextUp(api, userId, series.Id!)); + const suggestions = await getSuggestions(api, user.Id); + const nextUpPromises = suggestions.map((series) => + getNextUp(api, user.Id, series.Id) + ); const nextUpResults = await Promise.all(nextUpPromises); - - return nextUpResults.filter(item => item !== null); + + return nextUpResults.filter((item) => item !== null) || []; } catch (error) { - console.error('Error fetching data:', error); + console.error("Error fetching data:", error); return []; } - } - , + }, type: "ScrollingCollectionList", orientation: "horizontal", }, @@ -380,7 +379,8 @@ export default function index() { } // Function to get suggestions -async function getSuggestions(api: any, userId: string) { +async function getSuggestions(api: Api, userId: string | undefined) { + if (!userId) return []; const response = await getSuggestionsApi(api).getSuggestions({ userId, limit: 10, @@ -391,10 +391,15 @@ async function getSuggestions(api: any, userId: string) { } // Function to get the next up TV show for a series -async function getNextUp(api: any, userId: string, seriesId: string) { +async function getNextUp( + api: Api, + userId: string | undefined, + seriesId: string | undefined +) { + if (!userId || !seriesId) return null; const response = await getTvShowsApi(api).getNextUp({ userId, seriesId, }); return response.data.Items?.[0] ?? null; -} \ No newline at end of file +}