Skip to content

Commit

Permalink
Add shutdown button
Browse files Browse the repository at this point in the history
The button only appears on interactive mode because
we assume embedded mode is running inside a release
or similar.
  • Loading branch information
josevalim committed Jan 13, 2022
1 parent fc20b66 commit 95f143f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
12 changes: 4 additions & 8 deletions lib/livebook_web/live/explore_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ defmodule LivebookWeb.ExploreLive do
[lead_notebook_info | notebook_infos] = Explore.visible_notebook_infos()

{:ok,
assign(socket,
socket
|> SidebarHelpers.shared_home_handlers()
|> assign(
lead_notebook_info: lead_notebook_info,
notebook_infos: notebook_infos,
page_title: "Livebook - Explore"
Expand All @@ -25,13 +27,7 @@ defmodule LivebookWeb.ExploreLive do
<div class="flex grow h-full">
<SidebarHelpers.sidebar>
<SidebarHelpers.logo_item socket={@socket} />
<SidebarHelpers.break_item />
<SidebarHelpers.link_item
icon="settings-3-fill"
label="Settings"
path={Routes.settings_path(@socket, :page)}
active={false} />
<SidebarHelpers.user_item current_user={@current_user} path={Routes.explore_path(@socket, :user)} />
<SidebarHelpers.shared_home_footer socket={@socket} current_user={@current_user} />
</SidebarHelpers.sidebar>
<div class="grow px-6 py-8 overflow-y-auto">
<div class="max-w-screen-md w-full mx-auto px-4 pb-8 space-y-8">
Expand Down
12 changes: 4 additions & 8 deletions lib/livebook_web/live/home_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ defmodule LivebookWeb.HomeLive do
notebook_infos = Notebook.Explore.visible_notebook_infos() |> Enum.take(3)

{:ok,
assign(socket,
socket
|> SidebarHelpers.shared_home_handlers()
|> assign(
file: Livebook.Config.default_dir(),
file_info: %{exists: true, access: :read_write},
sessions: sessions,
Expand All @@ -31,13 +33,7 @@ defmodule LivebookWeb.HomeLive do
~H"""
<div class="flex grow h-full">
<SidebarHelpers.sidebar>
<SidebarHelpers.break_item />
<SidebarHelpers.link_item
icon="settings-3-fill"
label="Settings"
path={Routes.settings_path(@socket, :page)}
active={false} />
<SidebarHelpers.user_item current_user={@current_user} path={Routes.home_path(@socket, :user)} />
<SidebarHelpers.shared_home_footer socket={@socket} current_user={@current_user} />
</SidebarHelpers.sidebar>
<div class="grow px-6 py-8 overflow-y-auto">
<div class="max-w-screen-lg w-full mx-auto px-4 pb-8 space-y-4">
Expand Down
12 changes: 4 additions & 8 deletions lib/livebook_web/live/settings_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ defmodule LivebookWeb.SettingsLive do
file_systems_env = Livebook.Config.file_systems_as_env(file_systems)

{:ok,
assign(socket,
socket
|> SidebarHelpers.shared_home_handlers()
|> assign(
file_systems: file_systems,
file_systems_env: file_systems_env,
page_title: "Livebook - Settings"
Expand All @@ -24,13 +26,7 @@ defmodule LivebookWeb.SettingsLive do
<div class="flex grow h-full">
<SidebarHelpers.sidebar>
<SidebarHelpers.logo_item socket={@socket} />
<SidebarHelpers.break_item />
<SidebarHelpers.link_item
icon="settings-3-fill"
label="Settings"
path={Routes.settings_path(@socket, :page)}
active={false} />
<SidebarHelpers.user_item current_user={@current_user} path={Routes.settings_path(@socket, :user)} />
<SidebarHelpers.shared_home_footer socket={@socket} current_user={@current_user} />
</SidebarHelpers.sidebar>
<div class="grow px-6 py-8 overflow-y-auto">
<div class="max-w-screen-md w-full mx-auto px-4 pb-8 space-y-16">
Expand Down
54 changes: 51 additions & 3 deletions lib/livebook_web/live/sidebar_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule LivebookWeb.SidebarHelpers do
"""
def sidebar(assigns) do
~H"""
<nav class="w-16 flex flex-col items-center space-y-5 px-3 py-7 bg-gray-900">
<nav class="w-16 flex flex-col items-center space-y-4 px-3 py-7 bg-gray-900">
<%= render_slot(@inner_block) %>
</nav>
"""
Expand All @@ -23,7 +23,7 @@ defmodule LivebookWeb.SidebarHelpers do
def logo_item(assigns) do
~H"""
<span>
<%= live_patch to: Routes.home_path(@socket, :page), aria_label: "go to homepage" do %>
<%= live_patch to: Routes.home_path(@socket, :page), aria_label: "Go to homepage" do %>
<img src="/images/logo.png" height="40" width="40" alt="" />
<% end %>
</span>
Expand Down Expand Up @@ -54,6 +54,23 @@ defmodule LivebookWeb.SidebarHelpers do
"""
end

def shutdown_item(assigns) do
if :code.get_mode() == :interactive do
~H"""
<span class="tooltip right distant" data-tooltip="Shutdown">
<button class="text-2xl text-gray-400 hover:text-gray-50 focus:text-gray-50 rounded-xl h-10 w-10 flex items-center justify-center"
aria-label="Shutdown"
phx-click="shutdown">
<.remix_icon icon="shut-down-line" />
</button>
</span>
"""
else
~H"""
"""
end
end

def break_item(assigns) do
~H"""
<div class="grow"></div>
Expand All @@ -64,11 +81,42 @@ defmodule LivebookWeb.SidebarHelpers do
~H"""
<span class="tooltip right distant" data-tooltip="User profile">
<%= live_patch to: @path,
class: "text-gray-400 rounded-xl h-8 w-8 flex items-center justify-center",
class: "text-gray-400 rounded-xl h-8 w-8 flex items-center justify-center mt-2",
aria_label: "user profile" do %>
<.user_avatar user={@current_user} text_class="text-xs" />
<% end %>
</span>
"""
end

## Shared home functionality

@doc """
A footer shared across home, settings, explore, etc.
Note you must call `shared_home_handlers` on mount/3.
"""
def shared_home_footer(assigns) do
~H"""
<.break_item />
<.shutdown_item />
<.link_item
icon="settings-3-fill"
label="Settings"
path={Routes.settings_path(@socket, :page)}
active={false} />
<.user_item current_user={@current_user} path={Routes.home_path(@socket, :user)} />
"""
end

def shared_home_handlers(socket) do
attach_hook(socket, :shutdown, :handle_event, fn
"shutdown", _params, socket ->
System.stop()
{:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")}

_event, _params, socket ->
{:cont, socket}
end)
end
end

0 comments on commit 95f143f

Please sign in to comment.