defmodule LivebookWeb.SessionLive.CellComponent do use LivebookWeb, :live_component @impl true def render(assigns) do ~H"""
<%= render_cell(assigns) %>
""" end defp render_cell(%{cell_view: %{type: :markdown}} = assigns) do ~H""" <.cell_actions> <:secondary> <.enable_insert_mode_button /> <.cell_link_button cell_id={@cell_view.id} /> <.move_cell_up_button cell_id={@cell_view.id} /> <.move_cell_down_button cell_id={@cell_view.id} /> <.delete_cell_button cell_id={@cell_view.id} /> <.cell_body>
<.live_component module={LivebookWeb.SessionLive.CellEditorComponent} id={"#{@cell_view.id}-primary"} cell_id={@cell_view.id} tag="primary" source_view={@cell_view.source_view} language="markdown" />
<.content_skeleton empty={empty?(@cell_view.source_view)} />
""" end defp render_cell(%{cell_view: %{type: :code}} = assigns) do ~H""" <.cell_actions> <:primary> <.cell_evaluation_button session_id={@session_id} cell_id={@cell_view.id} validity={@cell_view.eval.validity} status={@cell_view.eval.status} reevaluate_automatically={@cell_view.reevaluate_automatically} reevaluates_automatically={@cell_view.eval.reevaluates_automatically} /> <:secondary> <.amplify_output_button /> <.cell_settings_button cell_id={@cell_view.id} session_id={@session_id} /> <.cell_link_button cell_id={@cell_view.id} /> <.move_cell_up_button cell_id={@cell_view.id} /> <.move_cell_down_button cell_id={@cell_view.id} /> <.delete_cell_button cell_id={@cell_view.id} /> <.cell_body>
<.live_component module={LivebookWeb.SessionLive.CellEditorComponent} id={"#{@cell_view.id}-primary"} cell_id={@cell_view.id} tag="primary" source_view={@cell_view.source_view} language="elixir" intellisense />
<.cell_status id={@cell_view.id} cell_view={@cell_view} />
<.evaluation_outputs cell_view={@cell_view} session_id={@session_id} session_pid={@session_pid} client_id={@client_id} /> """ end defp render_cell(%{cell_view: %{type: :setup}} = assigns) do ~H""" <.cell_actions> <:primary> <.setup_cell_evaluation_button cell_id={@cell_view.id} validity={@cell_view.eval.validity} status={@cell_view.eval.status} runtime={@runtime} /> <:secondary> <.package_search_button session_id={@session_id} runtime={@runtime} /> <.cell_link_button cell_id={@cell_view.id} /> <.setup_cell_info /> <.cell_body>
Notebook dependencies and setup <.cell_status id={"#{@cell_view.id}-1"} cell_view={@cell_view} />
<.live_component module={LivebookWeb.SessionLive.CellEditorComponent} id={"#{@cell_view.id}-primary"} cell_id={@cell_view.id} tag="primary" source_view={@cell_view.source_view} language="elixir" intellisense />
<.cell_status id={"#{@cell_view.id}-2"} cell_view={@cell_view} />
<.evaluation_outputs cell_view={@cell_view} session_id={@session_id} session_pid={@session_pid} client_id={@client_id} />
""" end defp render_cell(%{cell_view: %{type: :smart}} = assigns) do ~H""" <.cell_actions> <:primary> <.cell_evaluation_button session_id={@session_id} cell_id={@cell_view.id} validity={@cell_view.eval.validity} status={@cell_view.eval.status} reevaluate_automatically={false} reevaluates_automatically={@cell_view.eval.reevaluates_automatically} /> <:secondary> <.toggle_source_button /> <.convert_smart_cell_button cell_id={@cell_view.id} /> <.cell_link_button cell_id={@cell_view.id} /> <.move_cell_up_button cell_id={@cell_view.id} /> <.move_cell_down_button cell_id={@cell_view.id} /> <.delete_cell_button cell_id={@cell_view.id} /> <.cell_body>
<%= case @cell_view.status do %> <% :started -> %>
<.live_component module={LivebookWeb.JSViewComponent} id={@cell_view.id} js_view={@cell_view.js_view} session_id={@session_id} client_id={@client_id} /> <.live_component :if={@cell_view.editor} module={LivebookWeb.SessionLive.CellEditorComponent} id={"#{@cell_view.id}-secondary"} cell_id={@cell_view.id} tag="secondary" source_view={@cell_view.editor.source_view} language={@cell_view.editor.language} rounded={@cell_view.editor.placement} />
<% :dead -> %>
<%= if @installing? do %> Waiting for dependency installation to complete... <% else %> Run the notebook setup to show the contents of this Smart cell. <% end %>
<% :down -> %>
The Smart cell crashed unexpectedly, this is most likely a bug.
<% :starting -> %>
<.content_skeleton empty={false} />
<% end %>
<.cell_status id={"#{@cell_view.id}-1"} cell_view={@cell_view} />
<.live_component module={LivebookWeb.SessionLive.CellEditorComponent} id={"#{@cell_view.id}-primary"} cell_id={@cell_view.id} tag="primary" source_view={@cell_view.source_view} language="elixir" intellisense read_only />
<.cell_status id={"#{@cell_view.id}-2"} cell_view={@cell_view} />
<.evaluation_outputs cell_view={@cell_view} session_id={@session_id} session_pid={@session_pid} client_id={@client_id} /> """ end defp cell_actions(assigns) do assigns = assigns |> assign_new(:primary, fn -> [] end) |> assign_new(:secondary, fn -> [] end) ~H"""
<%= render_slot(@primary) %>
""" end defp cell_body(assigns) do ~H"""
<%= render_slot(@inner_block) %>
""" end defp cell_evaluation_button(%{status: :ready} = assigns) do ~H"""
<.menu id={"cell-#{@cell_id}-evaluation-menu"} position={:bottom_left} distant> <:toggle> <.menu_item variant={if(not @reevaluate_automatically, do: :selected, else: :default)}> <.menu_item variant={if(@reevaluate_automatically, do: :selected, else: :default)}>
""" end defp cell_evaluation_button(assigns) do ~H""" """ end defp setup_cell_evaluation_button(%{status: :ready} = assigns) do ~H"""
<%= unless Livebook.Runtime.fixed_dependencies?(@runtime) do %> <.menu id="setup-menu" position={:bottom_left} distant> <:toggle> <.menu_item> <% end %>
""" end defp setup_cell_evaluation_button(assigns) do ~H""" """ end defp enable_insert_mode_button(assigns) do ~H""" """ end defp toggle_source_button(assigns) do ~H""" """ end defp convert_smart_cell_button(assigns) do ~H""" """ end defp package_search_button(assigns) do ~H""" <%= if Livebook.Runtime.fixed_dependencies?(@runtime) do %> <% else %> <.link patch={~p"/sessions/#{@session_id}/package-search"} class="icon-button" role="button" data-btn-package-search > <.remix_icon icon="play-list-add-line" class="text-xl" /> <% end %> """ end defp cell_link_button(assigns) do ~H""" <.remix_icon icon="link" class="text-xl" /> """ end def amplify_output_button(assigns) do ~H""" """ end defp cell_settings_button(assigns) do ~H""" <.link patch={~p"/sessions/#{@session_id}/cell-settings/#{@cell_id}"} class="icon-button" aria-label="cell settings" role="button" > <.remix_icon icon="settings-3-line" class="text-xl" /> """ end defp move_cell_up_button(assigns) do ~H""" """ end defp move_cell_down_button(assigns) do ~H""" """ end defp delete_cell_button(assigns) do ~H""" """ end defp setup_cell_info(assigns) do ~H""" <.remix_icon icon="question-line" class="text-xl" /> """ end defp evaluation_outputs(assigns) do ~H"""
""" end defp empty?(%{source: ""} = _source_view), do: true defp empty?(_source_view), do: false defp cell_status(%{cell_view: %{eval: %{status: :evaluating}}} = assigns) do ~H""" <.cell_status_indicator variant={:progressing} change_indicator={true}> """ end defp cell_status(%{cell_view: %{eval: %{status: :queued}}} = assigns) do ~H""" <.cell_status_indicator variant={:waiting}> Queued """ end defp cell_status(%{cell_view: %{eval: %{validity: :evaluated}}} = assigns) do ~H""" <.cell_status_indicator variant={if(@cell_view.eval.errored, do: :error, else: :success)} change_indicator={true} tooltip={evaluated_label(@cell_view.eval.evaluation_time_ms)} > Evaluated """ end defp cell_status(%{cell_view: %{eval: %{validity: :stale}}} = assigns) do ~H""" <.cell_status_indicator variant={:warning} change_indicator={true}> Stale """ end defp cell_status(%{cell_view: %{eval: %{validity: :aborted}}} = assigns) do ~H""" <.cell_status_indicator variant={:inactive}> Aborted """ end defp cell_status(assigns), do: ~H"" attr :variant, :atom, required: true attr :tooltip, :string, default: nil attr :change_indicator, :boolean, default: false slot :inner_block, required: true defp cell_status_indicator(assigns) do ~H"""
<%= render_slot(@inner_block) %> *
<.status_indicator variant={@variant} />
""" end defp evaluated_label(time_ms) when is_integer(time_ms) do evaluation_time = if time_ms > 100 do seconds = time_ms |> Kernel./(1000) |> Float.floor(1) "#{seconds}s" else "#{time_ms}ms" end "Took " <> evaluation_time end defp evaluated_label(_time_ms), do: nil defp smart_cell_js_view_ref(%{type: :smart, status: :started, js_view: %{ref: ref}}), do: ref defp smart_cell_js_view_ref(_cell_view), do: nil end