nulla/lib/nulla_web/components/templates.ex

109 lines
2.5 KiB
Elixir

defmodule NullaWeb.PageHTML do
use NullaWeb, :html
embed_templates "templates/page/*"
end
defmodule NullaWeb.ActorHTML do
use NullaWeb, :html
embed_templates "templates/actor/*"
def format_birthdate(date) do
formatted = Date.to_string(date) |> String.replace("-", "/")
today = Date.utc_today()
age =
today.year - date.year -
if {today.month, today.day} < {date.month, date.day}, do: 1, else: 0
"#{formatted} (#{age} years old)"
end
def format_registration_date(date) do
now = Date.utc_today()
formatted = Date.to_string(date) |> String.replace("-", "/")
diff_days = Date.diff(now, date)
cond do
diff_days == 0 ->
"#{formatted} (today)"
diff_days == 1 ->
"#{formatted} (1 day ago)"
diff_days < 30 ->
"#{formatted} (#{diff_days} days ago)"
diff_days < 365 ->
year_diff = now.year - date.year
month_diff = now.month - date.month
day_correction = if now.day < date.day, do: -1, else: 0
months = year_diff * 12 + month_diff + day_correction
if months == 1 do
"#{formatted} (1 month ago)"
else
"#{formatted} (#{months} months ago)"
end
true ->
year_diff = now.year - date.year
years =
if {now.month, now.day} < {date.month, date.day}, do: year_diff - 1, else: year_diff
if years == 1 do
"#{formatted} (1 year ago)"
else
"#{formatted} (#{years} years ago)"
end
end
end
def format_note_datetime(datetime) do
Calendar.strftime(datetime, "%d %B %Y, %H:%M")
end
def format_note_datetime_diff(datetime) do
now = DateTime.utc_now()
diff_seconds = DateTime.diff(now, datetime)
cond do
diff_seconds < 60 ->
"now"
diff_seconds < 3600 ->
minutes = div(diff_seconds, 60)
"#{minutes}m ago"
diff_seconds < 86400 ->
hours = div(diff_seconds, 3600)
"#{hours}h ago"
diff_seconds < 604_800 ->
days = div(diff_seconds, 86400)
"#{days}d ago"
diff_seconds < 2_592_000 ->
weeks = div(diff_seconds, 604_800)
"#{weeks}w ago"
diff_seconds < 31_536_000 ->
months = div(diff_seconds, 2_592_000)
"#{months}mo ago"
true ->
years = div(diff_seconds, 31_536_000)
"#{years}y ago"
end
end
end
defmodule NullaWeb.NoteHTML do
use NullaWeb, :html
embed_templates "templates/note/*"
end