Skip to content

Commit

Permalink
update understander dev route to support multiple sources
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Sep 24, 2024
1 parent 2670f46 commit 45a4b02
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions core/lib/canary_web/live/dev/understander_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ defmodule CanaryWeb.Dev.UnderstanderLive do
~H"""
<div>
<form phx-change="source">
<Primer.select name="Source" options={@source_names} is_form_control />
<Primer.select
name="source[]"
options={Enum.map(@sources, & &1.name)}
is_form_control
is_multiple
is_auto_height
/>
</form>
<form class="flex flex-row gap-2 mt-4 items-center" phx-submit="submit">
Expand All @@ -30,36 +36,34 @@ defmodule CanaryWeb.Dev.UnderstanderLive do
@impl true
def mount(_params, _session, socket) do
sources = Canary.Sources.Source |> Ash.read!()
source_names = sources |> Enum.map(& &1.name)
current_source = sources |> Enum.at(0)

socket =
socket
|> assign(query: "")
|> assign(result: [])
|> assign(sources: sources)
|> assign(source_names: source_names)
|> assign(current_source: current_source)
|> assign(selected: [])

{:ok, socket}
end

@impl true
def handle_event("source", %{"Source" => source_name}, socket) do
current_source =
def handle_event("source", %{"source" => sources}, socket) do
selected =
socket.assigns.sources
|> Enum.find(&(&1.name == source_name))
|> Enum.filter(&(&1.name in sources))

socket =
socket
|> assign(current_source: current_source)
|> assign(selected: selected)

{:noreply, socket}
end

@impl true
def handle_event("submit", %{"query" => query}, socket) do
{:ok, words} = Canary.Query.Understander.run([socket.assigns.current_source], query)
keywords = Canary.Query.Understander.keywords(socket.assigns.selected)
{:ok, words} = Canary.Query.Understander.run(query, keywords)
{:noreply, socket |> assign(query: query, result: words)}
end
end

0 comments on commit 45a4b02

Please sign in to comment.