diff --git a/src/App.tsx b/src/App.tsx index d4188d2..66859a0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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); diff --git a/src/core/nuvioSync.ts b/src/core/nuvioSync.ts index 0f47fd7..7fecf65 100644 --- a/src/core/nuvioSync.ts +++ b/src/core/nuvioSync.ts @@ -41,6 +41,7 @@ export type NuvioImportStep = 'addons' | 'library' | 'progress' | 'history' | 'c export interface NuvioImportReport { errors: Partial>; + 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 }; } diff --git a/src/hooks/useNuvioConnectivity.ts b/src/hooks/useNuvioConnectivity.ts index e1d6580..c8d8ab7 100644 --- a/src/hooks/useNuvioConnectivity.ts +++ b/src/hooks/useNuvioConnectivity.ts @@ -24,7 +24,7 @@ async function pushLocalToNuvio(profile: UserProfile): Promise { ]); } -export function useNuvioConnectivity(activeProfile: UserProfile | null, onSynced?: () => void | Promise) { +export function useNuvioConnectivity(activeProfile: UserProfile | null, onSynced?: (changed: boolean) => void | Promise) { 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); })(); }