|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useCallback, useMemo, useState } from 'react' |
| 4 | +import { StudioHero } from '@/app/(landing)/studio/hero' |
| 5 | +import { FeaturedGrid, PostGrid } from '@/app/(landing)/studio/post-grid' |
| 6 | +import { CATEGORIES, getCategoryById, getPrimaryCategory } from '@/app/(landing)/studio/tag-colors' |
| 7 | + |
| 8 | +interface SerializedPost { |
| 9 | + slug: string |
| 10 | + title: string |
| 11 | + description: string |
| 12 | + date: string |
| 13 | + ogImage: string |
| 14 | + readingTime?: number |
| 15 | + tags: string[] |
| 16 | + author: { id: string; name: string; avatarUrl?: string; url?: string } |
| 17 | + authors?: { id: string; name: string; avatarUrl?: string; url?: string }[] |
| 18 | + featured?: boolean |
| 19 | +} |
| 20 | + |
| 21 | +interface StudioFeedProps { |
| 22 | + posts: SerializedPost[] |
| 23 | + initialTag?: string | null |
| 24 | +} |
| 25 | + |
| 26 | +export function StudioFeed({ posts, initialTag }: StudioFeedProps) { |
| 27 | + const [activeTag, setActiveTag] = useState<string | null>(initialTag ?? null) |
| 28 | + |
| 29 | + const categoryCounts = useMemo(() => { |
| 30 | + const counts: Record<string, number> = {} |
| 31 | + for (const cat of CATEGORIES) counts[cat.id] = 0 |
| 32 | + for (const post of posts) { |
| 33 | + const cat = getPrimaryCategory(post.tags) |
| 34 | + counts[cat.id] = (counts[cat.id] ?? 0) + 1 |
| 35 | + } |
| 36 | + return counts |
| 37 | + }, [posts]) |
| 38 | + |
| 39 | + const filtered = useMemo(() => { |
| 40 | + if (!activeTag) return posts |
| 41 | + return posts.filter((p) => getPrimaryCategory(p.tags).id === activeTag) |
| 42 | + }, [posts, activeTag]) |
| 43 | + |
| 44 | + const sorted = useMemo(() => { |
| 45 | + return [...filtered].sort((a, b) => { |
| 46 | + if (a.featured && !b.featured) return -1 |
| 47 | + if (!a.featured && b.featured) return 1 |
| 48 | + return new Date(b.date).getTime() - new Date(a.date).getTime() |
| 49 | + }) |
| 50 | + }, [filtered]) |
| 51 | + |
| 52 | + const featured = !activeTag ? sorted.filter((p) => p.featured) : [] |
| 53 | + const feed = !activeTag ? sorted.filter((p) => !p.featured) : sorted |
| 54 | + const activeCategory = activeTag ? getCategoryById(activeTag) : null |
| 55 | + |
| 56 | + const handleCategoryClick = useCallback((catId: string | null) => { |
| 57 | + setActiveTag(catId) |
| 58 | + const url = catId ? `/studio?tag=${encodeURIComponent(catId)}` : '/studio' |
| 59 | + window.history.replaceState(null, '', url) |
| 60 | + }, []) |
| 61 | + |
| 62 | + const sidebarItems = useMemo( |
| 63 | + () => [ |
| 64 | + { id: null, label: 'All Posts', count: posts.length, color: '#00F701' }, |
| 65 | + ...CATEGORIES.map((cat) => ({ |
| 66 | + id: cat.id as string | null, |
| 67 | + label: cat.label, |
| 68 | + count: categoryCounts[cat.id] ?? 0, |
| 69 | + color: cat.color, |
| 70 | + })), |
| 71 | + ], |
| 72 | + [posts.length, categoryCounts] |
| 73 | + ) |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className='flex flex-1 flex-col lg:flex-row'> |
| 77 | + <Sidebar items={sidebarItems} activeId={activeTag} onSelect={handleCategoryClick} /> |
| 78 | + <main className='relative flex-1'> |
| 79 | + <div className='flex flex-col'> |
| 80 | + {!activeTag && <StudioHero />} |
| 81 | + <div className='mx-auto w-full max-w-5xl py-12'> |
| 82 | + {activeCategory && ( |
| 83 | + <div className='mb-8 flex items-center gap-3'> |
| 84 | + <span className='font-mono text-[10px] uppercase tracking-widest text-[#666]'> |
| 85 | + Filtered by: |
| 86 | + </span> |
| 87 | + <span |
| 88 | + className='px-2 py-0.5 font-mono text-[10px] uppercase tracking-wider' |
| 89 | + style={{ |
| 90 | + border: `1px solid ${activeCategory.color}`, |
| 91 | + color: activeCategory.color, |
| 92 | + }} |
| 93 | + > |
| 94 | + {activeCategory.label} |
| 95 | + </span> |
| 96 | + <button |
| 97 | + type='button' |
| 98 | + onClick={() => handleCategoryClick(null)} |
| 99 | + className='font-mono text-[10px] uppercase tracking-wider text-[#999] transition-colors hover:text-[#ECECEC]' |
| 100 | + > |
| 101 | + Clear |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + {featured.length > 0 && ( |
| 106 | + <section className='mb-10'> |
| 107 | + <h2 className='mb-8 flex items-center gap-2 font-mono text-[11px] uppercase tracking-widest text-[#666]'> |
| 108 | + <span className='inline-block h-2 w-2 bg-[#FA4EDF]' aria-hidden='true' /> |
| 109 | + Featured Content |
| 110 | + </h2> |
| 111 | + <FeaturedGrid posts={featured} /> |
| 112 | + </section> |
| 113 | + )} |
| 114 | + {feed.length > 0 && ( |
| 115 | + <section> |
| 116 | + <h2 className='mb-8 flex items-center gap-2 font-mono text-[11px] uppercase tracking-widest text-[#666]'> |
| 117 | + <span className='inline-block h-2 w-2 bg-[#00F701]' aria-hidden='true' /> |
| 118 | + {activeCategory ? activeCategory.label : 'All Posts'} |
| 119 | + </h2> |
| 120 | + <PostGrid posts={feed} /> |
| 121 | + </section> |
| 122 | + )} |
| 123 | + {sorted.length === 0 && ( |
| 124 | + <div className='py-20 text-center'> |
| 125 | + <p className='text-[14px] text-[#666]'>No posts found.</p> |
| 126 | + <button |
| 127 | + type='button' |
| 128 | + onClick={() => handleCategoryClick(null)} |
| 129 | + className='mt-4 inline-block font-mono text-[12px] uppercase tracking-wider text-[#999] transition-colors hover:text-[#ECECEC]' |
| 130 | + > |
| 131 | + View all posts |
| 132 | + </button> |
| 133 | + </div> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + </main> |
| 138 | + </div> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +import { useEffect, useRef } from 'react' |
| 143 | +import { motion, useReducedMotion } from 'framer-motion' |
| 144 | +import { Search } from 'lucide-react' |
| 145 | + |
| 146 | +interface SidebarItem { |
| 147 | + id: string | null |
| 148 | + label: string |
| 149 | + count: number |
| 150 | + color: string |
| 151 | +} |
| 152 | + |
| 153 | +interface SidebarProps { |
| 154 | + items: SidebarItem[] |
| 155 | + activeId: string | null |
| 156 | + onSelect: (id: string | null) => void |
| 157 | +} |
| 158 | + |
| 159 | +function Sidebar({ items, activeId, onSelect }: SidebarProps) { |
| 160 | + const shouldReduceMotion = useReducedMotion() |
| 161 | + const listRef = useRef<HTMLUListElement>(null) |
| 162 | + const itemRefs = useRef<Map<string, HTMLLIElement>>(new Map()) |
| 163 | + const [highlight, setHighlight] = useState<{ top: number; height: number } | null>(null) |
| 164 | + |
| 165 | + const activeItem = items.find((item) => item.id === activeId) ?? items[0] |
| 166 | + |
| 167 | + useEffect(() => { |
| 168 | + const key = activeId ?? 'all' |
| 169 | + const el = itemRefs.current.get(key) |
| 170 | + const list = listRef.current |
| 171 | + if (!el || !list) { |
| 172 | + setHighlight(null) |
| 173 | + return |
| 174 | + } |
| 175 | + const listRect = list.getBoundingClientRect() |
| 176 | + const elRect = el.getBoundingClientRect() |
| 177 | + setHighlight({ |
| 178 | + top: elRect.top - listRect.top, |
| 179 | + height: elRect.height, |
| 180 | + }) |
| 181 | + }, [activeId]) |
| 182 | + |
| 183 | + return ( |
| 184 | + <aside className='flex w-full shrink-0 flex-col border-r border-[#2A2A2A] bg-[#1C1C1C] p-8 lg:sticky lg:top-[52px] lg:h-[calc(100vh-52px)] lg:w-72 lg:overflow-y-auto'> |
| 185 | + <div className='flex h-full flex-col'> |
| 186 | + <div className='mb-10'> |
| 187 | + <h2 className='mb-4 font-mono text-[10px] uppercase tracking-widest text-[#666]'> |
| 188 | + Find Insights |
| 189 | + </h2> |
| 190 | + <div className='relative'> |
| 191 | + <input |
| 192 | + type='text' |
| 193 | + placeholder='SEARCH COMING SOON...' |
| 194 | + disabled |
| 195 | + className='w-full cursor-not-allowed border border-[#2A2A2A] bg-[#232323] px-4 py-2 font-mono text-[11px] text-[#ECECEC] opacity-50 placeholder:text-[#666]' |
| 196 | + style={{ borderRadius: '5px' }} |
| 197 | + aria-label='Search blog posts (coming soon)' |
| 198 | + /> |
| 199 | + <Search |
| 200 | + className='absolute right-3 top-2.5 h-3.5 w-3.5 text-[#666]' |
| 201 | + aria-hidden='true' |
| 202 | + /> |
| 203 | + </div> |
| 204 | + </div> |
| 205 | + <div className='flex flex-col'> |
| 206 | + <h2 className='mb-3 font-mono text-[10px] uppercase tracking-widest text-[#ECECEC]'> |
| 207 | + Categories |
| 208 | + </h2> |
| 209 | + <ul ref={listRef} className='relative flex flex-col'> |
| 210 | + {activeItem && highlight && ( |
| 211 | + <motion.div |
| 212 | + className='absolute left-0 right-0 rounded-sm' |
| 213 | + style={{ |
| 214 | + backgroundColor: `${activeItem.color}0D`, |
| 215 | + border: `1px solid ${activeItem.color}`, |
| 216 | + height: highlight.height, |
| 217 | + }} |
| 218 | + animate={{ y: highlight.top }} |
| 219 | + transition={ |
| 220 | + shouldReduceMotion |
| 221 | + ? { duration: 0 } |
| 222 | + : { type: 'spring', duration: 0.3, bounce: 0 } |
| 223 | + } |
| 224 | + /> |
| 225 | + )} |
| 226 | + {items.map((item) => { |
| 227 | + const isActive = item.id === activeId |
| 228 | + const key = item.id ?? 'all' |
| 229 | + |
| 230 | + return ( |
| 231 | + <li |
| 232 | + key={key} |
| 233 | + ref={(el) => { |
| 234 | + if (el) itemRefs.current.set(key, el) |
| 235 | + }} |
| 236 | + > |
| 237 | + <button |
| 238 | + type='button' |
| 239 | + onClick={() => onSelect(item.id)} |
| 240 | + className={`relative flex w-full items-center justify-between rounded-sm px-3 py-2 text-left text-[13px] transition-colors duration-150 ease ${ |
| 241 | + isActive |
| 242 | + ? '' |
| 243 | + : '[@media(hover:hover)]:hover:bg-[#232323] [@media(hover:hover)]:hover:text-[#ECECEC]' |
| 244 | + }`} |
| 245 | + style={{ color: isActive ? item.color : '#999' }} |
| 246 | + > |
| 247 | + <span className='relative z-10'>{item.label}</span> |
| 248 | + <span |
| 249 | + className='relative z-10 font-mono text-[10px]' |
| 250 | + style={{ |
| 251 | + padding: '2px 6px', |
| 252 | + borderRadius: '2px', |
| 253 | + border: isActive ? `1px solid ${item.color}` : '1px solid #2A2A2A', |
| 254 | + color: isActive ? item.color : '#666', |
| 255 | + }} |
| 256 | + > |
| 257 | + {String(item.count).padStart(2, '0')} |
| 258 | + </span> |
| 259 | + </button> |
| 260 | + </li> |
| 261 | + ) |
| 262 | + })} |
| 263 | + </ul> |
| 264 | + </div> |
| 265 | + </div> |
| 266 | + </aside> |
| 267 | + ) |
| 268 | +} |
0 commit comments