fix(desktop): skip the background Nuvio sync's home reload when nothing actually changed

Every profile switch already triggers an explicit homeLoadRequested.
The background Nuvio health-check/sync (useNuvioConnectivity) also
called onSynced unconditionally once its own import finished a couple
seconds later, forcing a second full reload/flash even when the sync
found nothing new. importNuvioProfileData now reports whether it
actually changed continueWatching/progress/watched, and the reload is
skipped when it didn't.
This commit is contained in:
KhooLy 2026-07-28 20:31:18 +03:00
parent afb66e4e89
commit a78d39ba2b
3 changed files with 26 additions and 12 deletions

View file

@ -417,7 +417,8 @@ export default function App() {
}, [updateState]);
const activeProfileId = activeProfile?.id;
const handleNuvioSynced = useCallback(async () => {
const handleNuvioSynced = useCallback(async (changed: boolean) => {
if (!changed) return;
invalidateLibraryKeyCache();
const profiles = await loadProfiles();
setAllProfiles(profiles);

View file

@ -41,6 +41,7 @@ export type NuvioImportStep = 'addons' | 'library' | 'progress' | 'history' | 'c
export interface NuvioImportReport {
errors: Partial<Record<NuvioImportStep, string>>;
changed: boolean;
}
export interface NuvioImportOptions {
@ -250,7 +251,7 @@ export async function importNuvioProfileData(
const freshProfile = await freshNuvioProfile(profile).catch(() => profile);
const token = freshProfile.nuvioAccessToken;
const profileIdx = freshProfile.nuvioProfileIndex ?? 1;
if (!token) return { errors: { library: 'Missing Nuvio token' } };
if (!token) return { errors: { library: 'Missing Nuvio token' }, changed: false };
const suffix = profileStorageSuffix(profile);
const profileKey = `library_${suffix}`;
@ -405,5 +406,9 @@ export async function importNuvioProfileData(
onStep?.('collections', false, errors.collections);
}
return { errors };
const changed = JSON.stringify(continueWatchingBefore) !== JSON.stringify(libDoc.continueWatching)
|| JSON.stringify(progressBefore) !== JSON.stringify(libDoc.progress)
|| JSON.stringify(watchedBefore) !== JSON.stringify(libDoc.watched);
return { errors, changed };
}

View file

@ -24,7 +24,7 @@ async function pushLocalToNuvio(profile: UserProfile): Promise<void> {
]);
}
export function useNuvioConnectivity(activeProfile: UserProfile | null, onSynced?: () => void | Promise<void>) {
export function useNuvioConnectivity(activeProfile: UserProfile | null, onSynced?: (changed: boolean) => void | Promise<void>) {
const [serverDown, setServerDown] = useState(false);
const [justRecovered, setJustRecovered] = useState(false);
const [dismissed, setDismissed] = useState(false);
@ -65,26 +65,34 @@ export function useNuvioConnectivity(activeProfile: UserProfile | null, onSynced
setDismissed(false);
setTimeout(() => { if (!cancelled) setJustRecovered(false); }, 2000);
void (async () => {
await importNuvioProfileData(profile)
.then((report) => recordNuvioSyncMeta(report))
.catch((err) => recordNuvioSyncMeta({ errors: { library: err instanceof Error ? err.message : String(err) } }));
const report = await importNuvioProfileData(profile)
.then((report) => { void recordNuvioSyncMeta(report); return report; })
.catch((err) => {
const errorReport = { errors: { library: err instanceof Error ? err.message : String(err) }, changed: false };
void recordNuvioSyncMeta(errorReport);
return errorReport;
});
if (cancelled) return;
await refreshNuvioProfiles(profile).catch(() => profile);
if (cancelled) return;
await pushLocalToNuvio(profile).catch(() => undefined);
if (cancelled) return;
await onSynced?.();
await onSynced?.(report.changed);
})();
} else if (!down && !pulledRemote) {
pulledRemote = true;
void (async () => {
await importNuvioProfileData(profile)
.then((report) => recordNuvioSyncMeta(report))
.catch((err) => recordNuvioSyncMeta({ errors: { library: err instanceof Error ? err.message : String(err) } }));
const report = await importNuvioProfileData(profile)
.then((report) => { void recordNuvioSyncMeta(report); return report; })
.catch((err) => {
const errorReport = { errors: { library: err instanceof Error ? err.message : String(err) }, changed: false };
void recordNuvioSyncMeta(errorReport);
return errorReport;
});
if (cancelled) return;
await refreshNuvioProfiles(profile).catch(() => profile);
if (cancelled) return;
await onSynced?.();
await onSynced?.(report.changed);
})();
}