Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(profile): harden avatar fallback, admin ring in sidebar, dedupe auth-status fetch

Bingxi Zhao (Frank)86ff120668c9
diff --git a/web/components/UserAvatar.tsx b/web/components/UserAvatar.tsx
index 26b87b65c..6505d0cb1 100644
--- a/web/components/UserAvatar.tsx
+++ b/web/components/UserAvatar.tsx
@@ -108,13 +108,18 @@ export function UserAvatar({
const isImage =
descriptor.kind === "image" && Boolean(userId) && !imageBroken;
+ // A stored marker can name an icon/color this build doesn't know (an older
+ // client, or a hand-set marker). Validate against the known sets and fall
+ // back to the deterministic avatar so it never renders blank
+ // (AVATAR_COLORS[unknown] would be undefined -> an invisible avatar).
+ const fallback = fallbackAvatarFor(username);
let iconName: string;
let colorName: string;
if (descriptor.kind === "icon") {
- iconName = descriptor.icon;
- colorName = descriptor.color;
+ iconName = descriptor.icon in AVATAR_ICONS ? descriptor.icon : fallback.icon;
+ colorName =
+ descriptor.color in AVATAR_COLORS ? descriptor.color : fallback.color;
} else {
- const fallback = fallbackAvatarFor(username);
iconName = fallback.icon;
colorName = fallback.color;
}
diff --git a/web/components/auth/ProfileLink.tsx b/web/components/auth/ProfileLink.tsx
index 421c6bb90..d579cdd98 100644
--- a/web/components/auth/ProfileLink.tsx
+++ b/web/components/auth/ProfileLink.tsx
@@ -31,6 +31,7 @@ export function ProfileLink({ collapsed = false }: ProfileLinkProps) {
username={status.username}
userId={status.user_id}
avatar={status.avatar}
+ role={status.role}
size={collapsed ? 18 : 16}
/>
);
diff --git a/web/hooks/useAuthStatus.ts b/web/hooks/useAuthStatus.ts
index dd9abb09f..113d6564a 100644
--- a/web/hooks/useAuthStatus.ts
+++ b/web/hooks/useAuthStatus.ts
@@ -31,19 +31,35 @@ const INITIAL: AuthStatusState = {
* constant, so it works identically on Docker (read-only rootfs), the PyPI
* `deeptutor start` launcher, and source dev.
*/
-export function useAuthStatus(): AuthStatusState {
- const [state, setState] = useState<AuthStatusState>(INITIAL);
+// Several components (sidebar Admin / Logout / Profile links) mount this hook
+// at once. Share a single in-flight request so a page load makes one
+// /api/v1/auth/status call instead of one per consumer, and clear it once
+// settled so a later mount (e.g. after login/logout) fetches fresh.
+let inflight: Promise<AuthStatusState> | null = null;
- useEffect(() => {
- let alive = true;
- fetchAuthStatus().then((status) => {
- if (!alive) return;
- setState({
+function loadAuthStatus(): Promise<AuthStatusState> {
+ if (!inflight) {
+ inflight = fetchAuthStatus()
+ .then((status) => ({
enabled: Boolean(status?.enabled),
authenticated: Boolean(status?.authenticated),
isAdmin: status?.role === "admin",
loading: false,
+ }))
+ .finally(() => {
+ inflight = null;
});
+ }
+ return inflight;
+}
+
+export function useAuthStatus(): AuthStatusState {
+ const [state, setState] = useState<AuthStatusState>(INITIAL);
+
+ useEffect(() => {
+ let alive = true;
+ loadAuthStatus().then((next) => {
+ if (alive) setState(next);
});
return () => {
alive = false;