"""
end
defp grant_access_message(assigns) do
~H"""
<.remix_icon
icon="error-warning-fill"
class="align-middle text-2xl flex text-gray-100 rounded-lg py-2"
/>
There is a secret named
<%= @secret.name %>
in the <%= hub_label(@hub) %> workspace. Allow this notebook to access it?
<.button
color="gray"
phx-click="select_secret"
phx-value-name={@secret.name}
phx-value-hub={true}
phx-target={@target}
>
Grant access
"""
end
@impl true
def handle_event("save", %{"secret" => attrs}, socket) do
changeset = Secrets.change_secret(%Secret{}, attrs)
with {:ok, secret} <- Ecto.Changeset.apply_action(changeset, :insert),
:ok <- save_secret(socket, secret, changeset) do
Session.set_secret(socket.assigns.session.pid, secret)
{:noreply,
socket
|> push_patch(to: socket.assigns.return_to)
|> push_secret_selected(secret.name)}
else
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
{:transport_error, error} ->
{:noreply,
socket
|> push_patch(to: ~p"/sessions/#{socket.assigns.session.id}/secrets")
|> put_flash(:error, error)}
end
end
def handle_event("validate", %{"secret" => attrs}, socket) do
changeset =
%Secret{}
|> Secrets.change_secret(attrs)
|> Map.replace!(:action, :validate)
{:noreply, assign(socket, changeset: changeset)}
end
def handle_event("select_secret", %{"name" => secret_name} = attrs, socket) do
if attrs["hub"] do
secret = Enum.find(socket.assigns.hub_secrets, &(&1.name == secret_name))
if secret && !Session.Data.secret_toggled?(secret, socket.assigns.secrets) do
Session.set_secret(socket.assigns.session.pid, secret)
end
end
{:noreply,
socket
|> push_patch(to: socket.assigns.return_to)
|> push_secret_selected(secret_name)}
end
defp push_secret_selected(%{assigns: %{select_secret_metadata: nil}} = socket, _), do: socket
defp push_secret_selected(
%{assigns: %{select_secret_metadata: %{ref: ref}}} = socket,
secret_name
) do
push_event(socket, "secret_selected", %{select_secret_ref: ref, secret_name: secret_name})
end
defp title(%{assigns: %{select_secret_metadata: nil}}), do: "Add secret"
defp title(%{assigns: %{select_secret_metadata: %{options: %{"title" => title}}}}), do: title
defp title(_), do: "Select secret"
defp save_secret(socket, secret, changeset) do
if secret.hub_id do
with {:error, errors} <- Hubs.create_secret(socket.assigns.hub, secret) do
{:error,
changeset
|> Livebook.Utils.put_changeset_errors(errors)
|> Map.replace!(:action, :validate)}
end
else
:ok
end
end
defp hub_label(hub), do: "#{hub.hub_emoji} #{hub.hub_name}"
end