From 67d16f4d3e3edbfaa209468f306850b00c9ae2be Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 1 Mar 2024 16:49:07 -0600 Subject: [PATCH] chore: dedupe results Signed-off-by: jolheiser --- internal/html/repo_search.templ | 40 ++++++- internal/html/repo_search_templ.go | 177 +++++++++++++++++++++++------ 2 files changed, 180 insertions(+), 37 deletions(-) diff --git a/internal/html/repo_search.templ b/internal/html/repo_search.templ index aba8e9e..0afeeec 100644 --- a/internal/html/repo_search.templ +++ b/internal/html/repo_search.templ @@ -9,13 +9,33 @@ type SearchContext struct { Results []git.GrepResult } +func (s SearchContext) DedupeResults() [][]git.GrepResult { + var ( + results [][]git.GrepResult + currentFile string + currentResults []git.GrepResult + ) + for _, result := range s.Results { + if result.File == currentFile { + currentResults = append(currentResults, result) + continue + } + if currentFile != "" { + results = append(results, currentResults) + } + currentFile = result.File + currentResults = []git.GrepResult{result} + } + + return results +} + templ RepoSearch(sc SearchContext) { @base(sc.BaseContext) { @repoHeaderComponent(sc.RepoHeaderComponentContext)
- for _, result := range sc.Results { - -
@templ.Raw(result.Content)
+ for _, results := range sc.DedupeResults() { + @repoSearchResult(sc.RepoHeaderComponentContext.Name, sc.RepoHeaderComponentContext.Ref, results) } } } +templ repoSearchResult(repo, ref string, results []git.GrepResult) { + +
@templ.Raw(results[0].Content)
+ if len(results) > 1 { +
+ { fmt.Sprintf("%d ", len(results[1:])) }more + for _, result := range results[1:] { + +
@templ.Raw(result.Content)
+ } +
+ } +} + diff --git a/internal/html/repo_search_templ.go b/internal/html/repo_search_templ.go index 7e26c2b..1645b39 100644 --- a/internal/html/repo_search_templ.go +++ b/internal/html/repo_search_templ.go @@ -19,6 +19,27 @@ type SearchContext struct { Results []git.GrepResult } +func (s SearchContext) DedupeResults() [][]git.GrepResult { + var ( + results [][]git.GrepResult + currentFile string + currentResults []git.GrepResult + ) + for _, result := range s.Results { + if result.File == currentFile { + currentResults = append(currentResults, result) + continue + } + if currentFile != "" { + results = append(results, currentResults) + } + currentFile = result.File + currentResults = []git.GrepResult{result} + } + + return results +} + func RepoSearch(sc SearchContext) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) @@ -55,38 +76,8 @@ func RepoSearch(sc SearchContext) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - for _, result := range sc.Results { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templ.Raw(result.Content).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + for _, results := range sc.DedupeResults() { + templ_7745c5c3_Err = repoSearchResult(sc.RepoHeaderComponentContext.Name, sc.RepoHeaderComponentContext.Ref, results).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -104,11 +95,11 @@ func RepoSearch(sc SearchContext) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Var6 := ` + templ_7745c5c3_Var4 := ` const search = new URLSearchParams(window.location.search).get("q"); if (search !== "") document.querySelector("#search").value = search; ` - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var6) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var4) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -122,3 +113,121 @@ func RepoSearch(sc SearchContext) templ.Component { return templ_7745c5c3_Err }) } + +func repoSearchResult(repo, ref string, results []git.GrepResult) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var5 := templ.GetChildren(ctx) + if templ_7745c5c3_Var5 == nil { + templ_7745c5c3_Var5 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templ.Raw(results[0].Content).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if len(results) > 1 { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var8 string + templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d ", len(results[1:]))) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `repo_search.templ`, Line: 51, Col: 50} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var9 := `more` + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var9) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, result := range results[1:] { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templ.Raw(result.Content).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +}