mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-22 16:46:10 -08:00
The stats library now lists each anime once. Grouping is a view over imm_anime rows computed from a cached AniList relation graph; no rows are merged and identity keys are unchanged. - Schema 27: imm_anilist_media + imm_anilist_relations cache every AniList entry reached from the library and all its anime edges. A background refresher walks from owned AniList ids (depth 6, 250 fetches per run, 7-day staleness) through the shared AniList rate limiter; a failed fetch leaves cached rows untouched. - FRANCHISE_RELATION_TYPES (PREQUEL, SEQUEL, PARENT, SIDE_STORY, SUMMARY, COMPILATION, CONTAINS) joins entries; an entry that names the other only through an excluded type (a spin-off's original) vetoes the join. - GET /api/stats/anime/groups returns cards with summed totals, the root entry's title, and parts in release order. Entries without cached relations stay single cards identical to before. - imm_anime.group_override (NULL = AniList, 0 = on its own, positive = join the franchise containing that AniList id) with PUT /api/stats/anime/:id/group and Move to Another Anime / Show On Its Own / Put Back Where AniList Has It on the title page. - SSH sync merges the override last-writer-wins on group_override_updated_at and skips the rebuildable relation cache.
123 lines
4.3 KiB
TypeScript
123 lines
4.3 KiB
TypeScript
import { useCallback, useMemo, useState, useEffect, useRef } from 'react';
|
|
import { getStatsClient } from './useStatsApi';
|
|
import type { AnimeLibraryGroup, StatsAnimeMergeRecommendation } from '../types/stats';
|
|
|
|
const BACKGROUND_REFRESH_MS = 30_000;
|
|
|
|
/**
|
|
* The library as franchise cards (`groups`) plus every entry on its own
|
|
* (`anime`), which merging and duplicate review work on.
|
|
*/
|
|
export function useAnimeLibrary() {
|
|
const [groups, setGroups] = useState<AnimeLibraryGroup[]>([]);
|
|
const anime = useMemo(() => groups.flatMap((group) => group.parts), [groups]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [recommendations, setRecommendations] = useState<StatsAnimeMergeRecommendation[]>([]);
|
|
const [dismissingRecommendationId, setDismissingRecommendationId] = useState<number | null>(null);
|
|
const [recommendationActionError, setRecommendationActionError] = useState<string | null>(null);
|
|
const [reloadToken, setReloadToken] = useState(0);
|
|
// Once the library has rendered, a failed background poll must not replace
|
|
// it with the error screen (and unmount any open dialog with it).
|
|
const hasLoadedRef = useRef(false);
|
|
// Ids dismissed or merged away locally. A recommendations response that was
|
|
// already in flight when the user acted would otherwise resurrect them.
|
|
// Dismissed and merged rows never return to pending server-side, so the set
|
|
// only ever suppresses genuinely stale data.
|
|
const removedRecommendationIdsRef = useRef<Set<number>>(new Set());
|
|
|
|
const reload = useCallback(() => {
|
|
setReloadToken((token) => token + 1);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
const client = getStatsClient();
|
|
client
|
|
.getAnimeLibraryGroups()
|
|
.then((data) => {
|
|
if (!cancelled) {
|
|
hasLoadedRef.current = true;
|
|
setGroups(data);
|
|
setError(null);
|
|
}
|
|
})
|
|
.catch((err: Error) => {
|
|
if (!cancelled && !hasLoadedRef.current) setError(err.message);
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) setLoading(false);
|
|
});
|
|
|
|
// Recommendation support is deliberately non-blocking. An older backend
|
|
// should still be able to display its library even when this endpoint is
|
|
// unavailable.
|
|
client
|
|
.getAnimeMergeRecommendations()
|
|
.then((data) => {
|
|
if (!cancelled) {
|
|
setRecommendations(
|
|
data.recommendations.filter(
|
|
(item) => !removedRecommendationIdsRef.current.has(item.recommendationId),
|
|
),
|
|
);
|
|
setRecommendationActionError(null);
|
|
}
|
|
})
|
|
.catch(() => {
|
|
// Preserve the last confirmed set. A transient polling failure should
|
|
// not make a pending review silently disappear.
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [reloadToken]);
|
|
|
|
useEffect(() => {
|
|
const refreshOnFocus = () => reload();
|
|
const interval = window.setInterval(reload, BACKGROUND_REFRESH_MS);
|
|
window.addEventListener('focus', refreshOnFocus);
|
|
return () => {
|
|
window.clearInterval(interval);
|
|
window.removeEventListener('focus', refreshOnFocus);
|
|
};
|
|
}, [reload]);
|
|
|
|
const dismissRecommendation = useCallback(async (recommendationId: number) => {
|
|
setDismissingRecommendationId(recommendationId);
|
|
setRecommendationActionError(null);
|
|
try {
|
|
await getStatsClient().dismissAnimeMergeRecommendation(recommendationId);
|
|
removedRecommendationIdsRef.current.add(recommendationId);
|
|
setRecommendations((current) =>
|
|
current.filter((item) => item.recommendationId !== recommendationId),
|
|
);
|
|
} catch {
|
|
setRecommendationActionError('Could not dismiss this suggestion. Try again.');
|
|
} finally {
|
|
setDismissingRecommendationId(null);
|
|
}
|
|
}, []);
|
|
|
|
const clearRecommendation = useCallback((recommendationId: number) => {
|
|
removedRecommendationIdsRef.current.add(recommendationId);
|
|
setRecommendations((current) =>
|
|
current.filter((item) => item.recommendationId !== recommendationId),
|
|
);
|
|
setRecommendationActionError(null);
|
|
}, []);
|
|
|
|
return {
|
|
groups,
|
|
anime,
|
|
loading,
|
|
error,
|
|
reload,
|
|
recommendations,
|
|
dismissRecommendation,
|
|
dismissingRecommendationId,
|
|
recommendationActionError,
|
|
clearRecommendation,
|
|
};
|
|
}
|