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:
@@ -237,8 +237,9 @@ function LinkForm({
|
||||
</div>
|
||||
)}
|
||||
<FieldDescription>
|
||||
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.
|
||||
</FieldDescription>
|
||||
</Field>
|
||||
<Field>
|
||||
|
||||
@@ -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 <Icon key={[id, name, url, icon].join("|")} {...props} />
|
||||
const { id, name, url, icon, lightIcon } = props.link
|
||||
return <Icon key={[id, name, url, icon, lightIcon].join("|")} {...props} />
|
||||
}
|
||||
|
||||
function Icon({ link, className }: IconProps) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 { 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$/)
|
||||
})
|
||||
|
||||
+15
-1
@@ -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<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. */
|
||||
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`,
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user