diff --git a/src/app/components/user-profile/UserHero.tsx b/src/app/components/user-profile/UserHero.tsx index dac5b208..e5fa8daf 100644 --- a/src/app/components/user-profile/UserHero.tsx +++ b/src/app/components/user-profile/UserHero.tsx @@ -24,29 +24,41 @@ import { stopPropagation } from '../../utils/keyboard'; import { useRoom } from '../../hooks/useRoom'; import { useSableCosmetics } from '../../hooks/useSableCosmetics'; import { useNickname } from '../../hooks/useNickname'; +import { useBlobCache } from '../../hooks/useBlobCache'; type UserHeroProps = { userId: string; avatarUrl?: string; + bannerUrl?: string; presence?: UserPresence; }; -export function UserHero({ userId, avatarUrl, presence }: UserHeroProps) { +export function UserHero({ userId, avatarUrl, bannerUrl, presence }: UserHeroProps) { const [viewAvatar, setViewAvatar] = useState(); + const cachedBannerUrl = useBlobCache(bannerUrl); + const cachedAvatarUrl = useBlobCache(avatarUrl); + + const coverUrl = cachedBannerUrl || cachedAvatarUrl; + const isFallbackCover = !cachedBannerUrl && !!cachedAvatarUrl; + return (
- {avatarUrl && ( - {userId} + {coverUrl && ( + {`${userId} )}
diff --git a/src/app/components/user-profile/UserRoomProfile.tsx b/src/app/components/user-profile/UserRoomProfile.tsx index 3489dbfb..1a31acfa 100644 --- a/src/app/components/user-profile/UserRoomProfile.tsx +++ b/src/app/components/user-profile/UserRoomProfile.tsx @@ -35,6 +35,7 @@ import { useSpoilerClickHandler } from '../../hooks/useSpoilerClickHandler'; const KNOWN_KEYS = [ 'moe.sable.app.bio', 'chat.commet.profile_bio', + 'chat.commet.profile_banner', 'io.fsky.nyx.pronouns', 'us.cloke.msc4175.tz', 'm.tz', @@ -213,6 +214,14 @@ export function UserRoomProfile({ userId, initialProfile }: UserRoomProfileProps ? fetchedProfile : (initialProfile as UserProfile) || fetchedProfile; + const parsedBanner = typeof extendedProfile.bannerUrl === 'string' + ? extendedProfile.bannerUrl.replace(/^"|"$/g, '') + : undefined; + + const bannerHttpUrl = parsedBanner + ? mxcUrlToHttp(mx, parsedBanner, useAuthentication) ?? undefined + : undefined; + const handleMessage = () => { closeUserRoomProfile(); const directSearchParam: DirectCreateSearchParams = { @@ -253,6 +262,7 @@ export function UserRoomProfile({ userId, initialProfile }: UserRoomProfileProps diff --git a/src/app/components/user-profile/styles.css.ts b/src/app/components/user-profile/styles.css.ts index 8a7791fe..33fcd096 100644 --- a/src/app/components/user-profile/styles.css.ts +++ b/src/app/components/user-profile/styles.css.ts @@ -23,7 +23,11 @@ export const UserHeroCover = style({ height: '100%', width: '100%', objectFit: 'cover', - filter: 'blur(16px)', + objectPosition: 'center', +}); + +export const UserHeroCoverFallback = style({ + filter: 'blur(16px) brightness(50%)', transform: 'scale(2)', }); diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index f6471011..40692cef 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -977,12 +977,16 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli return; } + const rawBanner = (info as any)['chat.commet.profile_banner']; + const parsedBanner = typeof rawBanner === 'string' ? rawBanner.replace(/^"|"$/g, '') : undefined; + const normalized: any = { avatarUrl: info.avatar_url, displayName: info.displayname, pronouns: (info as any)['io.fsky.nyx.pronouns'], timezone: (info as any)['us.cloke.msc4175.tz'] || (info as any)['m.tz'], bio: (info as any)['moe.sable.app.bio'] || (info as any)['chat.commet.profile_bio'], + bannerUrl: parsedBanner, extended: info, _updatedAt: now, }; diff --git a/src/app/features/settings/account/Profile.tsx b/src/app/features/settings/account/Profile.tsx index 22d5d6ce..9fba38d6 100644 --- a/src/app/features/settings/account/Profile.tsx +++ b/src/app/features/settings/account/Profile.tsx @@ -215,6 +215,179 @@ function ProfileAvatar({ profile, userId }: ProfileProps) { ); } +function ProfileBanner({ profile, userId }: ProfileProps) { + const mx = useMatrixClient(); + const useAuthentication = useMediaAuthentication(); + const [alertRemove, setAlertRemove] = useState(false); + + const [stagedUrl, setStagedUrl] = useState(); + const [isRemoving, setIsRemoving] = useState(false); + + const bannerUrl = profile.bannerUrl + ? mxcUrlToHttp(mx, profile.bannerUrl, useAuthentication) ?? undefined + : undefined; + + useEffect(() => { + if (bannerUrl) { + setStagedUrl(undefined); + } + }, [bannerUrl]); + + const [imageFile, setImageFile] = useState(); + const imageFileURL = useObjectURL(imageFile); + + const uploadAtom = useMemo(() => { + if (imageFile) return createUploadAtom(imageFile); + return undefined; + }, [imageFile]); + + const pickFile = useFilePicker(setImageFile, false); + + const handlePick = useCallback(() => { + setIsRemoving(false); + setStagedUrl(undefined); + pickFile('image/*'); + }, [pickFile]); + + const handleRemoveUpload = useCallback(() => { + setImageFile(undefined); + }, []); + + const handleUploaded = useCallback( + (upload: UploadSuccess) => { + const { mxc } = upload; + + if (imageFileURL) setStagedUrl(imageFileURL); + + mx.setExtendedProfileProperty?.("chat.commet.profile_banner", mxc); + setImageFile(undefined); + }, + [mx, imageFileURL] + ); + + const handleRemoveBanner = async () => { + setIsRemoving(true); + setStagedUrl(undefined); + setImageFile(undefined); + + await mx.setExtendedProfileProperty?.("chat.commet.profile_banner", null); + + setAlertRemove(false); + }; + + const previewUrl = isRemoving ? undefined : (imageFileURL || stagedUrl || bannerUrl); + + return ( + + Profile Banner + + } + > + + + {previewUrl ? ( + Banner Preview + ) : ( + + No Banner Set + + )} + + + {uploadAtom ? ( + + + + ) : ( + + + {bannerUrl && ( + + )} + + )} + + + }> + + setAlertRemove(false), + clickOutsideDeactivates: true, + escapeDeactivates: stopPropagation, + }} + > + +
+ + Remove Banner + + setAlertRemove(false)} radii="300"> + + +
+ + Are you sure you want to remove profile banner? + + +
+
+
+
+
+ ); +} + function ProfileDisplayName({ profile, userId }: ProfileProps) { const mx = useMatrixClient(); const capabilities = useCapabilities(); @@ -409,6 +582,7 @@ export function Profile() { direction="Column" gap="400" > + diff --git a/src/app/hooks/useBlobCache.ts b/src/app/hooks/useBlobCache.ts new file mode 100644 index 00000000..3b5475da --- /dev/null +++ b/src/app/hooks/useBlobCache.ts @@ -0,0 +1,42 @@ +import { useState, useEffect } from 'react'; + +const imageBlobCache = new Map(); + +export function useBlobCache(url?: string): string | undefined { + const [cacheState, setCacheState] = useState<{ sourceUrl?: string; blobUrl?: string }>({ + sourceUrl: url, + blobUrl: url ? imageBlobCache.get(url) : undefined, + }); + + if (url !== cacheState.sourceUrl) { + setCacheState({ + sourceUrl: url, + blobUrl: url ? imageBlobCache.get(url) : undefined, + }); + } + + useEffect(() => { + let isMounted = true; + + if (url && !imageBlobCache.has(url)) { + fetch(url, { mode: 'cors' }) + .then((res) => (res.ok ? res.blob() : Promise.reject())) + .then((blob) => { + if (isMounted) { + const objectUrl = URL.createObjectURL(blob); + imageBlobCache.set(url, objectUrl); + setCacheState({ sourceUrl: url, blobUrl: objectUrl }); + } + }) + .catch(() => { + // silently fail... mrow + }); + } + + return () => { + isMounted = false; + }; + }, [url]); + + return cacheState.blobUrl || url; +} \ No newline at end of file