peer count in UI

This commit is contained in:
2025-11-14 00:03:32 +01:00
parent 32e909448f
commit b9e3e760d9
4 changed files with 97 additions and 1 deletions
@@ -208,6 +208,23 @@ async fn update_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tau
}
}
#[tauri::command]
async fn get_peer_count(state: tauri::State<'_, LanSpreadState>) -> tauri::Result<usize> {
let peer_ctrl_arc = state.inner().peer_ctrl.clone();
let peer_ctrl = peer_ctrl_arc.read().await.clone();
if let Some(peer_ctrl) = peer_ctrl {
// Send a request to get peer count
if let Err(e) = peer_ctrl.send(PeerCommand::GetPeerCount) {
log::error!("Failed to send GetPeerCount message to peer: {e:?}");
}
}
// For now, we'll return 0 and rely on the event-based updates
// The UI will get the actual count through peer-count-updated events
Ok(0)
}
#[cfg(target_os = "windows")]
fn run_as_admin(file: &str, params: &str, dir: &str) -> bool {
use std::{ffi::OsStr, os::windows::ffi::OsStrExt};
@@ -602,7 +619,8 @@ pub fn run() {
install_game,
run_game,
update_game_directory,
update_game
update_game,
get_peer_count
])
.manage(lanspread_state)
.setup({
@@ -847,6 +865,12 @@ pub fn run() {
log::error!("Failed to emit peer-lost event: {e}");
}
}
PeerEvent::PeerCountUpdated(count) => {
log::info!("Peer count updated: {count}");
if let Err(e) = app_handle.emit("peer-count-updated", Some(count)) {
log::error!("Failed to emit peer-count-updated event: {e}");
}
}
}
}
});
@@ -284,3 +284,10 @@ h1.align-center {
font-weight: bold;
color: #4866b9;
}
.top-left-peer-count {
position: absolute;
top: 20px;
left: 20px;
z-index: 1001;
}
@@ -44,6 +44,7 @@ const App = () => {
const [searchTerm, setSearchTerm] = useState('');
const [gameDir, setGameDir] = useState('');
const [currentFilter, setCurrentFilter] = useState<GameFilter>('available');
const [totalPeerCount, setTotalPeerCount] = useState(0);
const checkingPeersTimeouts = useRef<Record<string, ReturnType<typeof setTimeout>>>({});
const getFilteredGames = (games: Game[], filter: GameFilter): Game[] => {
@@ -154,6 +155,17 @@ const App = () => {
setupDownloadFailedListener();
setupNoPeersListener();
const setupPeerCountListener = async () => {
const unlisten = await listen('peer-count-updated', (event) => {
const count = event.payload as number;
console.log(`🗲 peer-count-updated ${count} event received`);
setTotalPeerCount(count);
});
return unlisten;
};
setupPeerCountListener();
}, [gameDir]);
useEffect(() => {
@@ -427,6 +439,13 @@ const App = () => {
return (
<main className="container">
<div className="fixed-header">
<div className="top-left-peer-count">
{totalPeerCount > 0 && (
<span className="peer-count">
👥 {totalPeerCount}
</span>
)}
</div>
<h1 className="align-center">SoftLAN Launcher</h1>
<div className="main-header">
{gameDir ? (