From b10f9797cb15d0e8a4e4b5014138dc5bfd875d88 Mon Sep 17 00:00:00 2001 From: href Date: Tue, 7 Sep 2021 08:14:33 +0200 Subject: live fixes/improvements --- lib/lsg_web/components/component.ex | 4 ++++ lib/lsg_web/components/event_component.ex | 7 +++++++ lib/lsg_web/components/message_component.ex | 2 ++ lib/lsg_web/live/chat_live.ex | 32 +++++++++++++++++++++++------ lib/lsg_web/live/chat_live.html.heex | 7 ++----- 5 files changed, 41 insertions(+), 11 deletions(-) (limited to 'lib') 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: + <%= Date.to_string(@date) %> + """ + end + def content(assigns = %{event: %{type: :quit}}) do ~H""" 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""" + +
<%= @message.sender.nick %>
<%= @text %>
""" 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 %>
  • - - <%= message.sender.nick %> - - -
  • <% end %> -- cgit v1.2.3