cannery/lib/cannery_web/live/home_live.ex

137 lines
4.1 KiB
Elixir
Raw Normal View History

2021-09-10 00:28:53 -04:00
defmodule CanneryWeb.HomeLive do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview for the home page
"""
2021-03-11 21:12:55 -05:00
use CanneryWeb, :live_view
2022-01-22 21:40:29 -05:00
alias Cannery.Accounts
2021-03-11 21:12:55 -05:00
@impl true
2021-09-02 23:31:14 -04:00
def mount(_params, session, socket) do
2021-09-10 00:28:53 -04:00
admins = Accounts.list_users_by_role(:admin)
{:ok, socket |> assign_defaults(session) |> assign(query: "", results: %{}, admins: admins)}
2021-03-11 21:12:55 -05:00
end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
2021-09-02 23:31:14 -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} ->
{: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
2022-02-07 23:50:32 -05:00
@impl true
def render(assigns) do
~H"""
<div class="mx-auto px-8 sm:px-16 flex flex-col justify-center items-center text-center space-y-4 max-w-3xl">
<h1 class="title text-primary-500 text-2xl">
2022-02-08 23:25:09 -05:00
<%= gettext("Welcome to %{name}", name: "Cannery") %>
2022-02-07 23:50:32 -05:00
</h1>
2022-02-08 23:25:09 -05:00
<h2 class="title text-primary-500 text-lg">
<%= gettext("The self-hosted firearm tracker website") %>
2022-02-07 23:50:32 -05:00
</h2>
<hr class="hr" />
<ul class="flex flex-col space-y-4 text-center">
2022-02-08 23:25:09 -05:00
<li class="flex flex-col justify-center items-center
space-y-2">
2022-02-07 23:50:32 -05:00
<b class="whitespace-nowrap">
2022-02-08 23:25:09 -05:00
<%= gettext("Easy to Use:") %>
2022-02-07 23:50:32 -05:00
</b>
2022-02-08 23:25:09 -05:00
<p>
<%= gettext("%{name} lets you easily keep an eye on your ammo levels before and after range day",
name: "Cannery"
) %>
</p>
2022-02-07 23:50:32 -05:00
</li>
2022-02-08 23:25:09 -05:00
<li class="flex flex-col justify-center items-center
space-y-2">
2022-02-07 23:50:32 -05:00
<b class="whitespace-nowrap">
2022-02-08 23:25:09 -05:00
<%= gettext("Secure:") %>
2022-02-07 23:50:32 -05:00
</b>
<p>
2022-02-08 23:25:09 -05:00
<%= gettext("Self-host your own instance, or use an instance from someone you trust.") %>
<%= gettext("Your data stays with you, period") %>
2022-02-07 23:50:32 -05:00
</p>
</li>
2022-02-08 23:25:09 -05:00
<li class="flex flex-col justify-center items-center
space-y-2">
2022-02-07 23:50:32 -05:00
<b class="whitespace-nowrap">
2022-02-08 23:25:09 -05:00
<%= gettext("Simple:") %>
2022-02-07 23:50:32 -05:00
</b>
2022-02-08 23:25:09 -05:00
<p>
<%= gettext("Access from any internet-capable device") %>
</p>
2022-02-07 23:50:32 -05:00
</li>
</ul>
2022-02-08 23:25:09 -05:00
<hr class="hr">
2022-02-07 23:50:32 -05:00
<ul class="flex flex-col space-y-2 text-center justify-center">
2022-02-08 23:25:09 -05:00
<h2 class="title text-primary-500 text-lg">
<%= gettext("Instance Information") %>
2022-02-07 23:50:32 -05:00
</h2>
<li class="flex flex-col justify-center space-x-2">
2022-02-08 23:25:09 -05:00
<b>
<%= gettext("Admins:") %>
</b>
2022-02-07 23:50:32 -05:00
<p>
<%= if @admins |> Enum.empty?() do %>
2022-02-08 23:25:09 -05:00
<%= link(dgettext("prompts", "Register to setup %{name}", name: "Cannery"),
2022-02-07 23:50:32 -05:00
class: "hover:underline",
to: Routes.user_registration_path(CanneryWeb.Endpoint, :new)
) %>
<% else %>
<div class="flex flex-wrap justify-center space-x-2">
<%= for admin <- @admins do %>
<a class="hover:underline" href={"mailto:#{admin.email}"}>
<%= admin.email %>
</a>
<% end %>
</div>
<% end %>
</p>
</li>
<li class="flex flex-row justify-center space-x-2">
<b>Registration:</b>
<p>
<%= Application.get_env(:cannery, CanneryWeb.Endpoint)[:registration]
|> case do
2022-02-08 23:25:09 -05:00
"public" -> gettext("Public Signups")
_ -> gettext("Invite Only")
2022-02-07 23:50:32 -05:00
end %>
</p>
</li>
</ul>
</div>
"""
end
2021-03-11 21:12:55 -05:00
defp search(query) do
if not CanneryWeb.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