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.
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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<Backup> => {
|
||||
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 }) => {
|
||||
|
||||
@@ -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 = `<!DOCTYPE NETSCAPE-Bookmark-file-1>
|
||||
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
|
||||
<TITLE>Bookmarks</TITLE>
|
||||
<H1>Bookmarks</H1>
|
||||
<DL><p>
|
||||
<DT><A HREF="https://example.com/top" ADD_DATE="1">Top level</A>
|
||||
<DT><H3 ADD_DATE="1" PERSONAL_TOOLBAR_FOLDER="true">Bookmarks bar</H3>
|
||||
<DL><p>
|
||||
<DT><A HREF="https://git.example.com/?a=1&b=2" ICON="data:image/png;base64,AAAA">Git & "CI" ü</A>
|
||||
<DT><H3>Media</H3>
|
||||
<DL><p>
|
||||
<DT><A HREF="http://plex.lan:32400/web">Plex</A>
|
||||
<DT><A HREF="javascript:alert(1)">A bookmarklet</A>
|
||||
<DT><A HREF="https://nameless.example.com/x"></A>
|
||||
</DL><p>
|
||||
<DT><A HREF="place:sort=8">Most visited</A>
|
||||
</DL><p>
|
||||
<DT><H3>Empty folder</H3>
|
||||
<DL><p>
|
||||
</DL><p>
|
||||
</DL><p>`
|
||||
|
||||
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("<html><body>Hello</body></html>"), null)
|
||||
assert.equal(parseBookmarks('{"dashboards": []}'), null)
|
||||
const many = Array.from(
|
||||
{ length: 502 },
|
||||
(_, i) => `<DT><A HREF="https://example.com/${i}">${"n".repeat(70)}</A>`
|
||||
).join("")
|
||||
const found = parseBookmarks(`<DL>${many}</DL>`)
|
||||
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)
|
||||
})
|
||||
@@ -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<typeof backupSchema>
|
||||
|
||||
/** 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<string, string> = {
|
||||
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<string, Backup["dashboards"][0]["cards"][0]>()
|
||||
const folders: string[] = [] // the path to where we are
|
||||
let heading: string | null = null // a folder name that waits for its <DL>
|
||||
let skipped = 0
|
||||
const tags =
|
||||
/<DL\b|<\/DL>|<H3\b[^>]*>([\s\S]*?)<\/H3>|<A\b([^>]*)>([\s\S]*?)<\/A>/gi
|
||||
for (const [tag, folder, attributes, title] of html.matchAll(tags)) {
|
||||
if (/^<DL/i.test(tag)) {
|
||||
folders.push(heading ?? "")
|
||||
heading = null
|
||||
} else if (/^<\/DL/i.test(tag)) folders.pop()
|
||||
else if (/^<H3/i.test(tag)) heading = text(folder)
|
||||
else {
|
||||
const href = /\bHREF\s*=\s*"([^"]*)"/i.exec(attributes)
|
||||
const url = httpUrl.safeParse(text(href?.[1] ?? ""))
|
||||
// two folders with the same name stay two cards
|
||||
const path = folders.join("\n")
|
||||
const card = cards.get(path) ?? {
|
||||
name: (folders.filter(Boolean).at(-1) ?? "Bookmarks").slice(0, 40),
|
||||
links: [],
|
||||
}
|
||||
if (
|
||||
!url.success ||
|
||||
card.links.length >= 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 }
|
||||
}
|
||||
@@ -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<HTMLInputElement>(null)
|
||||
const importFile = useRef<HTMLInputElement>(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
|
||||
<div className="min-h-svh bg-app">
|
||||
@@ -616,6 +667,72 @@ function Settings() {
|
||||
</SettingsRow>
|
||||
</SettingsSection>
|
||||
|
||||
<SettingsSection id="transfer" className={section}>
|
||||
<SettingsSectionHeader>
|
||||
<SettingsSectionTitle>Export and import</SettingsSectionTitle>
|
||||
<SettingsSectionDescription>
|
||||
Keep a copy of your dashboards, move them to another
|
||||
account, or start from the bookmarks of your browser.
|
||||
</SettingsSectionDescription>
|
||||
</SettingsSectionHeader>
|
||||
<SettingsRow>
|
||||
<SettingsRowText>
|
||||
<div className="leading-tight font-bold">Export</div>
|
||||
<SettingsRowDescription>
|
||||
One file with all dashboards, their cards and links.
|
||||
Images that you uploaded are not in it.
|
||||
</SettingsRowDescription>
|
||||
</SettingsRowText>
|
||||
<SettingsRowControl>
|
||||
<Button
|
||||
onClick={() =>
|
||||
download().catch(() => setError("Could not export."))
|
||||
}
|
||||
>
|
||||
<Download size={15} /> Export
|
||||
</Button>
|
||||
</SettingsRowControl>
|
||||
</SettingsRow>
|
||||
<SettingsRow>
|
||||
<SettingsRowText>
|
||||
<div className="leading-tight font-bold">Import</div>
|
||||
<SettingsRowDescription>
|
||||
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.
|
||||
</SettingsRowDescription>
|
||||
{imported && (
|
||||
<SettingsRowDescription
|
||||
role="status"
|
||||
className="text-success-fg"
|
||||
>
|
||||
{imported}
|
||||
</SettingsRowDescription>
|
||||
)}
|
||||
</SettingsRowText>
|
||||
<SettingsRowControl>
|
||||
<input
|
||||
ref={importFile}
|
||||
type="file"
|
||||
accept=".json,.html,.htm,application/json,text/html"
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const chosen = e.target.files?.[0]
|
||||
if (chosen) void importFrom(chosen)
|
||||
e.target.value = ""
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
disabled={status === "saving"}
|
||||
onClick={() => importFile.current?.click()}
|
||||
>
|
||||
<Upload size={15} /> Choose a file
|
||||
</Button>
|
||||
</SettingsRowControl>
|
||||
</SettingsRow>
|
||||
</SettingsSection>
|
||||
|
||||
<SettingsSection id="account" className={section}>
|
||||
<SettingsSectionHeader>
|
||||
<SettingsSectionTitle>Account</SettingsSectionTitle>
|
||||
|
||||
Reference in New Issue
Block a user