import { useCallback, useEffect, useState } from 'react'; import { useMatrixClient } from './useMatrixClient'; import { useMediaAuthentication } from './useMediaAuthentication'; import { CollectibleProfileFields, StoredCollectible, extractCollectiblesFromProfile, saveAvatarDecoration, saveNameplate, saveProfileEffect, } from '../utils/profileFields'; import { clearProfileCache, getCachedProfileFields, getProfileFields, registerProfileCacheListener, } from '../utils/profileCache'; import { getProfileAssetUrl } from '../utils/profileAssetCache'; import { CollectibleKind } from '../utils/discordCollectibles'; export function useUserCollectibles(): [ CollectibleProfileFields, (kind: CollectibleKind, collectible: StoredCollectible | undefined) => Promise, boolean, ] { const mx = useMatrixClient(); const [collectibles, setCollectibles] = useState({}); const [loading, setLoading] = useState(true); useEffect(() => { const userId = mx.getUserId(); if (!userId) { setLoading(false); return undefined; } let cancelled = false; const load = async () => { setLoading(true); try { const data = extractCollectiblesFromProfile(await getProfileFields(mx, userId)); if (!cancelled) setCollectibles(data); } catch { if (!cancelled) setCollectibles({}); } if (!cancelled) setLoading(false); }; load(); return () => { cancelled = true; }; }, [mx]); const updateCollectible = useCallback( async (kind: CollectibleKind, collectible: StoredCollectible | undefined) => { if (kind === 'profile_effect') { await saveProfileEffect(mx, collectible); } else if (kind === 'nameplate') { await saveNameplate(mx, collectible); } else { await saveAvatarDecoration(mx, collectible); } clearProfileCache(mx.getUserId()); setCollectibles((prev) => ({ ...prev, [kind]: collectible })); }, [mx], ); return [collectibles, updateCollectible, loading]; } /** * Shared profile fields for another user, served from the profile cache so a * timeline of many messages by the same author resolves one request per user. */ export function useOtherUserCollectibles(userId: string): CollectibleProfileFields & { assetUrls: Record; } { const mx = useMatrixClient(); const useAuthentication = useMediaAuthentication(); const [collectibles, setCollectibles] = useState(() => { const cached = getCachedProfileFields(userId); return cached ? extractCollectiblesFromProfile(cached) : {}; }); const [assetUrls, setAssetUrls] = useState>({}); useEffect(() => { if (!userId) { setCollectibles({}); setAssetUrls({}); return undefined; } let cancelled = false; const load = async () => { const data = extractCollectiblesFromProfile(await getProfileFields(mx, userId)); if (cancelled) return; setCollectibles(data); const urls: Record = {}; const entries = [ ['profile_effect', data.profile_effect], ['nameplate', data.nameplate], ['avatar_decoration', data.avatar_decoration], ] as const; for (const [kind, collectible] of entries) { if (collectible) { for (const [role, mxc] of Object.entries(collectible.assets)) { const blobUrl = await getProfileAssetUrl(mx, mxc, useAuthentication); if (blobUrl) urls[`${kind}:${role}`] = blobUrl; } } } if (!cancelled) setAssetUrls(urls); }; const unsubscribe = registerProfileCacheListener((changedUserId) => { if (changedUserId === userId) load(); }); load(); return () => { cancelled = true; unsubscribe(); }; }, [mx, useAuthentication, userId]); return { ...collectibles, assetUrls }; }