|
1 | 1 | import { useConst } from '@siberiacancode/reactuse'; |
| 2 | +import { HeartIcon } from 'lucide-react'; |
| 3 | +import { useState } from 'react'; |
| 4 | + |
| 5 | +const PALETTES = [ |
| 6 | + ['#0f172a', '#1e293b', '#475569', '#94a3b8', '#cbd5e1'], |
| 7 | + ['#7c2d12', '#c2410c', '#f97316', '#fdba74', '#fff7ed'], |
| 8 | + ['#064e3b', '#047857', '#10b981', '#6ee7b7', '#ecfdf5'], |
| 9 | + ['#1e1b4b', '#4338ca', '#818cf8', '#c7d2fe', '#eef2ff'], |
| 10 | + ['#831843', '#be185d', '#ec4899', '#f9a8d4', '#fdf2f8'] |
| 11 | +]; |
| 12 | + |
| 13 | +const GRID_COLS = 35; |
| 14 | +const GRID_ROWS = 20; |
| 15 | + |
| 16 | +const random = <T,>(items: T[]): T => items[Math.floor(Math.random() * items.length)]; |
| 17 | + |
| 18 | +const formatPostedAt = (date: Date) => |
| 19 | + date.toLocaleDateString('en-US', { |
| 20 | + month: 'short', |
| 21 | + day: 'numeric', |
| 22 | + year: 'numeric' |
| 23 | + }); |
| 24 | + |
| 25 | +const generatePost = () => { |
| 26 | + const palette = random(PALETTES); |
| 27 | + const pixels = Array.from({ length: GRID_COLS * GRID_ROWS }).map(() => random(palette)); |
| 28 | + |
| 29 | + return { |
| 30 | + pixels, |
| 31 | + palette, |
| 32 | + author: 'siberiacancode', |
| 33 | + postedAt: formatPostedAt(new Date()) |
| 34 | + }; |
| 35 | +}; |
2 | 36 |
|
3 | 37 | const Demo = () => { |
4 | | - const mountTime = useConst(() => new Date().toTimeString()); |
5 | | - const obj = useConst({ a: Math.random() }); |
| 38 | + const post = useConst(generatePost); |
| 39 | + |
| 40 | + const [liked, setLiked] = useState(false); |
| 41 | + const [likeCount, setLikeCount] = useState(() => Math.floor(Math.random() * 60) + 20); |
| 42 | + |
| 43 | + const onToggleLike = () => { |
| 44 | + setLiked(!liked); |
| 45 | + setLikeCount(liked ? likeCount - 1 : likeCount + 1); |
| 46 | + }; |
6 | 47 |
|
7 | 48 | return ( |
8 | | - <div> |
9 | | - <p> |
10 | | - Mount time: <code>{mountTime}</code> |
11 | | - </p> |
12 | | - <p> |
13 | | - Value from constant object: <code>{obj.a}</code> |
14 | | - </p> |
15 | | - </div> |
| 49 | + <section className='flex min-w-xs flex-col items-center rounded-2xl border p-4 md:min-w-md'> |
| 50 | + <div className='flex w-full flex-col gap-4 overflow-hidden'> |
| 51 | + <div className='flex items-center justify-between'> |
| 52 | + <span className='text-sm font-medium'>@{post.author}</span> |
| 53 | + </div> |
| 54 | + |
| 55 | + <div |
| 56 | + style={{ |
| 57 | + gridTemplateColumns: `repeat(${GRID_COLS}, 1fr)`, |
| 58 | + aspectRatio: `${GRID_COLS} / ${GRID_ROWS}` |
| 59 | + }} |
| 60 | + className='grid w-full' |
| 61 | + > |
| 62 | + {post.pixels.map((color, index) => ( |
| 63 | + <div key={index} className='rounded-xl' style={{ backgroundColor: color }} /> |
| 64 | + ))} |
| 65 | + </div> |
| 66 | + |
| 67 | + <div className='flex items-center justify-between gap-4 py-2'> |
| 68 | + <button data-variant='ghost' type='button' onClick={onToggleLike}> |
| 69 | + <HeartIcon |
| 70 | + className='pointer-events-none size-4' |
| 71 | + fill={liked ? '#ef4444' : 'none'} |
| 72 | + stroke={liked ? '#ef4444' : 'currentColor'} |
| 73 | + /> |
| 74 | + <span>{likeCount} likes</span> |
| 75 | + </button> |
| 76 | + |
| 77 | + <div className='text-muted-foreground text-xs'>{post.postedAt}</div> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + </section> |
16 | 81 | ); |
17 | 82 | }; |
18 | 83 |
|
|
0 commit comments