|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { useCallback, useMemo, useState } from 'react' |
4 | | -import { useRouter } from 'next/navigation' |
| 3 | +import { useCallback, useEffect, useMemo, useState } from 'react' |
5 | 4 | import { BlogStudioSidebar } from '@/app/(landing)/blog/studio-sidebar-client' |
| 5 | +import { PostGrid } from '@/app/(landing)/blog/post-grid' |
| 6 | +import { CATEGORIES, getPrimaryCategory } from '@/app/(landing)/blog/tag-colors' |
| 7 | +import type { BlogMeta } from '@/lib/blog/schema' |
6 | 8 |
|
7 | 9 | interface AuthorWithSidebarProps { |
8 | | - allPosts: { tags: string[] }[] |
| 10 | + allPosts: BlogMeta[] |
| 11 | + authorPosts: BlogMeta[] |
9 | 12 | activeTag: string | null |
| 13 | + initialQuery: string |
10 | 14 | children: React.ReactNode |
11 | 15 | } |
12 | 16 |
|
13 | | -export function AuthorWithSidebar({ allPosts, activeTag, children }: AuthorWithSidebarProps) { |
14 | | - const router = useRouter() |
15 | | - const [query, setQuery] = useState('') |
| 17 | +export function AuthorWithSidebar({ allPosts, authorPosts, activeTag, initialQuery, children }: AuthorWithSidebarProps) { |
| 18 | + const [selectedTag, setSelectedTag] = useState<string | null>(activeTag) |
| 19 | + const [query, setQuery] = useState(initialQuery) |
| 20 | + |
| 21 | + const syncUrl = useCallback((tag: string | null, q: string) => { |
| 22 | + const params = new URLSearchParams() |
| 23 | + if (tag) params.set('tag', tag) |
| 24 | + if (q) params.set('q', q) |
| 25 | + const search = params.toString() |
| 26 | + const basePath = window.location.pathname |
| 27 | + window.history.replaceState(null, '', search ? `${basePath}?${search}` : basePath) |
| 28 | + }, []) |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const onPopState = () => { |
| 32 | + const params = new URLSearchParams(window.location.search) |
| 33 | + setSelectedTag(params.get('tag')) |
| 34 | + setQuery(params.get('q') ?? '') |
| 35 | + } |
| 36 | + window.addEventListener('popstate', onPopState) |
| 37 | + return () => window.removeEventListener('popstate', onPopState) |
| 38 | + }, []) |
| 39 | + |
| 40 | + const lowerQ = query.trim().toLowerCase() |
| 41 | + |
| 42 | + const filteredAuthorPosts = useMemo(() => { |
| 43 | + const validTag = selectedTag && CATEGORIES.some((c) => c.id === selectedTag) ? selectedTag : null |
| 44 | + |
| 45 | + let filtered = authorPosts |
| 46 | + |
| 47 | + if (validTag) filtered = authorPosts.filter((p) => getPrimaryCategory(p.tags).id === validTag) |
| 48 | + |
| 49 | + if (lowerQ) { |
| 50 | + filtered = filtered.filter((p) => { |
| 51 | + const haystack = [ |
| 52 | + p.title, |
| 53 | + p.description, |
| 54 | + ...p.tags, |
| 55 | + p.author.name, |
| 56 | + ...(p.authors?.map((a) => a.name) ?? []), |
| 57 | + ] |
| 58 | + .join(' ') |
| 59 | + .toLowerCase() |
| 60 | + return haystack.includes(lowerQ) |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + return filtered |
| 65 | + }, [authorPosts, lowerQ, selectedTag]) |
| 66 | + |
| 67 | + const sidebarPosts = useMemo(() => allPosts.map((p) => ({ tags: p.tags })), [allPosts]) |
16 | 68 |
|
17 | 69 | const handleChangeQuery = useCallback( |
18 | 70 | (value: string) => { |
19 | 71 | setQuery(value) |
20 | | - const trimmed = value.trim() |
21 | | - router.push(trimmed ? `/blog?q=${encodeURIComponent(trimmed)}` : '/blog') |
| 72 | + setSelectedTag(null) |
| 73 | + syncUrl(null, value.trim()) |
22 | 74 | }, |
23 | | - [router] |
| 75 | + [syncUrl] |
24 | 76 | ) |
25 | 77 |
|
26 | 78 | const handleSelectTag = useCallback( |
27 | 79 | (id: string | null) => { |
28 | 80 | setQuery('') |
29 | | - router.push(id ? `/blog?tag=${encodeURIComponent(id)}` : '/blog') |
| 81 | + setSelectedTag(id) |
| 82 | + syncUrl(id, '') |
30 | 83 | }, |
31 | | - [router] |
| 84 | + [syncUrl] |
32 | 85 | ) |
33 | 86 |
|
34 | | - const sidebarPosts = useMemo(() => allPosts.map((p) => ({ tags: p.tags })), [allPosts]) |
35 | | - |
36 | 87 | return ( |
37 | 88 | <div className='flex min-h-0 flex-1 flex-col overflow-x-clip px-4 sm:px-6 lg:flex-row lg:px-12'> |
38 | 89 | <BlogStudioSidebar |
39 | 90 | posts={sidebarPosts} |
40 | | - activeTag={activeTag} |
| 91 | + activeTag={selectedTag} |
41 | 92 | query={query} |
42 | 93 | onChangeQuery={handleChangeQuery} |
43 | 94 | onSelectTag={handleSelectTag} |
44 | 95 | /> |
45 | | - <main className='relative min-w-0 flex-1'>{children}</main> |
| 96 | + <main className='relative min-w-0 flex-1'> |
| 97 | + <div className='mx-auto w-full max-w-5xl px-4 py-16 sm:px-0 lg:mr-8 lg:px-0 lg:py-16'> |
| 98 | + {children} |
| 99 | + {filteredAuthorPosts.length === 0 ? ( |
| 100 | + <div className='py-20 text-center'> |
| 101 | + <p className='text-[#666] text-[14px]'> |
| 102 | + {lowerQ ? `No posts matching "${query.trim()}".` : 'No posts found.'} |
| 103 | + </p> |
| 104 | + </div> |
| 105 | + ) : ( |
| 106 | + <PostGrid posts={filteredAuthorPosts} /> |
| 107 | + )} |
| 108 | + </div> |
| 109 | + </main> |
46 | 110 | </div> |
47 | 111 | ) |
48 | 112 | } |
0 commit comments