Skip to content

Commit 078f9b9

Browse files
committed
Apply review comments
1 parent dd36df4 commit 078f9b9

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

lib/livebook_web/helpers/session_helpers.ex

+28
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,32 @@ defmodule LivebookWeb.SessionHelpers do
274274
Livebook.Session.register_file(session_pid, path, key)
275275
end
276276
end
277+
278+
@logout_topic "logout"
279+
280+
@doc """
281+
Subscribes to #{@logout_topic} topic.
282+
"""
283+
@spec subscribe() :: :ok | {:error, term()}
284+
def subscribe do
285+
Phoenix.PubSub.subscribe(Livebook.PubSub, @logout_topic)
286+
end
287+
288+
@doc """
289+
Shows the confirmation modal to logout user from Livebook.
290+
"""
291+
@spec confirm_logout(Socket.t()) :: Socket.t()
292+
def confirm_logout(socket) do
293+
on_confirm = fn socket ->
294+
Phoenix.PubSub.broadcast(Livebook.PubSub, @logout_topic, :logout)
295+
put_flash(socket, :info, "Livebook is logging out. You will be redirected soon.")
296+
end
297+
298+
confirm(socket, on_confirm,
299+
title: "Log out",
300+
description: "Are you sure you want to log out Livebook now?",
301+
confirm_text: "Log out",
302+
confirm_icon: "logout-box-line"
303+
)
304+
end
277305
end

lib/livebook_web/live/hooks/app_auth_hook.ex

+2-13
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule LivebookWeb.AppAuthHook do
4242

4343
def on_mount(:default, %{"slug" => slug}, session, socket) do
4444
if connected?(socket) do
45-
Phoenix.PubSub.subscribe(Livebook.PubSub, "app")
45+
LivebookWeb.SessionHelpers.subscribe()
4646
end
4747

4848
livebook_authenticated? = livebook_authenticated?(session, socket)
@@ -85,18 +85,7 @@ defmodule LivebookWeb.AppAuthHook do
8585
defp handle_info(_event, socket), do: {:cont, socket}
8686

8787
defp handle_event("logout", %{}, socket) do
88-
on_confirm = fn socket ->
89-
Phoenix.PubSub.broadcast(Livebook.PubSub, "app", :logout)
90-
put_flash(socket, :info, "Livebook is logging out. You will be redirected soon.")
91-
end
92-
93-
{:halt,
94-
LivebookWeb.Confirm.confirm(socket, on_confirm,
95-
title: "Log out",
96-
description: "Are you sure you want to log out Livebook now?",
97-
confirm_text: "Log out",
98-
confirm_icon: "logout-box-line"
99-
)}
88+
{:halt, LivebookWeb.SessionHelpers.confirm_logout(socket)}
10089
end
10190

10291
defp handle_event(_event, _params, socket), do: {:cont, socket}

lib/livebook_web/live/hooks/sidebar_hook.ex

+2-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule LivebookWeb.SidebarHook do
99
def on_mount(:default, _params, _session, socket) do
1010
if connected?(socket) do
1111
Livebook.Hubs.Broadcasts.subscribe([:crud, :connection])
12+
LivebookWeb.SessionHelpers.subscribe()
1213
Phoenix.PubSub.subscribe(Livebook.PubSub, "sidebar")
1314
end
1415

@@ -67,18 +68,7 @@ defmodule LivebookWeb.SidebarHook do
6768
end
6869

6970
defp handle_event("logout", _params, socket) do
70-
on_confirm = fn socket ->
71-
Phoenix.PubSub.broadcast(Livebook.PubSub, "sidebar", :logout)
72-
put_flash(socket, :info, "Livebook is logging out. You will be redirected soon.")
73-
end
74-
75-
{:halt,
76-
confirm(socket, on_confirm,
77-
title: "Log out",
78-
description: "Are you sure you want to log out Livebook now?",
79-
confirm_text: "Log out",
80-
confirm_icon: "logout-box-line"
81-
)}
71+
{:halt, LivebookWeb.SessionHelpers.confirm_logout(socket)}
8272
end
8373

8474
defp handle_event(_event, _params, socket), do: {:cont, socket}

0 commit comments

Comments
 (0)