|
|
@@ -1,39 +1,14 @@
|
|
|
defmodule ToyWeb.PageLive do
|
|
|
use ToyWeb, :live_view
|
|
|
+ alias Toy.Features
|
|
|
|
|
|
@impl true
|
|
|
def mount(_params, _session, socket) do
|
|
|
- {:ok, assign(socket, query: "", results: %{})}
|
|
|
+ {:ok, assign(socket, repo_started: Toy.Features.repo_started?())}
|
|
|
end
|
|
|
|
|
|
@impl true
|
|
|
- def handle_event("suggest", %{"q" => query}, socket) do
|
|
|
- {:noreply, assign(socket, results: search(query), query: query)}
|
|
|
- end
|
|
|
-
|
|
|
- @impl true
|
|
|
- def handle_event("search", %{"q" => query}, socket) do
|
|
|
- case search(query) do
|
|
|
- %{^query => vsn} ->
|
|
|
- {:noreply, redirect(socket, 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 ToyWeb.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}
|
|
|
+ def handle_event("start_repo", %{}, socket) do
|
|
|
+ with {:ok, _} <- Features.start_repo(), do: {:noreply, socket |> assign(repo_started: true)}
|
|
|
end
|
|
|
end
|