diff options
author | Alan Pearce | 2024-12-31 14:56:37 +0100 |
---|---|---|
committer | Alan Pearce | 2025-01-02 15:05:56 +0100 |
commit | 58643f38bdd62dc538a03865b1f81eb77d957779 (patch) | |
tree | bf2c5ccce1592ae8bc30508be38510f15fe31b5e | |
parent | 1872c47e393e9c4c811f3b90ec2e0ac4cd8767b2 (diff) | |
download | searchix-58643f38bdd62dc538a03865b1f81eb77d957779.tar.lz searchix-58643f38bdd62dc538a03865b1f81eb77d957779.tar.zst searchix-58643f38bdd62dc538a03865b1f81eb77d957779.zip |
feat: show scores in results table in dev mode
-rw-r--r-- | frontend/static/search.js | 11 | ||||
-rw-r--r-- | frontend/static/style.css | 5 | ||||
-rw-r--r-- | internal/components/combined.templ | 9 | ||||
-rw-r--r-- | internal/components/dev.templ | 18 | ||||
-rw-r--r-- | internal/components/options.templ | 13 | ||||
-rw-r--r-- | internal/components/packages.templ | 13 | ||||
-rw-r--r-- | internal/components/page.templ | 7 | ||||
-rw-r--r-- | internal/config/config.go | 3 |
8 files changed, 74 insertions, 5 deletions
diff --git a/frontend/static/search.js b/frontend/static/search.js index 760ca28..52fa2f4 100644 --- a/frontend/static/search.js +++ b/frontend/static/search.js @@ -28,10 +28,21 @@ if (window.trustedTypes && trustedTypes.createPolicy) { }); } +function openSiblingDialog(ev) { + const dialog = ev.target.nextElementSibling; + dialog.showModal(); + dialog.querySelector("button").addEventListener("click", function () { + dialog.close(); + }); +} + function addOpenDialogListeners(results) { results.querySelectorAll("a.open-dialog").forEach(function (element) { element.addEventListener("click", handleDialogOpen); }); + results.querySelectorAll("a.open-sibling-dialog").forEach(function (element) { + element.addEventListener("click", openSiblingDialog); + }); } function paginationLinkClicked(ev) { diff --git a/frontend/static/style.css b/frontend/static/style.css index 60b38c8..8766844 100644 --- a/frontend/static/style.css +++ b/frontend/static/style.css @@ -185,3 +185,8 @@ li { p:last-child { margin-bottom: unset; } + +.open-sibling-dialog { + cursor: pointer; + text-decoration: underline; +} diff --git a/internal/components/combined.templ b/internal/components/combined.templ index eeacdb5..3beddcd 100644 --- a/internal/components/combined.templ +++ b/internal/components/combined.templ @@ -1,6 +1,7 @@ package components import ( + "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/index" "go.alanpearce.eu/searchix/internal/nix" ) @@ -11,6 +12,9 @@ templ Combined(result *index.Result) { <tr> <th scope="col">Attribute</th> <th scope="col">Description</th> + if config.DevMode { + <th scope="col">Score</th> + } </tr> </thead> <tbody> @@ -31,6 +35,11 @@ templ Combined(result *index.Result) { } } </td> + if config.DevMode { + <td> + @score(hit) + </td> + } </tr> } </tbody> diff --git a/internal/components/dev.templ b/internal/components/dev.templ new file mode 100644 index 0000000..a03eb4b --- /dev/null +++ b/internal/components/dev.templ @@ -0,0 +1,18 @@ +package components + +import ( + "go.alanpearce.eu/searchix/internal/index" + "strconv" +) + +templ score(h index.DocumentMatch) { + <a class="open-sibling-dialog"> + { strconv.FormatFloat(h.Score, 'f', 2, 64) } + </a> + <dialog> + <button autofocus>Close</button> + <pre> + { h.Expl.String() } + </pre> + </dialog> +} diff --git a/internal/components/options.templ b/internal/components/options.templ index 8e31132..097f66f 100644 --- a/internal/components/options.templ +++ b/internal/components/options.templ @@ -1,6 +1,7 @@ package components import ( + "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/index" "go.alanpearce.eu/searchix/internal/nix" ) @@ -11,19 +12,22 @@ templ Options(result *index.Result) { <tr> <th scope="col">Title</th> <th scope="col">Description</th> + if config.DevMode { + <th scope="col">Score</th> + } </tr> </thead> <tbody> for _, hit := range result.Hits { if m := convertMatch[nix.Option](hit.Data); m != nil { - @optionRow(*m) + @optionRow(hit, *m) } } </tbody> </table> } -templ optionRow(o nix.Option) { +templ optionRow(hit index.DocumentMatch, o nix.Option) { <tr> <td> @openDialogLink(o.Name) @@ -32,5 +36,10 @@ templ optionRow(o nix.Option) { @markdown(firstSentence(o.Description)) <dialog id={ o.Name }></dialog> </td> + if config.DevMode { + <td> + @score(hit) + </td> + } </tr> } diff --git a/internal/components/packages.templ b/internal/components/packages.templ index b90fcd5..6e14026 100644 --- a/internal/components/packages.templ +++ b/internal/components/packages.templ @@ -1,6 +1,7 @@ package components import ( + "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/index" "go.alanpearce.eu/searchix/internal/nix" ) @@ -12,19 +13,22 @@ templ Packages(result *index.Result) { <th scope="col">Attribute</th> <th scope="col">Name</th> <th scope="col">Description</th> + if config.DevMode { + <th scope="col">Score</th> + } </tr> </thead> <tbody> for _, hit := range result.Hits { if m := convertMatch[nix.Package](hit.Data); m != nil { - @packageRow(*m) + @packageRow(hit, *m) } } </tbody> </table> } -templ packageRow(p nix.Package) { +templ packageRow(hit index.DocumentMatch, p nix.Package) { <tr> <td> @openDialogLink(p.Attribute) @@ -35,5 +39,10 @@ templ packageRow(p nix.Package) { <td> { p.Description } </td> + if config.DevMode { + <td> + @score(hit) + </td> + } </tr> } diff --git a/internal/components/page.templ b/internal/components/page.templ index b6c9a82..127d1ba 100644 --- a/internal/components/page.templ +++ b/internal/components/page.templ @@ -15,7 +15,12 @@ templ Page(tdata TemplateData) { <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> - <title>Searchix</title> + <title> + Searchix + if config.DevMode { + (Dev) + } + </title> for _, sheet := range tdata.Assets.Stylesheets { <link href={ sheet.URL } rel="stylesheet" integrity={ "sha256-" + sheet.Base64SHA256 }/> } diff --git a/internal/config/config.go b/internal/config/config.go index b5f2ee4..33ce1da 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,6 +12,7 @@ import ( ) var Version string +var DevMode bool type URL struct { *url.URL @@ -127,6 +128,8 @@ func GetConfig(filename string, log *log.Logger) (*Config, error) { } } + DevMode = config.Web.Environment == "development" + config.Web.ContentSecurityPolicy.ScriptSrc = append( config.Web.ContentSecurityPolicy.ScriptSrc, config.Web.BaseURL.JoinPath("/static/").String(), |