2023-06-09 19:13:27 +00:00
|
|
|
defmodule PrymnWeb.ServerLive.Index do
|
2023-07-26 19:41:52 +00:00
|
|
|
require Logger
|
2023-08-25 21:51:04 +00:00
|
|
|
|
2023-09-05 17:54:59 +00:00
|
|
|
alias Prymn.{Agents, Servers}
|
2023-06-09 19:13:27 +00:00
|
|
|
|
2023-07-09 16:41:41 +00:00
|
|
|
use PrymnWeb, :live_view
|
|
|
|
|
2023-06-09 19:13:27 +00:00
|
|
|
@impl true
|
|
|
|
def mount(_params, _session, socket) do
|
2023-07-09 16:41:41 +00:00
|
|
|
servers = Servers.list_servers()
|
2024-02-01 21:54:23 +00:00
|
|
|
agents = Agents.from_servers(servers)
|
2023-07-09 16:41:41 +00:00
|
|
|
|
2023-08-25 21:51:04 +00:00
|
|
|
{:ok,
|
|
|
|
socket
|
|
|
|
|> assign(:servers, servers)
|
2024-02-01 21:54:23 +00:00
|
|
|
|> assign(:agents, agents)}
|
2023-06-09 19:13:27 +00:00
|
|
|
end
|
|
|
|
|
2023-11-25 14:49:51 +00:00
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class="mx-auto max-w-2xl">
|
|
|
|
<.header>
|
|
|
|
Your servers
|
|
|
|
<small class="block">
|
|
|
|
<%= "#{Enum.count(@servers)} servers" %>
|
|
|
|
</small>
|
|
|
|
<:actions>
|
|
|
|
<Button.primary patch={~p"/servers/new"}>Connect a Server</Button.primary>
|
|
|
|
</:actions>
|
|
|
|
</.header>
|
2024-02-01 21:54:23 +00:00
|
|
|
<div class="mt-10 space-y-5" phx-update="replace" id="servers">
|
|
|
|
<.link :for={server <- @servers} navigate={~p"/servers/#{server}"} class="group flex">
|
|
|
|
<.status_bar agent={@agents[server.id]} />
|
|
|
|
<div class="flex-1 rounded-r-lg bg-gray-100 p-5 transition-colors group-hover:bg-black group-hover:text-white">
|
2023-11-25 14:49:51 +00:00
|
|
|
<h2 class="text-xl"><%= server.name %></h2>
|
2024-02-01 21:54:23 +00:00
|
|
|
<div class="flex flex-row flex-wrap justify-between lg:text-sm">
|
|
|
|
<span>IP: <%= server.public_ip || "N/A" %></span>
|
|
|
|
</div>
|
2023-11-25 14:49:51 +00:00
|
|
|
</div>
|
|
|
|
</.link>
|
|
|
|
</div>
|
|
|
|
<.modal :if={@live_action == :new} id="server-modal" show on_cancel={JS.patch(~p"/servers")}>
|
|
|
|
<.live_component module={PrymnWeb.ServerLive.NewServer} id={:new} patch={~p"/servers"} />
|
|
|
|
</.modal>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-06-09 19:13:27 +00:00
|
|
|
@impl true
|
2023-08-19 18:14:07 +00:00
|
|
|
def handle_params(_params, _url, socket) do
|
|
|
|
socket =
|
|
|
|
case socket.assigns.live_action do
|
|
|
|
:new -> assign(socket, :page_title, gettext("Connect a Server"))
|
|
|
|
:index -> assign(socket, :page_title, gettext("Listing Servers"))
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2023-06-09 19:13:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2023-06-23 07:05:10 +00:00
|
|
|
def handle_info({:connect, %Servers.Server{} = server}, socket) do
|
2023-07-26 19:41:52 +00:00
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> update(:servers, fn servers -> [server | servers] end)}
|
2023-07-09 16:41:41 +00:00
|
|
|
end
|
|
|
|
|
2024-02-04 18:13:35 +00:00
|
|
|
# def handle_info(%Agents.Agent{} = agent, socket) do
|
|
|
|
# id = String.to_integer(agent.id)
|
|
|
|
# {:noreply, update(socket, :agents, &Map.put(&1, id, agent))}
|
|
|
|
# end
|
2023-06-09 19:13:27 +00:00
|
|
|
|
2023-07-26 19:41:52 +00:00
|
|
|
def handle_info(msg, state) do
|
|
|
|
Logger.debug("received unexpected message #{inspect(msg)}")
|
|
|
|
{:noreply, state}
|
2023-06-09 19:13:27 +00:00
|
|
|
end
|
2023-08-25 21:51:04 +00:00
|
|
|
|
2024-02-01 21:54:23 +00:00
|
|
|
defp status_bar(assigns) do
|
|
|
|
assigns =
|
|
|
|
assign(assigns, :class, [
|
|
|
|
"w-3 rounded-l-lg",
|
|
|
|
assigns.agent.status == :connected && "bg-teal-500",
|
|
|
|
assigns.agent.status == :disconnected && "bg-red-500"
|
|
|
|
])
|
2023-08-28 20:32:42 +00:00
|
|
|
|
2024-02-01 21:54:23 +00:00
|
|
|
~H"""
|
|
|
|
<div class={@class}></div>
|
|
|
|
"""
|
2023-08-25 21:51:04 +00:00
|
|
|
end
|
2023-06-09 19:13:27 +00:00
|
|
|
end
|