From af73a386ed70c9b1e2571d2b71b92ce831a53012 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Sat, 21 Nov 2026 19:21:37 +0100 Subject: [PATCH] feat: export and import of dashboards, also from browser bookmarks Settings downloads all dashboards with their cards and links as one JSON file and loads such a file again. The bookmarks file of a browser becomes a dashboard with a card per folder. An import only adds. --- README.md | 6 ++ src/lib/dashboard.functions.ts | 89 +++++++++++++++++++++++++ src/lib/transfer.test.ts | 99 ++++++++++++++++++++++++++++ src/lib/transfer.ts | 116 ++++++++++++++++++++++++++++++++ src/routes/settings.tsx | 117 +++++++++++++++++++++++++++++++++ 5 files changed, 427 insertions(+) create mode 100644 src/lib/transfer.test.ts create mode 100644 src/lib/transfer.ts diff --git a/README.md b/README.md index c03119c..392d268 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,12 @@ Build it yourself: `docker build -t dahoam .` 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 Settings → Wallpaper has a switch for the others. +- **Export and import**: Settings → Export and import downloads all dashboards with their + cards and links as one JSON file, and loads such a file again. It also takes the bookmarks + file that a browser exports (HTML) and makes a dashboard "Bookmarks" of it, with a card per + folder. An import adds dashboards and replaces nothing. Uploaded wallpapers and icons are + not part of an export. +- **Password**: Settings → Account changes it. That signs you out on your other devices. - **Clock**: Settings has five fonts and three styles. Glass shows the wallpaper blurred through the digits, Tinted takes its color from the wallpaper. diff --git a/src/lib/dashboard.functions.ts b/src/lib/dashboard.functions.ts index 7a51b2b..4a7eb3a 100644 --- a/src/lib/dashboard.functions.ts +++ b/src/lib/dashboard.functions.ts @@ -17,6 +17,8 @@ import { libraryNames } from "@/lib/icon-library" import { lightVariant } from "@/lib/icons" import { ENGINES, parseSuggestions } from "@/lib/search" import { isReachable, statusChecks } from "@/lib/status" +import { backupSchema, portable } from "@/lib/transfer" +import type { Backup } from "@/lib/transfer" const httpUrl = z.url({ protocol: /^https?$/ }).max(2000) @@ -361,6 +363,93 @@ export const saveWallpaper = createServerFn({ method: "POST" }) await db.delete(wallpaper).where(eq(wallpaper.dashboardId, dashboardId)) }) +/** Every dashboard of the user with its cards and links, see backupSchema. */ +export const exportData = createServerFn({ method: "GET" }).handler( + async (): Promise => { + const user = await requireUser() + const [dashboards, cards, links] = await Promise.all([ + db + .select() + .from(dashboard) + .where(eq(dashboard.userId, user.id)) + .orderBy(asc(dashboard.createdAt)), + db + .select() + .from(card) + .where(eq(card.userId, user.id)) + .orderBy(asc(card.position), asc(card.createdAt)), + db + .select() + .from(link) + .where(eq(link.userId, user.id)) + .orderBy(asc(link.position), asc(link.createdAt)), + ]) + return { + dashboards: dashboards.map((d) => ({ + name: d.name, + wallpaperUrl: portable(d.wallpaperUrl), + wallpaperDim: d.wallpaperDim, + cards: cards + .filter((c) => c.dashboardId === d.id) + .map((c) => ({ + name: c.name, + links: links + .filter((l) => l.cardId === c.id) + .map((l) => ({ + name: l.name, + url: l.url, + icon: portable(l.icon), + })), + })), + })), + } + } +) + +/** Adds the dashboards of an export or of a bookmarks file. Nothing is replaced. */ +export const importData = createServerFn({ method: "POST" }) + .validator(backupSchema) + .handler(async ({ data }) => { + const user = await requireUser() + const userId = user.id + const added = { dashboards: data.dashboards.length, cards: 0, links: 0 } + // all of the file or nothing of it + await db.transaction(async (tx) => { + for (const d of data.dashboards) { + const [{ id: dashboardId }] = await tx + .insert(dashboard) + .values({ + userId, + name: d.name, + wallpaperUrl: d.wallpaperUrl, + wallpaperDim: d.wallpaperDim, + }) + .returning({ id: dashboard.id }) + for (const [position, c] of d.cards.entries()) { + const [{ id: cardId }] = await tx + .insert(card) + .values({ userId, dashboardId, position, name: c.name }) + .returning({ id: card.id }) + added.cards++ + added.links += c.links.length + if (c.links.length) + await tx.insert(link).values( + c.links.map((l, at) => ({ + userId, + cardId, + position: at, + name: l.name, + url: l.url, + // an upload of another account would not load here + icon: portable(l.icon), + })) + ) + } + } + }) + return added + }) + export const suggest = createServerFn({ method: "GET" }) .validator(z.object({ q: z.string().max(200), searxng: z.boolean() })) .handler(async ({ data }) => { diff --git a/src/lib/transfer.test.ts b/src/lib/transfer.test.ts new file mode 100644 index 0000000..831841c --- /dev/null +++ b/src/lib/transfer.test.ts @@ -0,0 +1,99 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { backupSchema, parseBookmarks, portable } from "./transfer.ts" + +// what Firefox and Chrome write, shortened +const BOOKMARKS = ` + +Bookmarks +

Bookmarks

+

+

Top level +

Bookmarks bar

+

+

Git & "CI" ü +

Media

+

+

Plex +
A bookmarklet +
+

+

Most visited +

+

Empty folder

+

+

+

` + +test("parseBookmarks: a card per folder that has bookmarks", () => { + const found = parseBookmarks(BOOKMARKS) + assert.ok(found) + assert.equal(found.skipped, 2) // the bookmarklet and place: + assert.deepEqual(found.backup, { + dashboards: [ + { + name: "Bookmarks", + cards: [ + { + name: "Bookmarks", + links: [{ name: "Top level", url: "https://example.com/top" }], + }, + { + name: "Bookmarks bar", + links: [ + { name: 'Git & "CI" ü', url: "https://git.example.com/?a=1&b=2" }, + ], + }, + { + name: "Media", + links: [ + { name: "Plex", url: "http://plex.lan:32400/web" }, + { + name: "nameless.example.com", + url: "https://nameless.example.com/x", + }, + ], + }, + ], + }, + ], + }) + // what it makes is what an import takes + assert.ok(backupSchema.safeParse(found.backup).success) +}) + +test("parseBookmarks: limits, long names, files without bookmarks", () => { + assert.equal(parseBookmarks("Hello"), null) + assert.equal(parseBookmarks('{"dashboards": []}'), null) + const many = Array.from( + { length: 502 }, + (_, i) => `

${"n".repeat(70)}` + ).join("") + const found = parseBookmarks(`
${many}
`) + assert.ok(found) + assert.equal(found.backup.dashboards[0].cards[0].links.length, 500) + assert.equal(found.backup.dashboards[0].cards[0].links[0].name.length, 60) + assert.equal(found.skipped, 2) + assert.ok(backupSchema.safeParse(found.backup).success) +}) + +test("backupSchema refuses what is not a web address", () => { + const withUrl = (url: string) => ({ + dashboards: [ + { name: "D", cards: [{ name: "C", links: [{ name: "L", url }] }] }, + ], + }) + assert.ok(backupSchema.safeParse(withUrl("https://example.com")).success) + assert.ok(!backupSchema.safeParse(withUrl("javascript:alert(1)")).success) + assert.ok(!backupSchema.safeParse({ dashboards: [] }).success) +}) + +test("portable: uploads stay with their account", () => { + assert.equal(portable("/api/icon?id=1"), null) + assert.equal(portable("plex"), "plex") + assert.equal( + portable("https://example.com/a.png"), + "https://example.com/a.png" + ) + assert.equal(portable(null), null) +}) diff --git a/src/lib/transfer.ts b/src/lib/transfer.ts new file mode 100644 index 0000000..e470d9b --- /dev/null +++ b/src/lib/transfer.ts @@ -0,0 +1,116 @@ +// Export and import: the shape of the file, and how a browser's bookmarks become one. +import { z } from "zod" + +const httpUrl = z.url({ protocol: /^https?$/ }).max(2000) +const MAX_CARDS = 100 +const MAX_LINKS = 500 // per card + +/** + * What an export holds and an import takes: dashboards with their cards and links. + * Uploaded wallpapers and icons are not in it, they belong to one account. + */ +export const backupSchema = z.object({ + dashboards: z + .array( + z.object({ + name: z.string().trim().min(1).max(40), + // an image on the web; null or missing: the default photo + wallpaperUrl: httpUrl.nullish(), + wallpaperDim: z.int().min(0).max(100).optional(), + cards: z + .array( + z.object({ + name: z.string().trim().min(1).max(40), + links: z + .array( + z.object({ + name: z.string().trim().min(1).max(60), + url: httpUrl, + // an image on the web or a name in the icon library + icon: z.string().trim().max(2000).nullish(), + }) + ) + .max(MAX_LINKS), + }) + ) + .max(MAX_CARDS), + }) + ) + .min(1) + .max(20), +}) +export type Backup = z.infer + +/** An upload ("/api/icon?id=…") only works for the account that made it. */ +export const portable = (address?: string | null) => + !address || address.startsWith("/") ? null : address + +const ENTITIES: Record = { + amp: "&", + lt: "<", + gt: ">", + quot: '"', + apos: "'", +} +const text = (html: string) => + html + .replace(/<[^>]*>/g, "") + .replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (entity, code: string) => { + if (code[0] !== "#") return ENTITIES[code.toLowerCase()] ?? entity + const point = + code[1].toLowerCase() === "x" + ? parseInt(code.slice(2), 16) + : parseInt(code.slice(1), 10) + return point <= 0x10ffff ? String.fromCodePoint(point) : entity + }) + .trim() + +/** + * The bookmarks file that every browser exports (HTML) as one dashboard: a card per + * folder that has bookmarks, named like the folder. `skipped` counts what was left out: + * addresses that are not http(s), and what is over the limits of an import. + * Null when the file has no bookmark in it. + */ +export function parseBookmarks(html: string) { + const cards = new Map() + const folders: string[] = [] // the path to where we are + let heading: string | null = null // a folder name that waits for its
+ let skipped = 0 + const tags = + /|]*>([\s\S]*?)<\/H3>|]*)>([\s\S]*?)<\/A>/gi + for (const [tag, folder, attributes, title] of html.matchAll(tags)) { + if (/^
= MAX_LINKS || + (!cards.has(path) && cards.size >= MAX_CARDS) + ) { + skipped++ + continue + } + cards.set(path, card) + card.links.push({ + name: (text(title) || new URL(url.data).hostname).slice(0, 60), + url: url.data, + }) + } + } + if (!cards.size) return null + const backup: Backup = { + dashboards: [{ name: "Bookmarks", cards: [...cards.values()] }], + } + return { backup, skipped } +} diff --git a/src/routes/settings.tsx b/src/routes/settings.tsx index ffdaeab..6bf0a25 100644 --- a/src/routes/settings.tsx +++ b/src/routes/settings.tsx @@ -8,10 +8,12 @@ import { useRouter, } from "@tanstack/react-router" import { + ArrowDownUp, ArrowLeft, Check, CircleUser, Clock, + Download, Image, LayoutDashboard, LogOut, @@ -33,13 +35,17 @@ import { import type { ClockFont, ClockStyle } from "@/lib/clock" import { deleteDashboard, + exportData, getDashboard, + importData, saveDashboard, saveSettings, saveWallpaper, } from "@/lib/dashboard.functions" import { ENGINES, ENGINE_NAMES } from "@/lib/search" import type { Engine } from "@/lib/search" +import { parseBookmarks } from "@/lib/transfer" +import type { Backup } from "@/lib/transfer" import { Button } from "@/vendor/collabry/components/Button/Button" import { Field, @@ -95,6 +101,7 @@ const SECTIONS = [ { id: "clock", label: "Clock", icon: Clock }, { id: "search", label: "Search", icon: Search }, { id: "wallpaper", label: "Wallpaper", icon: Image }, + { id: "transfer", label: "Export and import", icon: ArrowDownUp }, { id: "account", label: "Account", icon: CircleUser }, ] @@ -130,6 +137,9 @@ function Settings() { const router = useRouter() const hash = useLocation({ select: (l) => l.hash }) || SECTIONS[0].id const file = useRef(null) + const importFile = useRef(null) + // what the last import added + const [imported, setImported] = useState("") const [status, setStatus] = useState<"idle" | "saving" | "saved">("idle") const [error, setError] = useState("") // follows the slider at once, the save comes a moment later @@ -174,6 +184,47 @@ function Settings() { const uploaded = !!data.wallpaperUrl?.startsWith("/") + const download = async () => { + const backup = await exportData() + const url = URL.createObjectURL( + new Blob([JSON.stringify(backup, null, 2)], { type: "application/json" }) + ) + const a = document.createElement("a") + a.href = url + a.download = `dahoam-${new Date().toISOString().slice(0, 10)}.json` + a.click() + URL.revokeObjectURL(url) + } + // an export of this app (JSON) or the bookmarks file of a browser (HTML) + const importFrom = (chosen: File) => + run(async () => { + setImported("") + if (chosen.size > 5 * 1024 * 1024) return "The file is larger than 5 MB." + const content = await chosen.text() + let backup: unknown + let skipped = 0 + try { + backup = JSON.parse(content) + } catch { + const bookmarks = parseBookmarks(content) + if (!bookmarks) return "This file has no dashboards or bookmarks in it." + ;({ backup, skipped } = bookmarks) + } + // whether it really is one, the server checks + const added = await importData({ data: backup as Backup }).catch( + () => null + ) + if (!added) return "This file is not an export of Dahoam." + const count = (n: number, thing: string) => + `${n} ${thing}${n === 1 ? "" : "s"}` + setImported( + `Added ${count(added.dashboards, "dashboard")} with ${count(added.cards, "card")} and ${count(added.links, "link")}.` + + (skipped + ? ` ${count(skipped, "bookmark")} left out: not a web address, or more than a card holds.` + : "") + ) + }) + return ( // A page of its own: solid Collabry surfaces, no wallpaper behind it
@@ -616,6 +667,72 @@ function Settings() { + + + Export and import + + Keep a copy of your dashboards, move them to another + account, or start from the bookmarks of your browser. + + + + +
Export
+ + One file with all dashboards, their cards and links. + Images that you uploaded are not in it. + +
+ + + +
+ + +
Import
+ + An export of Dahoam, or the bookmarks file that your + browser exports (HTML): a dashboard "Bookmarks" with a + card per folder. An import adds dashboards, it replaces + nothing. + + {imported && ( + + {imported} + + )} +
+ + { + const chosen = e.target.files?.[0] + if (chosen) void importFrom(chosen) + e.target.value = "" + }} + /> + + +
+
+ Account