mirror of
https://github.com/transatoshi-mw/grin-explorer.git
synced 2025-10-21 13:33:41 +00:00
improved query search
This commit is contained in:
41
src/main.rs
41
src/main.rs
@@ -161,51 +161,56 @@ async fn output(commit: &str) -> Template {
|
|||||||
|
|
||||||
|
|
||||||
// Handling search request.
|
// Handling search request.
|
||||||
// Using Option<&str> to match '/search' query without input params.
|
// Using Option<&str> to match '/search' query without query params.
|
||||||
// https://github.com/rwf2/Rocket/issues/608
|
// https://github.com/rwf2/Rocket/issues/608
|
||||||
#[get("/search?<input>")]
|
#[get("/search?<query>")]
|
||||||
pub async fn search(input: Option<&str>) -> Either<Template, Redirect> {
|
pub async fn search(query: Option<&str>) -> Either<Template, Redirect> {
|
||||||
// Unwrap Option and forward to Search page if no parameters
|
// Unwrap Option and forward to Search page if no parameters
|
||||||
let input = match input {
|
let query = match query {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => return Either::Left(Template::render("search", context! {
|
None => return Either::Left(Template::render("search", context! {
|
||||||
route: "search",
|
route: "search",
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Trim and lowercase the input
|
// Trim and lowercase the query
|
||||||
let input = input.trim().to_lowercase();
|
let query = query.trim().to_lowercase();
|
||||||
|
|
||||||
// Check for valid chars
|
// Check for valid chars
|
||||||
if input.chars().all(|x| (x >= 'a' && x <= 'f') || (x >= '0' && x <= '9')) == true {
|
if query.chars().all(|x| (x >= 'a' && x <= 'f') || (x >= '0' && x <= '9')) == true {
|
||||||
|
|
||||||
// Block number
|
// Block number
|
||||||
if input.chars().all(char::is_numeric) == true {
|
if query.chars().all(char::is_numeric) == true {
|
||||||
return Either::Right(Redirect::to(uri!(block_details_by_height(input))));
|
return Either::Right(Redirect::to(uri!(block_details_by_height(query))));
|
||||||
|
|
||||||
// Block hash
|
// Block hash
|
||||||
} else if input.len() == 64 {
|
} else if query.len() == 64 {
|
||||||
return Either::Right(Redirect::to(uri!(block_header_by_hash(input))));
|
return Either::Right(Redirect::to(uri!(block_header_by_hash(query))));
|
||||||
|
|
||||||
// Kernel or Unspent Output
|
// Kernel or Unspent Output
|
||||||
} else if input.len() == 66 {
|
} else if query.len() == 66 {
|
||||||
// First search for Kernel.
|
// First search for Kernel.
|
||||||
// If found, redirect to Kernel page, otherwise search for Unspent Output.
|
// If found, redirect to Kernel page, otherwise search for Unspent Output.
|
||||||
// As we can't distinguish between Kernel and Output, this will produce a redundant
|
// As we can't distinguish between Kernel and Output, this will produce redundant
|
||||||
// get_kernel call, but will allow for better UI (no need to ask user to input the type
|
// get_kernel and get_output calls, but will allow for better UI (no need to ask user to
|
||||||
// of the search request).
|
// input the type of the search request).
|
||||||
let mut kernel = Kernel::new();
|
let mut kernel = Kernel::new();
|
||||||
|
let mut output = Output::new();
|
||||||
|
|
||||||
let _ = requests::get_kernel(&input, &mut kernel).await;
|
let _ = requests::get_kernel(&query, &mut kernel).await;
|
||||||
|
|
||||||
if kernel.excess.is_empty() == false {
|
if kernel.excess.is_empty() == false {
|
||||||
// Here we are redirecting to kernel page and call get_kernel again there.
|
// Here we are redirecting to kernel page and call get_kernel again there.
|
||||||
// Kernel page is a separate route and we want it to be accessed directly and
|
// Kernel page is a separate route and we want it to be accessed directly and
|
||||||
// via search functionality.
|
// via search functionality.
|
||||||
return Either::Right(Redirect::to(uri!(kernel(input))));
|
return Either::Right(Redirect::to(uri!(kernel(query))));
|
||||||
} else {
|
} else {
|
||||||
// If Kernel not found, then search for Unspent Output
|
// If Kernel not found, then search for Unspent Output
|
||||||
return Either::Right(Redirect::to(uri!(output(input))));
|
let _ = requests::get_output(&query, &mut output).await;
|
||||||
|
|
||||||
|
if output.commit.is_empty() == false {
|
||||||
|
return Either::Right(Redirect::to(uri!(output(query))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -108,7 +108,7 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<form class="input-group my-3" role="search" action="/search" method="GET" autocomplete="off">
|
<form class="input-group my-3" role="search" action="/search" method="GET" autocomplete="off">
|
||||||
<input class="form-control ms-0 me-2" type="search" placeholder="Explore Grin Network" aria-label="Search" name="input" required>
|
<input class="form-control ms-0 me-2" type="search" placeholder="Explore Grin Network" aria-label="Search" name="query" required>
|
||||||
<button class="btn btn-outline-secondary btn-search" type="submit">
|
<button class="btn btn-outline-secondary btn-search" type="submit">
|
||||||
<i class="bi bi-search"></i>
|
<i class="bi bi-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
@@ -7,7 +7,7 @@
|
|||||||
<div class="card border-start-0 border-end-0 rounded-0">
|
<div class="card border-start-0 border-end-0 rounded-0">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h4>No results found.</h4><br>
|
<h4>No results found.</h4><br>
|
||||||
<div class="value-text mb-2">Supported search inputs:</div>
|
<div class="value-text mb-2">Supported search queries:</div>
|
||||||
<div class="value-text"><i class="bi bi-dot"></i> Block Number</div>
|
<div class="value-text"><i class="bi bi-dot"></i> Block Number</div>
|
||||||
<div class="value-text"><i class="bi bi-dot"></i> Block Hash</div>
|
<div class="value-text"><i class="bi bi-dot"></i> Block Hash</div>
|
||||||
<div class="value-text"><i class="bi bi-dot"></i> Kernel</div>
|
<div class="value-text"><i class="bi bi-dot"></i> Kernel</div>
|
||||||
|
@@ -11,13 +11,13 @@
|
|||||||
<div class="darkorange-text text-end" hx-get="/rpc/block/latest" hx-trigger="load, every 10s"></div>
|
<div class="darkorange-text text-end" hx-get="/rpc/block/latest" hx-trigger="load, every 10s"></div>
|
||||||
</div>
|
</div>
|
||||||
<form class="input-group" role="search" action="/search" method="GET" autocomplete="off">
|
<form class="input-group" role="search" action="/search" method="GET" autocomplete="off">
|
||||||
<input class="form-control text-center ms-0 me-2" type="search" placeholder="Explore Grin Network" aria-label="Search" name="input" required>
|
<input class="form-control text-center ms-0 me-2" type="search" placeholder="Explore Grin Network" aria-label="Search" name="query" required>
|
||||||
<button class="btn btn-outline-secondary btn-search" type="submit">
|
<button class="btn btn-outline-secondary btn-search" type="submit">
|
||||||
<i class="bi bi-search"></i>
|
<i class="bi bi-search"></i>
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
<br><br>
|
<br><br>
|
||||||
<div class="value-text mb-2">Supported search inputs:</div>
|
<div class="value-text mb-2">Supported search queries:</div>
|
||||||
<div class="value-text"><i class="bi bi-dot"></i> Block Number</div>
|
<div class="value-text"><i class="bi bi-dot"></i> Block Number</div>
|
||||||
<div class="value-text"><i class="bi bi-dot"></i> Block Hash</div>
|
<div class="value-text"><i class="bi bi-dot"></i> Block Hash</div>
|
||||||
<div class="value-text"><i class="bi bi-dot"></i> Kernel</div>
|
<div class="value-text"><i class="bi bi-dot"></i> Kernel</div>
|
||||||
|
Reference in New Issue
Block a user