|
| 1 | +'use client' |
| 2 | + |
1 | 3 | import { Book } from '@/lib/types/book' |
| 4 | +import { memo, ViewTransition } from 'react' |
2 | 5 | import { |
3 | 6 | Card, |
4 | 7 | CardContent, |
5 | 8 | CardDescription, |
6 | 9 | CardHeader, |
7 | 10 | CardTitle, |
8 | | -} from '@/components/ui/card' |
| 11 | +} from '../ui/card' |
9 | 12 | import Image from 'next/image' |
10 | | -import { Check } from 'lucide-react' |
11 | | -import { Fragment } from 'react/jsx-runtime' |
| 13 | +import clsx from 'clsx' |
12 | 14 |
|
13 | | -interface BookSelectCardProps { |
| 15 | +export const SelectedBook = memo<{ |
14 | 16 | book: Book |
15 | | - isSelected: boolean |
16 | | - onToggle: (book: Book) => void |
17 | | - ImageWrapper?: React.FC<React.PropsWithChildren> |
18 | | -} |
19 | | - |
20 | | -export const BookSelectCard: React.FC<BookSelectCardProps> = ({ |
21 | | - book, |
22 | | - isSelected, |
23 | | - onToggle, |
24 | | - ImageWrapper = Fragment, |
25 | | -}) => { |
| 17 | + onDeselect?: (bookID: string) => void |
| 18 | + disabled?: boolean |
| 19 | +}>(({ book, onDeselect, disabled }) => { |
26 | 20 | return ( |
27 | | - <Card |
28 | | - className={`cursor-pointer transition-all duration-200 hover:shadow-md ${ |
29 | | - isSelected ? 'ring-1 ring-primary bg-primary/5' : '' |
30 | | - }`} |
31 | | - onClick={() => onToggle(book)} |
| 21 | + <div |
| 22 | + onClick={() => (disabled ? null : onDeselect?.(book.id))} |
| 23 | + className={clsx( |
| 24 | + 'shrink-0 relative left-0 transition-all not-first-of-type:-ml-12', |
| 25 | + 'hover:transition-all hover:-translate-y-4 hover:transform-none', |
| 26 | + 'peer peer-hover:left-12 peer-hover:transition-all', |
| 27 | + '[transform:perspective(800px)_rotateY(20deg)]', |
| 28 | + !disabled && 'hover:cursor-pointer', |
| 29 | + 'group' |
| 30 | + )} |
32 | 31 | > |
33 | | - <CardHeader className="pb-3"> |
34 | | - <div className="relative mx-auto mb-4 flex justify-center"> |
35 | | - <ImageWrapper |
36 | | - children={ |
37 | | - <Image |
38 | | - src={book.cover ?? '/book-placeholder.svg'} |
39 | | - alt={`${book.title} cover`} |
40 | | - width={100} |
41 | | - height={150} |
42 | | - /> |
43 | | - } |
| 32 | + <ViewTransition name={book.id}> |
| 33 | + <div className="relative"> |
| 34 | + <Image |
| 35 | + src={book?.cover ?? '/book-placeholder.svg'} |
| 36 | + alt={book.title + "'s cover"} |
| 37 | + width={128} |
| 38 | + height={192} |
| 39 | + className="shadow-md rounded-lg w-32 h-48 place-self-center object-cover" |
44 | 40 | /> |
45 | | - {isSelected && ( |
46 | | - <div className="absolute -top-2 -right-2 w-6 h-6 bg-primary rounded-full flex items-center justify-center"> |
47 | | - <Check className="h-3 w-3 text-white" /> |
48 | | - </div> |
49 | | - )} |
50 | | - </div> |
51 | | - <CardTitle className="text-base line-clamp-2">{book.title}</CardTitle> |
52 | | - <CardDescription className="line-clamp-1"> |
53 | | - {book.author} |
54 | | - </CardDescription> |
55 | | - </CardHeader> |
56 | | - <CardContent className="pt-0"> |
57 | | - <div className="space-y-2"> |
58 | | - <div className="flex items-center gap-2 text-sm text-muted-foreground"> |
59 | | - <span>{book.year}</span> |
60 | | - <span>•</span> |
61 | | - <span>{book.code}</span> |
| 41 | + <div className="absolute inset-0 bg-black/70 opacity-0 group-hover:opacity-100 transition-opacity rounded-lg flex flex-col justify-end p-2"> |
| 42 | + <p className="text-white text-xs font-semibold line-clamp-2 mb-1"> |
| 43 | + {book.title} |
| 44 | + </p> |
| 45 | + <p className="text-white/90 text-xs font-mono">{book.code}</p> |
62 | 46 | </div> |
63 | 47 | </div> |
64 | | - </CardContent> |
65 | | - </Card> |
| 48 | + </ViewTransition> |
| 49 | + </div> |
| 50 | + ) |
| 51 | +}) |
| 52 | + |
| 53 | +SelectedBook.displayName = 'SelectedBook' |
| 54 | + |
| 55 | +// Memoize the card component to prevent unnecessary re-renders |
| 56 | +export const UnselectedBook = memo<{ |
| 57 | + book: Book |
| 58 | + onSelect?: (bookID: string) => void |
| 59 | + disabled?: boolean |
| 60 | +}>(({ book, onSelect, disabled }) => { |
| 61 | + return ( |
| 62 | + <ViewTransition name={'card-' + book.id}> |
| 63 | + <Card |
| 64 | + className={clsx( |
| 65 | + 'cursor-pointer transition-all duration-200 hover:shadow-md', |
| 66 | + disabled && 'opacity-50 cursor-not-allowed' |
| 67 | + )} |
| 68 | + onClick={disabled ? undefined : () => onSelect?.(book.id)} |
| 69 | + > |
| 70 | + <CardHeader className="pb-3 text-center"> |
| 71 | + <ViewTransition name={book.id}> |
| 72 | + <Image |
| 73 | + src={book.cover ?? '/book-placeholder.svg'} |
| 74 | + alt={`${book.title} cover`} |
| 75 | + width={96} |
| 76 | + height={144} |
| 77 | + className="mx-auto w-24 h-[9rem] rounded-lg" |
| 78 | + /> |
| 79 | + </ViewTransition> |
| 80 | + <CardTitle className="text-base line-clamp-2">{book.title}</CardTitle> |
| 81 | + <CardDescription className="line-clamp-1"> |
| 82 | + {book.author} |
| 83 | + </CardDescription> |
| 84 | + </CardHeader> |
| 85 | + <CardContent className="flex items-center justify-center gap-2 text-sm text-muted-foreground"> |
| 86 | + <span>{book.year}</span> |
| 87 | + <span>•</span> |
| 88 | + <span>{book.code}</span> |
| 89 | + </CardContent> |
| 90 | + </Card> |
| 91 | + </ViewTransition> |
66 | 92 | ) |
67 | | -} |
| 93 | +}) |
| 94 | + |
| 95 | +UnselectedBook.displayName = 'UnselectedBook' |
| 96 | + |
| 97 | +export const BookSelectCard = UnselectedBook |
0 commit comments