feat: several dashboards with a default, icon search and icon upload
Dashboards are separate sets of cards (Home, Work). Settings manages them and sets the one that opens at sign-in, the header and the search bar switch. Migration 0006 puts the existing cards on a dashboard "Home". The link dialog searches dashboard-icons and uploads custom icons (/api/icon, up to 1 MB, SVG served sandboxed). Wallpaper and icons share the upload checks.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// Where a link's icon comes from. Pure, so it can be tested without a browser.
|
||||
|
||||
const LIBRARY = "https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons"
|
||||
|
||||
/** JSON with the file names of every icon in the library: `{ png: ["plex.png", …] }`. */
|
||||
export const LIBRARY_INDEX = `${LIBRARY}/tree.json`
|
||||
|
||||
/** How a link stores an icon that the user uploaded: this plus the icon's id. */
|
||||
export const UPLOADED_ICON = "/api/icon?id="
|
||||
|
||||
const slug = (name: string) =>
|
||||
name
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-|-$/g, "")
|
||||
|
||||
export const libraryIcon = (name: string) => `${LIBRARY}/png/${slug(name)}.png`
|
||||
|
||||
/** An image address or an upload, not a name in the library. */
|
||||
export const isImageAddress = (icon: string) =>
|
||||
/^https?:\/\//.test(icon) || icon.startsWith(UPLOADED_ICON)
|
||||
|
||||
/** Icon sources to try in order; the letter tile is the last resort. */
|
||||
export function iconSources({
|
||||
name,
|
||||
url,
|
||||
icon,
|
||||
}: {
|
||||
name: string
|
||||
url: string
|
||||
icon: string | null
|
||||
}) {
|
||||
return [
|
||||
icon && isImageAddress(icon) ? icon : libraryIcon(icon || name),
|
||||
`${new URL(url).origin}/favicon.ico`,
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* Library icons for what the user typed: names that start with it, then names that
|
||||
* contain it. In both groups the shortest first, so "plex" is before "plex-alt".
|
||||
*/
|
||||
export function searchIcons(names: string[], query: string, limit = 42) {
|
||||
const q = slug(query)
|
||||
const rank = (n: string) => (n.startsWith(q) ? 0 : 1000) + n.length
|
||||
return names
|
||||
.filter((n) => n.includes(q))
|
||||
.sort((a, b) => rank(a) - rank(b))
|
||||
.slice(0, limit)
|
||||
}
|
||||
Reference in New Issue
Block a user