2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.HomeLive do
|
|
|
|
use LivebookWeb, :live_view
|
2021-01-08 05:13:17 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
import LivebookWeb.UserHelpers
|
2021-06-03 03:51:43 +08:00
|
|
|
import LivebookWeb.SessionHelpers
|
2021-05-04 02:03:19 +08:00
|
|
|
|
2021-04-22 05:02:09 +08:00
|
|
|
alias Livebook.{SessionSupervisor, Session, LiveMarkdown, Notebook}
|
2021-02-21 23:54:44 +08:00
|
|
|
|
|
|
|
@impl true
|
2021-06-16 01:33:16 +08:00
|
|
|
def mount(_params, %{"current_user_id" => current_user_id} = session, socket) do
|
2021-02-21 23:54:44 +08:00
|
|
|
if connected?(socket) do
|
2021-03-04 05:56:28 +08:00
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions")
|
2021-05-04 02:03:19 +08:00
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "users:#{current_user_id}")
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
2021-06-16 01:33:16 +08:00
|
|
|
current_user = build_current_user(session, socket)
|
2021-02-21 23:54:44 +08:00
|
|
|
session_summaries = sort_session_summaries(SessionSupervisor.get_session_summaries())
|
2021-06-03 03:51:43 +08:00
|
|
|
notebook_infos = Notebook.Explore.notebook_infos() |> Enum.take(3)
|
2021-02-21 23:54:44 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
{:ok,
|
|
|
|
assign(socket,
|
|
|
|
current_user: current_user,
|
|
|
|
path: default_path(),
|
2021-06-03 03:51:43 +08:00
|
|
|
session_summaries: session_summaries,
|
|
|
|
notebook_infos: notebook_infos
|
2021-05-04 02:03:19 +08:00
|
|
|
)}
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
2021-01-08 05:13:17 +08:00
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~L"""
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="flex flex-grow h-full">
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_component LivebookWeb.SidebarComponent,
|
2021-06-03 03:51:43 +08:00
|
|
|
id: :sidebar,
|
|
|
|
items: [
|
|
|
|
%{type: :break},
|
|
|
|
%{type: :user, current_user: @current_user, path: Routes.home_path(@socket, :user)}
|
|
|
|
] %>
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="flex-grow px-6 py-8 overflow-y-auto">
|
2021-06-03 03:51:43 +08:00
|
|
|
<div class="max-w-screen-lg w-full mx-auto px-4 pb-8 space-y-4">
|
|
|
|
<div class="flex flex-col space-y-2 items-center sm:flex-row sm:space-y-0 sm:justify-between sm:pb-4 pb-8 border-b border-gray-200">
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="text-2xl text-gray-800 font-semibold">
|
2021-06-03 03:51:43 +08:00
|
|
|
<img src="/images/logo-with-text.png" class="h-[50px]" alt="Livebook" />
|
2021-03-20 21:10:15 +08:00
|
|
|
</div>
|
2021-04-04 18:42:46 +08:00
|
|
|
<div class="flex space-x-2 pt-2">
|
2021-04-23 23:40:13 +08:00
|
|
|
<%= live_patch to: Routes.home_path(@socket, :import, "url"),
|
|
|
|
class: "button button-outlined-gray whitespace-nowrap" do %>
|
|
|
|
Import
|
|
|
|
<% end %>
|
2021-03-31 03:42:02 +08:00
|
|
|
<button class="button button-blue"
|
|
|
|
phx-click="new">
|
|
|
|
New notebook
|
|
|
|
</button>
|
|
|
|
</div>
|
2021-03-20 21:10:15 +08:00
|
|
|
</div>
|
2021-06-03 03:51:43 +08:00
|
|
|
<div class="h-80">
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_component LivebookWeb.PathSelectComponent,
|
2021-03-20 21:10:15 +08:00
|
|
|
id: "path_select",
|
|
|
|
path: @path,
|
|
|
|
extnames: [LiveMarkdown.extension()],
|
|
|
|
running_paths: paths(@session_summaries),
|
2021-04-13 04:59:48 +08:00
|
|
|
phx_target: nil,
|
|
|
|
phx_submit: nil do %>
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="flex justify-end space-x-2">
|
|
|
|
<%= content_tag :button,
|
2021-04-21 18:43:13 +08:00
|
|
|
class: "button button-outlined-gray whitespace-nowrap",
|
2021-03-20 21:10:15 +08:00
|
|
|
phx_click: "fork",
|
|
|
|
disabled: not path_forkable?(@path) do %>
|
|
|
|
<%= remix_icon("git-branch-line", class: "align-middle mr-1") %>
|
|
|
|
<span>Fork</span>
|
|
|
|
<% end %>
|
|
|
|
<%= if path_running?(@path, @session_summaries) do %>
|
2021-04-23 23:40:13 +08:00
|
|
|
<%= live_redirect "Join session", to: Routes.session_path(@socket, :page, session_id_by_path(@path, @session_summaries)),
|
2021-03-26 00:39:18 +08:00
|
|
|
class: "button button-blue" %>
|
2021-03-20 21:10:15 +08:00
|
|
|
<% else %>
|
2021-04-14 22:27:35 +08:00
|
|
|
<%= tag :span, if(File.regular?(@path) and not file_writable?(@path),
|
|
|
|
do: [class: "tooltip top", aria_label: "This file is write-protected, please fork instead"],
|
|
|
|
else: []
|
|
|
|
) %>
|
|
|
|
<%= content_tag :button, "Open",
|
|
|
|
class: "button button-blue",
|
|
|
|
phx_click: "open",
|
|
|
|
disabled: not path_openable?(@path, @session_summaries) %>
|
|
|
|
</span>
|
2021-03-20 21:10:15 +08:00
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
2021-06-03 03:51:43 +08:00
|
|
|
<div class="py-12">
|
|
|
|
<div class="mb-4 flex justify-between items-center">
|
|
|
|
<h2 class="text-xl font-semibold text-gray-800">
|
|
|
|
Explore
|
|
|
|
</h2>
|
|
|
|
<%= live_redirect to: Routes.explore_path(@socket, :page),
|
|
|
|
class: "flex items-center text-blue-600" do %>
|
|
|
|
<span class="font-semibold">See all</span>
|
|
|
|
<%= remix_icon("arrow-right-line", class: "align-middle ml-1") %>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
|
|
|
|
<%= for {info, idx} <- Enum.with_index(@notebook_infos) do %>
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_component LivebookWeb.NotebookCardComponent,
|
2021-06-03 03:51:43 +08:00
|
|
|
id: "notebook-card-#{idx}",
|
|
|
|
notebook_info: info %>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="py-12">
|
|
|
|
<h2 class="mb-4 text-xl font-semibold text-gray-800">
|
2021-03-26 02:04:49 +08:00
|
|
|
Running sessions
|
2021-06-03 03:51:43 +08:00
|
|
|
</h2>
|
2021-03-20 21:10:15 +08:00
|
|
|
<%= if @session_summaries == [] do %>
|
|
|
|
<div class="p-5 flex space-x-4 items-center border border-gray-200 rounded-lg">
|
|
|
|
<div>
|
|
|
|
<%= remix_icon("windy-line", class: "text-gray-400 text-xl") %>
|
|
|
|
</div>
|
|
|
|
<div class="text-gray-600">
|
|
|
|
You do not have any running sessions.
|
|
|
|
<br>
|
2021-03-26 02:04:49 +08:00
|
|
|
Please create a new one by clicking <span class="font-semibold">“New notebook”</span>
|
2021-03-20 21:10:15 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<% else %>
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_component LivebookWeb.HomeLive.SessionsComponent,
|
2021-03-20 21:10:15 +08:00
|
|
|
id: "sessions_list",
|
|
|
|
session_summaries: @session_summaries %>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-08 05:13:17 +08:00
|
|
|
</div>
|
2021-04-02 20:00:49 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
<%= if @live_action == :user do %>
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_modal LivebookWeb.UserComponent,
|
2021-05-04 02:03:19 +08:00
|
|
|
id: :user_modal,
|
|
|
|
modal_class: "w-full max-w-sm",
|
|
|
|
user: @current_user,
|
|
|
|
return_to: Routes.home_path(@socket, :page) %>
|
|
|
|
<% end %>
|
|
|
|
|
2021-04-13 05:24:26 +08:00
|
|
|
<%= if @live_action == :close_session do %>
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_modal LivebookWeb.HomeLive.CloseSessionComponent,
|
2021-04-13 05:24:26 +08:00
|
|
|
id: :close_session_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-xl",
|
2021-04-02 20:00:49 +08:00
|
|
|
return_to: Routes.home_path(@socket, :page),
|
|
|
|
session_summary: @session_summary %>
|
|
|
|
<% end %>
|
2021-04-23 23:40:13 +08:00
|
|
|
|
|
|
|
<%= if @live_action == :import do %>
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_modal LivebookWeb.HomeLive.ImportComponent,
|
2021-04-23 23:40:13 +08:00
|
|
|
id: :import_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-xl",
|
2021-04-23 23:40:13 +08:00
|
|
|
return_to: Routes.home_path(@socket, :page),
|
|
|
|
tab: @tab %>
|
|
|
|
<% end %>
|
2021-01-08 05:13:17 +08:00
|
|
|
"""
|
|
|
|
end
|
2021-02-21 23:54:44 +08:00
|
|
|
|
2021-03-20 21:10:15 +08:00
|
|
|
@impl true
|
2021-03-26 00:39:18 +08:00
|
|
|
def handle_params(%{"session_id" => session_id}, _url, socket) do
|
|
|
|
session_summary = Enum.find(socket.assigns.session_summaries, &(&1.session_id == session_id))
|
|
|
|
{:noreply, assign(socket, session_summary: session_summary)}
|
|
|
|
end
|
|
|
|
|
2021-04-23 23:40:13 +08:00
|
|
|
def handle_params(%{"tab" => tab}, _url, socket) do
|
|
|
|
{:noreply, assign(socket, tab: tab)}
|
|
|
|
end
|
|
|
|
|
2021-03-20 21:10:15 +08:00
|
|
|
def handle_params(_params, _url, socket), do: {:noreply, socket}
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
@impl true
|
|
|
|
def handle_event("set_path", %{"path" => path}, socket) do
|
|
|
|
{:noreply, assign(socket, path: path)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("new", %{}, socket) do
|
2021-06-03 03:51:43 +08:00
|
|
|
{:noreply, create_session(socket)}
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("fork", %{}, socket) do
|
|
|
|
{notebook, messages} = import_notebook(socket.assigns.path)
|
|
|
|
socket = put_import_flash_messages(socket, messages)
|
2021-04-22 05:02:09 +08:00
|
|
|
notebook = Notebook.forked(notebook)
|
2021-04-05 00:55:51 +08:00
|
|
|
images_dir = Session.images_dir_for_notebook(socket.assigns.path)
|
2021-06-03 03:51:43 +08:00
|
|
|
{:noreply, create_session(socket, notebook: notebook, copy_images_from: images_dir)}
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("open", %{}, socket) do
|
|
|
|
{notebook, messages} = import_notebook(socket.assigns.path)
|
|
|
|
socket = put_import_flash_messages(socket, messages)
|
2021-06-03 03:51:43 +08:00
|
|
|
{:noreply, create_session(socket, notebook: notebook, path: socket.assigns.path)}
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
2021-03-26 02:04:49 +08:00
|
|
|
def handle_event("fork_session", %{"id" => session_id}, socket) do
|
|
|
|
data = Session.get_data(session_id)
|
2021-04-22 05:02:09 +08:00
|
|
|
notebook = Notebook.forked(data.notebook)
|
2021-04-05 00:55:51 +08:00
|
|
|
%{images_dir: images_dir} = Session.get_summary(session_id)
|
2021-06-03 03:51:43 +08:00
|
|
|
{:noreply, create_session(socket, notebook: notebook, copy_images_from: images_dir)}
|
2021-03-26 02:04:49 +08:00
|
|
|
end
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
@impl true
|
|
|
|
def handle_info({:session_created, id}, socket) do
|
|
|
|
summary = Session.get_summary(id)
|
|
|
|
session_summaries = sort_session_summaries([summary | socket.assigns.session_summaries])
|
|
|
|
{:noreply, assign(socket, session_summaries: session_summaries)}
|
|
|
|
end
|
|
|
|
|
2021-04-13 05:24:26 +08:00
|
|
|
def handle_info({:session_closed, id}, socket) do
|
2021-02-21 23:54:44 +08:00
|
|
|
session_summaries = Enum.reject(socket.assigns.session_summaries, &(&1.session_id == id))
|
|
|
|
{:noreply, assign(socket, session_summaries: session_summaries)}
|
|
|
|
end
|
|
|
|
|
2021-04-23 23:40:13 +08:00
|
|
|
def handle_info({:import_content, content}, socket) do
|
|
|
|
{notebook, messages} = Livebook.LiveMarkdown.Import.notebook_from_markdown(content)
|
|
|
|
socket = put_import_flash_messages(socket, messages)
|
2021-06-03 03:51:43 +08:00
|
|
|
{:noreply, create_session(socket, notebook: notebook)}
|
2021-04-23 23:40:13 +08:00
|
|
|
end
|
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
def handle_info(
|
|
|
|
{:user_change, %{id: id} = user},
|
|
|
|
%{assigns: %{current_user: %{id: id}}} = socket
|
|
|
|
) do
|
|
|
|
{:noreply, assign(socket, :current_user, user)}
|
|
|
|
end
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
def handle_info(_message, socket), do: {:noreply, socket}
|
|
|
|
|
2021-04-15 20:51:27 +08:00
|
|
|
defp default_path(), do: Livebook.Config.root_path() <> "/"
|
2021-02-21 23:54:44 +08:00
|
|
|
|
|
|
|
defp sort_session_summaries(session_summaries) do
|
|
|
|
Enum.sort_by(session_summaries, & &1.notebook_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp paths(session_summaries) do
|
|
|
|
Enum.map(session_summaries, & &1.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp path_forkable?(path) do
|
|
|
|
File.regular?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp path_openable?(path, session_summaries) do
|
2021-04-14 22:27:35 +08:00
|
|
|
File.regular?(path) and not path_running?(path, session_summaries) and file_writable?(path)
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp path_running?(path, session_summaries) do
|
|
|
|
running_paths = paths(session_summaries)
|
|
|
|
path in running_paths
|
|
|
|
end
|
|
|
|
|
2021-04-14 22:27:35 +08:00
|
|
|
defp file_writable?(path) do
|
|
|
|
case File.stat(path) do
|
|
|
|
{:ok, stat} -> stat.access in [:read_write, :write]
|
|
|
|
{:error, _} -> false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
defp import_notebook(path) do
|
|
|
|
content = File.read!(path)
|
|
|
|
LiveMarkdown.Import.notebook_from_markdown(content)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp put_import_flash_messages(socket, []), do: socket
|
|
|
|
|
|
|
|
defp 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
|
|
|
|
|
|
|
|
defp session_id_by_path(path, session_summaries) do
|
|
|
|
summary = Enum.find(session_summaries, &(&1.path == path))
|
|
|
|
summary.session_id
|
|
|
|
end
|
2021-01-08 05:13:17 +08:00
|
|
|
end
|