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 { UserRound } from "lucide-react";
import { useSession } from "@/components/session-provider";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
const FALLBACK_COLOURS = [ const FALLBACK_COLOURS = [
@@ -41,18 +42,25 @@ function getInitials(username: string) {
} }
export default function Avatar({ export default function Avatar({
avatarURL, avatarURL: _avatarURL,
name, name: _name,
username, username,
size, size,
textClass = "text-xs", textClass = "text-xs",
strong = false,
}: { }: {
avatarURL?: string | null; avatarURL?: string | null;
name?: string; name?: string;
username?: string; username?: string;
size?: number; size?: number;
textClass?: string; 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 const backgroundClass = username
? FALLBACK_COLOURS[hashStringToIndex(username, FALLBACK_COLOURS.length)] ? FALLBACK_COLOURS[hashStringToIndex(username, FALLBACK_COLOURS.length)]
: "bg-muted"; : "bg-muted";

View File

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