feat: Dahoam start page with accounts, cards and search
Replica of the reference start page: wallpaper, serif clock, search bar and link cards, as a TanStack Start app. - Better Auth: email/password and OIDC single sign-on - Postgres with Drizzle for the auth tables, cards, links, settings and uploaded wallpapers; the server applies the migrations on start - cards group links; links are dragged inside and between cards - search with DuckDuckGo, SearXNG or Google, suggestions and / commands - settings page: name, search engine, wallpaper, account - UI from collabry-components, copied into src/vendor/collabry - Dockerfile and a Gitea workflow that checks the code, builds the image for amd64 and arm64 and pushes it to the registry on main
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import { useSortable } from "@dnd-kit/sortable"
|
||||
import { CSS } from "@dnd-kit/utilities"
|
||||
import { Pencil } from "lucide-react"
|
||||
import { cn } from "@/vendor/collabry/lib/cn"
|
||||
|
||||
export type HomeLink = {
|
||||
id: string
|
||||
cardId: string
|
||||
name: string
|
||||
url: string
|
||||
icon: string | null
|
||||
}
|
||||
|
||||
const dashboardIcon = (name: string) =>
|
||||
`https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/png/${name
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-|-$/g, "")}.png`
|
||||
|
||||
/** Icon sources to try in order; the letter tile is the last resort. */
|
||||
function iconSources({ name, url, icon }: HomeLink) {
|
||||
return [
|
||||
icon && /^https?:\/\//.test(icon) ? icon : dashboardIcon(icon || name),
|
||||
`${new URL(url).origin}/favicon.ico`,
|
||||
]
|
||||
}
|
||||
|
||||
type IconProps = { link: HomeLink; className?: string }
|
||||
|
||||
/** Starts the fallback chain again when the link changes. */
|
||||
export function LinkIcon(props: IconProps) {
|
||||
const { id, name, url, icon } = props.link
|
||||
return <Icon key={[id, name, url, icon].join("|")} {...props} />
|
||||
}
|
||||
|
||||
function Icon({ link, className }: IconProps) {
|
||||
const sources = iconSources(link)
|
||||
const [index, setIndex] = useState(0)
|
||||
const img = useRef<HTMLImageElement>(null)
|
||||
const next = () => setIndex((i) => i + 1)
|
||||
|
||||
// an image that failed before hydration never fires onError
|
||||
useEffect(() => {
|
||||
if (img.current?.complete && img.current.naturalWidth === 0) next()
|
||||
}, [index])
|
||||
|
||||
if (index >= sources.length)
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"grid place-items-center rounded-2xl bg-white/15 font-serif text-2xl text-white uppercase",
|
||||
className
|
||||
)}
|
||||
>
|
||||
{link.name[0]}
|
||||
</span>
|
||||
)
|
||||
return (
|
||||
<img
|
||||
ref={img}
|
||||
src={sources[index]}
|
||||
alt=""
|
||||
referrerPolicy="no-referrer"
|
||||
draggable={false}
|
||||
onError={next}
|
||||
className={cn("rounded-2xl object-contain drop-shadow-lg", className)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const tile =
|
||||
"flex w-24 flex-col items-center gap-2.5 rounded-2xl px-2 py-3 text-base text-white/90"
|
||||
|
||||
/** Icon and name. Also what follows the pointer while a link is dragged. */
|
||||
export function TileFace({
|
||||
link,
|
||||
className,
|
||||
}: {
|
||||
link: HomeLink
|
||||
className?: string
|
||||
}) {
|
||||
return (
|
||||
<span className={cn(tile, className)}>
|
||||
<LinkIcon link={link} className="size-14" />
|
||||
<span className="line-clamp-2 w-full text-center leading-tight break-words [text-shadow:0_1px_8px_rgb(0_0_0/0.6)]">
|
||||
{link.name}
|
||||
</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
/** A link in a card. Drag it (mouse, long press, or Space and the arrow keys) to move it. */
|
||||
export function LinkTile({
|
||||
link,
|
||||
onEdit,
|
||||
}: {
|
||||
link: HomeLink
|
||||
onEdit: () => void
|
||||
}) {
|
||||
const {
|
||||
attributes,
|
||||
listeners,
|
||||
setNodeRef,
|
||||
transform,
|
||||
transition,
|
||||
isDragging,
|
||||
} = useSortable({ id: link.id })
|
||||
return (
|
||||
<div
|
||||
ref={setNodeRef}
|
||||
style={{ transform: CSS.Translate.toString(transform), transition }}
|
||||
className={cn("group relative", isDragging && "opacity-30")}
|
||||
>
|
||||
<a
|
||||
href={link.url}
|
||||
rel="noreferrer"
|
||||
// the browser's own link drag would win over ours
|
||||
draggable={false}
|
||||
aria-describedby={attributes["aria-describedby"]}
|
||||
{...listeners}
|
||||
className="block touch-manipulation rounded-2xl transition-colors outline-none select-none [-webkit-touch-callout:none] hover:bg-white/10 focus-visible:bg-white/10 focus-visible:ring-2 focus-visible:ring-white/60"
|
||||
>
|
||||
<TileFace link={link} />
|
||||
</a>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={`Edit ${link.name}`}
|
||||
onClick={onEdit}
|
||||
className="absolute top-1 right-1 grid size-6 place-items-center rounded-full bg-black/70 text-white opacity-0 transition-opacity outline-none group-hover:opacity-100 focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-white/60 pointer-coarse:opacity-80"
|
||||
>
|
||||
<Pencil className="size-3" />
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user