Skip to content

Commit

Permalink
add copy button in keywords dev route
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Sep 24, 2024
1 parent 48466ee commit 0de37c7
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions core/lib/canary_web/live/dev/keywords_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ defmodule CanaryWeb.Dev.KeywordsLive do
use CanaryWeb, :live_view
alias PrimerLive.Component, as: Primer

require Ash.Query

@impl true
def render(assigns) do
~H"""
<div>
<form phx-change="source">
<Primer.select name="Source" options={@source_names} is_form_control />
</form>
<Primer.box is_scrollable style="max-height: 600px; margin-top: 18px">
<Primer.box :if={@current_source} is_scrollable style="max-height: 600px; margin-top: 18px">
<:header>
<span><%= length(@current_source.overview.keywords) %> Keywords</span>
<div class="flex items-center justify-between">
<span><%= length(@current_source.overview.keywords) %> Keywords</span>
<Primer.button
is_small
aria-label="Copy"
id={"key-#{@current_source.id}"}
phx-hook="Clipboard"
data-clipboard-text={keywords_for_copy(@current_source.overview.keywords)}
>
<Primer.octicon name="paste-16" />
</Primer.button>
</div>
</:header>
<:row :for={word <- @current_source.overview.keywords}>
<span><%= word %></span>
Expand All @@ -23,15 +38,18 @@ defmodule CanaryWeb.Dev.KeywordsLive do

@impl true
def mount(_params, _session, socket) do
sources = Canary.Sources.Source |> Ash.read!()
sources =
Canary.Sources.Source
|> Ash.Query.filter(not is_nil(overview))
|> Ash.read!()

source_names = sources |> Enum.map(& &1.name)
current_source = sources |> Enum.at(0)

socket =
socket
|> assign(sources: sources)
|> assign(source_names: source_names)
|> assign(current_source: current_source)
|> assign(current_source: nil)

{:ok, socket}
end
Expand All @@ -48,4 +66,9 @@ defmodule CanaryWeb.Dev.KeywordsLive do

{:noreply, socket}
end

defp keywords_for_copy(keywords) do
rendered = keywords |> Enum.map(&"\"#{&1}\"") |> Enum.join(",")
"[#{rendered}]"
end
end

0 comments on commit 0de37c7

Please sign in to comment.