defmodule Flames.Dashboard.Helpers do
use Phoenix.Component
attr :type, :string, default: nil
attr :color, :atom, default: :red
attr :class, :string, default: nil
attr :rest, :global, include: ~w(disabled form name value)
slot(:inner_block, required: true)
def button(assigns) do
~H"""
"""
end
def button_classes_for_color(:red) do
"bg-rose-600 active:bg-rose-800 focus:bg-rose-700 hover:bg-rose-700"
end
def button_classes_for_color(:amber) do
"bg-amber-600 active:bg-amber-800 focus:bg-amber-700 hover:bg-amber-700"
end
def button_classes_for_color(_) do
"bg-yellow-500 active:bg-yellow-800 focus:bg-yellow-700 hover:bg-yellow-700"
end
attr :level, :string
def level(assigns) do
~H"""
<%= @level %>
"""
end
def color_for_level("error"), do: "bg-rose-600"
def color_for_level("warn"), do: "bg-amber-400"
def color_for_level("warning"), do: "bg-amber-400"
def color_for_level(_other), do: "bg-gradient-to-r from-amber-400 to-rose-600"
def display_timestamp(%DateTime{} = timestamp) do
timestamp
|> DateTime.to_naive()
|> display_timestamp()
end
def display_timestamp(%NaiveDateTime{} = timestamp) do
timezone = Application.get_env(:flames, :timezone, "Etc/UTC")
timestamp
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.shift_zone!(timezone)
|> Calendar.strftime("%a %b %d, %Y %I:%M %p")
end
def display_module(atom) when is_atom(atom) do
atom
|> Atom.to_string()
|> display_module()
end
def display_module(atom) when is_binary(atom) do
if String.starts_with?(atom, ":") || String.starts_with?(atom, "Elixir.") do
String.to_existing_atom(atom) |> inspect()
else
atom
end
end
def last_incident_timestamp(%Flames.Error{
incidents: [%Flames.Error.Incident{timestamp: timestamp} | _]
}) do
display_timestamp(timestamp)
end
def last_incident_timestamp(%Flames.Error{timestamp: timestamp}) do
display_timestamp(timestamp)
end
def last_incident_timestamp(_error) do
"(Unknown)"
end
end