Convert universal search into a live component

This commit is contained in:
Claudio Ortolina
2025-09-21 21:21:16 +03:00
parent 64cb917a4e
commit a1b9994837
3 changed files with 107 additions and 104 deletions
@@ -130,4 +130,4 @@
</div> </div>
</main> </main>
{MusicLibraryWeb.UniversalSearchLive.Index.live(@socket)} <.live_component module={MusicLibraryWeb.UniversalSearchLive.Index} id="universal-search" />
@@ -1,15 +1,11 @@
defmodule MusicLibraryWeb.UniversalSearchLive.Index do defmodule MusicLibraryWeb.UniversalSearchLive.Index do
use MusicLibraryWeb, :live_view use MusicLibraryWeb, :live_component
import MusicLibraryWeb.SearchComponents import MusicLibraryWeb.SearchComponents
alias MusicLibrary.Search alias MusicLibrary.Search
alias Phoenix.LiveView.ColocatedHook alias Phoenix.LiveView.ColocatedHook
def live(socket) do
live_render(socket, __MODULE__, id: "universal-search", session: %{})
end
def universal_search_trigger(assigns) do def universal_search_trigger(assigns) do
~H""" ~H"""
<script :type={ColocatedHook} name=".SearchGlobalShortcut"> <script :type={ColocatedHook} name=".SearchGlobalShortcut">
@@ -47,7 +43,7 @@ defmodule MusicLibraryWeb.UniversalSearchLive.Index do
end end
@impl true @impl true
def mount(_params, _session, socket) do def mount(socket) do
{:ok, {:ok,
socket socket
|> assign(:show_modal, false) |> assign(:show_modal, false)
@@ -1,10 +1,11 @@
<.structured_modal <div id={@id}>
<.structured_modal
:if={@show_modal} :if={@show_modal}
id="universal-search-root" id="universal-search-root"
open={@show_modal} open={@show_modal}
on_close={JS.push("close_modal", value: %{})} on_close={JS.push("close_modal", value: %{}, target: "#universal-search")}
> >
<form class="mt-4 text-sm" phx-change="search" phx-submit="search"> <form class="mt-4 text-sm" phx-change="search" phx-submit="search" phx-target={@myself}>
<div class="relative"> <div class="relative">
<.icon <.icon
name="hero-magnifying-glass" name="hero-magnifying-glass"
@@ -43,6 +44,7 @@
phx-click="navigate_to_record" phx-click="navigate_to_record"
phx-value-id={record.id} phx-value-id={record.id}
phx-value-type="collection" phx-value-type="collection"
phx-target={@myself}
/> />
<:actions> <:actions>
<.view_all_button <.view_all_button
@@ -51,6 +53,7 @@
target="collection" target="collection"
phx-click="navigate_to_collection" phx-click="navigate_to_collection"
phx-value-query={@search_query} phx-value-query={@search_query}
phx-target={@myself}
/> />
</:actions> </:actions>
</.search_result_group> </.search_result_group>
@@ -70,6 +73,7 @@
phx-click="navigate_to_record" phx-click="navigate_to_record"
phx-value-id={record.id} phx-value-id={record.id}
phx-value-type="wishlist" phx-value-type="wishlist"
phx-target={@myself}
/> />
<:actions> <:actions>
<.view_all_button <.view_all_button
@@ -78,6 +82,7 @@
target="wishlist" target="wishlist"
phx-click="navigate_to_wishlist" phx-click="navigate_to_wishlist"
phx-value-query={@search_query} phx-value-query={@search_query}
phx-target={@myself}
/> />
</:actions> </:actions>
</.search_result_group> </.search_result_group>
@@ -96,10 +101,12 @@
image_data_hash={image_data_hash} image_data_hash={image_data_hash}
phx-click="navigate_to_artist" phx-click="navigate_to_artist"
phx-value-id={artist.musicbrainz_id} phx-value-id={artist.musicbrainz_id}
phx-target={@myself}
/> />
</.search_result_group> </.search_result_group>
</div> </div>
<!-- Footer with keyboard shortcuts --> <!-- Footer with keyboard shortcuts -->
<.results_footer total_results={@total_results} /> <.results_footer total_results={@total_results} />
</.structured_modal> </.structured_modal>
</div>