11defmodule SearchComponent do
2+ require Logger
23 use ExplorerWeb , :live_component
34
45 @ impl true
5- def handle_event ( "search_batch" , % { "batch " => % { "merkle_root" => input_hash } } , socket ) do
6- input_hash
6+ def handle_event ( "search_batch" , % { "search " => search } , socket ) do
7+ search
78 |> ( fn hash ->
89 if String . match? ( hash , ~r/ ^0x[a-fA-F0-9]+$/ ) , do: { :ok , hash } , else: :invalid_hash
910 end ) . ( )
1011 |> case do
1112 { :ok , hash } ->
12- case Proofs . get_number_of_batches_containing_proof ( hash ) do
13- 0 -> { :noreply , push_navigate ( socket , to: ~p" /batches/#{ hash } " ) }
14- _ -> { :noreply , push_navigate ( socket , to: ~p" /search?q=#{ hash } " ) }
13+ cond do
14+ # See if the hash belongs to a proof in a batch
15+ # If so, redirect to search to show all the batches where this proofs exists
16+ Proofs . get_number_of_batches_containing_proof ( hash ) > 0 ->
17+ { :noreply , push_navigate ( socket , to: ~p" /search?q=#{ hash } " ) }
18+
19+ # See if the hash belongs to the root of a batch
20+ Batches . get_batch ( % { merkle_root: hash } ) != nil ->
21+ { :noreply , push_navigate ( socket , to: ~p" /batches/#{ hash } " ) }
22+
23+ # See if the hash belongs to an aggregated proof merkle root
24+ ( proof = AggregatedProofs . get_newest_aggregated_proof_by_merkle_root ( hash ) ) != nil ->
25+ { :noreply , push_navigate ( socket , to: ~p" /aggregated_proofs/#{ proof . id } " ) }
26+
27+ # Finally, see if the hash belongs to a proof of an aggregated proof
28+ ( proof = AggregationModeProof . get_newest_proof_by_hash ( hash ) ) != nil ->
29+ { :noreply , push_navigate ( socket , to: ~p" /aggregated_proofs/#{ proof . agg_proof_id } " ) }
30+
31+ # Otherwise inform the user nothing was found
32+ true ->
33+ { :noreply ,
34+ socket
35+ |> put_flash! ( :error , "No batch or proof was found with the provided hash." ) }
1536 end
1637
1738 :invalid_hash ->
1839 { :noreply ,
1940 socket
20- |> assign ( batch_merkle_root: input_hash )
21- |> put_flash! ( :error , "Please enter a valid proof batch hash (0x69...)." ) }
41+ |> put_flash! ( :error , "Please enter a valid hash (0x69...)." ) }
2242 end
2343 end
2444
@@ -42,8 +62,8 @@ defmodule SearchComponent do
4262 id = { "input_#{ assigns . id } " }
4363 class = "pr-10 w-full text-foreground rounded-lg border-foreground/20 bg-card focus:border-foreground/20 focus:ring-accent text-sm "
4464 type = "search "
45- placeholder = "Search batch by batch hash or proof hash "
46- name = "batch[merkle_root] "
65+ placeholder = "Search by batch hash or proof hash "
66+ name = "search "
4767 />
4868 < . icon name = "hero-magnifying-glass-solid " class = "absolute right-3 text-foreground/20 size-5 hover:text-foreground " />
4969 </ form >
0 commit comments