|
| 1 | +import Link from 'next/link'; |
| 2 | +import Image from 'next/image'; |
| 3 | +import { signOut } from 'next-auth/react'; |
| 4 | + |
| 5 | +interface MypageItem { |
| 6 | + icon: string; |
| 7 | + label: string; |
| 8 | + href?: string; |
| 9 | + onClick?: () => void; |
| 10 | + type?: 'link' | 'button'; |
| 11 | +} |
| 12 | + |
| 13 | +interface MypageSectionProps { |
| 14 | + title?: string; |
| 15 | + items: MypageItem[]; |
| 16 | + outerDivClassName?: string; |
| 17 | + innerDivClassName?: string; |
| 18 | + gridClassName?: string; |
| 19 | + itemClassName?: string; |
| 20 | + iconContainerClassName?: string; |
| 21 | + labelClassName?: string; |
| 22 | +} |
| 23 | + |
| 24 | +export default function MypageSection({ |
| 25 | + title, |
| 26 | + items, |
| 27 | + outerDivClassName = 'border-t border-gray-200', |
| 28 | + innerDivClassName = 'mx-5 rounded-lg border border-gray-200 px-5 py-5', |
| 29 | + gridClassName = 'flex justify-center gap-8', |
| 30 | + itemClassName = 'flex flex-col items-center gap-2', |
| 31 | + iconContainerClassName = 'flex h-12 w-12 items-center justify-center', |
| 32 | + labelClassName = 'text-center text-xs font-medium whitespace-pre-wrap text-gray-700', |
| 33 | +}: MypageSectionProps) { |
| 34 | + const handleItemClick = async (item: MypageItem) => { |
| 35 | + if (item.type === 'button' && item.onClick) { |
| 36 | + item.onClick(); |
| 37 | + } else if (item.type === 'button' && item.label === '로그아웃') { |
| 38 | + await signOut({ callbackUrl: '/' }); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className={outerDivClassName}> |
| 44 | + {title && <h2 className="px-5 py-4 text-lg font-bold">{title}</h2>} |
| 45 | + <div className={innerDivClassName}> |
| 46 | + <div className={gridClassName}> |
| 47 | + {items.map((item) => { |
| 48 | + const content = ( |
| 49 | + <> |
| 50 | + <div className={iconContainerClassName}> |
| 51 | + <Image |
| 52 | + src={item.icon} |
| 53 | + alt={item.label} |
| 54 | + className="h-full w-full" |
| 55 | + width={48} |
| 56 | + height={48} |
| 57 | + /> |
| 58 | + </div> |
| 59 | + <span className={labelClassName}>{item.label}</span> |
| 60 | + </> |
| 61 | + ); |
| 62 | + |
| 63 | + if (item.type === 'button') { |
| 64 | + return ( |
| 65 | + <button |
| 66 | + key={item.label} |
| 67 | + onClick={() => handleItemClick(item)} |
| 68 | + className={itemClassName} |
| 69 | + > |
| 70 | + {content} |
| 71 | + </button> |
| 72 | + ); |
| 73 | + } |
| 74 | + return ( |
| 75 | + <Link |
| 76 | + key={item.label} |
| 77 | + href={item.href || '#'} |
| 78 | + className={itemClassName} |
| 79 | + > |
| 80 | + {content} |
| 81 | + </Link> |
| 82 | + ); |
| 83 | + })} |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
0 commit comments