|
| 1 | +defmodule ExplorerWeb.Search.Index do |
| 2 | + use ExplorerWeb, :live_view |
| 3 | + |
| 4 | + @page_size 15 |
| 5 | + |
| 6 | + @impl true |
| 7 | + def mount(%{"q" => hash}, _session, socket) do |
| 8 | + total_pages = |
| 9 | + Proofs.get_number_of_batches_containing_proof(hash) |
| 10 | + |> div(@page_size) |
| 11 | + |> Kernel.ceil() |
| 12 | + |> max(1) |
| 13 | + |
| 14 | + {:ok, |
| 15 | + assign(socket, |
| 16 | + page_title: "Search Results For #{hash |> Helpers.shorten_hash()}", |
| 17 | + hash: hash, |
| 18 | + total_pages: total_pages |
| 19 | + )} |
| 20 | + end |
| 21 | + |
| 22 | + @impl true |
| 23 | + def handle_params(params, _url, socket) do |
| 24 | + hash = params["q"] |
| 25 | + page_param = Integer.parse(params["page"] || "1") |
| 26 | + |
| 27 | + current_page = |
| 28 | + case page_param do |
| 29 | + {page, _} when page > 0 -> page |
| 30 | + _ -> 1 |
| 31 | + end |
| 32 | + |
| 33 | + case Proofs.get_batches_containing_proof(hash, current_page, @page_size) do |
| 34 | + [] -> |
| 35 | + {:noreply, push_navigate(socket, to: ~p"/batches/#{hash}")} |
| 36 | + |
| 37 | + results -> |
| 38 | + {:noreply, |
| 39 | + assign(socket, |
| 40 | + page_title: "Search Results For #{hash |> Helpers.shorten_hash()}", |
| 41 | + results: results, |
| 42 | + current_page: current_page |
| 43 | + )} |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + @impl true |
| 48 | + def render(assigns) do |
| 49 | + ~H""" |
| 50 | + <div class="flex flex-col space-y-3 text-foreground px-1 sm:max-w-lg md:max-w-3xl lg:max-w-5xl mx-auto capitalize"> |
| 51 | + <.card_preheding> |
| 52 | + Search Results for "<%= @hash |> Helpers.shorten_hash() %>" |
| 53 | + </.card_preheding> |
| 54 | + <%= if @results != nil or @results != [] do %> |
| 55 | + <.table id="results" rows={@results}> |
| 56 | + <:col :let={result} label="Batch Hash" class="text-left"> |
| 57 | + <.link |
| 58 | + navigate={~p"/batches/#{result}"} |
| 59 | + class="flex justify-between group group-hover:text-foreground/80" |
| 60 | + > |
| 61 | + <span class="items-center group-hover:text-foreground/80 hidden md:inline"> |
| 62 | + <%= result %> |
| 63 | + </span> |
| 64 | + <span class="items-center group-hover:text-foreground/80 md:hidden"> |
| 65 | + <%= result |> Helpers.shorten_hash(12) %> |
| 66 | + </span> |
| 67 | + <.right_arrow /> |
| 68 | + <.tooltip> |
| 69 | + <%= result %> |
| 70 | + </.tooltip> |
| 71 | + </.link> |
| 72 | + </:col> |
| 73 | + </.table> |
| 74 | + <div class="flex gap-x-2 justify-center items-center"> |
| 75 | + <%= if @current_page != 1 do %> |
| 76 | + <.link patch={~p"/search?q=#{@hash}&page=#{@current_page - 1}"}> |
| 77 | + <.button |
| 78 | + icon="arrow-left-solid" |
| 79 | + icon_class="group-hover:-translate-x-1 transition-all duration-150" |
| 80 | + class="text-muted-foreground size-10 group" |
| 81 | + > |
| 82 | + <span class="sr-only">Previous Page</span> |
| 83 | + </.button> |
| 84 | + </.link> |
| 85 | + <% else %> |
| 86 | + <.button |
| 87 | + icon="arrow-left-solid" |
| 88 | + class="text-muted-foreground size-10 group pointer-events-none opacity-50" |
| 89 | + disabled |
| 90 | + > |
| 91 | + <span class="sr-only">Previous Page</span> |
| 92 | + </.button> |
| 93 | + <% end %> |
| 94 | + <p> |
| 95 | + <%= @current_page %> / <%= @total_pages %> |
| 96 | + </p> |
| 97 | + <%= if @current_page != @total_pages do %> |
| 98 | + <.link patch={~p"/search?q=#{@hash}&page=#{@current_page + 1}"}> |
| 99 | + <.button |
| 100 | + icon="arrow-right-solid" |
| 101 | + icon_class="group-hover:translate-x-1 transition-all duration-150" |
| 102 | + class="text-muted-foreground size-10 group" |
| 103 | + > |
| 104 | + <span class="sr-only">Next Page</span> |
| 105 | + </.button> |
| 106 | + </.link> |
| 107 | + <% else %> |
| 108 | + <.button |
| 109 | + icon="arrow-right-solid" |
| 110 | + class="text-muted-foreground size-10 group pointer-events-none opacity-50" |
| 111 | + disabled |
| 112 | + > |
| 113 | + <span class="sr-only">Next Page</span> |
| 114 | + </.button> |
| 115 | + <% end %> |
| 116 | + </div> |
| 117 | + <% else %> |
| 118 | + <.card_background class="overflow-x-auto min-h-[38.45rem] flex flex-col items-center justify-center gap-2"> |
| 119 | + <p class="text-lg text-muted-foreground">No matching batches found.</p> |
| 120 | + </.card_background> |
| 121 | + <% end %> |
| 122 | + </div> |
| 123 | + """ |
| 124 | + end |
| 125 | +end |
0 commit comments