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:
@@ -4,7 +4,7 @@ import { and, asc, eq, inArray, sql } from "drizzle-orm"
|
||||
import { z } from "zod"
|
||||
import { DEFAULT_WALLPAPER_DIM } from "@/components/wallpaper"
|
||||
import { db } from "@/db"
|
||||
import { card, link, settings, wallpaper } from "@/db/schema"
|
||||
import { card, dashboard, link, settings, wallpaper } from "@/db/schema"
|
||||
import { auth, requireUser, signUpEnabled, ssoName } from "@/lib/auth"
|
||||
import { CLOCK_FONTS, CLOCK_STYLES } from "@/lib/clock"
|
||||
import { ENGINES, parseSuggestions } from "@/lib/search"
|
||||
@@ -19,6 +19,14 @@ async function requireCard(userId: string, cardId: string) {
|
||||
if (!found.length) throw new Error("Unknown card")
|
||||
}
|
||||
|
||||
async function requireDashboard(userId: string, dashboardId: string) {
|
||||
const found = await db
|
||||
.select({ id: dashboard.id })
|
||||
.from(dashboard)
|
||||
.where(and(eq(dashboard.id, dashboardId), eq(dashboard.userId, userId)))
|
||||
if (!found.length) throw new Error("Unknown dashboard")
|
||||
}
|
||||
|
||||
/** The position after the last link of a card. */
|
||||
const endOf = (cardId: string) =>
|
||||
sql<number>`(select coalesce(max(${link.position}) + 1, 0) from ${link} where ${link.cardId} = ${cardId})`
|
||||
@@ -31,17 +39,44 @@ export const getAuthInfo = createServerFn({ method: "GET" }).handler(
|
||||
})
|
||||
)
|
||||
|
||||
/** Everything the dashboard renders, or null when signed out. */
|
||||
export const getDashboard = createServerFn({ method: "GET" }).handler(
|
||||
async () => {
|
||||
/**
|
||||
* Everything the dashboard renders, or null when signed out. Without a
|
||||
* `dashboardId`, or with one that does not exist, it is the default dashboard.
|
||||
*/
|
||||
export const getDashboard = createServerFn({ method: "GET" })
|
||||
// any string: the id comes from the address bar, a wrong one is not an error
|
||||
.validator(z.object({ dashboardId: z.string().optional() }).optional())
|
||||
.handler(async ({ data }) => {
|
||||
const session = await auth.api.getSession({ headers: getRequestHeaders() })
|
||||
if (!session) return null
|
||||
const userId = session.user.id
|
||||
const [cards, links, rows] = await Promise.all([
|
||||
const [dashboards, rows] = await Promise.all([
|
||||
db
|
||||
.select({ id: dashboard.id, name: dashboard.name })
|
||||
.from(dashboard)
|
||||
.where(eq(dashboard.userId, userId))
|
||||
.orderBy(asc(dashboard.createdAt)),
|
||||
db.select().from(settings).where(eq(settings.userId, userId)),
|
||||
])
|
||||
// Cards need a dashboard and links need a card, so nobody is left without one.
|
||||
// shortcut: two first loads at the same moment make two of each, add a unique
|
||||
// constraint if that ever shows up
|
||||
if (!dashboards.length)
|
||||
dashboards.push(
|
||||
...(await db
|
||||
.insert(dashboard)
|
||||
.values({ userId, name: "Home" })
|
||||
.returning({ id: dashboard.id, name: dashboard.name }))
|
||||
)
|
||||
const s = rows.at(0) // no row until the first save
|
||||
const has = (id?: string | null) => dashboards.find((d) => d.id === id)?.id
|
||||
const defaultDashboardId = has(s?.defaultDashboardId) ?? dashboards[0].id
|
||||
const dashboardId = has(data?.dashboardId) ?? defaultDashboardId
|
||||
const [cards, links] = await Promise.all([
|
||||
db
|
||||
.select({ id: card.id, name: card.name })
|
||||
.from(card)
|
||||
.where(eq(card.userId, userId))
|
||||
.where(and(eq(card.userId, userId), eq(card.dashboardId, dashboardId)))
|
||||
.orderBy(asc(card.createdAt)),
|
||||
db
|
||||
.select({
|
||||
@@ -52,23 +87,22 @@ export const getDashboard = createServerFn({ method: "GET" }).handler(
|
||||
icon: link.icon,
|
||||
})
|
||||
.from(link)
|
||||
.where(eq(link.userId, userId))
|
||||
.innerJoin(card, eq(card.id, link.cardId))
|
||||
.where(and(eq(link.userId, userId), eq(card.dashboardId, dashboardId)))
|
||||
.orderBy(asc(link.position), asc(link.createdAt)),
|
||||
db.select().from(settings).where(eq(settings.userId, userId)),
|
||||
])
|
||||
// Links need a card to live in, so nobody is left without one.
|
||||
// shortcut: two first loads at the same moment make two cards, add a unique
|
||||
// constraint if that ever shows up
|
||||
if (!cards.length)
|
||||
cards.push(
|
||||
...(await db
|
||||
.insert(card)
|
||||
.values({ userId, name: "Apps" })
|
||||
.values({ userId, dashboardId, name: "Apps" })
|
||||
.returning({ id: card.id, name: card.name }))
|
||||
)
|
||||
const s = rows.at(0) // no row until the first save
|
||||
return {
|
||||
user: { name: session.user.name, email: session.user.email },
|
||||
dashboards,
|
||||
dashboardId,
|
||||
defaultDashboardId,
|
||||
cards: cards.map((c) => ({
|
||||
...c,
|
||||
links: links.filter((l) => l.cardId === c.id),
|
||||
@@ -81,8 +115,7 @@ export const getDashboard = createServerFn({ method: "GET" }).handler(
|
||||
clockStyle: s?.clockStyle ?? "solid",
|
||||
searxngUrl: process.env.SEARXNG_URL || null,
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
export const saveLink = createServerFn({ method: "POST" })
|
||||
.validator(
|
||||
@@ -119,9 +152,11 @@ export const saveCard = createServerFn({ method: "POST" })
|
||||
z.object({
|
||||
id: z.uuid().optional(),
|
||||
name: z.string().trim().min(1).max(40),
|
||||
// where a new card goes. A card that exists stays on its dashboard.
|
||||
dashboardId: z.uuid(),
|
||||
})
|
||||
)
|
||||
.handler(async ({ data: { id, name } }) => {
|
||||
.handler(async ({ data: { id, name, dashboardId } }) => {
|
||||
const user = await requireUser()
|
||||
if (id) {
|
||||
await db
|
||||
@@ -129,10 +164,41 @@ export const saveCard = createServerFn({ method: "POST" })
|
||||
.set({ name })
|
||||
.where(and(eq(card.id, id), eq(card.userId, user.id)))
|
||||
} else {
|
||||
await db.insert(card).values({ name, userId: user.id })
|
||||
await requireDashboard(user.id, dashboardId)
|
||||
await db.insert(card).values({ name, dashboardId, userId: user.id })
|
||||
}
|
||||
})
|
||||
|
||||
export const saveDashboard = createServerFn({ method: "POST" })
|
||||
.validator(
|
||||
z.object({
|
||||
id: z.uuid().optional(),
|
||||
name: z.string().trim().min(1).max(40),
|
||||
})
|
||||
)
|
||||
.handler(async ({ data: { id, name } }) => {
|
||||
const user = await requireUser()
|
||||
if (id) {
|
||||
await db
|
||||
.update(dashboard)
|
||||
.set({ name })
|
||||
.where(and(eq(dashboard.id, id), eq(dashboard.userId, user.id)))
|
||||
} else {
|
||||
await db.insert(dashboard).values({ name, userId: user.id })
|
||||
}
|
||||
})
|
||||
|
||||
/** Deletes the dashboard and, through the foreign keys, its cards and their links. */
|
||||
export const deleteDashboard = createServerFn({ method: "POST" })
|
||||
.validator(z.object({ id: z.uuid() }))
|
||||
.handler(async ({ data }) => {
|
||||
const user = await requireUser()
|
||||
const mine = eq(dashboard.userId, user.id)
|
||||
if ((await db.$count(dashboard, mine)) < 2)
|
||||
throw new Error("The last dashboard stays")
|
||||
await db.delete(dashboard).where(and(eq(dashboard.id, data.id), mine))
|
||||
})
|
||||
|
||||
/** Deletes the card and, through the foreign key, its links. */
|
||||
export const deleteCard = createServerFn({ method: "POST" })
|
||||
.validator(z.object({ id: z.uuid() }))
|
||||
@@ -187,6 +253,7 @@ export const saveSettings = createServerFn({ method: "POST" })
|
||||
z
|
||||
.object({
|
||||
title: z.string().trim().min(1).max(40),
|
||||
defaultDashboardId: z.uuid(),
|
||||
searchEngine: z.enum(ENGINES),
|
||||
// external image, the user's own upload, or null for the default
|
||||
wallpaperUrl: z
|
||||
@@ -201,6 +268,8 @@ export const saveSettings = createServerFn({ method: "POST" })
|
||||
)
|
||||
.handler(async ({ data }) => {
|
||||
const user = await requireUser()
|
||||
if (data.defaultDashboardId)
|
||||
await requireDashboard(user.id, data.defaultDashboardId)
|
||||
await db
|
||||
.insert(settings)
|
||||
.values({ ...data, userId: user.id })
|
||||
|
||||
Reference in New Issue
Block a user