Skip to content
Source
Sign inSign up
samwillis/neon-glow-demo
Open History to see the last commit

Shell.tsx

55 lines3.6 KB

1import { useEffect, useState, type ReactNode } from 'react';
2import { Link, useRouterState } from '@tanstack/react-router';
3import { Bell, Compass, FilePenLine, House, Info, Plus, UserRound } from 'lucide-react';
4import { social } from '@glow/client/social';
5import { Account, Logo, SignIn, useMyProfile } from './components';
6import { usePlatform } from './context';
7import styles from './Social.module.css';
8​
9function useUnread() {
10 const { api, user } = usePlatform(), [unread, setUnread] = useState(0);
11 const path = useRouterState({ select: state => state.location.pathname });
12 useEffect(() => {
13 if (!user) { setUnread(0); return; }
14 let active = true;
15 const load = () => social(api).inbox().then(page => { if (active) setUnread(page.unread); }).catch(() => {});
16 void load(); const timer = setInterval(load, 60_000);
17 return () => { active = false; clearInterval(timer); };
18 }, [api, user?.id, path]);
19 return unread;
21​
22// Rendered once by the layout route so the sidebar (and its logo animation) persists across pages.
23export function Shell({ children }: { children: ReactNode }) {
24 const { user, requireSignIn } = usePlatform(), unread = useUnread();
25 const path = useRouterState({ select: state => state.location.pathname }), search = useRouterState({ select: state => state.location.searchStr });
26 const immersive = path === '/';
27 const mine = useMyProfile(), me = user ? `/u/${mine?.handle ?? user.id}` : null;
28 // Desktop leads with Make; the mobile tab bar keeps Make in the centre (see CSS order).
29 const drafts = path.startsWith('/drafts') || search.includes('tab=drafts');
30 const items = [
31 { to: '/make', label: 'Make', icon: Plus, active: path.startsWith('/make'), make: true, mobileOrder: 3 },
32 { to: '/', label: 'Feed', icon: House, active: path === '/', mobileOrder: 1 },
33 { to: '/discover', label: 'Discover', icon: Compass, active: path.startsWith('/discover'), mobileOrder: 2 },
34 { to: '/notifications', label: 'Notifications', icon: Bell, active: path.startsWith('/notifications'), badge: unread, mobileOrder: 4 },
35 { to: me ?? '/make', label: 'Profile', icon: UserRound, active: !!user && (path === me || path === `/u/${user.id}`) && !drafts, signIn: !user, mobileOrder: 5 },
36 { to: '/drafts', label: 'Drafts', icon: FilePenLine, active: drafts, desktopOnly: true, mobileOrder: 6 },
37 { to: '/about', label: 'About', icon: Info, active: path.startsWith('/about'), desktopOnly: true, mobileOrder: 7 },
38 ];
39 return <div className={`${styles.shell} ${immersive ? styles.immersive : ''}`}>
40 <nav className={styles.rail} aria-label="Main">
41 <div className={styles.railLogo}><Logo /></div>
42 {items.map(item => {
43 const Icon = item.icon;
44 const placement = { order: item.mobileOrder } as const;
45 const content = <><span className={styles.navIcon}><Icon size={item.make ? 22 : 21} strokeWidth={item.active ? 2.6 : 2} />{!!item.badge && <span className={styles.badge}>{item.badge > 9 ? '9+' : item.badge}</span>}</span><span className={styles.navLabel}>{item.label}</span></>;
46 const className = `${styles.navItem} ${item.make ? styles.navMake : ''} ${item.desktopOnly ? styles.desktopOnly : ''}`;
47 if (item.signIn) return <button key={item.label} type="button" className={className} style={placement} onClick={requireSignIn}>{content}</button>;
48 return <Link key={item.label} to={item.to} className={className} style={placement} data-active={item.active || undefined} aria-current={item.active ? 'page' : undefined}>{content}</Link>;
49 })}
50 <div className={styles.railFooter}><Account row /></div>
51 </nav>
52 <main className={styles.shellMain}>{children}</main>
53 <SignIn />
54 </div>;