2021-06-03 03:51:43 +08:00
|
|
|
defmodule LivebookWeb.ExploreLive do
|
|
|
|
use LivebookWeb, :live_view
|
|
|
|
|
|
|
|
import LivebookWeb.UserHelpers
|
|
|
|
import LivebookWeb.SessionHelpers
|
|
|
|
|
2021-08-14 03:17:43 +08:00
|
|
|
alias LivebookWeb.{SidebarHelpers, ExploreHelpers, PageHelpers}
|
2021-06-03 03:51:43 +08:00
|
|
|
alias Livebook.Notebook.Explore
|
|
|
|
|
|
|
|
@impl true
|
2021-06-16 01:33:16 +08:00
|
|
|
def mount(_params, %{"current_user_id" => current_user_id} = session, socket) do
|
2021-06-03 03:51:43 +08:00
|
|
|
if connected?(socket) do
|
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "users:#{current_user_id}")
|
|
|
|
end
|
|
|
|
|
2021-06-16 01:33:16 +08:00
|
|
|
current_user = build_current_user(session, socket)
|
2021-06-03 03:51:43 +08:00
|
|
|
|
|
|
|
[lead_notebook_info | notebook_infos] = Explore.notebook_infos()
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
assign(socket,
|
|
|
|
current_user: current_user,
|
|
|
|
lead_notebook_info: lead_notebook_info,
|
|
|
|
notebook_infos: notebook_infos
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2021-06-03 03:51:43 +08:00
|
|
|
<div class="flex flex-grow h-full">
|
2021-07-07 20:32:49 +08:00
|
|
|
<SidebarHelpers.sidebar>
|
|
|
|
<SidebarHelpers.logo_item socket={@socket} />
|
|
|
|
<SidebarHelpers.break_item />
|
2021-08-14 03:17:43 +08:00
|
|
|
<SidebarHelpers.link_item
|
|
|
|
icon="settings-3-fill"
|
|
|
|
label="Settings"
|
|
|
|
path={Routes.settings_path(@socket, :page)}
|
|
|
|
active={false} />
|
2021-07-07 20:32:49 +08:00
|
|
|
<SidebarHelpers.user_item current_user={@current_user} path={Routes.explore_path(@socket, :user)} />
|
|
|
|
</SidebarHelpers.sidebar>
|
2021-06-03 03:51:43 +08:00
|
|
|
<div class="flex-grow px-6 py-8 overflow-y-auto">
|
|
|
|
<div class="max-w-screen-md w-full mx-auto px-4 pb-8 space-y-8">
|
|
|
|
<div>
|
2021-08-14 03:17:43 +08:00
|
|
|
<PageHelpers.title text="Explore" socket={@socket} />
|
2021-06-03 03:51:43 +08:00
|
|
|
<p class="mt-4 text-gray-700">
|
|
|
|
Check out a number of examples showcasing various parts of the Elixir ecosystem.
|
|
|
|
Click on any notebook you like and start playing around with it!
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="p-8 bg-gray-900 rounded-2xl flex space-x-4 shadow-xl">
|
|
|
|
<div class="self-end max-w-sm">
|
|
|
|
<h3 class="text-xl text-gray-50 font-semibold">
|
|
|
|
<%= @lead_notebook_info.title %>
|
|
|
|
</h3>
|
|
|
|
<p class="mt-2 text-sm text-gray-300">
|
|
|
|
<%= @lead_notebook_info.description %>
|
|
|
|
</p>
|
|
|
|
<div class="mt-4">
|
2021-07-07 20:32:49 +08:00
|
|
|
<%= live_patch "Let's go",
|
|
|
|
to: Routes.explore_path(@socket, :notebook, @lead_notebook_info.slug),
|
2021-06-03 03:51:43 +08:00
|
|
|
class: "button button-blue" %>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="flex-grow hidden md:flex flex items-center justify-center">
|
2021-07-07 20:32:49 +08:00
|
|
|
<img src={@lead_notebook_info.image_url} height="120" width="120" alt="livebook" />
|
2021-06-03 03:51:43 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
2021-07-07 20:32:49 +08:00
|
|
|
<%# Note: it's fine to use stateless components in this comprehension,
|
|
|
|
because @notebook_infos never change %>
|
|
|
|
<%= for info <- @notebook_infos do %>
|
|
|
|
<ExploreHelpers.notebook_card notebook_info={info} socket={@socket} />
|
2021-06-03 03:51:43 +08:00
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<%= if @live_action == :user do %>
|
2021-06-03 19:53:03 +08:00
|
|
|
<%= live_modal LivebookWeb.UserComponent,
|
2021-07-07 20:32:49 +08:00
|
|
|
id: "user",
|
2021-06-03 03:51:43 +08:00
|
|
|
modal_class: "w-full max-w-sm",
|
|
|
|
user: @current_user,
|
|
|
|
return_to: Routes.explore_path(@socket, :page) %>
|
|
|
|
<% end %>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2021-06-15 03:25:18 +08:00
|
|
|
def handle_params(%{"slug" => "new"}, _url, socket) do
|
|
|
|
{:noreply, create_session(socket)}
|
|
|
|
end
|
|
|
|
|
2021-06-03 03:51:43 +08:00
|
|
|
def handle_params(%{"slug" => slug}, _url, socket) do
|
2021-06-08 19:28:53 +08:00
|
|
|
{notebook, images} = Explore.notebook_by_slug!(slug)
|
|
|
|
{:noreply, create_session(socket, notebook: notebook, images: images)}
|
2021-06-03 03:51:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_params(_params, _url, socket), do: {:noreply, socket}
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info(
|
|
|
|
{:user_change, %{id: id} = user},
|
|
|
|
%{assigns: %{current_user: %{id: id}}} = socket
|
|
|
|
) do
|
|
|
|
{:noreply, assign(socket, :current_user, user)}
|
|
|
|
end
|
|
|
|
end
|