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 %>
<% end %>
<% end %>
"""
end
defp on_smart_cell_click(%{requirement: nil} = definition, section_id, cell_id) do
insert_smart_cell(definition, section_id, cell_id)
end
defp on_smart_cell_click(%{requirement: %{}} = definition, section_id, cell_id) do
with_confirm(
JS.push("add_smart_cell_dependencies", value: %{kind: definition.kind})
|> 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