mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-24 12:26:07 +08:00
Use default directory when using Persistence or AddFileEntry components (#2250)
This commit is contained in:
parent
e0cb0fb1c8
commit
a97df0e536
4 changed files with 40 additions and 3 deletions
|
|
@ -197,4 +197,19 @@ defmodule Livebook.Settings do
|
||||||
_ -> FileSystem.File.new(Livebook.Config.local_file_system())
|
_ -> FileSystem.File.new(Livebook.Config.local_file_system())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Gets default directory based on given hub.
|
||||||
|
"""
|
||||||
|
@spec default_dir(Livebook.Hubs.Provider.t()) :: FileSystem.File.t()
|
||||||
|
def default_dir(hub) do
|
||||||
|
file_systems = Livebook.Hubs.get_file_systems(hub)
|
||||||
|
file = default_dir()
|
||||||
|
|
||||||
|
if Enum.any?(file_systems, &(&1.id == file.file_system_id)) do
|
||||||
|
file
|
||||||
|
else
|
||||||
|
Livebook.Config.local_file_system_home()
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ defmodule LivebookWeb.SessionLive.AddFileEntryFileComponent do
|
||||||
def mount(socket) do
|
def mount(socket) do
|
||||||
{:ok,
|
{:ok,
|
||||||
assign(socket,
|
assign(socket,
|
||||||
file: Livebook.Config.local_file_system_home(),
|
|
||||||
file_info: %{exists: true},
|
file_info: %{exists: true},
|
||||||
changeset: changeset(),
|
changeset: changeset(),
|
||||||
error_message: nil,
|
error_message: nil,
|
||||||
|
|
@ -48,7 +47,10 @@ defmodule LivebookWeb.SessionLive.AddFileEntryFileComponent do
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(assigns, socket) do
|
def update(assigns, socket) do
|
||||||
{:ok, assign(socket, assigns)}
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> assign(assigns)
|
||||||
|
|> assign_new(:file, fn -> Livebook.Settings.default_dir(assigns.hub) end)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ defmodule LivebookWeb.SessionLive.PersistenceComponent do
|
||||||
case assigns.session.origin do
|
case assigns.session.origin do
|
||||||
# If it's a forked notebook, default to the same folder
|
# If it's a forked notebook, default to the same folder
|
||||||
{:file, file} -> FileSystem.File.containing_dir(file)
|
{:file, file} -> FileSystem.File.containing_dir(file)
|
||||||
_ -> Livebook.Config.local_file_system_home()
|
nil -> Livebook.Settings.default_dir(assigns.hub)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|> assign_new(:saved_file, fn -> file end)
|
|> assign_new(:saved_file, fn -> file end)
|
||||||
|
|
|
||||||
|
|
@ -911,6 +911,26 @@ defmodule LivebookWeb.SessionLiveTest do
|
||||||
|
|
||||||
assert %{notebook: %{persist_outputs: true}} = Session.get_data(session.pid)
|
assert %{notebook: %{persist_outputs: true}} = Session.get_data(session.pid)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@tag :tmp_dir
|
||||||
|
test "showing all files from default directory",
|
||||||
|
%{conn: conn, session: session, tmp_dir: tmp_dir} do
|
||||||
|
old_dir = Livebook.Settings.default_dir()
|
||||||
|
new_dir = Livebook.FileSystem.File.local(tmp_dir)
|
||||||
|
|
||||||
|
Livebook.Settings.set_default_dir(new_dir)
|
||||||
|
|
||||||
|
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/settings/file")
|
||||||
|
|
||||||
|
assert render(view) =~ new_dir.path
|
||||||
|
|
||||||
|
Livebook.Settings.set_default_dir(old_dir)
|
||||||
|
|
||||||
|
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/settings/file")
|
||||||
|
|
||||||
|
refute render(view) =~ new_dir.path
|
||||||
|
assert render(view) =~ old_dir.path
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "completion" do
|
describe "completion" do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue