defmodule LivebookWeb.SessionLive.InsertButtonsComponent do
use LivebookWeb, :live_component
def render(assigns) do
~H"""
<.menu id={"#{@id}-block-menu"} position="left">
<:toggle>
<:content>
<%= cond do %>
<% not Livebook.Runtime.connected?(@runtime) -> %>
<% @smart_cell_definitions == [] -> %>
<% true -> %>
<.menu id={"#{@id}-smart-menu"} position="left">
<:toggle>
<:content>
<%= for definition <- Enum.sort_by(@smart_cell_definitions, & &1.name) do %>
<.smart_cell_insert_button
definition={definition}
section_id={@section_id}
cell_id={@cell_id} />
<% end %>
<% end %>
"""
end
defp smart_cell_insert_button(%{definition: %{requirement: %{variants: [_, _ | _]}}} = assigns) do
~H"""
<.submenu>
<:content>
<%= for {variant, idx} <- Enum.with_index(@definition.requirement.variants) do %>
<% end %>
"""
end
defp smart_cell_insert_button(assigns) do
~H"""
"""
end
defp on_smart_cell_click(%{requirement: nil} = definition, _variant_idx, section_id, cell_id) do
insert_smart_cell(definition, section_id, cell_id)
end
defp on_smart_cell_click(%{requirement: %{}} = definition, variant_idx, section_id, cell_id) do
with_confirm(
JS.push("add_smart_cell_dependencies",
value: %{kind: definition.kind, variant_idx: variant_idx}
)
|> insert_smart_cell(definition, section_id, cell_id),
title: "Add package",
description: ~s'''
The “#{definition.name}“ smart cell requires #{definition.requirement.name}.
Do you want to add it as a dependency and reinstall dependencies?
''',
confirm_text: "Add and reinstall",
confirm_icon: "add-line",
danger: false
)
end
defp insert_smart_cell(js \\ %JS{}, definition, section_id, cell_id) do
JS.push(js, "insert_cell_below",
value: %{
type: "smart",
kind: definition.kind,
section_id: section_id,
cell_id: cell_id
}
)
end
end