diff --git a/README.md b/README.md index 5786fb7..b8ffbeb 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,11 @@ Build it yourself: `docker build -t dahoam .` [dashboard-icons](https://github.com/homarr-labs/dashboard-icons), then from the site's `/favicon.ico`. Or type in the field to search that library and click an icon, paste an image URL, or upload your own image (PNG, JPEG, WebP, AVIF or SVG, up to 1 MB). -- **Search**: `/` focuses the bar. `!w cats` uses DuckDuckGo bangs. `/plex` jumps to - a link, `/settings`, `/add link`, `/add card`, `/add dashboard` and `/sign out` run commands. +- **Search**: `/` focuses the bar. `!w cats` uses DuckDuckGo bangs. `/settings`, `/add link`, + `/add card`, `/add dashboard` and `/sign out` run commands. +- **Open a link from the search bar**: type the start of its name (`jel` for Jellyfin). The + link is highlighted and Enter opens it. To search the web for the same word, press the up + arrow first, so that no row is highlighted. `/jel` shows only your links and commands. - **Wallpaper**: Settings (or the pencil at the bottom right) takes an image URL or an upload (JPEG, PNG, WebP, AVIF, up to 10 MB). A slider sets how much it is darkened. Each dashboard has its own: the pencil opens the one of the dashboard you are on, and diff --git a/src/components/search-bar.tsx b/src/components/search-bar.tsx index bcda541..00ff806 100644 --- a/src/components/search-bar.tsx +++ b/src/components/search-bar.tsx @@ -7,7 +7,12 @@ import { cn } from "@/vendor/collabry/lib/cn" import { LinkIcon } from "@/components/link-tile" import type { HomeLink } from "@/components/link-tile" import { suggest } from "@/lib/dashboard.functions" -import { ENGINE_NAMES, effectiveEngine, searchUrl } from "@/lib/search" +import { + ENGINE_NAMES, + effectiveEngine, + matchLinks, + searchUrl, +} from "@/lib/search" import type { Engine } from "@/lib/search" export type Command = { label: string; icon: ReactNode; run: () => void } @@ -25,7 +30,8 @@ export function SearchBar({ }) { const [value, setValue] = useState("") const [fetched, setFetched] = useState([]) - const [active, setActive] = useState(-1) + // the row that the arrow keys or the mouse chose, null until then + const [moved, setMoved] = useState(null) const [focused, setFocused] = useState(false) const input = useRef(null) @@ -57,7 +63,7 @@ export function SearchBar({ }).catch(() => []) if (stale) return setFetched(result) - setActive(-1) // the highlighted row now holds another suggestion + setMoved(null) // the highlighted row now holds another suggestion }, 150) return () => { stale = true @@ -66,23 +72,34 @@ export function SearchBar({ }, [query, commandMode, used]) const needle = value.slice(1).trim().toLowerCase() + const matched = matchLinks(links, commandMode ? needle : query) + const linkItem = (link: HomeLink): Command => ({ + label: link.name, + icon: , + run: () => window.location.assign(link.url), + }) const items: Command[] = commandMode ? [ - ...links.map((link) => ({ - label: link.name, - icon: , - run: () => window.location.assign(link.url), - })), - ...commands, - ].filter((item) => item.label.toLowerCase().includes(needle)) + ...matched.links.map(linkItem), + ...commands.filter((c) => c.label.toLowerCase().includes(needle)), + ] : query - ? fetched.map((s) => ({ - label: s, - icon: , - run: () => search(s), - })) + ? [ + // a few, so the suggestions stay in view + ...matched.links.slice(0, 4).map(linkItem), + ...fetched.map((s) => ({ + label: s, + icon: , + run: () => search(s), + })), + ] : [] const open = focused && items.length > 0 + // Enter takes the highlighted row. Until the user moves, that is the best match: + // with / the first row, else a link whose name starts with what was typed. + // Without one, Enter searches the web. + const best = commandMode ? items.length > 0 : matched.starts > 0 + const active = moved ?? (best ? 0 : -1) const run = (item: Command) => { setValue("") @@ -96,9 +113,7 @@ export function SearchBar({ className="relative w-full max-w-[680px]" onSubmit={(e) => { e.preventDefault() - // in command mode Enter takes the best match - const item = - active >= 0 ? items[active] : commandMode ? items[0] : undefined + const item = active >= 0 ? items[active] : undefined if (item) run(item) else if (query && !commandMode) search(query) }} @@ -115,7 +130,7 @@ export function SearchBar({ value={value} onChange={(e) => { setValue(e.target.value) - setActive(-1) + setMoved(null) }} onFocus={() => setFocused(true)} onBlur={() => setFocused(false)} @@ -124,7 +139,10 @@ export function SearchBar({ if (!open || (e.key !== "ArrowDown" && e.key !== "ArrowUp")) return e.preventDefault() const step = e.key === "ArrowDown" ? 1 : -1 - setActive((active + step + items.length) % items.length) + // the rows and, between the last and the first, no row: Enter then + // searches what was typed, also when a link matches it + const stops = items.length + 1 + setMoved(((active + 1 + step + stops) % stops) - 1) }} placeholder={`Search ${ENGINE_NAMES[used]}, or type / for commands`} aria-label="Search" @@ -159,7 +177,7 @@ export function SearchBar({ role="option" aria-selected={i === active} onClick={() => run(item)} - onMouseMove={() => setActive(i)} + onMouseMove={() => setMoved(i)} className={cn( "flex cursor-pointer items-center gap-3 rounded-xl px-3.5 py-2.5", i === active && "bg-white/10" diff --git a/src/lib/search.test.ts b/src/lib/search.test.ts index e075032..781dc34 100644 --- a/src/lib/search.test.ts +++ b/src/lib/search.test.ts @@ -1,6 +1,27 @@ import assert from "node:assert/strict" import { test } from "node:test" -import { parseSuggestions, searchUrl } from "./search.ts" +import { matchLinks, parseSuggestions, searchUrl } from "./search.ts" + +test("matchLinks", () => { + const links = ["Grafana", "Home Assistant", "IT-Tools", "Sonarr"].map( + (name) => ({ name }) + ) + const names = (typed: string) => { + const { links: found, starts } = matchLinks(links, typed) + return [found.map((l) => l.name).join(","), starts] + } + // the start of the name or of a word in it comes first and counts as a start + assert.deepEqual(names("so"), ["Sonarr", 1]) + assert.deepEqual(names(" ASSIST "), ["Home Assistant", 1]) + assert.deepEqual(names("tools"), ["IT-Tools", 1]) + // only inside a word: listed, but Enter does not open it + assert.deepEqual(names("ana"), ["Grafana", 0]) + assert.deepEqual(names("a"), ["Home Assistant,Grafana,Sonarr", 1]) + assert.deepEqual(names("plex"), ["", 0]) + // a query with a space is one phrase + assert.deepEqual(names("home a"), ["Home Assistant", 1]) + assert.deepEqual(names("grafana docs"), ["", 0]) +}) test("searchUrl", () => { assert.equal( diff --git a/src/lib/search.ts b/src/lib/search.ts index 56b08b3..ea35b72 100644 --- a/src/lib/search.ts +++ b/src/lib/search.ts @@ -29,6 +29,26 @@ export function searchUrl( return `https://duckduckgo.com/?q=${q}` } +/** + * The links for what was typed. First the ones where the name or a word of it starts + * with it (`starts` of them: Enter opens the first), then the ones that contain it. + */ +export function matchLinks( + links: TLink[], + typed: string +) { + const q = typed.trim().toLowerCase() + const starts: TLink[] = [] + const contains: TLink[] = [] + for (const link of links) { + const name = link.name.toLowerCase() + const words = [name, ...name.split(/[^\p{L}\p{N}]+/u)] + if (words.some((word) => word.startsWith(q))) starts.push(link) + else if (name.includes(q)) contains.push(link) + } + return { links: [...starts, ...contains], starts: starts.length } +} + /** Both DuckDuckGo and SearXNG answer in OpenSearch form: [query, [suggestions]] */ export function parseSuggestions(body: unknown): string[] { const list = Array.isArray(body) && Array.isArray(body[1]) ? body[1] : body