internal/components/results.go (view raw)
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | package components
import (
"maps"
"slices"
"github.com/blevesearch/bleve/v2/search"
"alin.ovh/searchix/internal/config"
g "alin.ovh/gomponents"
. "alin.ovh/gomponents/html"
)
func Results(r ResultData) g.Node {
if r.Results == nil {
return Br()
}
if r.Results.Total == 0 {
return Span(Role("status"), g.Text("Nothing found"))
}
var content g.Node
switch r.Source.Importer {
case config.Options:
content = Options(r.Results)
case config.Packages:
content = Packages(r.Results)
case config.All:
content = Combined(r.Results)
}
return g.Group([]g.Node{
Facets(r),
content,
Footer(
g.Attr("aria-label", "pagination"),
Nav(
ID("pagination"),
g.If(r.SearchNav.Prev != "",
A(Class("button"), Href(r.SearchNav.Prev), Rel("prev"), g.Text("Prev")),
),
g.If(r.SearchNav.Next != "",
A(Class("button"), Href(r.SearchNav.Next), Rel("next"), g.Text("Next")),
),
),
Span(
Role("status"),
g.Textf("%d results", r.Results.Total),
),
g.Text(" "),
g.If(r.SearchNav.All != "",
A(Href(r.SearchNav.All), g.Text("Show All")),
),
),
})
}
func Facets(r ResultData) g.Node {
return Nav(
Class("facets"),
g.Map(slices.Sorted(maps.Keys(r.Results.Facets)), func(name string) g.Node {
facet := r.Results.Facets[name]
return Div(Class("facet"),
H3(g.Text(name)),
Ul(
g.Map(facet.Terms.Terms(), func(term *search.TermFacet) g.Node {
t := term.Term
if t == "" {
t = "None"
}
return Term(
g.If(
r.SearchNav.HasFacet(facet.Field, term.Term),
A(
Href(r.SearchNav.RemoveFacet(facet.Field, term.Term)),
TitleAttr("remove"),
g.Text(t),
),
A(
Href(r.SearchNav.AddFacet(facet.Field, term.Term)),
g.Text(t),
),
),
Span(
Class("count"),
g.Textf("%d", term.Count),
),
)
}),
g.If(facet.Missing > 0,
Li(Class("term"),
A(Href(r.SearchNav.AddFacet(facet.Field, "")), g.Text("None")),
Span(Class("count"), g.Textf("%d", facet.Missing)),
),
),
),
)
}),
)
}
func Term(children ...g.Node) g.Node {
return Li(Class("term"), g.Group(children))
}
func ResultsPage(r ResultData) g.Node {
return SearchPage(r.TemplateData, r, Results(r))
}
func openDialogLink(attr string) g.Node {
return A(Class("open-dialog"), Href(attr), g.Text(attr))
}
func openCombinedDialogLink(attr string) g.Node {
return A(Class("open-dialog"), Href("/"+attr), g.Text(attr))
}
|