use user state if possible to avoid stale data

This commit is contained in:
Oliver Bryan
2026-01-09 06:18:32 +00:00
parent ac0de68d47
commit 9eba7b1137
2 changed files with 11 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import { UserRound } from "lucide-react";
import { useSession } from "@/components/session-provider";
import { cn } from "@/lib/utils";
const FALLBACK_COLOURS = [
@@ -41,18 +42,25 @@ function getInitials(username: string) {
}
export default function Avatar({
avatarURL,
name,
avatarURL: _avatarURL,
name: _name,
username,
size,
textClass = "text-xs",
strong = false,
}: {
avatarURL?: string | null;
name?: string;
username?: string;
size?: number;
textClass?: string;
strong?: boolean;
}) {
// if the username matches the authed user, use their avatarURL and name (avoid stale data)
const { user } = useSession();
const avatarURL = !strong && username && user && username === user.username ? user.avatarURL : _avatarURL;
const name = !strong && username && user && username === user.username ? user.name : _name;
const backgroundClass = username
? FALLBACK_COLOURS[hashStringToIndex(username, FALLBACK_COLOURS.length)]
: "bg-muted";

View File

@@ -61,6 +61,7 @@ export function UploadAvatar({
avatarURL={avatarURL}
size={24}
textClass={"text-4xl"}
strong
/>
{!uploading && showEdit && (