The UI was desktop-first: no viewport meta (phones rendered at ~980px CSS width, killing every breakpoint), an always-inline 240px sidebar squeezing phone content, fixed p-6/100vh chrome, a 6-column bookmarks table, fixed w-60/w-72 rails on collections/tags/contacts, a 12-tab settings strip that overflowed, and w-72 kanban columns. This pass makes every screen usable on a phone while leaving the desktop layout intact. - layout.tsx: viewport meta (width=device-width, initial-scale=1). - app-shell/sidebar: below lg the sidebar is an overlay drawer (closed by default, hamburger opens, backdrop/navigation/resize-to-desktop closes); at lg+ it is the static expand/collapse rail exactly as before. Separate mobileOpen state so closing the drawer never collapses the desktop rail. h-screen -> h-dvh; main p-6 -> p-3 sm:p-6. - lib/use-media-query.ts: hydration-safe useSyncExternalStore media hook (server snapshot false) so surfaces swap whole layouts without duplicating hidden DOM that would break the suites' strict text locators. - bookmarks: table swaps to tappable cards below sm (title/host/status/tags/ updated + open/trash); shared trash handler; detail sheet unchanged. - collections/tags/contacts: rails become master-detail on phones — full-width list first, tap into detail, Back returns; collections no longer auto-jump into the first item on mobile; phone padding. - settings: tab strip scrolls horizontally (triggers shrink-0) instead of overflowing the page. - kanban/collection boards: columns w-[85vw] max-w-72/64 with a peek of the next column; dvh heights. - calendar: compact cells + single-letter weekday headers on phones. - ui/dialog: max-h-[calc(100dvh-2rem)] + internal scroll + phone width guard so tall forms never clip; ui/tabs triggers shrink-0. - top-bar: alerts panel fits 320px; theme/avatar collapse on the smallest widths; hamburger gains an accessible name. - connector settings (MCP/WebDAV/CalDAV/CardDAV): connection rows wrap and the name gets a width floor — the action cluster previously squeezed the name to zero width on phones (a real bug the new mobile test caught). Verification: new mobile regression suite (390x844 context) proves drawer open/close-on-nav, bookmark cards + sheet, tags master-detail + Back, scrollable settings tabs reaching WebDAV, kanban column fits, and zero horizontal overflow on all eight screens; all 19 existing desktop tests still pass (20/20). Real-stack e2e fully passing. yarn lint / SSR eslint / build: 0 errors.
193 lines
7.2 KiB
TypeScript
193 lines
7.2 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect, useCallback, useRef } from 'react'
|
|
import { useSession } from 'next-auth/react'
|
|
import { usePathname, useRouter } from 'next/navigation'
|
|
import { Sidebar } from '@/components/sidebar'
|
|
import { TopBar } from '@/components/top-bar'
|
|
import { AddBookmarkDialog } from '@/components/add-bookmark-dialog'
|
|
import { QuickFind } from '@/components/quick-find'
|
|
import { resetReferenceCache } from '@/lib/reference-data'
|
|
|
|
export type Bookmark = {
|
|
id: string
|
|
url: string
|
|
title: string | null
|
|
description: string | null
|
|
favicon: string | null
|
|
ogImage: string | null
|
|
ogTitle: string | null
|
|
ogDescription: string | null
|
|
notes: string | null
|
|
dueDate: string | null
|
|
// Owner-only fields (review PRIV-02): shared readers receive a narrowed
|
|
// projection without contacts, recipients, alerts or mailbox watches.
|
|
alertAt?: string | null
|
|
alertSent?: boolean
|
|
kanbanStatus: string
|
|
kanbanColumnId?: string | null
|
|
// Owner-only board order within a column. Shared-reader projections omit it.
|
|
kanbanPosition?: number
|
|
imapWatchEnabled?: boolean
|
|
imapQuery?: string | null
|
|
isPublic: boolean
|
|
ownerId: string
|
|
contactId?: string | null
|
|
// Optimistic concurrency revision (review CR-04): every owner-driven PUT
|
|
// must send the version it edited from; 409 means a newer edit exists.
|
|
version?: number
|
|
contact?: any | null
|
|
tags: { tag: { id: string; name: string; color: string; ownerId?: string } }[]
|
|
sharedWith?: any[]
|
|
owner?: any
|
|
createdAt: string
|
|
updatedAt: string
|
|
}
|
|
|
|
export type TagWithCount = {
|
|
id: string
|
|
name: string
|
|
color: string
|
|
ownerId: string
|
|
_count: { bookmarks: number }
|
|
sharedWith?: any[]
|
|
}
|
|
|
|
export function AppShell({ children }: { children: React.ReactNode }) {
|
|
const { data: session } = useSession()
|
|
const router = useRouter()
|
|
const pathname = usePathname()
|
|
const [sidebarOpen, setSidebarOpen] = useState(true)
|
|
// Mobile (<lg) navigation is an overlay drawer, closed by default; the
|
|
// desktop rail keeps its own expanded/collapsed state. Two states so a
|
|
// phone never boots with a 240px rail squeezing the content, and closing
|
|
// the drawer never collapses the desktop rail.
|
|
const [mobileNavOpen, setMobileNavOpen] = useState(false)
|
|
const [addDialogOpen, setAddDialogOpen] = useState(false)
|
|
const [quickFindOpen, setQuickFindOpen] = useState(false)
|
|
const [alerts, setAlerts] = useState<any[]>([])
|
|
const [me, setMe] = useState<{ isAdmin: boolean; viewingAs: { name: string | null; email: string } | null } | null>(null)
|
|
|
|
useEffect(() => {
|
|
if (!session?.user) return
|
|
fetch('/api/me')
|
|
.then((r) => (r.ok ? r.json() : null))
|
|
.then(setMe)
|
|
.catch((e) => console.error('Failed to load account info', e))
|
|
}, [session?.user])
|
|
|
|
// Navigating from the drawer closes it (phone pattern); the desktop rail
|
|
// is unaffected because the drawer state is separate.
|
|
useEffect(() => { const t = setTimeout(() => setMobileNavOpen(false), 0); return () => clearTimeout(t) }, [pathname])
|
|
|
|
// Crossing into desktop while the drawer is open must not leave a
|
|
// label-expanded drawer stuck over the static rail.
|
|
useEffect(() => {
|
|
const mql = window.matchMedia('(min-width: 1024px)')
|
|
const onChange = () => { if (mql.matches) setMobileNavOpen(false) }
|
|
mql.addEventListener('change', onChange)
|
|
return () => mql.removeEventListener('change', onChange)
|
|
}, [])
|
|
|
|
const exitViewAs = useCallback(async () => {
|
|
try {
|
|
await fetch('/api/admin/view-as', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: null }) })
|
|
} catch { /* navigating to /admin surfaces any persistent failure */ }
|
|
// Review L-6: leaving view-as restores the admin identity.
|
|
resetReferenceCache()
|
|
window.location.href = '/admin'
|
|
}, [])
|
|
|
|
// Poll alerts. The API returns every un-dismissed alert on each poll, so
|
|
// track already-notified IDs per tab to avoid re-firing every 5 minutes.
|
|
const notifiedAlerts = useRef<Set<string>>(new Set())
|
|
useEffect(() => {
|
|
if (!session?.user) return
|
|
const check = async () => {
|
|
try {
|
|
const res = await fetch('/api/alerts/check')
|
|
if (res.ok) {
|
|
const data = await res.json()
|
|
setAlerts(data ?? [])
|
|
if (data?.length && typeof Notification !== 'undefined' && Notification.permission === 'granted') {
|
|
for (const a of data) {
|
|
const id = String(a?.id ?? a?.bookmarkId ?? '')
|
|
if (!id || notifiedAlerts.current.has(id)) continue
|
|
notifiedAlerts.current.add(id)
|
|
new Notification('superbrain alert', { body: `${a?.title ?? 'Bookmark'} is due!`, icon: '/favicon.svg' })
|
|
}
|
|
}
|
|
}
|
|
} catch { /* ignore */ }
|
|
}
|
|
check()
|
|
const interval = setInterval(check, 5 * 60 * 1000)
|
|
return () => clearInterval(interval)
|
|
}, [session?.user])
|
|
|
|
// Review L-5: notification permission is requested from the alert bell on
|
|
// an explicit user action (top-bar.tsx), not on every app mount.
|
|
|
|
const refreshPage = useCallback(() => {
|
|
window.dispatchEvent(new Event('superbrain:bookmarks-changed'))
|
|
router.refresh()
|
|
}, [router])
|
|
|
|
const dismissAlert = useCallback(async (bookmarkId: string) => {
|
|
try {
|
|
const res = await fetch(`/api/alerts/${bookmarkId}`, { method: 'DELETE' })
|
|
if (!res.ok) return
|
|
} catch { return }
|
|
setAlerts((prev: any[]) => (prev ?? []).filter((a: any) => a?.id !== bookmarkId))
|
|
}, [])
|
|
|
|
if (!session?.user) return null
|
|
|
|
return (
|
|
<div className="flex h-dvh overflow-hidden bg-background">
|
|
{mobileNavOpen && (
|
|
<div className="fixed inset-0 z-30 bg-black/50 lg:hidden" aria-hidden onClick={() => setMobileNavOpen(false)} />
|
|
)}
|
|
<Sidebar
|
|
open={sidebarOpen}
|
|
mobileOpen={mobileNavOpen}
|
|
onToggle={() => setSidebarOpen(!sidebarOpen)}
|
|
onCloseMobile={() => setMobileNavOpen(false)}
|
|
onQuickFind={() => { setMobileNavOpen(false); setQuickFindOpen(true) }}
|
|
pathname={pathname}
|
|
isAdmin={!!me?.isAdmin}
|
|
/>
|
|
<div className="flex flex-1 flex-col overflow-hidden">
|
|
{me?.viewingAs && (
|
|
<div className="flex items-center justify-center gap-3 bg-amber-500 px-4 py-1.5 text-sm font-medium text-amber-950">
|
|
<span>Admin view — viewing as {me.viewingAs.name || me.viewingAs.email}</span>
|
|
<button onClick={exitViewAs} className="rounded bg-amber-950/15 px-2 py-0.5 text-xs font-semibold hover:bg-amber-950/25">Exit</button>
|
|
</div>
|
|
)}
|
|
<TopBar
|
|
user={session.user as any}
|
|
alerts={alerts}
|
|
onDismissAlert={dismissAlert}
|
|
onAddBookmark={() => setAddDialogOpen(true)}
|
|
onToggleSidebar={() => setMobileNavOpen(!mobileNavOpen)}
|
|
onQuickFind={() => setQuickFindOpen(true)}
|
|
/>
|
|
<main className="flex-1 overflow-auto p-3 sm:p-6">
|
|
{children}
|
|
</main>
|
|
</div>
|
|
<AddBookmarkDialog
|
|
open={addDialogOpen}
|
|
onClose={() => setAddDialogOpen(false)}
|
|
onCreated={() => { setAddDialogOpen(false); refreshPage() }}
|
|
/>
|
|
<QuickFind
|
|
open={quickFindOpen}
|
|
onOpenChange={setQuickFindOpen}
|
|
isAdmin={!!me?.isAdmin}
|
|
onAddBookmark={() => setAddDialogOpen(true)}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|