all repos — searchix @ 58643f38bdd62dc538a03865b1f81eb77d957779

Search engine for NixOS, nix-darwin, home-manager and NUR users

feat: show scores in results table in dev mode
Alan Pearce alan@alanpearce.eu
Tue, 31 Dec 2024 14:56:37 +0100
commit

58643f38bdd62dc538a03865b1f81eb77d957779

parent

1872c47e393e9c4c811f3b90ec2e0ac4cd8767b2

M frontend/static/search.jsfrontend/static/search.js
@@ -28,9 +28,20 @@ createHTML: (string) => string,   });
 }
 
+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);
   });
 }
 
M frontend/static/style.cssfrontend/static/style.css
@@ -185,3 +185,8 @@ p:last-child {
   margin-bottom: unset;
 }
+
+.open-sibling-dialog {
+  cursor: pointer;
+  text-decoration: underline;
+}
M internal/components/combined.templinternal/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 @@ <thead> 			<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 @@ { firstSentence(o.Description) } 								}
 						}
 					</td>
+					if config.DevMode {
+						<td>
+							@score(hit)
+						</td>
+					}
 				</tr>
 			}
 		</tbody>
A 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>
+}
M internal/components/options.templinternal/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 @@ <thead> 			<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 @@ <td> 			@markdown(firstSentence(o.Description))
 			<dialog id={ o.Name }></dialog>
 		</td>
+		if config.DevMode {
+			<td>
+				@score(hit)
+			</td>
+		}
 	</tr>
 }
M internal/components/packages.templinternal/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 @@ <tr> 				<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 @@ </td> 		<td>
 			{ p.Description }
 		</td>
+		if config.DevMode {
+			<td>
+				@score(hit)
+			</td>
+		}
 	</tr>
 }
M internal/components/page.templinternal/components/page.templ
@@ -15,7 +15,12 @@ <html lang="en-GB"> 		<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 }/>
 			}
M internal/config/config.gointernal/config/config.go
@@ -12,6 +12,7 @@ "go.alanpearce.eu/x/log" )
 
 var Version string
+var DevMode bool
 
 type URL struct {
 	*url.URL
@@ -126,6 +127,8 @@ 			return nil, errors.Wrap(err, "config error")
 		}
 	}
+
+	DevMode = config.Web.Environment == "development"
 
 	config.Web.ContentSecurityPolicy.ScriptSrc = append(
 		config.Web.ContentSecurityPolicy.ScriptSrc,