diff --git a/README.md b/README.md
index b8ffbeb..5c8ca1f 100644
--- a/README.md
+++ b/README.md
@@ -68,7 +68,8 @@ Build it yourself: `docker build -t dahoam .`
first. Keyboard: Space on the grip next to the plus, arrow keys, Space.
- **Icons**: leave the field empty and the icon is looked up by name in
[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,
+ site's `/favicon.ico`. When the library has a light version of the icon (`github-light`),
+ that one is used, because the cards are dark. 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. `/settings`, `/add link`,
`/add card`, `/add dashboard` and `/sign out` run commands.
@@ -89,7 +90,7 @@ Build it yourself: `docker build -t dahoam .`
- SSO does not take over an existing password account with the same email
(Better Auth blocks it, because password emails are not verified). Use one or the other per email.
- Icon lookups send the link name to the jsDelivr CDN. The link dialog also loads the
- library's list of icons from there.
+ library's list of icons from there, and so does the server, once a day.
- Uploaded icons are stored in Postgres and only their owner can load them. One that no
link uses any more is deleted with that user's next upload.
- UI components come from [collabry-components](https://git.nhaiden.io/niklas/collabry-components),
diff --git a/src/components/link-dialog.tsx b/src/components/link-dialog.tsx
index b020458..616c445 100644
--- a/src/components/link-dialog.tsx
+++ b/src/components/link-dialog.tsx
@@ -237,8 +237,9 @@ function LinkForm({
)}
- Empty: found from the name, then from the site's favicon. Upload a
- PNG, JPEG, WebP, AVIF or SVG, up to 1 MB.
+ Empty: found from the name (the light version, when the library has
+ one), then from the site's favicon. Upload a PNG, JPEG, WebP, AVIF
+ or SVG, up to 1 MB.
diff --git a/src/components/link-tile.tsx b/src/components/link-tile.tsx
index 77c0821..0c4a77b 100644
--- a/src/components/link-tile.tsx
+++ b/src/components/link-tile.tsx
@@ -12,14 +12,16 @@ export type HomeLink = {
name: string
url: string
icon: string | null
+ /** the library's light version of the icon, when the link has no icon of its own */
+ lightIcon: string | null
}
type IconProps = { link: HomeLink; className?: string }
/** Starts the fallback chain again when the link changes. */
export function LinkIcon(props: IconProps) {
- const { id, name, url, icon } = props.link
- return
+ const { id, name, url, icon, lightIcon } = props.link
+ return
}
function Icon({ link, className }: IconProps) {
diff --git a/src/lib/dashboard.functions.ts b/src/lib/dashboard.functions.ts
index 49d4d70..5899123 100644
--- a/src/lib/dashboard.functions.ts
+++ b/src/lib/dashboard.functions.ts
@@ -13,6 +13,8 @@ import {
} from "@/db/schema"
import { auth, requireUser, signUpEnabled, ssoName } from "@/lib/auth"
import { CLOCK_FONTS, CLOCK_STYLES } from "@/lib/clock"
+import { libraryNames } from "@/lib/icon-library"
+import { lightVariant } from "@/lib/icons"
import { ENGINES, parseSuggestions } from "@/lib/search"
const httpUrl = z.url({ protocol: /^https?$/ }).max(2000)
@@ -92,7 +94,8 @@ export const getDashboard = createServerFn({ method: "GET" })
const byDefault = find(s?.defaultDashboardId) ?? dashboards[0]
const current = find(data?.dashboardId) ?? byDefault
const dashboardId = current.id
- const [cards, links] = await Promise.all([
+ const [library, cards, links] = await Promise.all([
+ libraryNames(),
db
.select({ id: card.id, name: card.name })
.from(card)
@@ -130,7 +133,13 @@ export const getDashboard = createServerFn({ method: "GET" })
defaultDashboardId: byDefault.id,
cards: cards.map((c) => ({
...c,
- links: links.filter((l) => l.cardId === c.id),
+ links: links
+ .filter((l) => l.cardId === c.id)
+ .map((l) => ({
+ ...l,
+ // an icon that the user chose stays as it is
+ lightIcon: l.icon ? null : lightVariant(library, l.name),
+ })),
})),
title: s?.title ?? "Dahoam",
searchEngine: s?.searchEngine ?? "duckduckgo",
diff --git a/src/lib/icon-library.ts b/src/lib/icon-library.ts
new file mode 100644
index 0000000..77ee187
--- /dev/null
+++ b/src/lib/icon-library.ts
@@ -0,0 +1,31 @@
+// Server side: what the icon library has. The link dialog loads the same index in the browser.
+import { LIBRARY_INDEX } from "@/lib/icons"
+
+const DAY = 24 * 60 * 60 * 1000
+let known: { names: Set; at: number } | undefined
+
+/**
+ * The names of the library's icons, kept in memory for a day. Empty when the
+ * library cannot be reached: the dashboard then shows the plain icons.
+ */
+export async function libraryNames() {
+ if (known && Date.now() - known.at < DAY) return known.names
+ try {
+ const res = await fetch(LIBRARY_INDEX, {
+ // the page waits for this once a day at most, so not for long
+ signal: AbortSignal.timeout(2000),
+ })
+ const index = (await res.json()) as { png: string[] }
+ known = {
+ names: new Set(index.png.map((file) => file.replace(/\.png$/, ""))),
+ at: Date.now(),
+ }
+ } catch {
+ // what was known before, and the next try in five minutes
+ known = {
+ names: known?.names ?? new Set(),
+ at: Date.now() - DAY + 5 * 60 * 1000,
+ }
+ }
+ return known.names
+}
diff --git a/src/lib/icons.test.ts b/src/lib/icons.test.ts
index 041d4b1..63ab5cd 100644
--- a/src/lib/icons.test.ts
+++ b/src/lib/icons.test.ts
@@ -1,6 +1,11 @@
import assert from "node:assert/strict"
import { test } from "node:test"
-import { UPLOADED_ICON, iconSources, searchIcons } from "./icons.ts"
+import {
+ UPLOADED_ICON,
+ iconSources,
+ lightVariant,
+ searchIcons,
+} from "./icons.ts"
const link = { name: "Home Assistant", url: "https://ha.example.com/lovelace" }
const first = (icon: string | null) => iconSources({ ...link, icon })[0]
@@ -12,6 +17,23 @@ test("no icon: looked up by name, then the site's favicon", () => {
])
})
+test("the light version is used for a link without an icon of its own", () => {
+ const library = new Set(["github", "github-light", "plex"])
+ assert.equal(lightVariant(library, " GitHub "), "github-light")
+ assert.equal(lightVariant(library, "Plex"), null)
+ const github = { name: "GitHub", url: "https://github.com" }
+ const lightIcon = "github-light"
+ assert.match(
+ iconSources({ ...github, icon: null, lightIcon })[0],
+ /\/png\/github-light\.png$/
+ )
+ // the user chose the dark one
+ assert.match(
+ iconSources({ ...github, icon: "github", lightIcon })[0],
+ /\/png\/github\.png$/
+ )
+})
+
test("an icon name is looked up in the library", () => {
assert.match(first(" Plex "), /\/png\/plex\.png$/)
})
diff --git a/src/lib/icons.ts b/src/lib/icons.ts
index 58dae01..acad54c 100644
--- a/src/lib/icons.ts
+++ b/src/lib/icons.ts
@@ -17,6 +17,15 @@ const slug = (name: string) =>
export const libraryIcon = (name: string) => `${LIBRARY}/png/${slug(name)}.png`
+/**
+ * The library has a light version of some icons (`github-light`), made for dark
+ * backgrounds like the cards. Its name for a link that is called `name`, or null.
+ */
+export function lightVariant(libraryNames: Set, name: string) {
+ const light = `${slug(name)}-light`
+ return libraryNames.has(light) ? light : null
+}
+
/** An image address or an upload, not a name in the library. */
export const isImageAddress = (icon: string) =>
/^https?:\/\//.test(icon) || icon.startsWith(UPLOADED_ICON)
@@ -26,13 +35,18 @@ export function iconSources({
name,
url,
icon,
+ lightIcon,
}: {
name: string
url: string
icon: string | null
+ /** see lightVariant; only used when the link has no icon of its own */
+ lightIcon?: string | null
}) {
return [
- icon && isImageAddress(icon) ? icon : libraryIcon(icon || name),
+ icon && isImageAddress(icon)
+ ? icon
+ : libraryIcon(icon || lightIcon || name),
`${new URL(url).origin}/favicon.ico`,
]
}