|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useRouter } from "next/navigation" |
| 4 | +import { UserProfile } from "@/types/user" |
| 5 | + |
| 6 | +interface CreateButtonProps { |
| 7 | + user: UserProfile | null |
| 8 | + createPath?: string |
| 9 | + onClick?: (e: React.MouseEvent) => void |
| 10 | + label?: string |
| 11 | + showLabelOnMobile?: boolean |
| 12 | + className?: string |
| 13 | + ariaLabel?: string |
| 14 | +} |
| 15 | + |
| 16 | +export default function CreateButton({ |
| 17 | + user, |
| 18 | + createPath, |
| 19 | + onClick, |
| 20 | + label = "Create", |
| 21 | + showLabelOnMobile = true, |
| 22 | + className = "shadow-xl", |
| 23 | + ariaLabel |
| 24 | +}: CreateButtonProps) { |
| 25 | + const router = useRouter() |
| 26 | + |
| 27 | + const handleCreate = (e: React.MouseEvent) => { |
| 28 | + e.preventDefault() |
| 29 | + |
| 30 | + if (onClick) { |
| 31 | + onClick(e) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if (!createPath) return |
| 36 | + |
| 37 | + if (user) { |
| 38 | + router.push(createPath) |
| 39 | + } else { |
| 40 | + localStorage.setItem("postAuthRedirect", createPath) |
| 41 | + router.push("/auth") |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const baseClassName = "flex items-center gap-1.5 py-1.5 px-3 sm:py-2 sm:px-4 rounded-lg shadow-xl border-2 border-black transition-all duration-200 hover:scale-[1.02]" |
| 46 | + const fullClassName = `${baseClassName} ${className}`.trim() |
| 47 | + |
| 48 | + const buttonStyle = { |
| 49 | + backgroundColor: '#f1f5f9', // slate-100 |
| 50 | + boxShadow: "0 1px 2px 0 rgba(0, 0, 0, 0.05)", |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <button |
| 55 | + onClick={handleCreate} |
| 56 | + className={fullClassName} |
| 57 | + style={buttonStyle} |
| 58 | + aria-label={ariaLabel || `Create ${label}`} |
| 59 | + > |
| 60 | + <svg |
| 61 | + xmlns="http://www.w3.org/2000/svg" |
| 62 | + fill="none" |
| 63 | + viewBox="0 0 24 24" |
| 64 | + strokeWidth="2" |
| 65 | + stroke="currentColor" |
| 66 | + className="w-4 h-4 sm:w-5 sm:h-5" |
| 67 | + style={{ color: '#1f2937' }} |
| 68 | + > |
| 69 | + <path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /> |
| 70 | + </svg> |
| 71 | + {label && ( |
| 72 | + <span |
| 73 | + className={`font-medium text-sm sm:text-base ${showLabelOnMobile ? '' : 'hidden sm:inline'}`} |
| 74 | + style={{ color: '#1f2937' }} |
| 75 | + > |
| 76 | + {label} |
| 77 | + </span> |
| 78 | + )} |
| 79 | + </button> |
| 80 | + ) |
| 81 | +} |
0 commit comments