livebook/lib/livebook_web/live/session_live/secrets_component.ex
Cristine Guadelupe 72ea4cebf9
Secrets modal - new layout (#1441)
* Secrets modal - new layout

* Layout adjustments

* Layout adjustments

* Applying suggestions

* Update lib/livebook_web/live/session_live/secrets_component.ex

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
2022-09-27 17:26:21 -03:00

179 lines
5.7 KiB
Elixir

defmodule LivebookWeb.SessionLive.SecretsComponent do
use LivebookWeb, :live_component
@impl true
def update(assigns, socket) do
socket = assign(socket, assigns)
socket =
if socket.assigns[:data] do
socket
else
assign(socket, data: %{"name" => prefill_secret_name(socket), "value" => ""})
end
{:ok, socket}
end
@impl true
def render(assigns) do
~H"""
<div class="p-6 max-w-4xl flex flex-col space-y-5">
<h3 class="text-2xl font-semibold text-gray-800">
Add secret
</h3>
<div class="flex flex-columns gap-4">
<%= if @select_secret_ref do %>
<div class="basis-1/2 grow-0 pr-4 border-r">
<div class="flex flex-col space-y-4">
<p class="text-gray-700">
Choose a secret
</p>
<div class="flex flex-wrap gap-4">
<%= for secret <- @secrets do %>
<.choice_button
active={secret.name == @preselect_name}
value={secret.name}
phx-target={@myself}
phx-click="select_secret"
class={
if secret.name == @preselect_name,
do: "text-xs rounded-full",
else: "text-xs rounded-full text-gray-700 hover:bg-gray-200"
}
>
<%= secret.name %>
</.choice_button>
<% end %>
<%= if @secrets == [] do %>
<div class="w-full text-center text-gray-400 border rounded-lg p-8">
<.remix_icon icon="folder-lock-line" class="align-middle text-2xl" />
<span class="mt-1 block text-sm text-gray-700">
Secrets not found. <br /> Add to see them here.
</span>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
<.form
let={f}
for={:data}
phx-submit="save"
phx-change="validate"
autocomplete="off"
phx-target={@myself}
errors={data_errors(@data)}
class="basis-1/2 grow"
>
<div class="flex flex-col space-y-4">
<%= if @select_secret_ref do %>
<p class="text-gray-700">
Add new secret
</p>
<% end %>
<.input_wrapper form={f} field={:name}>
<div class="input-label">
Name <span class="text-xs text-gray-500">(alphanumeric and underscore)</span>
</div>
<%= text_input(f, :name,
value: @data["name"],
class: "input",
autofocus: !@prefill_secret_name,
spellcheck: "false"
) %>
</.input_wrapper>
<.input_wrapper form={f} field={:value}>
<div class="input-label">Value</div>
<%= text_input(f, :value,
value: @data["value"],
class: "input",
autofocus: !!@prefill_secret_name || unavailable_secret?(@preselect_name, @secrets),
spellcheck: "false"
) %>
</.input_wrapper>
<div class="flex space-x-2">
<button class="button-base button-blue" type="submit" disabled={f.errors != []}>
<.remix_icon icon="add-line" class="align-middle" />
<span class="font-normal">Add</span>
</button>
<%= live_patch("Cancel", to: @return_to, class: "button-base button-outlined-gray") %>
</div>
</div>
</.form>
</div>
</div>
"""
end
@impl true
def handle_event("save", %{"data" => data}, socket) do
secret_name = String.upcase(data["name"])
if data_errors(data) == [] do
secret = %{name: secret_name, value: data["value"]}
Livebook.Session.put_secret(socket.assigns.session.pid, secret)
{:noreply,
socket |> push_patch(to: socket.assigns.return_to) |> push_secret_selected(secret_name)}
else
{:noreply, assign(socket, data: data)}
end
end
def handle_event("select_secret", %{"value" => secret_name}, socket) do
{:noreply,
socket |> push_patch(to: socket.assigns.return_to) |> push_secret_selected(secret_name)}
end
def handle_event("validate", %{"data" => data}, socket) do
{:noreply, assign(socket, data: data)}
end
defp data_errors(data) do
Enum.flat_map(data, fn {key, value} ->
if error = data_error(key, value) do
[{String.to_existing_atom(key), {error, []}}]
else
[]
end
end)
end
defp data_error("name", value) do
cond do
String.match?(value, ~r/^\w+$/) -> nil
value == "" -> "can't be blank"
true -> "is invalid"
end
end
defp data_error("value", ""), do: "can't be blank"
defp data_error(_key, _value), do: nil
defp push_secret_selected(%{assigns: %{select_secret_ref: nil}} = socket, _), do: socket
defp push_secret_selected(%{assigns: %{select_secret_ref: ref}} = socket, secret_name) do
push_event(socket, "secret_selected", %{select_secret_ref: ref, secret_name: secret_name})
end
defp prefill_secret_name(socket) do
case socket.assigns.prefill_secret_name do
nil ->
if unavailable_secret?(socket.assigns.preselect_name, socket.assigns.secrets),
do: socket.assigns.preselect_name,
else: ""
prefill ->
prefill
end
end
defp unavailable_secret?(nil, _), do: false
defp unavailable_secret?("", _), do: false
defp unavailable_secret?(preselect_name, secrets) do
preselect_name not in Enum.map(secrets, & &1.name)
end
end