updated
This commit is contained in:
@@ -11,6 +11,10 @@ type PersonAppearances = {
|
||||
raw: PersonHistoryEntry[];
|
||||
byShow: Record<number, number[]>;
|
||||
showIds: number[];
|
||||
partnersByShow: Record<
|
||||
number,
|
||||
{ seasonNumber: number; partner?: PersonHistoryEntry["partner"] }[]
|
||||
>;
|
||||
};
|
||||
|
||||
type PersonContextType = {
|
||||
@@ -32,23 +36,34 @@ export const PersonProvider = ({ children }: { children: ReactNode }) => {
|
||||
const buildAppearances = (
|
||||
entries: PersonHistoryEntry[]
|
||||
): PersonAppearances => {
|
||||
const byShow: Record<number, Set<number>> = {};
|
||||
const byShowSet: Record<number, Set<number>> = {};
|
||||
const partnersByShow: PersonAppearances["partnersByShow"] = {};
|
||||
for (const e of entries) {
|
||||
if (!byShow[e.showId]) byShow[e.showId] = new Set();
|
||||
byShow[e.showId].add(e.seasonNumber);
|
||||
if (!byShowSet[e.showId]) byShowSet[e.showId] = new Set();
|
||||
byShowSet[e.showId].add(e.seasonNumber);
|
||||
if (!partnersByShow[e.showId]) partnersByShow[e.showId] = [];
|
||||
partnersByShow[e.showId].push({
|
||||
seasonNumber: e.seasonNumber,
|
||||
partner: e.partner ?? undefined,
|
||||
});
|
||||
}
|
||||
const byShowSorted: Record<number, number[]> = Object.fromEntries(
|
||||
Object.entries(byShow).map(([showId, seasonsSet]) => [
|
||||
const byShow: Record<number, number[]> = Object.fromEntries(
|
||||
Object.entries(byShowSet).map(([showId, seasons]) => [
|
||||
Number(showId),
|
||||
Array.from(seasonsSet).sort((a, b) => a - b),
|
||||
Array.from(seasons).sort((a, b) => a - b),
|
||||
])
|
||||
);
|
||||
|
||||
Object.values(partnersByShow).forEach((arr) =>
|
||||
arr.sort((a, b) => a.seasonNumber - b.seasonNumber)
|
||||
);
|
||||
return {
|
||||
raw: entries,
|
||||
byShow: byShowSorted,
|
||||
showIds: Object.keys(byShowSorted)
|
||||
byShow,
|
||||
showIds: Object.keys(byShow)
|
||||
.map(Number)
|
||||
.sort((a, b) => a - b),
|
||||
partnersByShow,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -65,7 +80,7 @@ export const PersonProvider = ({ children }: { children: ReactNode }) => {
|
||||
...err,
|
||||
[personId]: e?.message || "Fehler beim Laden",
|
||||
}));
|
||||
return { raw: [], byShow: {}, showIds: [] };
|
||||
return { raw: [], byShow: {}, showIds: [], partnersByShow: {} };
|
||||
} finally {
|
||||
setLoading((l) => ({ ...l, [personId]: false }));
|
||||
}
|
||||
@@ -90,7 +105,7 @@ export const PersonProvider = ({ children }: { children: ReactNode }) => {
|
||||
const getSeasonsForShow = useCallback(
|
||||
async (personId: number, showId: number) => {
|
||||
const app = await getPersonAppearances(personId);
|
||||
return app.byShow[showId] || [];
|
||||
return (app.byShow as Record<number, number[]>)[showId] || [];
|
||||
},
|
||||
[getPersonAppearances]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user