Skip to content

Commit ea1fe57

Browse files
committed
feat: improve user avatar handling and display in LatestUsers component
- Added a placeholder for user avatars when profile photos are not available. - Enhanced the UserItem component to conditionally render user information and ensure proper handling of undefined user data. - Updated the avatar image source logic for better reliability and user experience.
1 parent 2db6598 commit ea1fe57

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

src/components/widgets/LatestUsers.tsx

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _t from "@/i18n/_t";
22
import Link from "next/link";
33
import * as userActions from "@/backend/services/user.action";
44
import { User } from "@/backend/models/domain-models";
5+
import { getAvatarPlaceholder } from "@/lib/utils";
56
import Image from "next/image";
67
import getFileUrl from "@/utils/getFileUrl";
78

@@ -18,46 +19,51 @@ const LatestUsers = async () => {
1819
Array.from({ length: 10 }).map((_, i) => <UserSkeleton key={i} />)} */}
1920

2021
{/* <pre>{JSON.stringify(data, null, 2)}</pre> */}
21-
{usersResponse?.nodes.map((user) => (
22-
<UserItem key={user.id} user={user!} />
23-
))}
22+
{usersResponse?.nodes.map((user) =>
23+
user ? <UserItem key={user.id} user={user} /> : null,
24+
)}
2425
</div>
2526
</div>
2627
);
2728
};
2829

2930
export default LatestUsers;
3031

31-
const UserItem = ({ user }: { user: User }) => (
32-
<div className="flex items-center">
33-
{user?.profile_photo && (
32+
const UserItem = ({ user }: { user: User }) => {
33+
const avatarSrc =
34+
getFileUrl(user.profile_photo) ||
35+
getAvatarPlaceholder(user.name ?? user.username ?? "");
36+
37+
return (
38+
<div className="flex items-center">
3439
<Link href={`/@${user.username}`}>
3540
<div className="size-10 overflow-hidden rounded-full">
3641
<Image
37-
src={getFileUrl(user?.profile_photo)!}
38-
alt={user?.name!}
42+
src={avatarSrc}
43+
alt={user.name ?? user.username}
3944
loading="lazy"
45+
unoptimized={!user.profile_photo}
4046
className="h-auto w-full"
4147
width={40}
4248
height={40}
4349
/>
4450
</div>
4551
</Link>
46-
)}
4752

48-
<div className="ml-2">
49-
<h3 className="text-dark text-base">
50-
<Link href={`/@${user?.username}`} className="text-foreground">
51-
{user.name}
52-
</Link>
53-
</h3>
54-
<p className="text-muted-foreground text-xs">
55-
{new Date(user.created_at).toLocaleDateString("bn-BD", {
56-
month: "long",
57-
day: "numeric",
58-
year: "numeric",
59-
})}
60-
</p>
53+
<div className="ml-2">
54+
<h3 className="text-dark text-base">
55+
<Link href={`/@${user.username}`} className="text-foreground">
56+
{user.name}
57+
</Link>
58+
</h3>
59+
<p className="text-muted-foreground text-xs">
60+
{new Date(user.created_at).toLocaleDateString("bn-BD", {
61+
month: "long",
62+
day: "numeric",
63+
year: "numeric",
64+
})}
65+
</p>
66+
</div>
6167
</div>
62-
</div>
63-
);
68+
);
69+
};

0 commit comments

Comments
 (0)