defmodule LivebookWeb.SessionLive.CellComponent do
use LivebookWeb, :live_component
import LivebookWeb.SessionHelpers
@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>
<.cell_editor
cell_id={@cell_view.id}
tag="primary"
empty={@cell_view.empty}
language="markdown"
/>
<.content_skeleton empty={@cell_view.empty} />
"""
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>
<.cell_icon cell_type={:code} language={:erlang} />
<.cell_settings_button cell_id={@cell_view.id} session_id={@session_id} />
<.amplify_output_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>
<.cell_editor
cell_id={@cell_view.id}
tag="primary"
empty={@cell_view.empty}
language={@cell_view.language}
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} />
<.cell_editor
cell_id={@cell_view.id}
tag="primary"
empty={@cell_view.empty}
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} />
<.amplify_output_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>
<%= 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}
/>
<.cell_editor
:if={@cell_view.editor}
cell_id={@cell_view.id}
tag="secondary"
empty={@cell_view.editor.empty}
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.
Restart Smart cell
<% :starting -> %>
<.content_skeleton empty={false} />
<% end %>
<.cell_status id={"#{@cell_view.id}-1"} cell_view={@cell_view} />
<.cell_editor
cell_id={@cell_view.id}
tag="primary"
empty={@cell_view.empty}
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) %>
<%= render_slot(@secondary) %>
"""
end
defp cell_body(assigns) do
~H"""
<%= render_slot(@inner_block) %>
"""
end
defp cell_evaluation_button(%{status: :ready} = assigns) do
~H"""
<%= cond do %>
<% @reevaluates_automatically -> %>
<.remix_icon icon="check-line" class="text-xl" />
Reevaluates automatically
<% @validity == :evaluated -> %>
<.remix_icon icon="play-circle-fill" class="text-xl" />
Reevaluate
<% true -> %>
<.remix_icon icon="play-circle-fill" class="text-xl" />
Evaluate
<% end %>
<.menu id={"cell-#{@cell_id}-evaluation-menu"} position={:bottom_left} distant>
<:toggle>
<.remix_icon icon="arrow-down-s-line" class="text-xl" />
<.menu_item variant={if(not @reevaluate_automatically, do: :selected, else: :default)}>
<.remix_icon icon="check-line" class={if(@reevaluate_automatically, do: "invisible")} />
Evaluate on demand
<.menu_item variant={if(@reevaluate_automatically, do: :selected, else: :default)}>
<.remix_icon icon="check-line" class={if(not @reevaluate_automatically, do: "invisible")} />
Reevaluate automatically
"""
end
defp cell_evaluation_button(assigns) do
~H"""
<.remix_icon icon="stop-circle-fill" class="text-xl" />
Stop
"""
end
defp setup_cell_evaluation_button(%{status: :ready} = assigns) do
~H"""
<%= if @validity == :fresh do %>
<.remix_icon icon="play-circle-fill" class="text-xl" />
Setup
<% else %>
<.remix_icon icon="restart-fill" class="text-xl" />
Reconnect and setup
<% end %>
<%= unless Livebook.Runtime.fixed_dependencies?(@runtime) do %>
<.menu id="setup-menu" position={:bottom_left} distant>
<:toggle>
<.remix_icon icon="arrow-down-s-line" class="text-xl" />
<.menu_item>
<.remix_icon icon="play-circle-fill" />
Setup without cache
<% end %>
"""
end
defp setup_cell_evaluation_button(assigns) do
~H"""
<.remix_icon icon="stop-circle-fill" class="text-xl" />
Stop
"""
end
defp enable_insert_mode_button(assigns) do
~H"""
<.remix_icon icon="pencil-line" class="text-xl" />
"""
end
defp toggle_source_button(assigns) do
~H"""
<.remix_icon icon="code-line" class="text-xl" />
"""
end
defp convert_smart_cell_button(assigns) do
~H"""
<.remix_icon icon="pencil-line" class="text-xl" />
"""
end
defp package_search_button(assigns) do
~H"""
<%= if Livebook.Runtime.fixed_dependencies?(@runtime) do %>
<.remix_icon icon="play-list-add-line" class="text-xl" />
<% 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"""
<.remix_icon icon="zoom-in-line" class="text-xl" />
"""
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"""
<.remix_icon icon="arrow-up-s-line" class="text-xl" />
"""
end
defp move_cell_down_button(assigns) do
~H"""
<.remix_icon icon="arrow-down-s-line" class="text-xl" />
"""
end
defp delete_cell_button(assigns) do
~H"""
<.remix_icon icon="delete-bin-6-line" class="text-xl" />
"""
end
defp setup_cell_info(assigns) do
~H"""
<.remix_icon icon="question-line" class="text-xl" />
"""
end
attr :cell_id, :string, required: true
attr :tag, :string, required: true
attr :empty, :boolean, required: true
attr :language, :string, required: true
attr :intellisense, :boolean, default: false
attr :read_only, :boolean, default: false
attr :rounded, :atom, default: :both
defp cell_editor(assigns) do
~H"""
<.content_skeleton bg_class="bg-gray-500" empty={@empty} />
"""
end
defp rounded_class(:both), do: "rounded-lg"
defp rounded_class(:top), do: "rounded-t-lg"
defp rounded_class(:bottom), do: "rounded-b-lg"
defp evaluation_outputs(assigns) do
~H"""
"""
end
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"""
"""
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