diff options
author | href <href@random.sh> | 2021-09-07 08:14:33 +0200 |
---|---|---|
committer | href <href@random.sh> | 2021-09-07 08:14:33 +0200 |
commit | b10f9797cb15d0e8a4e4b5014138dc5bfd875d88 (patch) | |
tree | 67877b6b91db23f768720245d0b5754a2670d87b | |
parent | alcoolog: fix ets (diff) |
live fixes/improvements
-rw-r--r-- | lib/lsg_web/components/component.ex | 4 | ||||
-rw-r--r-- | lib/lsg_web/components/event_component.ex | 7 | ||||
-rw-r--r-- | lib/lsg_web/components/message_component.ex | 2 | ||||
-rw-r--r-- | lib/lsg_web/live/chat_live.ex | 32 | ||||
-rw-r--r-- | lib/lsg_web/live/chat_live.html.heex | 7 |
5 files changed, 41 insertions, 11 deletions
diff --git a/lib/lsg_web/components/component.ex b/lib/lsg_web/components/component.ex index 37d75e3..d504129 100644 --- a/lib/lsg_web/components/component.ex +++ b/lib/lsg_web/components/component.ex @@ -3,6 +3,10 @@ defmodule LSGWeb.Component do @date_time_default_format "%F %H:%M" @date_time_formats %{"time-24-with-seconds" => "%H:%M:%S"} + def naive_date_time_utc(assigns = %{at: nil}) do + "" + end + def naive_date_time_utc(assigns = %{format: format}) do assigns = assign(assigns, :format, Map.get(@date_time_formats, format, format)) ~H""" diff --git a/lib/lsg_web/components/event_component.ex b/lib/lsg_web/components/event_component.ex index 3b9cd3b..fa81d19 100644 --- a/lib/lsg_web/components/event_component.ex +++ b/lib/lsg_web/components/event_component.ex @@ -1,6 +1,13 @@ defmodule LSGWeb.EventComponent do use Phoenix.Component + def content(assigns = %{event: %{type: :day_changed}}) do + ~H""" + Day changed: + <span class="reason"><%= Date.to_string(@date) %></span> + """ + end + def content(assigns = %{event: %{type: :quit}}) do ~H""" <LSGWeb.Component.nick self={@self} nick={@user.nick} user_id={@user.id} account_id={@user.account} /> diff --git a/lib/lsg_web/components/message_component.ex b/lib/lsg_web/components/message_component.ex index 2381411..5997754 100644 --- a/lib/lsg_web/components/message_component.ex +++ b/lib/lsg_web/components/message_component.ex @@ -3,6 +3,8 @@ defmodule LSGWeb.MessageComponent do def content(assigns) do ~H""" + <LSGWeb.Component.naive_date_time_utc datetime={@message.at} format="time-24-with-seconds" /> + <div class="inline-block font-bold flex-none cursor-default"><%= @message.sender.nick %></div> <div class="inline-block flex-grow cursor-default"><%= @text %></div> """ end diff --git a/lib/lsg_web/live/chat_live.ex b/lib/lsg_web/live/chat_live.ex index e7a44db..e84d880 100644 --- a/lib/lsg_web/live/chat_live.ex +++ b/lib/lsg_web/live/chat_live.ex @@ -23,7 +23,9 @@ defmodule LSGWeb.ChatLive do end) backlog = case LSG.IRC.BufferPlugin.select_buffer(connection.network, chan) do - {backlog, _} -> Enum.reverse(backlog) + {backlog, _} -> + {backlog, _} = Enum.reduce(backlog, {backlog, nil}, &reduce_contextual_event/2) + Enum.reverse(backlog) _ -> [] end @@ -54,7 +56,7 @@ defmodule LSGWeb.ChatLive do if user = IRC.UserTrack.lookup(id) do socket = socket |> assign(:users, Map.put(socket.assigns.users, id, user)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> append_to_backlog(event) {:noreply, socket} else {:noreply, socket} @@ -64,21 +66,21 @@ defmodule LSGWeb.ChatLive do def handle_info({:irc, :event, event = %{type: :nick, user_id: id, nick: nick}}, socket) do socket = socket |> assign(:users, update_in(socket.assigns.users, [id, :nick], nick)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> append_to_backlog(event) {:noreply, socket} end def handle_info({:irc, :event, event = %{type: :quit, user_id: id}}, socket) do socket = socket |> assign(:users, Map.delete(socket.assigns.users, id)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> append_to_backlog(event) {:noreply, socket} end def handle_info({:irc, :event, event = %{type: :part, user_id: id}}, socket) do socket = socket |> assign(:users, Map.delete(socket.assigns.users, id)) - |> assign(:backlog, socket.assigns.backlog ++ [event]) + |> append_to_backlog(event) {:noreply, socket} end @@ -87,8 +89,9 @@ defmodule LSGWeb.ChatLive do end def handle_info({:irc, :text, message}, socket) do + IO.inspect({:live_message, message}) socket = socket - |> assign(:backlog, socket.assigns.backlog ++ [message]) + |> append_to_backlog(message) {:noreply, socket} end @@ -97,4 +100,21 @@ defmodule LSGWeb.ChatLive do {:noreply, socket} end + defp append_to_backlog(socket, line) do + {add, _} = reduce_contextual_event(line, {[], List.last(socket.assigns.backlog)}) + assign(socket, :backlog, socket.assigns.backlog ++ add) + end + + defp reduce_contextual_event(line, {acc, nil}) do + {[line | acc], line} + end + defp reduce_contextual_event(line, {acc, last}) do + if NaiveDateTime.to_date(last.at) != NaiveDateTime.to_date(line.at) do + {[%{type: :day_changed, date: NaiveDateTime.to_date(line.at), at: nil}, line | acc], line} + else + {[line | acc], line} + end + + end + end diff --git a/lib/lsg_web/live/chat_live.html.heex b/lib/lsg_web/live/chat_live.html.heex index 717be4f..fc1f582 100644 --- a/lib/lsg_web/live/chat_live.html.heex +++ b/lib/lsg_web/live/chat_live.html.heex @@ -27,14 +27,11 @@ <%= if is_map(message) && Map.get(message, :__struct__) == IRC.Message do %> <li class="flex gap-2 place-items-center message" data-account-id={message.account.id}> - <LSGWeb.Component.naive_date_time_utc datetime={message.at} format="time-24-with-seconds" /> - <span class="inline-block font-bold flex-none cursor-default"><%= message.sender.nick %></span> - <span class="inline-block flex-grow cursor-default"> - <LSGWeb.MessageComponent.content + <LSGWeb.MessageComponent.content + message={message} self={message.account.id == @account_id} text={message.text} /> - </span> </li> <% end %> |