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:
@@ -47,7 +47,7 @@ use mtgc_core::sync::remote::{
|
|||||||
};
|
};
|
||||||
use mtgc_core::sync::{self as core_sync, LibraryDoc, MergeReport};
|
use mtgc_core::sync::{self as core_sync, LibraryDoc, MergeReport};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::{AppHandle, Emitter, Manager};
|
||||||
|
|
||||||
use crate::state::{AppState, LocalLibrary, SyncAccount};
|
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()),
|
Err(e) => s.sync_error = Some(e.clone()),
|
||||||
}
|
}
|
||||||
drop(s);
|
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)
|
crate::logged("sync_now", result).map(Some)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
src/App.tsx
20
src/App.tsx
@@ -515,6 +515,18 @@ export default function App() {
|
|||||||
if (statusLoaded) refreshLibraries();
|
if (statusLoaded) refreshLibraries();
|
||||||
}, [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
|
/** Another library was opened, or the list changed: the open library may
|
||||||
* be a different one now, so everything refetches. */
|
* be a different one now, so everything refetches. */
|
||||||
const librariesChanged = useCallback(
|
const librariesChanged = useCallback(
|
||||||
@@ -544,18 +556,12 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
setDeckRefreshSignal((v) => v + 1);
|
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;
|
return report;
|
||||||
} finally {
|
} finally {
|
||||||
setSyncing(false);
|
setSyncing(false);
|
||||||
api.syncStatus().then(setSync).catch(() => {});
|
api.syncStatus().then(setSync).catch(() => {});
|
||||||
}
|
}
|
||||||
}, [refreshLibraries]);
|
}, []);
|
||||||
|
|
||||||
// Automatic sync, whenever signed in (and not waiting on the first-sync
|
// Automatic sync, whenever signed in (and not waiting on the first-sync
|
||||||
// choice): once now, on every trip to or from the background — leaving
|
// choice): once now, on every trip to or from the background — leaving
|
||||||
|
|||||||
Reference in New Issue
Block a user