defmodule LivebookWeb.SessionLive.DependencySearchLive do use LivebookWeb, :live_view @impl true def mount( _params, %{"session" => session, "runtime" => runtime, "return_to" => return_to}, socket ) do socket = assign(socket, session: session, runtime: runtime, return_to: return_to, search: "", search_ref: nil, entries: [], error_message: nil ) socket = if connected?(socket), do: do_search(socket, ""), else: socket {:ok, socket} end @impl true def render(assigns) do ~H"""

Dependency search

Find external packages for your notebook

<%= cond do %> <% @error_message -> %>
<%= @error_message %>
<% @entries == [] -> %>
<.remix_icon icon="windy-line" class="text-xl" />
No results
<% true -> %> <%= for {entry, idx} <- Enum.with_index(@entries) do %> <.dependency_entry entry={entry} idx={idx} /> <% end %> <% end %>
""" end defp dependency_entry(assigns) do ~H"""
<%= if @entry[:url] do %> <%= @entry.name %> <% else %> <%= @entry.name %> <% end %> <%= @entry.version %>
<%= @entry.description %>
""" end @impl true def handle_event("search", %{"search" => search}, socket) do {:noreply, do_search(socket, search)} end @impl true def handle_event("submit", %{}, socket) do socket = case socket.assigns.entries do [] -> socket [first | _] -> add_dependency(socket, first.dependency) end {:noreply, socket} end @impl true def handle_event("add", %{"idx" => idx}, socket) do entry = Enum.fetch!(socket.assigns.entries, idx) socket = add_dependency(socket, entry.dependency) {:noreply, socket} end @impl true def handle_info( {:runtime_search_dependencies_response, ref, response}, %{assigns: %{search_ref: ref}} = socket ) do socket = case response do {:ok, entries} -> assign(socket, entries: entries, error_message: nil) {:error, message} -> assign(socket, error_message: Livebook.Utils.upcase_first(message)) end {:noreply, assign(socket, search_ref: nil)} end def handle_info(_message, socket), do: {:noreply, socket} defp do_search(socket, search) do search_ref = Livebook.Runtime.search_dependencies(socket.assigns.runtime, self(), search) assign(socket, search_ref: search_ref, search: search) end defp add_dependency(socket, dependency) do Livebook.Session.add_dependencies(socket.assigns.session.pid, [dependency]) push_patch(socket, to: socket.assigns.return_to) end end