Allow autosave to be reenable in the UI, closes #2534

This commit is contained in:
José Valim 2024-04-01 10:25:33 +02:00
parent 4d61937c2d
commit c699fbbd79
4 changed files with 52 additions and 10 deletions

View file

@ -18,6 +18,12 @@ defmodule LivebookWeb.OpenLive do
sessions = Sessions.list_sessions() |> Enum.filter(&(&1.mode == :default))
recent_notebooks = Livebook.NotebookManager.recent_notebooks()
show_autosave_note? =
case Livebook.Settings.autosave_path() do
nil -> false
path -> match?({:ok, [_ | _]}, File.ls(path))
end
{:ok,
assign(socket,
tab: "file",
@ -25,7 +31,8 @@ defmodule LivebookWeb.OpenLive do
url: params["url"],
sessions: sessions,
recent_notebooks: recent_notebooks,
page_title: "Open - Livebook"
page_title: "Open - Livebook",
show_autosave_note?: show_autosave_note?
)}
end
@ -130,7 +137,8 @@ defmodule LivebookWeb.OpenLive do
</:card_icon>
</.live_component>
<% end %>
<div class="mt-3 text-gray-600 text-sm">
<div :if={@show_autosave_note?} class="mt-3 text-gray-600 text-sm">
Looking for unsaved notebooks? <.link
class="font-semibold"
navigate={~p"/open/storage?autosave=true"}

View file

@ -202,7 +202,15 @@ defmodule LivebookWeb.SettingsLive do
"""
end
defp autosave_path_select(%{state: %{file: nil}} = assigns), do: ~H""
defp autosave_path_select(%{state: %{file: nil}} = assigns) do
~H"""
<div>
<.button color="gray" id="enable-autosave" phx-click="open_autosave_path_select">
Enable
</.button>
</div>
"""
end
defp autosave_path_select(%{state: %{dialog_opened?: true}} = assigns) do
~H"""
@ -217,13 +225,14 @@ defmodule LivebookWeb.SettingsLive do
file_system_select_disabled={true}
target={self()}
>
<.button color="gray" phx-click="cancel_autosave_path" tabindex="-1">
<.button id="cancel-autosave" color="gray" phx-click="cancel_autosave_path" tabindex="-1">
Cancel
</.button>
<.button color="gray" phx-click="reset_autosave_path" tabindex="-1">
<.button id="reset-autosave" color="gray" phx-click="reset_autosave_path" tabindex="-1">
Reset
</.button>
<.button
id="save-autosave"
phx-click="set_autosave_path"
disabled={not Livebook.FileSystem.File.dir?(@state.file)}
tabindex="-1"
@ -241,7 +250,7 @@ defmodule LivebookWeb.SettingsLive do
<div class="grow">
<.text_field name={nil} readonly value={@state.file.path} />
</div>
<.button color="gray" phx-click="open_autosave_path_select">
<.button color="gray" id="change-autosave" phx-click="open_autosave_path_select">
Change
</.button>
</div>
@ -290,7 +299,12 @@ defmodule LivebookWeb.SettingsLive do
end
def handle_event("open_autosave_path_select", %{}, socket) do
{:noreply, update(socket, :autosave_path_state, &%{&1 | dialog_opened?: true})}
{:noreply,
update(
socket,
:autosave_path_state,
&%{&1 | dialog_opened?: true, file: &1.file || default_autosave_dir()}
)}
end
def handle_event("save", %{"update_check_enabled" => enabled}, socket) do

View file

@ -6,6 +6,11 @@ defmodule LivebookWeb.OpenLiveTest do
alias Livebook.{Sessions, Session, FileSystem}
describe "file selection" do
test "does not mention autosaving if disabled", %{conn: conn} do
refute conn |> get(~p"/open/storage") |> html_response(200) =~
"Looking for unsaved notebooks?"
end
test "updates the list of files as the input changes", %{conn: conn} do
{:ok, view, _} = live(conn, ~p"/open/storage")

View file

@ -10,9 +10,7 @@ defmodule LivebookWeb.SettingsLiveTest do
describe "environment variables configuration" do
test "list persisted environment variables", %{conn: conn} do
insert_env_var(:env_var, name: "MY_ENVIRONMENT_VAR")
{:ok, _view, html} = live(conn, ~p"/settings")
assert html =~ "MY_ENVIRONMENT_VAR"
assert conn |> get(~p"/settings") |> html_response(200) =~ "MY_ENVIRONMENT_VAR"
end
test "adds an environment variable", %{conn: conn} do
@ -87,4 +85,21 @@ defmodule LivebookWeb.SettingsLiveTest do
|> render() =~ env_var.name
end
end
describe "autosaving" do
# Autosaving can only be disable by directly changing storage
# but, given some users in the past had autosaving disabled,
# we take that into account.
test "can be enabled", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/settings")
view
|> element("#enable-autosave")
|> render_click()
view
|> element("#cancel-autosave")
|> render_click()
end
end
end