From 1558046d5f3561c55c615bd6056e07620752c686 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 18 Aug 2022 10:36:44 -0700 Subject: [PATCH] Use
    for homepage (#343) --- src/subcommand/server.rs | 2 +- src/subcommand/server/templates/home.rs | 54 ++++++++++++++++--------- templates/home.html | 8 ++-- tests/server.rs | 10 ++--- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index c73d026de3..fc8f711b73 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -243,7 +243,7 @@ impl Server { async fn home(index: extract::Extension>) -> impl IntoResponse { match index.blocks(100) { - Ok(blocks) => HomeHtml { blocks }.page().into_response(), + Ok(blocks) => HomeHtml::new(blocks).page().into_response(), Err(err) => { eprintln!("Error getting blocks: {err}"); ( diff --git a/src/subcommand/server/templates/home.rs b/src/subcommand/server/templates/home.rs index 4f23f1da93..ba3246c458 100644 --- a/src/subcommand/server/templates/home.rs +++ b/src/subcommand/server/templates/home.rs @@ -2,7 +2,21 @@ use super::*; #[derive(Display)] pub(crate) struct HomeHtml { - pub(crate) blocks: Vec<(u64, BlockHash)>, + last: u64, + hashes: Vec, +} + +impl HomeHtml { + pub(crate) fn new(blocks: Vec<(u64, BlockHash)>) -> Self { + Self { + last: blocks + .get(0) + .map(|(height, _)| height) + .cloned() + .unwrap_or(0), + hashes: blocks.into_iter().map(|(_, hash)| hash).collect(), + } + } } impl Content for HomeHtml { @@ -32,25 +46,29 @@ mod tests { #[test] fn home_html() { assert_regex_match!( -"

    Ordinals

    + "

    Ordinals

    Recent Blocks

    - +
      +
    1. 1{64}
    2. +
    3. 0{64}
    4. +
    ", - &HomeHtml { - blocks: vec![ - ( - 1, - "1111111111111111111111111111111111111111111111111111111111111111".parse().unwrap() - ), - ( - 0, - "0000000000000000000000000000000000000000000000000000000000000000".parse().unwrap() - ) - ], - }.to_string()); + &HomeHtml::new(vec![ + ( + 1, + "1111111111111111111111111111111111111111111111111111111111111111" + .parse() + .unwrap() + ), + ( + 0, + "0000000000000000000000000000000000000000000000000000000000000000" + .parse() + .unwrap() + ) + ],) + .to_string() + ); } } diff --git a/templates/home.html b/templates/home.html index afc215fa7b..600d82b601 100644 --- a/templates/home.html +++ b/templates/home.html @@ -8,8 +8,8 @@

    Ordinals

    Discord

    Recent Blocks

    -
      -%% for block in &self.blocks { -
    • {{block.0}} - {{block.1}}
    • +
        +%% for hash in &self.hashes { +
      1. {{hash}}
      2. %% } -
    +
diff --git a/tests/server.rs b/tests/server.rs index 79b12edb75..c0236c1b01 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -159,10 +159,10 @@ fn home() { ".*Ordinals.*

Ordinals

Recent Blocks

-.*", +
    +
  1. [[:xdigit:]]{64}
  2. +
  3. 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206
  4. +
.*", ); } @@ -175,7 +175,7 @@ fn home_block_limit() { state.request_regex( "/", 200, - ".*.*" + ".*
    \n(
  1. [[:xdigit:]]{64}
  2. \n){100}
.*" ); }