update: gemini fixes
This commit is contained in:
87
hooks/usePersonHistory.ts
Normal file
87
hooks/usePersonHistory.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { getPersonHistory, PersonHistoryRecord, PersonMini } from "@/apis/personHistoryApi";
|
||||
import { getShowById, Show } from "@/apis/showApi";
|
||||
|
||||
type SeasonEntry = {
|
||||
seasonNumber: number;
|
||||
partner: PersonMini | null;
|
||||
participants: PersonMini[];
|
||||
startDate: string | null;
|
||||
};
|
||||
|
||||
export type AppearanceGroup = {
|
||||
show: Show;
|
||||
seasons: SeasonEntry[];
|
||||
};
|
||||
|
||||
export const usePersonHistory = (personId: number) => {
|
||||
return useQuery({
|
||||
queryKey: ["personHistory", personId],
|
||||
queryFn: async () => {
|
||||
const history = await getPersonHistory(personId);
|
||||
|
||||
const grouped = new Map<number, Map<number, SeasonEntry>>();
|
||||
for (const h of history) {
|
||||
if (!Number.isFinite(h.showId) || h.showId <= 0) continue;
|
||||
const seasonsForShow =
|
||||
grouped.get(h.showId) ?? new Map<number, SeasonEntry>();
|
||||
|
||||
const existing = seasonsForShow.get(h.seasonNumber);
|
||||
if (existing) {
|
||||
seasonsForShow.set(h.seasonNumber, {
|
||||
seasonNumber: h.seasonNumber,
|
||||
partner: existing.partner ?? h.partner ?? null,
|
||||
participants: existing.participants.length
|
||||
? existing.participants
|
||||
: (h.seasonParticipants ?? []),
|
||||
startDate: existing.startDate ?? h.startDate ?? null,
|
||||
});
|
||||
} else {
|
||||
seasonsForShow.set(h.seasonNumber, {
|
||||
seasonNumber: h.seasonNumber,
|
||||
partner: h.partner ?? null,
|
||||
participants: h.seasonParticipants ?? [],
|
||||
startDate: h.startDate ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
grouped.set(h.showId, seasonsForShow);
|
||||
}
|
||||
|
||||
const showIds = Array.from(grouped.keys());
|
||||
|
||||
const shows = await Promise.all(
|
||||
showIds.map(async (id) => {
|
||||
try {
|
||||
const s = await getShowById(id);
|
||||
return s;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const validShows = shows.filter((s): s is Show => !!s);
|
||||
|
||||
const result: AppearanceGroup[] = validShows.map((s) => {
|
||||
const seasonsMap = grouped.get(s.id)!;
|
||||
const seasonsSorted = Array.from(seasonsMap.values()).sort(
|
||||
(a, b) => a.seasonNumber - b.seasonNumber
|
||||
);
|
||||
return {
|
||||
show: s,
|
||||
seasons: seasonsSorted,
|
||||
};
|
||||
});
|
||||
|
||||
result.sort((a, b) =>
|
||||
a.show.title.localeCompare(b.show.title, "de", {
|
||||
sensitivity: "base",
|
||||
})
|
||||
);
|
||||
|
||||
return result;
|
||||
},
|
||||
enabled: !!personId,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user