feat: offer to show all results on one page (if < 10000) Implements: https://todo.sr.ht/~alanpearce/searchix/5
Alan Pearce alan@alanpearce.eu
Sun, 05 Jan 2025 20:51:24 +0100
4 files changed, 19 insertions(+), 16 deletions(-)
M internal/components/data.go → internal/components/data.go
@@ -25,4 +25,5 @@ Query string Results *search.Result Prev string Next string + All string }
M internal/components/results.templ → internal/components/results.templ
@@ -1,9 +1,9 @@ package components import ( - "strconv" - "go.alanpearce.eu/searchix/internal/nix" "go.alanpearce.eu/searchix/internal/config" + "go.alanpearce.eu/searchix/internal/nix" + "strconv" ) func convertMatch[I nix.Importable](m nix.Importable) *I { @@ -37,6 +37,9 @@ <a class="button" href={ templ.SafeURL(r.Next) } rel="next">Next</a> } </nav> <span role="status">{ strconv.FormatUint(r.Results.Total, 10) } results</span> + if r.Next != r.Prev && r.Results.Total < config.MaxResultsShowAll { + <a href={ templ.SafeURL(r.All) }>Show All</a> + } </footer> } else { <span role="status">Nothing found</span>
M internal/config/config.go → internal/config/config.go
@@ -14,6 +14,8 @@ var Version string var DevMode bool +const MaxResultsShowAll = 10_000 + type URL struct { *url.URL }
M internal/server/mux.go → internal/server/mux.go
@@ -110,10 +110,14 @@ var pageSize int = search.DefaultPageSize var pageNumber = 1 if pg := r.URL.Query().Get("page"); pg != "" { pageNumber, err = strconv.Atoi(pg) - if err != nil || pageNumber <= 0 || pageNumber > math.MaxInt { + if err != nil || pageNumber > math.MaxInt { errorHandler(w, r, "Bad query string", http.StatusBadRequest) return + } + if pageNumber == 0 { + pageNumber = 1 + pageSize = math.MaxInt } } page := pagination.New(pageNumber, pageSize) @@ -129,6 +133,9 @@ errorHandler(w, r, err.Error(), http.StatusInternalServerError) return } + if pageSize == math.MaxInt && results.Total > config.MaxResultsShowAll { + errorHandler(w, r, "Too many results, use pagination", http.StatusBadRequest) + } tdata := components.ResultData{ TemplateData: components.TemplateData{ @@ -143,19 +150,6 @@ Results: results, } page.SetResults(results.Total) - log.Debug( - "pag", - "needed", - page.Needed, - "current", - page.Current, - "total", - results.Total, - "next", - page.Next, - "prev", - page.Prev, - ) if page.Needed { q, err := url.ParseQuery(r.URL.RawQuery) if err != nil { @@ -177,6 +171,9 @@ q.Set("page", strconv.Itoa(page.Prev)) } tdata.Prev = "search?" + q.Encode() } + + q.Set("page", "0") + tdata.All = "search?" + q.Encode() } w.Header().Add("Cache-Control", "max-age=300")