Reset chat state when navigating between entities
The Chat component kept stale messages and chat references when patching to a different artist/record because update/2 only refreshed the chat list, not the active chat state.
This commit is contained in:
@@ -10,17 +10,7 @@ defmodule MusicLibraryWeb.Components.Chat do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(socket) do
|
def mount(socket) do
|
||||||
{:ok,
|
{:ok, reset_chat_state(socket)}
|
||||||
socket
|
|
||||||
|> assign(:messages, [])
|
|
||||||
|> assign(:current_response, "")
|
|
||||||
|> assign(:loading, false)
|
|
||||||
|> assign(:error, nil)
|
|
||||||
|> assign(:view, :active)
|
|
||||||
|> assign(:chat, nil)
|
|
||||||
|> assign(:chats, [])
|
|
||||||
|> assign(:streaming_doc, nil)
|
|
||||||
|> assign(:has_history, false)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@@ -56,28 +46,12 @@ defmodule MusicLibraryWeb.Components.Chat do
|
|||||||
def update(assigns, socket) do
|
def update(assigns, socket) do
|
||||||
socket = assign(socket, assigns)
|
socket = assign(socket, assigns)
|
||||||
|
|
||||||
socket =
|
|
||||||
if changed?(socket, :entity) or changed?(socket, :musicbrainz_id) do
|
if changed?(socket, :entity) or changed?(socket, :musicbrainz_id) do
|
||||||
has_history = check_chat_history(socket.assigns)
|
{:ok, load_for_entity(socket)}
|
||||||
|
|
||||||
if has_history do
|
|
||||||
chats = Chats.list_chats(socket.assigns.entity, socket.assigns.musicbrainz_id)
|
|
||||||
|
|
||||||
socket
|
|
||||||
|> assign(:has_history, true)
|
|
||||||
|> assign(:chats, chats)
|
|
||||||
|> assign(:view, :list)
|
|
||||||
else
|
else
|
||||||
socket
|
|
||||||
|> assign(:has_history, false)
|
|
||||||
|> assign(:view, :active)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
socket
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
@@ -476,12 +450,33 @@ defmodule MusicLibraryWeb.Components.Chat do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp check_chat_history(%{entity: entity, musicbrainz_id: musicbrainz_id})
|
defp reset_chat_state(socket) do
|
||||||
when not is_nil(entity) and not is_nil(musicbrainz_id) do
|
socket
|
||||||
Chats.has_any_chats?(entity, musicbrainz_id)
|
|> assign(:messages, [])
|
||||||
|
|> assign(:current_response, "")
|
||||||
|
|> assign(:loading, false)
|
||||||
|
|> assign(:error, nil)
|
||||||
|
|> assign(:view, :active)
|
||||||
|
|> assign(:chat, nil)
|
||||||
|
|> assign(:chats, [])
|
||||||
|
|> assign(:streaming_doc, nil)
|
||||||
|
|> assign(:has_history, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp check_chat_history(_assigns), do: false
|
defp load_for_entity(socket) do
|
||||||
|
socket = reset_chat_state(socket)
|
||||||
|
|
||||||
|
if Chats.has_any_chats?(socket.assigns.entity, socket.assigns.musicbrainz_id) do
|
||||||
|
chats = Chats.list_chats(socket.assigns.entity, socket.assigns.musicbrainz_id)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:has_history, true)
|
||||||
|
|> assign(:chats, chats)
|
||||||
|
|> assign(:view, :list)
|
||||||
|
else
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp message_classes("user") do
|
defp message_classes("user") do
|
||||||
"ml-auto bg-red-500 dark:bg-red-700 text-white"
|
"ml-auto bg-red-500 dark:bg-red-700 text-white"
|
||||||
|
|||||||
Reference in New Issue
Block a user