Skip to content

Commit 7cbb484

Browse files
committed
Extract session loading logic into helper functions for better readability
1 parent 879bf3c commit 7cbb484

File tree

1 file changed

+2
-41
lines changed

1 file changed

+2
-41
lines changed

lib/lightning_web/live/ai_assistant/component.ex

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -388,49 +388,10 @@ defmodule LightningWeb.AiAssistant.Component do
388388

389389
case action do
390390
:new ->
391-
socket
392-
|> delegate_to_handler(:on_session_close)
393-
|> assign(:action, :new)
394-
|> assign(:session, nil)
395-
|> assign_async([:all_sessions, :pagination_meta], fn ->
396-
case handler.list_sessions(assigns, sort_direction,
397-
limit: @default_page_size
398-
) do
399-
%{sessions: sessions, pagination: pagination} ->
400-
{:ok, %{all_sessions: sessions, pagination_meta: pagination}}
401-
end
402-
end)
391+
load_and_show_session_list(socket, handler, assigns, sort_direction)
403392

404393
:show ->
405-
try do
406-
session = handler.get_session!(assigns)
407-
408-
message_loading =
409-
Enum.any?(session.messages, fn msg ->
410-
msg.role == :user && msg.status in [:pending, :processing]
411-
end)
412-
413-
socket
414-
|> assign(:session, session)
415-
|> assign(:action, :show)
416-
|> assign(
417-
:pending_message,
418-
if message_loading do
419-
AsyncResult.loading()
420-
else
421-
AsyncResult.ok(nil)
422-
end
423-
)
424-
|> delegate_to_handler(:on_session_open, [session])
425-
rescue
426-
Ecto.NoResultsError ->
427-
# Session doesn't exist or doesn't belong to this job
428-
# Fall back to showing the session list
429-
socket
430-
|> apply_action(:new)
431-
|> assign(:action, :new)
432-
|> assign(:session, nil)
433-
end
394+
load_and_show_session(socket, handler, assigns)
434395
end
435396
end
436397

0 commit comments

Comments
 (0)