livebook/lib/livebook_web/live/session_helpers.ex
Jonatan Kłosko bd8e06b5ce
Implement relative navigation between notebooks (#441)
* Use live redirect for local links in rendered markdown

* Resolve relative notebook URLs

* Bump LV

* Adds tests

* Handle nested relative path

* Handle child nested paths
2021-07-08 19:35:11 +02:00

46 lines
1.4 KiB
Elixir

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.
Accepts the same options as `Livebook.SessionSupervisor.create_session/1`.
"""
@spec create_session(Phoenix.LiveView.Socket.t(), keyword()) :: Phoenix.LiveView.Socket.t()
def create_session(socket, opts \\ []) do
case Livebook.SessionSupervisor.create_session(opts) do
{:ok, id} ->
push_redirect(socket, to: Routes.session_path(socket, :page, id))
{:error, reason} ->
put_flash(socket, :error, "Failed to create session: #{reason}")
end
end
@doc """
Formats the given list of notebook import messages and puts
into the info flash.
"""
@spec put_import_flash_messages(Phoenix.LiveView.Socket.t(), list(String.t())) ::
Phoenix.LiveView.Socket.t()
def put_import_flash_messages(socket, messages)
def put_import_flash_messages(socket, []), do: socket
def put_import_flash_messages(socket, messages) do
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
])
put_flash(socket, :info, flash)
end
end