2021-03-11 21:12:55 -05:00
|
|
|
defmodule LokalWeb.PageLive do
|
2022-01-22 20:44:38 -05:00
|
|
|
@moduledoc """
|
|
|
|
Liveview for the main home page
|
|
|
|
"""
|
|
|
|
|
2021-03-11 21:12:55 -05:00
|
|
|
use LokalWeb, :live_view
|
|
|
|
|
|
|
|
@impl true
|
2022-05-05 20:55:59 -04:00
|
|
|
def mount(_params, _session, socket) do
|
|
|
|
{:ok, socket |> assign(page_title: gettext("Home"), query: "", results: %{})}
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("suggest", %{"q" => query}, socket) do
|
2021-09-02 23:32:53 -04:00
|
|
|
{:noreply, socket |> assign(results: search(query), query: query)}
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("search", %{"q" => query}, socket) do
|
|
|
|
case search(query) do
|
|
|
|
%{^query => vsn} ->
|
2021-09-02 23:32:53 -04:00
|
|
|
{:noreply, socket |> redirect(external: "https://hexdocs.pm/#{query}/#{vsn}")}
|
2021-03-11 21:12:55 -05:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
{: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
|