Libraries: refresh the list after any sync that changes it

Only syncs started from the UI updated the list. One started in the backend, such as the sync after creating a library, left it stale: the new library kept saying it wasn't on the server yet, and Main stayed undeletable until the app reloaded. A sync that changes the libraries now emits an event the app refreshes on.
This commit is contained in:
2026-10-30 20:56:15 -05:00
parent b61be5065b
commit 27bc5c9844
2 changed files with 19 additions and 8 deletions

View File

@@ -47,7 +47,7 @@ use mtgc_core::sync::remote::{
};
use mtgc_core::sync::{self as core_sync, LibraryDoc, MergeReport};
use serde::Serialize;
use tauri::{AppHandle, Manager};
use tauri::{AppHandle, Emitter, Manager};
use crate::state::{AppState, LocalLibrary, SyncAccount};
@@ -585,6 +585,11 @@ async fn run_sync(app: AppHandle, choice: Option<FirstSync>) -> Result<Option<Sy
Err(e) => s.sync_error = Some(e.clone()),
}
drop(s);
// An event rather than the report alone: many syncs start here in the
// backend, with nothing in the UI waiting on their result.
if result.as_ref().is_ok_and(|r| r.libraries_changed) {
let _ = app.emit("libraries-changed", ());
}
crate::logged("sync_now", result).map(Some)
}

View File

@@ -515,6 +515,18 @@ export default function App() {
if (statusLoaded) refreshLibraries();
}, [statusLoaded, refreshLibraries]);
// A sync linked, downloaded, renamed or flagged a library — the open one's
// name in the top bar included.
useEffect(() => {
const un = listen("libraries-changed", () => {
refreshLibraries();
api.getStatus().then(setStatus).catch(() => {});
});
return () => {
un.then((f) => f());
};
}, [refreshLibraries]);
/** Another library was opened, or the list changed: the open library may
* be a different one now, so everything refetches. */
const librariesChanged = useCallback(
@@ -544,18 +556,12 @@ export default function App() {
}
setDeckRefreshSignal((v) => v + 1);
}
if (report?.libraries_changed) {
// Downloaded, renamed or deleted elsewhere — the open one's name in
// the top bar included.
refreshLibraries();
api.getStatus().then(setStatus).catch(() => {});
}
return report;
} finally {
setSyncing(false);
api.syncStatus().then(setSync).catch(() => {});
}
}, [refreshLibraries]);
}, []);
// Automatic sync, whenever signed in (and not waiting on the first-sync
// choice): once now, on every trip to or from the background — leaving