feat: light icons on the dark cards
A link without an icon of its own gets the library's light version when there is one (github-light), instead of a dark logo on a dark card. The server keeps the library's list of names for a day.
This commit is contained in:
@@ -68,7 +68,8 @@ Build it yourself: `docker build -t dahoam .`
|
|||||||
first. Keyboard: Space on the grip next to the plus, arrow keys, Space.
|
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
|
- **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
|
[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).
|
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`,
|
- **Search**: `/` focuses the bar. `!w cats` uses DuckDuckGo bangs. `/settings`, `/add link`,
|
||||||
`/add card`, `/add dashboard` and `/sign out` run commands.
|
`/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
|
- 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.
|
(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
|
- 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
|
- 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.
|
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),
|
- UI components come from [collabry-components](https://git.nhaiden.io/niklas/collabry-components),
|
||||||
|
|||||||
@@ -237,8 +237,9 @@ function LinkForm({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<FieldDescription>
|
<FieldDescription>
|
||||||
Empty: found from the name, then from the site's favicon. Upload a
|
Empty: found from the name (the light version, when the library has
|
||||||
PNG, JPEG, WebP, AVIF or SVG, up to 1 MB.
|
one), then from the site's favicon. Upload a PNG, JPEG, WebP, AVIF
|
||||||
|
or SVG, up to 1 MB.
|
||||||
</FieldDescription>
|
</FieldDescription>
|
||||||
</Field>
|
</Field>
|
||||||
<Field>
|
<Field>
|
||||||
|
|||||||
@@ -12,14 +12,16 @@ export type HomeLink = {
|
|||||||
name: string
|
name: string
|
||||||
url: string
|
url: string
|
||||||
icon: string | null
|
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 }
|
type IconProps = { link: HomeLink; className?: string }
|
||||||
|
|
||||||
/** Starts the fallback chain again when the link changes. */
|
/** Starts the fallback chain again when the link changes. */
|
||||||
export function LinkIcon(props: IconProps) {
|
export function LinkIcon(props: IconProps) {
|
||||||
const { id, name, url, icon } = props.link
|
const { id, name, url, icon, lightIcon } = props.link
|
||||||
return <Icon key={[id, name, url, icon].join("|")} {...props} />
|
return <Icon key={[id, name, url, icon, lightIcon].join("|")} {...props} />
|
||||||
}
|
}
|
||||||
|
|
||||||
function Icon({ link, className }: IconProps) {
|
function Icon({ link, className }: IconProps) {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ import {
|
|||||||
} from "@/db/schema"
|
} from "@/db/schema"
|
||||||
import { auth, requireUser, signUpEnabled, ssoName } from "@/lib/auth"
|
import { auth, requireUser, signUpEnabled, ssoName } from "@/lib/auth"
|
||||||
import { CLOCK_FONTS, CLOCK_STYLES } from "@/lib/clock"
|
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"
|
import { ENGINES, parseSuggestions } from "@/lib/search"
|
||||||
|
|
||||||
const httpUrl = z.url({ protocol: /^https?$/ }).max(2000)
|
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 byDefault = find(s?.defaultDashboardId) ?? dashboards[0]
|
||||||
const current = find(data?.dashboardId) ?? byDefault
|
const current = find(data?.dashboardId) ?? byDefault
|
||||||
const dashboardId = current.id
|
const dashboardId = current.id
|
||||||
const [cards, links] = await Promise.all([
|
const [library, cards, links] = await Promise.all([
|
||||||
|
libraryNames(),
|
||||||
db
|
db
|
||||||
.select({ id: card.id, name: card.name })
|
.select({ id: card.id, name: card.name })
|
||||||
.from(card)
|
.from(card)
|
||||||
@@ -130,7 +133,13 @@ export const getDashboard = createServerFn({ method: "GET" })
|
|||||||
defaultDashboardId: byDefault.id,
|
defaultDashboardId: byDefault.id,
|
||||||
cards: cards.map((c) => ({
|
cards: cards.map((c) => ({
|
||||||
...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",
|
title: s?.title ?? "Dahoam",
|
||||||
searchEngine: s?.searchEngine ?? "duckduckgo",
|
searchEngine: s?.searchEngine ?? "duckduckgo",
|
||||||
|
|||||||
@@ -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<string>; 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
|
||||||
|
}
|
||||||
+23
-1
@@ -1,6 +1,11 @@
|
|||||||
import assert from "node:assert/strict"
|
import assert from "node:assert/strict"
|
||||||
import { test } from "node:test"
|
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 link = { name: "Home Assistant", url: "https://ha.example.com/lovelace" }
|
||||||
const first = (icon: string | null) => iconSources({ ...link, icon })[0]
|
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", () => {
|
test("an icon name is looked up in the library", () => {
|
||||||
assert.match(first(" Plex "), /\/png\/plex\.png$/)
|
assert.match(first(" Plex "), /\/png\/plex\.png$/)
|
||||||
})
|
})
|
||||||
|
|||||||
+15
-1
@@ -17,6 +17,15 @@ const slug = (name: string) =>
|
|||||||
|
|
||||||
export const libraryIcon = (name: string) => `${LIBRARY}/png/${slug(name)}.png`
|
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<string>, 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. */
|
/** An image address or an upload, not a name in the library. */
|
||||||
export const isImageAddress = (icon: string) =>
|
export const isImageAddress = (icon: string) =>
|
||||||
/^https?:\/\//.test(icon) || icon.startsWith(UPLOADED_ICON)
|
/^https?:\/\//.test(icon) || icon.startsWith(UPLOADED_ICON)
|
||||||
@@ -26,13 +35,18 @@ export function iconSources({
|
|||||||
name,
|
name,
|
||||||
url,
|
url,
|
||||||
icon,
|
icon,
|
||||||
|
lightIcon,
|
||||||
}: {
|
}: {
|
||||||
name: string
|
name: string
|
||||||
url: string
|
url: string
|
||||||
icon: string | null
|
icon: string | null
|
||||||
|
/** see lightVariant; only used when the link has no icon of its own */
|
||||||
|
lightIcon?: string | null
|
||||||
}) {
|
}) {
|
||||||
return [
|
return [
|
||||||
icon && isImageAddress(icon) ? icon : libraryIcon(icon || name),
|
icon && isImageAddress(icon)
|
||||||
|
? icon
|
||||||
|
: libraryIcon(icon || lightIcon || name),
|
||||||
`${new URL(url).origin}/favicon.ico`,
|
`${new URL(url).origin}/favicon.ico`,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user