mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-01 12:41:43 +08:00
Allow autosave to be reenable in the UI, closes #2534
This commit is contained in:
parent
4d61937c2d
commit
c699fbbd79
4 changed files with 52 additions and 10 deletions
|
@ -18,6 +18,12 @@ defmodule LivebookWeb.OpenLive do
|
||||||
sessions = Sessions.list_sessions() |> Enum.filter(&(&1.mode == :default))
|
sessions = Sessions.list_sessions() |> Enum.filter(&(&1.mode == :default))
|
||||||
recent_notebooks = Livebook.NotebookManager.recent_notebooks()
|
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,
|
{:ok,
|
||||||
assign(socket,
|
assign(socket,
|
||||||
tab: "file",
|
tab: "file",
|
||||||
|
@ -25,7 +31,8 @@ defmodule LivebookWeb.OpenLive do
|
||||||
url: params["url"],
|
url: params["url"],
|
||||||
sessions: sessions,
|
sessions: sessions,
|
||||||
recent_notebooks: recent_notebooks,
|
recent_notebooks: recent_notebooks,
|
||||||
page_title: "Open - Livebook"
|
page_title: "Open - Livebook",
|
||||||
|
show_autosave_note?: show_autosave_note?
|
||||||
)}
|
)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -130,7 +137,8 @@ defmodule LivebookWeb.OpenLive do
|
||||||
</:card_icon>
|
</:card_icon>
|
||||||
</.live_component>
|
</.live_component>
|
||||||
<% end %>
|
<% 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
|
Looking for unsaved notebooks? <.link
|
||||||
class="font-semibold"
|
class="font-semibold"
|
||||||
navigate={~p"/open/storage?autosave=true"}
|
navigate={~p"/open/storage?autosave=true"}
|
||||||
|
|
|
@ -202,7 +202,15 @@ defmodule LivebookWeb.SettingsLive do
|
||||||
"""
|
"""
|
||||||
end
|
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
|
defp autosave_path_select(%{state: %{dialog_opened?: true}} = assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
|
@ -217,13 +225,14 @@ defmodule LivebookWeb.SettingsLive do
|
||||||
file_system_select_disabled={true}
|
file_system_select_disabled={true}
|
||||||
target={self()}
|
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
|
Cancel
|
||||||
</.button>
|
</.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
|
Reset
|
||||||
</.button>
|
</.button>
|
||||||
<.button
|
<.button
|
||||||
|
id="save-autosave"
|
||||||
phx-click="set_autosave_path"
|
phx-click="set_autosave_path"
|
||||||
disabled={not Livebook.FileSystem.File.dir?(@state.file)}
|
disabled={not Livebook.FileSystem.File.dir?(@state.file)}
|
||||||
tabindex="-1"
|
tabindex="-1"
|
||||||
|
@ -241,7 +250,7 @@ defmodule LivebookWeb.SettingsLive do
|
||||||
<div class="grow">
|
<div class="grow">
|
||||||
<.text_field name={nil} readonly value={@state.file.path} />
|
<.text_field name={nil} readonly value={@state.file.path} />
|
||||||
</div>
|
</div>
|
||||||
<.button color="gray" phx-click="open_autosave_path_select">
|
<.button color="gray" id="change-autosave" phx-click="open_autosave_path_select">
|
||||||
Change
|
Change
|
||||||
</.button>
|
</.button>
|
||||||
</div>
|
</div>
|
||||||
|
@ -290,7 +299,12 @@ defmodule LivebookWeb.SettingsLive do
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("open_autosave_path_select", %{}, socket) do
|
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
|
end
|
||||||
|
|
||||||
def handle_event("save", %{"update_check_enabled" => enabled}, socket) do
|
def handle_event("save", %{"update_check_enabled" => enabled}, socket) do
|
||||||
|
|
|
@ -6,6 +6,11 @@ defmodule LivebookWeb.OpenLiveTest do
|
||||||
alias Livebook.{Sessions, Session, FileSystem}
|
alias Livebook.{Sessions, Session, FileSystem}
|
||||||
|
|
||||||
describe "file selection" do
|
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
|
test "updates the list of files as the input changes", %{conn: conn} do
|
||||||
{:ok, view, _} = live(conn, ~p"/open/storage")
|
{:ok, view, _} = live(conn, ~p"/open/storage")
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,7 @@ defmodule LivebookWeb.SettingsLiveTest do
|
||||||
describe "environment variables configuration" do
|
describe "environment variables configuration" do
|
||||||
test "list persisted environment variables", %{conn: conn} do
|
test "list persisted environment variables", %{conn: conn} do
|
||||||
insert_env_var(:env_var, name: "MY_ENVIRONMENT_VAR")
|
insert_env_var(:env_var, name: "MY_ENVIRONMENT_VAR")
|
||||||
{:ok, _view, html} = live(conn, ~p"/settings")
|
assert conn |> get(~p"/settings") |> html_response(200) =~ "MY_ENVIRONMENT_VAR"
|
||||||
|
|
||||||
assert html =~ "MY_ENVIRONMENT_VAR"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "adds an environment variable", %{conn: conn} do
|
test "adds an environment variable", %{conn: conn} do
|
||||||
|
@ -87,4 +85,21 @@ defmodule LivebookWeb.SettingsLiveTest do
|
||||||
|> render() =~ env_var.name
|
|> render() =~ env_var.name
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue