|
1 | | -import Card from '../../components/card/card' |
| 1 | +import { useState } from 'react'; |
| 2 | +import Card from '../../components/card/card'; |
2 | 3 | import Layout from '../../components/layout/layout'; |
3 | 4 | import { getSortedPostsData } from '../../lib/posts'; |
| 5 | +import config from '../../lib/config'; |
4 | 6 |
|
5 | 7 | export async function getStaticProps() { |
6 | | - |
7 | 8 | try { |
8 | 9 | const allPostsData = await getSortedPostsData(); |
9 | | - |
10 | | - return { |
11 | | - props: { |
12 | | - allPostsData, |
13 | | - }, |
14 | | - }; |
15 | | - } catch(error) { |
16 | | - return { |
17 | | - props: { |
18 | | - allPostsData: [] |
19 | | - } |
20 | | - } |
| 10 | + return { props: { allPostsData } }; |
| 11 | + } catch (error) { |
| 12 | + return { props: { allPostsData: [] } }; |
21 | 13 | } |
22 | | - |
23 | 14 | } |
24 | 15 |
|
25 | | -export default function Blog ({ allPostsData }) { |
| 16 | +export default function Blog({ allPostsData }) { |
| 17 | + const [query, setQuery] = useState(''); |
| 18 | + |
| 19 | + const filtered = query.trim() |
| 20 | + ? allPostsData.filter( |
| 21 | + (post) => |
| 22 | + post.title?.toLowerCase().includes(query.toLowerCase()) || |
| 23 | + post.excerpt?.toLowerCase().includes(query.toLowerCase()) |
| 24 | + ) |
| 25 | + : allPostsData; |
| 26 | + |
26 | 27 | return ( |
27 | | - <Layout> |
28 | | - <section className='text-center pt-12 sm:pt-24 pb-16'> |
29 | | - <h1 className='text-4xl sm:text-7xl font-bold capitalize'> |
30 | | - Blog Posts |
31 | | - </h1> |
32 | | - </section> |
| 28 | + <Layout |
| 29 | + pageMeta={{ |
| 30 | + title: `Blog – ${config.siteName}`, |
| 31 | + description: 'Articles on Azure, DevOps, Kubernetes, Docker, and cloud-native engineering.', |
| 32 | + }} |
| 33 | + > |
| 34 | + <section className="text-center pt-12 sm:pt-24 pb-10"> |
| 35 | + <h1 className="text-4xl sm:text-7xl font-bold capitalize mb-8">Blog Posts</h1> |
33 | 36 |
|
34 | | - <div className='grid grid-cols-1 gap-6 sm:gap-8 max-w-screen-lg mx-auto pb-8'> |
35 | | - {allPostsData.map(post => <Card key={post.id} {...post} />)} |
36 | | - </div> |
37 | | - </Layout> |
38 | | - ); |
39 | | -}; |
| 37 | + {/* Search */} |
| 38 | + <div className="relative max-w-lg mx-auto"> |
| 39 | + <svg |
| 40 | + className="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" |
| 41 | + fill="none" |
| 42 | + stroke="currentColor" |
| 43 | + viewBox="0 0 24 24" |
| 44 | + > |
| 45 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-4.35-4.35M17 11A6 6 0 105 11a6 6 0 0012 0z" /> |
| 46 | + </svg> |
| 47 | + <input |
| 48 | + type="text" |
| 49 | + placeholder="Search posts…" |
| 50 | + value={query} |
| 51 | + onChange={(e) => setQuery(e.target.value)} |
| 52 | + className="w-full pl-10 pr-10 py-2.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 dark:focus:ring-blue-400" |
| 53 | + /> |
| 54 | + {query && ( |
| 55 | + <button |
| 56 | + onClick={() => setQuery('')} |
| 57 | + className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200" |
| 58 | + aria-label="Clear search" |
| 59 | + > |
| 60 | + <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 61 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /> |
| 62 | + </svg> |
| 63 | + </button> |
| 64 | + )} |
| 65 | + </div> |
| 66 | + |
| 67 | + {query && ( |
| 68 | + <p className="mt-3 text-sm text-gray-500 dark:text-gray-400"> |
| 69 | + {filtered.length} result{filtered.length !== 1 ? 's' : ''} for “{query}” |
| 70 | + </p> |
| 71 | + )} |
| 72 | + </section> |
40 | 73 |
|
| 74 | + <div className="grid grid-cols-1 gap-6 sm:gap-8 max-w-screen-lg mx-auto pb-16"> |
| 75 | + {filtered.length > 0 ? ( |
| 76 | + filtered.map((post) => <Card key={post.id} {...post} />) |
| 77 | + ) : ( |
| 78 | + <p className="text-center text-gray-500 dark:text-gray-400 py-12"> |
| 79 | + No posts match “{query}” |
| 80 | + </p> |
| 81 | + )} |
| 82 | + </div> |
| 83 | + </Layout> |
| 84 | + ); |
| 85 | +} |
0 commit comments