A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(app): show profile badges together once they load (#3580)
Hampus55a25cfaac71
diff --git a/fluxer_app/src/features/user/commands/UserProfileCommands.tsx b/fluxer_app/src/features/user/commands/UserProfileCommands.tsxindex befdf1034..4b5962045 100644--- a/fluxer_app/src/features/user/commands/UserProfileCommands.tsx+++ b/fluxer_app/src/features/user/commands/UserProfileCommands.tsx@@ -16,6 +16,7 @@ import {Profile, type ProfileWire} from '@app/features/user/models/Profile';import UserProfile from '@app/features/user/state/UserProfile';import UserProfileMobile from '@app/features/user/state/UserProfileMobile';import Users from '@app/features/user/state/Users';+import {warmProfileBadgeImages} from '@app/features/user/utils/ProfileUtils';import {ME} from '@fluxer/constants/src/AppConstants';import type {ProfileBadgePreviewRequest,@@ -66,6 +67,7 @@ async function requestProfile(userId: string, guildId?: string): Promise<Profilefunction storeProfile(profile: ProfileWire, guildId?: string): Profile {Users.handleUserUpdate(profile.user);const profileRecord = new Profile(profile, guildId);+ warmProfileBadgeImages(profileRecord.profileBadges);UserProfile.handleProfileCreate(profileRecord);return profileRecord;}diff --git a/fluxer_app/src/features/user/components/popouts/UserProfileBadges.module.css b/fluxer_app/src/features/user/components/popouts/UserProfileBadges.module.cssindex ba5a79a1d..dadd1b862 100644--- a/fluxer_app/src/features/user/components/popouts/UserProfileBadges.module.css+++ b/fluxer_app/src/features/user/components/popouts/UserProfileBadges.module.css@@ -13,6 +13,10 @@gap: 0.375rem;}+.pending {+ visibility: hidden;+}+.containerPopout {position: absolute;top: 7.0625rem;diff --git a/fluxer_app/src/features/user/components/popouts/UserProfileBadges.tsx b/fluxer_app/src/features/user/components/popouts/UserProfileBadges.tsxindex a8b74edd9..c2336d87a 100644--- a/fluxer_app/src/features/user/components/popouts/UserProfileBadges.tsx+++ b/fluxer_app/src/features/user/components/popouts/UserProfileBadges.tsx@@ -1,15 +1,17 @@// SPDX-License-Identifier: AGPL-3.0-or-later-import RuntimeConfig from '@app/features/app/state/RuntimeConfig';+import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';import * as PremiumModalCommands from '@app/features/premium/commands/PremiumModalCommands';import FocusRing from '@app/features/ui/focus_ring/FocusRing';import {Tooltip} from '@app/features/ui/tooltip/Tooltip';import {handleExternalLinkClick} from '@app/features/ui/utils/NativeUtils';import styles from '@app/features/user/components/popouts/UserProfileBadges.module.css';+import {profileBadgeImageUrl} from '@app/features/user/utils/ProfileUtils';import type {ProfileBadgeResponse} from '@fluxer/schema/src/domains/user/ProfileBadgeSchemas';import {clsx} from 'clsx';import {observer} from 'mobx-react-lite';import type React from 'react';+import {useEffect, useState} from 'react';interface UserProfileBadgesProps {badges: ReadonlyArray<ProfileBadgeResponse>;@@ -18,8 +20,32 @@ interface UserProfileBadgesProps {'data-flx'?: string;}-function badgeImageUrl(asset: string): string {- return `${RuntimeConfig.staticCdnEndpoint}/badges/${asset}.webp`;+const BADGE_REVEAL_TIMEOUT_MS = 1000;++function isSettledImage(url: string): boolean {+ return ImageCacheUtils.hasImage(url) || ImageCacheUtils.hasFailedImage(url);+}++function useBadgeImagesReady(badges: ReadonlyArray<ProfileBadgeResponse>): boolean {+ const key = badges.flatMap((badge) => (badge.asset === null ? [] : [profileBadgeImageUrl(badge.asset)])).join('\n');+ const settled = key === '' || key.split('\n').every(isSettledImage);+ const [readyKey, setReadyKey] = useState<string | null>(null);+ useEffect(() => {+ if (settled) return;+ const urls = key.split('\n');+ let pending = urls.length;+ const settle = () => {+ pending -= 1;+ if (pending === 0) setReadyKey(key);+ };+ const cancels = urls.map((url) => ImageCacheUtils.loadImage(url, settle, settle));+ const timer = window.setTimeout(() => setReadyKey(key), BADGE_REVEAL_TIMEOUT_MS);+ return () => {+ window.clearTimeout(timer);+ for (const cancel of cancels) cancel();+ };+ }, [key, settled]);+ return settled || readyKey === key;}function ProfileBadgeDetailsTooltip({@@ -35,7 +61,7 @@ function ProfileBadgeDetailsTooltip({<div className={styles.details} data-flx={dataFlx}>{badge.asset !== null && (<img- src={badgeImageUrl(badge.asset)}+ src={profileBadgeImageUrl(badge.asset)}alt=""className={styles.detailsArt}style={badge.aspect_ratio ? {width: 'auto', aspectRatio: badge.aspect_ratio} : undefined}@@ -61,6 +87,7 @@ function opensPremiumModal(badge: ProfileBadgeResponse): boolean {export const UserProfileBadges: React.FC<UserProfileBadgesProps> = observer(({badges, isModal = false, isMobile = false, 'data-flx': dataFlx = 'user.user-profile-badges'}) => {+ const ready = useBadgeImagesReady(badges);if (badges.length === 0) {return null;}@@ -103,12 +130,12 @@ export const UserProfileBadges: React.FC<UserProfileBadgesProps> = observer();};return (- <div className={containerClassName} data-flx={dataFlx}>+ <div className={clsx(containerClassName, !ready && styles.pending)} data-flx={dataFlx}>{badges.map((badge) => {const content =badge.asset !== null ? (<img- src={badgeImageUrl(badge.asset)}+ src={profileBadgeImageUrl(badge.asset)}alt={badge.accessible_label ?? badge.caption ?? badge.label}className={touch ? styles.badgeMobile : styles.badgeDesktop}style={badge.aspect_ratio ? {width: 'auto', aspectRatio: badge.aspect_ratio} : undefined}diff --git a/fluxer_app/src/features/user/utils/ProfileUtils.ts b/fluxer_app/src/features/user/utils/ProfileUtils.tsindex c2fe7298a..2ebadad22 100644--- a/fluxer_app/src/features/user/utils/ProfileUtils.ts+++ b/fluxer_app/src/features/user/utils/ProfileUtils.ts@@ -1,5 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import RuntimeConfig from '@app/features/app/state/RuntimeConfig';+import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';import {Profile} from '@app/features/user/models/Profile';import type {User} from '@app/features/user/models/User';import {ProfileFieldPrivacyFlags} from '@fluxer/constants/src/UserConstants';@@ -50,3 +52,13 @@ export function createMockProfile(profile_badges: options?.profileBadges ?? [],});}++export function profileBadgeImageUrl(asset: string): string {+ return `${RuntimeConfig.staticCdnEndpoint}/badges/${asset}.webp`;+}++export function warmProfileBadgeImages(badges: ReadonlyArray<ProfileBadgeResponse>): void {+ for (const badge of badges) {+ if (badge.asset !== null) ImageCacheUtils.warmImage(profileBadgeImageUrl(badge.asset));+ }+}