diff --git a/frontend/src/chat.ts b/frontend/src/chat.ts index 834a3a7..2808973 100644 --- a/frontend/src/chat.ts +++ b/frontend/src/chat.ts @@ -12,6 +12,7 @@ import type { Message, Messages, WebSocketMessage } from "./types"; import { formatTime } from "./utils/utils"; import { show as showContextMenu } from "./message-context-menu"; import { show as showUserProfileDialog } from "./user-profile-dialog"; +import defaultAvatar from "./images/default-avatar.png"; /** * Adds a new message to the chat interface @@ -41,11 +42,17 @@ export function addMessage(message: Message, isAuthor: boolean): void { profilePicDiv.classList.add('message-profile-pic'); const profileImg = document.createElement('img'); - profileImg.src = message.profile_picture || './src/images/default-avatar.png'; + profileImg.src = message.profile_picture || defaultAvatar; profileImg.alt = message.username; - profileImg.onerror = () => { - profileImg.src = './src/images/default-avatar.png'; - }; + + let errorLock = false + + profileImg.addEventListener("error", () => { + if (!errorLock) { + profileImg.src = defaultAvatar; + errorLock = true + } + }); // Add click handler to profile picture profileImg.style.cursor = 'pointer'; diff --git a/frontend/src/user-profile-dialog.ts b/frontend/src/user-profile-dialog.ts index bce721c..7900f2c 100644 --- a/frontend/src/user-profile-dialog.ts +++ b/frontend/src/user-profile-dialog.ts @@ -10,6 +10,7 @@ import { API_BASE_URL } from "./config"; import type { UserProfile } from "./types"; import { showError, showSuccess } from "./utils/notification"; import { formatTime } from "./utils/utils"; +import defaultAvatar from "./images/default-avatar.png"; let dialog = document.getElementById("user-profile-dialog")!; @@ -80,9 +81,15 @@ function populateDialog(profile: UserProfile): void { // Profile picture const profilePic = dialog.querySelector('.profile-picture') as HTMLImageElement; - profilePic.src = profile.profile_picture || './src/images/default-avatar.png'; + profilePic.src = profile.profile_picture || defaultAvatar; + + let errorLock = false + profilePic.addEventListener("error", () => { - profilePic.src = './src/images/default-avatar.png'; + if (!errorLock) { + profilePic.src = defaultAvatar; + errorLock = true; + } }); // Username