diff --git a/app/Avatar.tsx b/app/Avatar.tsx new file mode 100644 index 0000000..4954cea --- /dev/null +++ b/app/Avatar.tsx @@ -0,0 +1,40 @@ +type AvatarProps = { + username: string; + avatarUpdatedAt: string | null; + size?: "sm" | "md" | "lg"; + className?: string; +}; + +const SIZE_CLASSES: Record, string> = { + sm: "h-8 w-8 text-sm", + md: "h-10 w-10 text-base", + lg: "h-24 w-24 text-3xl", +}; + +export default function Avatar({ + username, + avatarUpdatedAt, + size = "md", + className = "", +}: AvatarProps) { + const sizeClass = SIZE_CLASSES[size]; + const base = `flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-neutral-700 font-bold text-white ${sizeClass} ${className}`; + + if (!avatarUpdatedAt) { + return
{username.charAt(0).toUpperCase()}
; + } + + const version = Date.parse(avatarUpdatedAt) || 0; + const src = `/api/avatars/${encodeURIComponent(username)}?v=${version}`; + + return ( +
+ {/* eslint-disable-next-line @next/next/no-img-element */} + {`${username} +
+ ); +} diff --git a/app/Composer.tsx b/app/Composer.tsx index 6ca0f01..ca07a33 100644 --- a/app/Composer.tsx +++ b/app/Composer.tsx @@ -3,8 +3,15 @@ import { useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { createPost } from "./actions"; +import Avatar from "./Avatar"; -export default function Composer({ username }: { username: string }) { +export default function Composer({ + username, + avatarUpdatedAt, +}: { + username: string; + avatarUpdatedAt: string | null; +}) { const router = useRouter(); const formRef = useRef(null); const [preview, setPreview] = useState(null); @@ -34,9 +41,11 @@ export default function Composer({ username }: { username: string }) { onSubmit={handleSubmit} className="flex gap-3 border-b border-neutral-800 p-4" > -
- {username.charAt(0).toUpperCase()} -
+