diff --git a/explorer_service/src/api.rs b/explorer_service/src/api.rs index d0785949..4dfd75cd 100644 --- a/explorer_service/src/api.rs +++ b/explorer_service/src/api.rs @@ -83,6 +83,17 @@ pub async fn get_block_by_id(block_id: BlockId) -> Result .map_err(|e| ServerFnError::ServerError(format!("RPC error: {}", e))) } +/// Get latest block ID +#[server] +pub async fn get_latest_block_id() -> Result { + use indexer_service_rpc::RpcClient as _; + let client = expect_context::(); + client + .get_last_finalized_block_id() + .await + .map_err(|e| ServerFnError::ServerError(format!("RPC error: {}", e))) +} + /// Get block by hash #[server] pub async fn get_block_by_hash(block_hash: HashType) -> Result { diff --git a/explorer_service/src/pages/main_page.rs b/explorer_service/src/pages/main_page.rs index ffd625c8..4a47536d 100644 --- a/explorer_service/src/pages/main_page.rs +++ b/explorer_service/src/pages/main_page.rs @@ -7,6 +7,8 @@ use crate::{ components::{AccountPreview, BlockPreview, TransactionPreview}, }; +const RECENT_BLOCKS_LIMIT: u64 = 10; + /// Main page component #[component] pub fn MainPage() -> impl IntoView { @@ -38,7 +40,21 @@ pub fn MainPage() -> impl IntoView { }); // Load recent blocks on mount - let recent_blocks_resource = Resource::new(|| (), |_| async { api::get_blocks(0, 10).await }); + let recent_blocks_resource = Resource::new( + || (), + |_| async { + match api::get_latest_block_id().await { + Ok(last_id) => { + api::get_blocks( + std::cmp::max(last_id.saturating_sub(RECENT_BLOCKS_LIMIT) as u32, 1), + RECENT_BLOCKS_LIMIT as u32, + ) + .await + } + Err(err) => Err(err), + } + }, + ); // Handle search - update URL parameter let on_search = move |ev: SubmitEvent| {