Layout.tsx82 lines, 2.7 KB
| 1 | import { useQuery } from '@tanstack/react-query' |
| 2 | import { Bell, House, User, type LucideIcon } from 'lucide-react' |
| 3 | import { NavLink, Outlet } from 'react-router' |
| 4 | import { useLive } from '../features/live/useLive' |
| 5 | import { unreadOptions } from '../features/notifications/unread' |
| 6 | import { Suggestions } from '../features/profile/Suggestions' |
| 7 | import { getSession } from '../lib/api' |
| 8 | import { Settings } from './Settings' |
| 9 | import { Toasts } from './ui/Toasts' |
| 10 | |
| 11 | const item = `relative flex items-center gap-4 rounded-full p-3 |
| 12 | text-mute hover:bg-hover hover:text-ink lg:px-4` |
| 13 | |
| 14 | export function Layout() { |
| 15 | const live = useLive() |
| 16 | const handle = getSession()?.handle |
| 17 | const unread = useQuery(unreadOptions).data ?? 0 |
| 18 | |
| 19 | const link = (to: string, Icon: LucideIcon, label: string, n = 0) => ( |
| 20 | <NavLink |
| 21 | to={to} |
| 22 | end |
| 23 | className={({ isActive }) => |
| 24 | isActive ? `${item} bg-hover font-semibold text-ink!` : item |
| 25 | } |
| 26 | > |
| 27 | <Icon size={22} aria-hidden /> |
| 28 | <span className="sr-only lg:not-sr-only">{label}</span> |
| 29 | {n > 0 && ( |
| 30 | <span |
| 31 | className="absolute top-1.5 left-7 rounded-full bg-accent px-1.5 |
| 32 | text-xs font-semibold text-on-accent tabular-nums" |
| 33 | > |
| 34 | {n > 99 ? '99+' : n} |
| 35 | <span className="sr-only"> unread</span> |
| 36 | </span> |
| 37 | )} |
| 38 | </NavLink> |
| 39 | ) |
| 40 | |
| 41 | return ( |
| 42 | <div |
| 43 | className="mx-auto max-w-6xl sm:grid sm:grid-cols-[5rem_1fr] |
| 44 | lg:grid-cols-[15rem_minmax(0,38rem)_1fr]" |
| 45 | > |
| 46 | <nav |
| 47 | aria-label="Main" |
| 48 | className="fixed inset-x-0 bottom-0 z-20 flex justify-around |
| 49 | border-t border-line bg-paper sm:sticky sm:top-0 sm:h-dvh |
| 50 | sm:flex-col sm:justify-start sm:gap-1 sm:border-0 sm:p-3" |
| 51 | > |
| 52 | <p className="hidden px-4 py-3 text-xl font-semibold lg:block"> |
| 53 | murmur<span className="text-accent-ink">.</span> |
| 54 | </p> |
| 55 | {link('/', House, 'Home')} |
| 56 | {link('/notifications', Bell, 'Notifications', unread)} |
| 57 | {link(`/u/${handle}`, User, 'Profile')} |
| 58 | <Settings |
| 59 | className="mt-auto hidden flex-col gap-1 sm:flex" |
| 60 | button={item} |
| 61 | label="sr-only lg:not-sr-only" |
| 62 | /> |
| 63 | </nav> |
| 64 | |
| 65 | <main className="min-h-dvh pb-16 sm:border-x sm:border-line sm:pb-0"> |
| 66 | <Outlet /> |
| 67 | </main> |
| 68 | |
| 69 | <aside className="sticky top-0 hidden h-dvh space-y-8 p-6 lg:block"> |
| 70 | <Suggestions /> |
| 71 | <p className="flex items-center gap-2 text-xs text-mute"> |
| 72 | <span |
| 73 | className={`size-1.5 rounded-full |
| 74 | ${live === 'live' ? 'bg-accent' : 'bg-line'}`} |
| 75 | /> |
| 76 | {live === 'live' ? 'Live' : 'Connecting…'} |
| 77 | </p> |
| 78 | </aside> |
| 79 | <Toasts /> |
| 80 | </div> |
| 81 | ) |
| 82 | } |