cannery/lib/lokal_web/live/page_live.ex
2023-01-07 21:00:14 -05:00

49 lines
1.2 KiB
Elixir

defmodule LokalWeb.PageLive do
@moduledoc """
Liveview for the main home page
"""
use LokalWeb, :live_view
@impl true
def mount(_params, session, socket) do
socket =
socket
|> assign_defaults(session)
|> assign(page_title: gettext("Home"), query: "", results: %{})
{:ok, socket}
end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
{:noreply, socket |> assign(results: search(query), query: query)}
end
@impl true
def handle_event("search", %{"q" => query}, socket) do
case search(query) do
%{^query => vsn} ->
{:noreply, socket |> redirect(external: "https://hexdocs.pm/#{query}/#{vsn}")}
_ ->
{:noreply,
socket
|> put_flash(:error, "No dependencies found matching \"#{query}\"")
|> assign(results: %{}, query: query)}
end
end
defp search(query) do
if not LokalWeb.Endpoint.config(:code_reloader) do
raise "action disabled when not in development"
end
for {app, desc, vsn} <- Application.started_applications(),
app = to_string(app),
String.starts_with?(app, query) and not List.starts_with?(desc, ~c"ERTS"),
into: %{},
do: {app, vsn}
end
end