improved query search

This commit is contained in:
aglkm
2025-07-03 13:57:31 +03:00
parent 6018cfa33f
commit ba6ecfcc1b
4 changed files with 27 additions and 22 deletions

View File

@@ -161,51 +161,56 @@ async fn output(commit: &str) -> Template {
// 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
#[get("/search?<input>")]
pub async fn search(input: Option<&str>) -> Either<Template, Redirect> {
#[get("/search?<query>")]
pub async fn search(query: Option<&str>) -> Either<Template, Redirect> {
// Unwrap Option and forward to Search page if no parameters
let input = match input {
let query = match query {
Some(value) => value,
None => return Either::Left(Template::render("search", context! {
route: "search",
})),
};
// Trim and lowercase the input
let input = input.trim().to_lowercase();
// Trim and lowercase the query
let query = query.trim().to_lowercase();
// 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
if input.chars().all(char::is_numeric) == true {
return Either::Right(Redirect::to(uri!(block_details_by_height(input))));
if query.chars().all(char::is_numeric) == true {
return Either::Right(Redirect::to(uri!(block_details_by_height(query))));
// Block hash
} else if input.len() == 64 {
return Either::Right(Redirect::to(uri!(block_header_by_hash(input))));
} else if query.len() == 64 {
return Either::Right(Redirect::to(uri!(block_header_by_hash(query))));
// Kernel or Unspent Output
} else if input.len() == 66 {
} else if query.len() == 66 {
// First search for Kernel.
// 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
// get_kernel call, but will allow for better UI (no need to ask user to input the type
// of the search request).
// As we can't distinguish between Kernel and Output, this will produce redundant
// get_kernel and get_output calls, but will allow for better UI (no need to ask user to
// input the type of the search request).
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 {
// 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
// via search functionality.
return Either::Right(Redirect::to(uri!(kernel(input))));
return Either::Right(Redirect::to(uri!(kernel(query))));
} else {
// 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))));
}
}
}
}

View File

@@ -108,7 +108,7 @@
<div class="row">
<div class="col-12">
<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">
<i class="bi bi-search"></i>
</button>

View File

@@ -7,7 +7,7 @@
<div class="card border-start-0 border-end-0 rounded-0">
<div class="card-body">
<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 Hash</div>
<div class="value-text"><i class="bi bi-dot"></i> Kernel</div>

View File

@@ -11,13 +11,13 @@
<div class="darkorange-text text-end" hx-get="/rpc/block/latest" hx-trigger="load, every 10s"></div>
</div>
<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">
<i class="bi bi-search"></i>
</button>
</form>
<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 Hash</div>
<div class="value-text"><i class="bi bi-dot"></i> Kernel</div>