Merge branch 'dev' of https://github.com/7w1/sable into dev

This commit is contained in:
7w1 committed 2026-02-24 21:47:42 -06:00
commit b92ed1232b
6 files changed
+254 -8

No files matched your search

+19 -7
View File
@@ -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<string>();
const cachedBannerUrl = useBlobCache(bannerUrl);
const cachedAvatarUrl = useBlobCache(avatarUrl);
const coverUrl = cachedBannerUrl || cachedAvatarUrl;
const isFallbackCover = !cachedBannerUrl && !!cachedAvatarUrl;
return (
<Box direction="Column" className={css.UserHero}>
<div
className={css.UserHeroCoverContainer}
style={{
backgroundColor: colorMXID(userId),
filter: avatarUrl ? undefined : 'brightness(50%)',
height: '80px',
overflow: 'hidden',
width: '100%'
}}
>
{avatarUrl && (
<img className={css.UserHeroCover} src={avatarUrl} alt={userId} draggable="false" />
{coverUrl && (
<img
className={classNames(
css.UserHeroCover,
isFallbackCover && css.UserHeroCoverFallback
)}
src={coverUrl}
alt={`${userId} cover`}
draggable="false"
/>
)}
</div>
<div className={css.UserHeroAvatarContainer}>
@@ -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
<UserHero
userId={userId}
avatarUrl={avatarUrl}
bannerUrl={bannerHttpUrl ?? undefined}
presence={presence && presence.lastActiveTs !== 0 ? presence : undefined}
/>
<Box direction="Column" gap="300" style={{ padding: config.space.S400 }}>
@@ -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)',
});
+4
View File
@@ -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,
};
@@ -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<string>();
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<File>();
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 (
<SettingTile
title={
<Text as="span" size="L400">
Profile Banner
</Text>
}
>
<Box direction="Column" gap="300" grow="Yes">
<Box
style={{
height: '100px',
width: '100%',
borderRadius: config.radii.R400,
overflow: 'hidden',
backgroundColor: 'var(--sable-surface-container)',
position: 'relative',
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
}}
>
{previewUrl ? (
<img
src={previewUrl}
key={previewUrl}
style={{ width: '100%', height: '100%', objectFit: 'cover' }}
alt="Banner Preview"
/>
) : (
<Box justifyContent="Center" alignItems="Center">
<Text priority="300" size="T200">No Banner Set</Text>
</Box>
)}
</Box>
{uploadAtom ? (
<Box gap="200" direction="Column">
<CompactUploadCardRenderer
uploadAtom={uploadAtom}
onRemove={handleRemoveUpload}
onComplete={handleUploaded}
/>
</Box>
) : (
<Box gap="200">
<Button
onClick={handlePick}
size="300"
variant="Secondary"
fill="Soft"
outlined
radii="300"
>
<Text size="B300">{bannerUrl ? 'Change Banner' : 'Upload Banner'}</Text>
</Button>
{bannerUrl && (
<Button
size="300"
variant="Critical"
fill="None"
radii="300"
onClick={() => setAlertRemove(true)}
>
<Text size="B300">Remove</Text>
</Button>
)}
</Box>
)}
</Box>
<Overlay open={alertRemove} backdrop={<OverlayBackdrop />}>
<OverlayCenter>
<FocusTrap
focusTrapOptions={{
initialFocus: false,
onDeactivate: () => setAlertRemove(false),
clickOutsideDeactivates: true,
escapeDeactivates: stopPropagation,
}}
>
<Dialog variant="Surface">
<Header
style={{
padding: `0 ${config.space.S200} 0 ${config.space.S400}`,
borderBottomWidth: config.borderWidth.B300,
}}
variant="Surface"
size="500"
>
<Box grow="Yes">
<Text size="H4">Remove Banner</Text>
</Box>
<IconButton size="300" onClick={() => setAlertRemove(false)} radii="300">
<Icon src={Icons.Cross} />
</IconButton>
</Header>
<Box style={{ padding: config.space.S400 }} direction="Column" gap="400">
<Text priority="400">Are you sure you want to remove profile banner?</Text>
<Button variant="Critical" onClick={handleRemoveBanner}>
<Text size="B400">Remove</Text>
</Button>
</Box>
</Dialog>
</FocusTrap>
</OverlayCenter>
</Overlay>
</SettingTile>
);
}
function ProfileDisplayName({ profile, userId }: ProfileProps) {
const mx = useMatrixClient();
const capabilities = useCapabilities();
@@ -409,6 +582,7 @@ export function Profile() {
direction="Column"
gap="400"
>
<ProfileBanner userId={userId} profile={profile} />
<ProfileAvatar userId={userId} profile={profile} />
<ProfileDisplayName userId={userId} profile={profile} />
</SequenceCard>
+42
View File
@@ -0,0 +1,42 @@
import { useState, useEffect } from 'react';
const imageBlobCache = new Map<string, string>();
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;
}