import { useEffect, useState, type ReactNode } from "react"; import type { EmailAddress } from "@/jmap/types"; import { avatarColor, initials } from "@/lib/address"; import { useContacts } from "@/store/contacts"; import { contactPhoto } from "@/lib/contacts"; export function Avatar({ who, size, className }: { who: EmailAddress | { name?: string | null; email?: string } | string | null | undefined; size?: "sm" | "lg" | "xl"; className?: string }) { const email = typeof who === "string" ? who : (who?.email ?? ""); const name = typeof who === "string" ? who : (who?.name ?? who?.email ?? ""); const photo = useContacts((s) => { if (!email || !s.loaded) return null; const c = s.lookupByEmail(email); return c && s.accountId ? contactPhoto(c, s.accountId) : null; }); return ( {photo ? : initials({ name, email })} ); } export function Switch({ checked, onChange, label, hint, disabled }: { checked: boolean; onChange: (v: boolean) => void; label?: ReactNode; hint?: ReactNode; disabled?: boolean }) { const sw = ( !disabled && onChange(!checked)} disabled={disabled} /> ); if (!label) return sw; return ( {label} {hint && {hint}} {sw} ); } export function Spinner({ size = "md", label }: { size?: "md" | "lg"; label?: string }) { return ( {label && {label}} ); } export function Empty({ icon, title, children }: { icon?: ReactNode; title: string; children?: ReactNode }) { return ( {icon} {title} {children && {children}} ); } export function useMediaQuery(q: string): boolean { const [m, setM] = useState(() => window.matchMedia(q).matches); useEffect(() => { const mq = window.matchMedia(q); const fn = () => setM(mq.matches); mq.addEventListener("change", fn); return () => mq.removeEventListener("change", fn); }, [q]); return m; } export const useIsMobile = () => useMediaQuery("(max-width: 768px)"); export const useIsNarrow = () => useMediaQuery("(max-width: 900px)"); /** * Whether the thing doing the pointing is a finger. * * The touch gestures hang off this rather than off `useIsMobile`, because the * two are different questions and both get asked: a tablet in landscape is a * wide screen that swipes, and a phone plugged into a mouse is a narrow one * that should not. Width decides the layout; this decides the gestures. */ export const useIsTouch = () => useMediaQuery("(pointer: coarse)"); export function Kbd({ keys }: { keys: string }) { return ( {keys.split(" ").map((k, i) => ( {i > 0 && then} {k.split("+").map((p, j) => ( {p === "mod" ? (navigator.platform.includes("Mac") ? "⌘" : "Ctrl") : p === "shift" ? "⇧" : p === "enter" ? "↵" : p === "esc" ? "Esc" : p} ))} ))} ); } export function ColorSwatches({ value, onChange, colors }: { value: string | null | undefined; onChange: (c: string) => void; colors?: string[] }) { const list = colors ?? CALENDAR_COLORS; return ( {list.map((c) => ( onChange(c)} aria-label={c} /> ))} ); } export const CALENDAR_COLORS = ["#0f766e", "#2563eb", "#7c3aed", "#db2777", "#dc2626", "#ea580c", "#ca8a04", "#16a34a", "#0891b2", "#4b5563", "#9333ea", "#be123c"];
{children}