import { useRef, useState } from "react" import { Link, createFileRoute, redirect, useLocation, useRouter, } from "@tanstack/react-router" import { ArrowLeft, Check, CircleUser, Clock, Image, LogOut, Search, SlidersHorizontal, Upload, } from "lucide-react" import { Logo } from "@/components/logo" import { DEFAULT_WALLPAPER } from "@/components/wallpaper" import { authClient } from "@/lib/auth-client" import { CLOCK_FONTS, CLOCK_FONT_NAMES, CLOCK_STYLES, CLOCK_STYLE_NAMES, } from "@/lib/clock" import type { ClockFont, ClockStyle } from "@/lib/clock" import { getDashboard, saveSettings } from "@/lib/dashboard.functions" import { ENGINES, ENGINE_NAMES } from "@/lib/search" import type { Engine } from "@/lib/search" import { Button } from "@/vendor/collabry/components/Button/Button" import { Field, FieldDescription, FieldLabel, Input, } from "@/vendor/collabry/components/Field/Field" import { PageActions, PageDescription, PageHeader, PageHeading, PageTitle, } from "@/vendor/collabry/components/Layout/Layout" import { SegmentedControl, SegmentedControlItem, } from "@/vendor/collabry/components/SegmentedControl/SegmentedControl" import { SettingsNav, SettingsNavItem, SettingsRow, SettingsRowControl, SettingsRowDescription, SettingsRowLabel, SettingsRowText, SettingsSection, SettingsSectionDescription, SettingsSectionHeader, SettingsSectionTitle, } from "@/vendor/collabry/components/Settings/Settings" import { Spinner } from "@/vendor/collabry/components/Spinner/Spinner" import { cn } from "@/vendor/collabry/lib/cn" export const Route = createFileRoute("/settings")({ loader: async () => { const data = await getDashboard() if (!data) throw redirect({ to: "/login" }) return data }, head: () => ({ meta: [{ title: "Settings" }] }), component: Settings, }) const SECTIONS = [ { id: "general", label: "General", icon: SlidersHorizontal }, { id: "clock", label: "Clock", icon: Clock }, { id: "search", label: "Search", icon: Search }, { id: "wallpaper", label: "Wallpaper", icon: Image }, { id: "account", label: "Account", icon: CircleUser }, ] // the nav links jump to a section; keep its title clear of the window edge const section = "[&>section]:scroll-mt-24" function Settings() { const data = Route.useLoaderData() const router = useRouter() const hash = useLocation({ select: (l) => l.hash }) || SECTIONS[0].id const file = useRef(null) const [status, setStatus] = useState<"idle" | "saving" | "saved">("idle") const [error, setError] = useState("") // Every control saves on its own. `action` returns a message when it failed. const run = async (action: () => Promise) => { setError("") setStatus("saving") const failure = await action().catch(() => "Could not save. Try again.") await router.invalidate() setError(failure || "") setStatus(failure ? "idle" : "saved") } const save = (patch: { title?: string searchEngine?: Engine wallpaperUrl?: string | null clockFont?: ClockFont clockStyle?: ClockStyle }) => run(() => saveSettings({ data: patch })) const upload = (image: File) => run(async () => { const res = await fetch("/api/wallpaper", { method: "POST", headers: { "Content-Type": image.type }, body: image, }) if (!res.ok) return res.text() }) const uploaded = !!data.wallpaperUrl?.startsWith("/") return ( // A page of its own: solid Collabry surfaces, no wallpaper behind it
{data.title}
Settings Changes are saved as you make them. {error && ( {error} )} {status === "saving" && ( <> Saving … )} {status === "saved" && ( <> Saved )}
{SECTIONS.map(({ id, label, icon: Icon }) => ( {label} ))}
General How this dashboard is called. Dashboard name Shown at the top left of the dashboard. e.key === "Enter" && e.currentTarget.blur() } onBlur={(e) => { const title = e.target.value.trim() if (title && title !== data.title) void save({ title }) else e.target.value = data.title }} /> Clock How the time on the dashboard looks.
Font
save({ clockFont: v as ClockFont })} > {CLOCK_FONTS.map((f) => ( {CLOCK_FONT_NAMES[f]} ))}
Style
Glass shows the wallpaper blurred through the digits. Tinted takes its color from the wallpaper.
save({ clockStyle: v as ClockStyle }) } > {CLOCK_STYLES.map((s) => ( {CLOCK_STYLE_NAMES[s]} ))}
Search Where the search bar sends you. DuckDuckGo !bangs work with every engine.
Search engine
{data.searxngUrl ? `SearXNG instance: ${data.searxngUrl}` : "SearXNG is off: set SEARXNG_URL on the server to use it."}
save({ searchEngine: v as Engine })} > {ENGINES.map((e) => ( {ENGINE_NAMES[e]} ))}
Wallpaper The picture behind your dashboard. Upload one (JPEG, PNG, WebP or AVIF, up to 10 MB) or use any image on the web.
Current wallpaper
Image address e.key === "Enter" && e.currentTarget.blur() } onBlur={(e) => { const url = e.target.value.trim() if (!url || url === data.wallpaperUrl) return if (e.target.checkValidity()) void save({ wallpaperUrl: url }) else setError( "The image address must start with http:// or https://" ) }} /> {uploaded ? "You use an uploaded image now." : data.wallpaperUrl ? "You use an image from the web now." : "You use the default photo now."}
{ const image = e.target.files?.[0] if (image) void upload(image) e.target.value = "" }} />
Account
{data.user.name}
{data.user.email}
) }