Allow a file system to be set as default in the UI (#1450)

This commit is contained in:
GitStart 2022-10-13 16:16:52 +03:00 committed by GitHub
parent 2554d08ed0
commit 577a1b7eb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 8 deletions

View file

@ -89,7 +89,15 @@ defmodule Livebook.Settings do
"""
@spec remove_file_system(file_system_id()) :: :ok
def remove_file_system(filesystem_id) do
Storage.delete(:filesystem, filesystem_id)
if default_file_system_id() == filesystem_id do
storage().delete_key(:settings, "global", :default_file_system_id)
end
storage().delete(:filesystem, filesystem_id)
end
defp storage() do
Livebook.Storage.current()
end
defp storage_to_fs(%{type: "s3"} = config) do
@ -225,4 +233,37 @@ defmodule Livebook.Settings do
defp broadcast_env_vars_change(message) do
Phoenix.PubSub.broadcast(Livebook.PubSub, "settings", message)
end
@doc """
Sets default file system.
"""
@spec set_default_file_system(file_system_id()) :: :ok
def set_default_file_system(file_system_id) do
storage().insert(:settings, "global", default_file_system_id: file_system_id)
end
@doc """
Settings default file system
"""
def default_file_system do
case storage().fetch(:filesystem, default_file_system_id()) do
{:ok, file} -> storage_to_fs(file)
:error -> Livebook.Config.local_filesystem()
end
end
@doc """
Get default file system id
"""
def default_file_system_id do
case storage().fetch_key(:settings, "global", :default_file_system_id) do
{:ok, default_file_system_id} -> default_file_system_id
:error -> "local"
end
end
@doc """
Settings default file system home
"""
def default_file_system_home, do: FileSystem.File.new(default_file_system())
end

View file

@ -462,7 +462,7 @@ defmodule LivebookWeb.HomeLive do
end
end
defp determine_file(_params), do: Livebook.Config.local_filesystem_home()
defp determine_file(_params), do: Livebook.Settings.default_file_system_home()
defp open_notebook(socket, file) do
case import_notebook(file) do

View file

@ -45,7 +45,6 @@ defmodule LivebookWeb.SessionLive.PersistenceLive do
<h3 class="text-2xl font-semibold text-gray-800">
Save to file
</h3>
<div class="w-full flex-col space-y-6">
<div class="h-full h-52">
<.live_component
@ -57,7 +56,6 @@ defmodule LivebookWeb.SessionLive.PersistenceLive do
submit_event={:confirm_file}
/>
</div>
<form
phx-change="set_options"
onsubmit="return false;"
@ -84,7 +82,6 @@ defmodule LivebookWeb.SessionLive.PersistenceLive do
/>
</div>
</div>
<span class="text-gray-700 whitespace-nowrap pt-2">
File: <%= normalize_file(@draft_file).path %>
</span>
@ -99,13 +96,11 @@ defmodule LivebookWeb.SessionLive.PersistenceLive do
>
Save
</button>
<%= live_patch("Cancel",
to: Routes.session_path(@socket, :page, @session.id),
class: "button-base button-outlined-gray"
) %>
</div>
<%= if @saved_file do %>
<button class="button-base button-outlined-red" phx-click="stop_saving">
Stop saving to file

View file

@ -21,7 +21,8 @@ defmodule LivebookWeb.SettingsLive do
dialog_opened?: false
},
update_check_enabled: Livebook.UpdateCheck.enabled?(),
page_title: "Livebook - Settings"
page_title: "Livebook - Settings",
default_file_system_id: Livebook.Settings.default_file_system_id()
)}
end
@ -116,6 +117,7 @@ defmodule LivebookWeb.SettingsLive do
<LivebookWeb.SettingsLive.FileSystemsComponent.render
file_systems={@file_systems}
socket={@socket}
default_file_system_id={@default_file_system_id}
/>
</div>
<!-- Environment variables configuration -->
@ -314,10 +316,16 @@ defmodule LivebookWeb.SettingsLive do
def handle_event("detach_file_system", %{"id" => file_system_id}, socket) do
Livebook.Settings.remove_file_system(file_system_id)
file_systems = Livebook.Settings.file_systems()
{:noreply, assign(socket, file_systems: file_systems)}
end
def handle_event("make_default_file_system", %{"id" => file_system_id}, socket) do
Livebook.Settings.set_default_file_system(file_system_id)
{:noreply, assign(socket, default_file_system_id: file_system_id)}
end
def handle_event("save", %{"update_check_enabled" => enabled}, socket) do
enabled = enabled == "true"
Livebook.UpdateCheck.set_enabled(enabled)

View file

@ -13,6 +13,7 @@ defmodule LivebookWeb.SettingsLive.FileSystemsComponent do
<div class="flex items-center space-x-12">
<.file_system_info file_system={file_system} />
</div>
<%= unless is_struct(file_system, FileSystem.Local) do %>
<button
class="button-base button-outlined-red"
@ -31,6 +32,18 @@ defmodule LivebookWeb.SettingsLive.FileSystemsComponent do
</button>
<% end %>
</div>
<div class="flex justify-end">
<%= unless @default_file_system_id == file_system_id do %>
<button
type="submit"
phx-click="make_default_file_system"
class="button-base button-outlined-gray"
phx-value-id={file_system_id}
>
Make default
</button>
<% end %>
</div>
<% end %>
</div>
<div class="flex">