1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require "pro_tacts/admin/dashboard"
require "pro_tacts/admin/search"
require "pro_tacts/admin/search_page"
require "pro_tacts/admin/search_results"
module ProTacts
class Web < Roda
# The search: the page a phone's header links to, and the results
# the search field fetches as the query is typed — on that page
# and in a wider screen's dialog (Admin::SearchField). The match
# stays in Admin::Search, the one place it is decided, rather than
# a second copy in the page's script
# (docs/plans/2026-09-30-live-search.md).
#
# Each step is a span (Web#span), and none carries the query.
hash_branch("search") do |r|
query = typecast_params.str("q", "").strip
r.is do
r.get do
response["Content-Type"] = "text/html; charset=utf-8"
results = search_results(query)
span("search.render") { Admin::SearchPage.call(login: @login, query:, results:) }
end
end
# The results alone, put under the input whole.
r.get "results" do
response["Content-Type"] = "text/html; charset=utf-8"
results = search_results(query)
span("search.render") { results.call }
end
end
private
# A blank query answers the most recently updated contacts, the
# dashboard's own list, so the search page is never empty before a
# word is typed. A query builds only the contacts it shows.
#: (String query) -> Admin::SearchResults
def search_results(query)
if query.empty?
recent = span("search.contacts_by_recency") { |step|
store.contacts_by_recency(limit: Admin::Dashboard::RECENT_LIMIT).tap { step.set_attribute("rows", it.length) }
}
return Admin::SearchResults.new(contacts: recent, groups: [], recent: true)
end
keys = span("search.search_keys_by_recency") { |step| store.search_keys_by_recency.tap { step.set_attribute("rows", it.length) } }
groups = span("search.all_groups") { |step| store.all_groups.tap { step.set_attribute("rows", it.length) } }
ids = span("search.match") { |step|
Admin::Search.contacts(keys, query:, groups:).tap {
step.set_attribute("words", query.split.length)
step.set_attribute("contacts", it.length)
}
}
shown = ids.first(Admin::SearchResults::LIMIT)
contacts = span("search.contacts_by_recency") { |step|
found = store.contacts_by_recency(shown).to_h {
[it.contact.id, it] #: [String, Store::RecentContact]
}
step.set_attribute("rows", found.length)
shown.map { found.fetch(it) }
}
Admin::SearchResults.new(contacts:, groups: Admin::Search.groups(groups, query:), more: ids.length - shown.length)
end
end
end