freedive/lib/freedive_web/live/service_live.ex

134 lines
3.1 KiB
Elixir

defmodule FreediveWeb.ServiceLive do
use FreediveWeb.LiliformLive
def render(assigns) do
~H"""
<.page name="Services" filters={@filters}>
<FreediveWeb.ServiceLive.Components.items_block
items={@items}
selected_item={@selected_item}
details={@details}
/>
</.page>
"""
end
def items() do
%{
"sshd" => %{
name: "sshd",
path: "/services/ssh",
icon: "blocks",
description: "Secure Shell Daemon",
enabled: true,
running: true
},
"pf" => %{
name: "pf",
path: "/services/pf",
icon: "shield",
description: "Packet Filter",
enabled: true,
running: true
},
"ntpdate" => %{
name: "ntpdate",
path: "/services/ntp",
icon: "clock",
description: "Network Time Protocol Daemon",
enabled: true,
running: false
},
"httpd" => %{
name: "httpd",
path: "/services/httpd",
icon: "globe",
description:
"Hypertext Transfer Protocol Daemon",
enabled: false,
running: false
}
}
end
def filters() do
[
%{
title: "Running",
key: :running,
icon: "circle-play",
active: true
},
%{
title: "Enabled",
key: :enabled,
icon: "circle-check",
active: false
}
]
end
def search(query) do
items()
|> Enum.filter(fn item ->
String.contains?(String.downcase(item.name), String.downcase(query))
end)
end
end
defmodule FreediveWeb.ServiceLive.Components do
use Liliform.Component
import Liliform.Icon
import Liliform.Panel
import Liliform.Title
@doc """
Returns items as panel-blocks.
"""
attr :items, :list, default: [], doc: "items"
attr :selected_item, :string, default: nil, doc: "selected item"
attr :details, :string, default: nil, doc: "details"
def items_block(%{details: true} = assigns) do
~H"""
<.link
class="panel-block pt-1 has-background-light"
phx-click="tap"
phx-value-name={@selected_item.name}
>
<span class="panel-icon">
<.icon for="arrow-left" aria-hidden="true" has-text-dark />
</span>
<div class="mt-2 ml-2">Back</div>
</.link>
<.panel_block_div>
<div class="columns">
<.title class="column">
<%= @selected_item.name %>
</.title>
<.subtitle class="column">
<%= @selected_item.description %>
</.subtitle>
</div>
</.panel_block_div>
"""
end
def items_block(assigns) do
~H"""
<%= for {_name, item} <- @items do %>
<.link
class={"panel-block pt-1 #{if @selected_item != nil and @selected_item.name == item.name, do: "has-background-info-light"}"}
phx-click="tap"
phx-value-name={item.name}
>
<span class="panel-icon">
<.icon for={item.icon} color="auto" aria-hidden="true" />
</span>
<div class="mt-2 ml-2"><%= item.name %></div>
</.link>
<% end %>
"""
end
end