update: gemini fixes

This commit is contained in:
Yordan Simeonov
2025-10-29 20:50:21 +11:00
parent 04a44bad7b
commit 9725d8bff1
23 changed files with 473 additions and 647 deletions

View File

@@ -1,12 +1,10 @@
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
useCallback,
} from "react";
import { getAutoComplete, AutoCompleteItem } from "@/apis/autoCompleteApi";
import { useAutoComplete } from "@/hooks/useAutoComplete";
import { AutoCompleteItem } from "@/apis/autoCompleteApi";
type DiscoveryContextType = {
query: string;
@@ -25,61 +23,15 @@ export const DiscoveryProvider = ({
children: React.ReactNode;
}) => {
const [query, setQuery] = useState("");
const [suggestions, setSuggestions] = useState<AutoCompleteItem[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const abortRef = useRef<AbortController | null>(null);
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const cacheRef = useRef<Record<string, AutoCompleteItem[]>>({});
const fetchSuggestions = useCallback((q: string) => {
if (abortRef.current) abortRef.current.abort();
if (!q.trim()) {
setSuggestions([]);
setLoading(false);
return;
}
const cached = cacheRef.current[q];
if (cached) {
setSuggestions(cached);
setLoading(false);
return;
}
const controller = new AbortController();
abortRef.current = controller;
setLoading(true);
setError(null);
getAutoComplete(q, 10, controller.signal)
.then((items) => {
cacheRef.current[q] = items;
setSuggestions(items);
})
.catch((e) => {
if (controller.signal.aborted) return;
setError(e.message || "Fehler");
})
.finally(() => {
if (!controller.signal.aborted) setLoading(false);
});
}, []);
useEffect(() => {
if (debounceRef.current) clearTimeout(debounceRef.current);
debounceRef.current = setTimeout(() => fetchSuggestions(query), 300);
return () => {
if (debounceRef.current) clearTimeout(debounceRef.current);
};
}, [query, fetchSuggestions]);
const { data: suggestions = [], isLoading: loading, error } = useAutoComplete(query);
const clear = () => {
setQuery("");
setSuggestions([]);
setError(null);
};
return (
<DiscoveryContext.Provider
value={{ query, setQuery, suggestions, loading, error, clear }}
value={{ query, setQuery, suggestions, loading, error: error?.message || null, clear }}
>
{children}
</DiscoveryContext.Provider>

View File

@@ -1,103 +0,0 @@
import { getSeason, SeasonParticipant } from "@/apis/seasonApi";
import React, {
createContext,
useCallback,
useContext,
useState,
} from "react";
type SeasonContextType = {
fetchSeasonParticipants: (
showId: number,
seasonNumber: number
) => Promise<SeasonParticipant[]>;
fetchSeasonCount: (showId: number) => Promise<number>;
fetchSeasonDates: (
showId: number,
seasonNumber: number
) => Promise<{ startDate?: string; endDate?: string | null } | null>;
};
const SeasonContext = createContext<SeasonContextType | null>(null);
export const SeasonProvider = ({ children }: { children: React.ReactNode }) => {
const [seasonCache, setSeasonCache] = useState<
Record<string, SeasonParticipant[]>
>({});
const [seasonCountCache, setSeasonCountCache] = useState<
Record<number, number>
>({});
const [datesCache, setDatesCache] = useState<
Record<string, { startDate?: string; endDate?: string | null }>
>({});
const fetchSeasonParticipants = useCallback(
async (showId: number, seasonNumber: number) => {
const key = `${showId}-${seasonNumber}`;
if (seasonCache[key]) return seasonCache[key];
try {
const season = await getSeason(showId, seasonNumber);
const participants = season?.participants ?? [];
setSeasonCache((c) => ({ ...c, [key]: participants }));
return participants;
} catch {
return [];
}
},
[seasonCache]
);
const fetchSeasonCount = useCallback(
async (showId: number) => {
if (seasonCountCache[showId] !== undefined)
return seasonCountCache[showId];
let n = 0;
for (let s = 1; s <= 50; s++) {
try {
const season = await getSeason(showId, s);
if (!season) break;
n = s;
} catch {
break;
}
}
setSeasonCountCache((c) => ({ ...c, [showId]: n }));
return n;
},
[seasonCountCache]
);
const fetchSeasonDates = useCallback(
async (showId: number, seasonNumber: number) => {
const key = `${showId}-${seasonNumber}`;
if (datesCache[key]) return datesCache[key];
try {
const season = await getSeason(showId, seasonNumber);
const dates = season
? { startDate: season.startDate, endDate: season.endDate }
: null;
if (dates) setDatesCache((c) => ({ ...c, [key]: dates }));
return dates;
} catch {
return null;
}
},
[datesCache]
);
return (
<SeasonContext.Provider
value={{ fetchSeasonParticipants, fetchSeasonCount, fetchSeasonDates }}
>
{children}
</SeasonContext.Provider>
);
};
export const useSeasonContext = () => {
const ctx = useContext(SeasonContext);
if (!ctx)
throw new Error("useSeasonContext must be used within a SeasonProvider");
return ctx;
};

View File

@@ -1,42 +0,0 @@
import { getShows, Show } from "@/apis/showApi";
import { createContext, useContext, useEffect, useState } from "react";
type ShowContextType = {
shows: Show[];
loading: boolean;
error: string | null;
};
const ShowContext = createContext<ShowContextType | null>(null);
export const ShowProvider = ({ children }: { children: React.ReactNode }) => {
const [shows, setShows] = useState<Show[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
(async () => {
try {
const data = await getShows();
setShows(data);
} catch {
setError("Failed to fetch shows");
} finally {
setLoading(false);
}
})();
}, []);
return (
<ShowContext.Provider value={{ shows, loading, error }}>
{children}
</ShowContext.Provider>
);
};
export const useShowContext = () => {
const ctx = useContext(ShowContext);
if (!ctx)
throw new Error("useShowContext must be used within a ShowProvider");
return ctx;
};

View File

@@ -1,59 +0,0 @@
import {
getStreamingImages,
StreamingServiceRaw,
} from "@/apis/streamingServiceApi";
import { createContext, useContext } from "react";
import React from "react";
type StreamingServiceContextType = {
streamingServices: Record<string, string>;
loading: boolean;
error: string | null;
};
const StreamingServiceContext =
createContext<StreamingServiceContextType | null>(null);
export const StreamingServiceProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [streamingServices, setStreamingServices] = React.useState<
Record<string, string>
>({});
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState<string | null>(null);
React.useEffect(() => {
(async () => {
try {
const data: StreamingServiceRaw[] = await getStreamingImages();
const mapped = Object.fromEntries(data.map((s) => [s.key, s.value]));
setStreamingServices(mapped);
} catch {
setError("Failed to fetch streaming services");
} finally {
setLoading(false);
}
})();
}, []);
return (
<StreamingServiceContext.Provider
value={{ streamingServices, loading, error }}
>
{children}
</StreamingServiceContext.Provider>
);
};
export const useStreamingServiceContext = () => {
const ctx = useContext(StreamingServiceContext);
if (!ctx)
throw new Error(
"useStreamingServiceContext must be used within a StreamingServiceProvider"
);
return ctx;
};