defmodule LivebookWeb.NotebookCardsComponent do use LivebookWeb, :live_component @impl true def render(assigns) do assigns = assign_new(assigns, :card_icon, fn -> nil end) ~H"""
<%= for {info, idx} <- Enum.with_index(@notebook_infos) do %>
<%= info.name %> <%= @card_icon && render_slot(@card_icon, {info, idx}) %>
<%= @added_at_label %> <%= format_datetime_relatively(info.added_at) %> ago
<%= if session = session_by_file(info.file, @sessions) do %> <.link navigate={~p"/sessions/#{session.id}"} class="text-blue-600 font-medium"> Join session <% else %> <% end %>
<% end %>
""" end @impl true def handle_event("open", %{"idx" => idx}, socket) do %{file: file} = Enum.at(socket.assigns.notebook_infos, idx) if file_running?(file, socket.assigns.sessions) do session = session_by_file(file, socket.assigns.sessions) {:noreply, push_navigate(socket, to: ~p"/sessions/#{session.id}")} else send(self(), {:open, file}) {:noreply, socket} end end def handle_event("fork", %{"idx" => idx}, socket) do %{file: file} = Enum.at(socket.assigns.notebook_infos, idx) send(self(), {:fork, file}) {:noreply, socket} end defp file_running?(file, sessions) do Enum.any?(sessions, &(&1.file == file)) end defp session_by_file(file, sessions) do Enum.find(sessions, &(&1.file == file)) end end