2021-06-03 03:51:43 +08:00
|
|
|
defmodule LivebookWeb.SessionHelpers do
|
|
|
|
import Phoenix.LiveView
|
|
|
|
alias LivebookWeb.Router.Helpers, as: Routes
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a new session, redirects on success,
|
|
|
|
puts an error flash message on failure.
|
|
|
|
|
2021-09-05 01:16:01 +08:00
|
|
|
Accepts the same options as `Livebook.Sessions.create_session/1`.
|
2021-06-03 03:51:43 +08:00
|
|
|
"""
|
|
|
|
@spec create_session(Phoenix.LiveView.Socket.t(), keyword()) :: Phoenix.LiveView.Socket.t()
|
|
|
|
def create_session(socket, opts \\ []) do
|
2021-08-14 03:17:43 +08:00
|
|
|
# Revert persistence options to default values if there is
|
|
|
|
# no file attached to the new session
|
|
|
|
opts =
|
|
|
|
if opts[:notebook] != nil and opts[:file] == nil do
|
|
|
|
Keyword.update!(opts, :notebook, &Livebook.Notebook.reset_persistence_options/1)
|
|
|
|
else
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
2021-09-05 01:16:01 +08:00
|
|
|
case Livebook.Sessions.create_session(opts) do
|
|
|
|
{:ok, session} ->
|
|
|
|
push_redirect(socket, to: Routes.session_path(socket, :page, session.id))
|
2021-06-03 03:51:43 +08:00
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
put_flash(socket, :error, "Failed to create session: #{reason}")
|
|
|
|
end
|
|
|
|
end
|
2021-07-09 01:35:11 +08:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Formats the given list of notebook import messages and puts
|
2021-10-22 05:21:54 +08:00
|
|
|
into the warning flash.
|
2021-07-09 01:35:11 +08:00
|
|
|
"""
|
2021-10-22 05:21:54 +08:00
|
|
|
@spec put_import_warnings(Phoenix.LiveView.Socket.t(), list(String.t())) ::
|
2021-07-09 01:35:11 +08:00
|
|
|
Phoenix.LiveView.Socket.t()
|
2021-10-22 05:21:54 +08:00
|
|
|
def put_import_warnings(socket, messages)
|
2021-07-09 01:35:11 +08:00
|
|
|
|
2021-10-22 05:21:54 +08:00
|
|
|
def put_import_warnings(socket, []), do: socket
|
2021-07-09 01:35:11 +08:00
|
|
|
|
2021-10-22 05:21:54 +08:00
|
|
|
def put_import_warnings(socket, messages) do
|
2021-07-09 01:35:11 +08:00
|
|
|
list =
|
|
|
|
messages
|
|
|
|
|> Enum.map(fn message -> ["- ", message] end)
|
|
|
|
|> Enum.intersperse("\n")
|
|
|
|
|
|
|
|
flash =
|
|
|
|
IO.iodata_to_binary([
|
|
|
|
"We found problems while importing the file and tried to autofix them:\n" | list
|
|
|
|
])
|
|
|
|
|
2021-10-22 05:21:54 +08:00
|
|
|
put_flash(socket, :warning, flash)
|
2021-07-09 01:35:11 +08:00
|
|
|
end
|
2022-01-22 06:24:47 +08:00
|
|
|
|
|
|
|
def uses_memory?(%{runtime: %{total: total}}) when total > 0, do: true
|
|
|
|
def uses_memory?(_), do: false
|
2021-06-03 03:51:43 +08:00
|
|
|
end
|