mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 09:31:17 -08:00
feat: Add pronouns and timezone options along with arbitrary field rendering
This commit is contained in:
1 parent
2056f05273
commit
24ee52f172
6 files changed
+329
-18
No files matched your search
@@ -12,6 +12,7 @@
|
||||
- Bug fix for images/links/anything that loads after room is opened, properly shifting the scroll to the bottom.
|
||||
- Client-side nicknames for other users. PR #3
|
||||
- Inline editor for editing messages.
|
||||
- Pronouns and timezones.
|
||||
- Pressing send on mobile no longer closes keyboard.
|
||||
- Pressing enter on mobile will always provide a newline, ignores the setting for desktop.
|
||||
- More reactive UI (literally just buttons shifting up and shrinking on click)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Box, Button, config, Icon, Icons, Text } from 'folds';
|
||||
import React from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { UserHero, UserHeroName } from './UserHero';
|
||||
import { getMxIdServer, mxcUrlToHttp } from '../../utils/matrix';
|
||||
import { getMemberAvatarMxc, getMemberDisplayName } from '../../utils/room';
|
||||
@@ -22,8 +23,84 @@ import { useMemberPowerCompare } from '../../hooks/useMemberPowerCompare';
|
||||
import { CreatorChip } from './CreatorChip';
|
||||
import { getDirectCreatePath, withSearchParam } from '../../pages/pathUtils';
|
||||
import { DirectCreateSearchParams } from '../../pages/paths';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { nicknamesAtom } from '../../state/nicknames';
|
||||
import { UserProfile, useUserProfile } from '../../hooks/useUserProfile';
|
||||
|
||||
|
||||
function UserExtendedSection({ profile }: { profile: UserProfile }) {
|
||||
const clamp = (str: string, len: number) => str.length > len ? `${str.slice(0, len)}...` : str;
|
||||
const [showMore, setShowMore] = useState(false);
|
||||
|
||||
const pronouns = profile.pronouns?.map((p: any) => clamp(p.summary, 20)).join("/");
|
||||
const timezone = profile.timezone ? clamp(profile.timezone, 64) : null;
|
||||
const localTime = timezone ? new Intl.DateTimeFormat([], {
|
||||
hour: 'numeric',
|
||||
minute: '2-digit',
|
||||
timeZone: timezone,
|
||||
}).format(new Date()) : null;
|
||||
|
||||
const unknownFields = Object.entries(profile.extended || {}).filter(([, value]) =>
|
||||
typeof value === 'string' || typeof value === 'number');
|
||||
|
||||
if (!pronouns && !localTime && unknownFields.length === 0) return null;
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="200" style={{ marginBottom: config.space.S100 }}>
|
||||
{pronouns && (
|
||||
<Box alignItems="Center" gap="200">
|
||||
<Icon size="100" src={Icons.User} style={{ opacity: 0.5 }} />
|
||||
<Text size="T300" priority="400">{pronouns}</Text>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{localTime && (
|
||||
<Box alignItems="Center" gap="200">
|
||||
<Icon size="100" src={Icons.Clock} style={{ opacity: 0.5 }} />
|
||||
<Box direction="Column">
|
||||
<Text size="T200" priority="400">{localTime}</Text>
|
||||
<Text size="T200" priority="300">Local Time ({profile.timezone})</Text>
|
||||
</Box>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{unknownFields.length > 0 && (
|
||||
<Box direction="Column" gap="100" style={{ marginTop: config.space.S100 }}>
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
fill="None"
|
||||
onClick={() => setShowMore(!showMore)}
|
||||
after={<Icon size="50" src={showMore ? Icons.ChevronTop : Icons.ChevronBottom} />}
|
||||
style={{ padding: '1rem', justifyContent: 'flex-start', width: 'fit-content' }}
|
||||
>
|
||||
<Text size="T200" priority="400">{showMore ? "Show less" : `+ ${unknownFields.length} more info`}</Text>
|
||||
</Button>
|
||||
|
||||
{showMore && (
|
||||
<Box
|
||||
direction="Column"
|
||||
style={{
|
||||
padding: config.space.S200,
|
||||
backgroundColor: 'var(--sable-surface-var-container)',
|
||||
borderRadius: config.radii.R400
|
||||
}}
|
||||
>
|
||||
{unknownFields.map(([key, value]) => (
|
||||
<Box key={key} direction="Column">
|
||||
<Text size="T200" priority="400" style={{ letterSpacing: '0.05em' }}>
|
||||
{clamp(key, 64).split('.').pop()?.replace(/_/g, ' ')}
|
||||
</Text>
|
||||
<Text size="T200" priority="300">{String(clamp(value, 64))}</Text>
|
||||
</Box>
|
||||
))}
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
type UserRoomProfileProps = {
|
||||
userId: string;
|
||||
@@ -70,6 +147,8 @@ export function UserRoomProfile({ userId }: UserRoomProfileProps) {
|
||||
navigate(withSearchParam(getDirectCreatePath(), directSearchParam));
|
||||
};
|
||||
|
||||
const extendedProfile = useUserProfile(userId);
|
||||
|
||||
return (
|
||||
<Box direction="Column">
|
||||
<UserHero
|
||||
@@ -96,6 +175,7 @@ export function UserRoomProfile({ userId }: UserRoomProfileProps) {
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
<UserExtendedSection profile={extendedProfile} />
|
||||
<Box alignItems="Center" gap="200" wrap="Wrap">
|
||||
{server && <ServerChip server={server} />}
|
||||
<ShareChip userId={userId} />
|
||||
@@ -142,4 +222,4 @@ export function UserRoomProfile({ userId }: UserRoomProfileProps) {
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
Spinner,
|
||||
} from 'folds';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { MatrixClient } from 'matrix-js-sdk';
|
||||
import { SequenceCard } from '../../../components/sequence-card';
|
||||
import { SequenceCardStyle } from '../styles.css';
|
||||
import { SettingTile } from '../../../components/setting-tile';
|
||||
@@ -43,6 +44,13 @@ import { ModalWide } from '../../../styles/Modal.css';
|
||||
import { createUploadAtom, UploadSuccess } from '../../../state/upload';
|
||||
import { CompactUploadCardRenderer } from '../../../components/upload-card';
|
||||
import { useCapabilities } from '../../../hooks/useCapabilities';
|
||||
import { TimezoneEditor } from './TimezoneEditor';
|
||||
import { PronounEditor } from './PronounEditor';
|
||||
|
||||
type PronounSet = {
|
||||
summary: string;
|
||||
language?: string;
|
||||
};
|
||||
|
||||
type ProfileProps = {
|
||||
profile: UserProfile;
|
||||
@@ -303,23 +311,86 @@ function ProfileDisplayName({ profile, userId }: ProfileProps) {
|
||||
);
|
||||
}
|
||||
|
||||
function ProfileExtended({ profile, userId }: ProfileProps) {
|
||||
const mx = useMatrixClient();
|
||||
|
||||
const pronouns = (profile.pronouns as PronounSet[]) || [];
|
||||
|
||||
// Unknown fields / unimplemented non-matrix-spec fields
|
||||
// Only renders them, can't edit or set
|
||||
const extendedFields = Object.entries(profile.extended || {});
|
||||
|
||||
const handleSaveField = useCallback((key: string, value: any) => {
|
||||
(mx as MatrixClient).setExtendedProfileProperty?.(key, value);
|
||||
}, [mx]);
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Extended Profile</Text>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column" gap="400">
|
||||
<TimezoneEditor
|
||||
current={profile.timezone}
|
||||
onSave={(tz) => {
|
||||
handleSaveField("us.cloke.msc4175.tz", tz);
|
||||
}}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column" gap="400">
|
||||
<PronounEditor
|
||||
current={pronouns}
|
||||
onSave={(p) => handleSaveField("io.fsky.nyx.pronouns", p)}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
{extendedFields.length > 0 && extendedFields.map(([key, value]) => {
|
||||
if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const strVal = String(value);
|
||||
if (
|
||||
(typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') ||
|
||||
strVal.length > 256
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column" gap="400">
|
||||
<SettingTile
|
||||
key={key}
|
||||
title={key.split('.').pop() || key}
|
||||
description={key}
|
||||
after={<Text size="T300" truncate>{strVal}</Text>}
|
||||
/>
|
||||
</SequenceCard>
|
||||
);
|
||||
})}
|
||||
</Box >
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function Profile() {
|
||||
const mx = useMatrixClient();
|
||||
const userId = mx.getUserId()!;
|
||||
const profile = useUserProfile(userId);
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Profile</Text>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<ProfileAvatar userId={userId} profile={profile} />
|
||||
<ProfileDisplayName userId={userId} profile={profile} />
|
||||
</SequenceCard>
|
||||
<Box direction="Column" gap="700">
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Profile</Text>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<ProfileAvatar userId={userId} profile={profile} />
|
||||
<ProfileDisplayName userId={userId} profile={profile} />
|
||||
</SequenceCard>
|
||||
</Box>
|
||||
<ProfileExtended userId={userId} profile={profile} />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import React, { useState, useEffect, ChangeEvent } from 'react';
|
||||
import { Input } from 'folds';
|
||||
import { SettingTile } from '../../../components/setting-tile';
|
||||
|
||||
type PronounSet = {
|
||||
summary: string;
|
||||
language?: string;
|
||||
};
|
||||
|
||||
type PronounEditorProps = {
|
||||
current: PronounSet[];
|
||||
onSave: (p: PronounSet[]) => void;
|
||||
};
|
||||
|
||||
export function PronounEditor({ current, onSave }: PronounEditorProps) {
|
||||
const initialString = current.map(p => p.summary).join("/");
|
||||
const [val, setVal] = useState(initialString);
|
||||
|
||||
useEffect(() => setVal(initialString), [initialString]);
|
||||
|
||||
const handleSave = () => {
|
||||
if (val === initialString) return;
|
||||
const safeVal = val.slice(0, 128);
|
||||
const next = safeVal
|
||||
.split('/')
|
||||
.map(s => s.trim())
|
||||
.filter(Boolean)
|
||||
.map(s => ({ summary: s.slice(0, 16) }));
|
||||
onSave(next);
|
||||
};
|
||||
|
||||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setVal(e.currentTarget.value);
|
||||
};
|
||||
|
||||
return (
|
||||
<SettingTile
|
||||
title="Pronouns"
|
||||
description="Separate with slashes (e.g. they/them)."
|
||||
after={
|
||||
<Input
|
||||
value={val}
|
||||
size="300"
|
||||
radii="300"
|
||||
variant="Secondary"
|
||||
placeholder="Add pronouns..."
|
||||
onChange={handleChange}
|
||||
onBlur={handleSave}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleSave()}
|
||||
style={{ width: '232px' }}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import React, { useMemo, useState, useEffect, ChangeEvent } from 'react';
|
||||
import { Box, IconButton, Icon, Icons, Input } from 'folds';
|
||||
import { SettingTile } from '../../../components/setting-tile';
|
||||
|
||||
interface IntlWithSupportedValues {
|
||||
supportedValuesOf(key: 'timeZone' | string): string[];
|
||||
}
|
||||
|
||||
type TimezoneEditorProps = {
|
||||
current?: string;
|
||||
onSave: (tz: string) => void;
|
||||
};
|
||||
|
||||
export function TimezoneEditor({ current, onSave }: TimezoneEditorProps) {
|
||||
const [val, setVal] = useState(current ?? '');
|
||||
const zones = useMemo(() => {
|
||||
const intlPolyfill = (Intl as unknown as IntlWithSupportedValues);
|
||||
return intlPolyfill.supportedValuesOf("timeZone");
|
||||
}, []);
|
||||
|
||||
useEffect(() => setVal(current ?? ''), [current]);
|
||||
|
||||
const handleSync = () => {
|
||||
const systemTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
setVal(systemTz);
|
||||
onSave(systemTz);
|
||||
};
|
||||
|
||||
const handleManualSave = () => {
|
||||
const trimmed = val.trim().slice(0, 64);
|
||||
const matchedZone = zones.find(z => z.toLowerCase() === trimmed.toLowerCase());
|
||||
if (matchedZone && matchedZone !== current) {
|
||||
onSave(matchedZone);
|
||||
setVal(matchedZone);
|
||||
}
|
||||
};
|
||||
|
||||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setVal(e.currentTarget.value);
|
||||
};
|
||||
|
||||
return (
|
||||
<SettingTile
|
||||
title="Timezone"
|
||||
description="Set manually or sync with your system."
|
||||
after={
|
||||
<Box gap="200" alignItems="Center">
|
||||
<Input
|
||||
list="tz-list"
|
||||
value={val}
|
||||
size="300"
|
||||
radii="300"
|
||||
variant="Secondary"
|
||||
placeholder="e.g. Europe/London"
|
||||
onChange={handleChange}
|
||||
onBlur={handleManualSave}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleManualSave()}
|
||||
style={{ width: '180px' }}
|
||||
/>
|
||||
<datalist id="tz-list">
|
||||
{zones.map((z) => (
|
||||
<option key={z} value={z}>
|
||||
{z}
|
||||
</option>
|
||||
))}
|
||||
</datalist>
|
||||
<IconButton
|
||||
size="300"
|
||||
variant="Secondary"
|
||||
onClick={handleSync}
|
||||
radii="300"
|
||||
title="Sync with system"
|
||||
>
|
||||
<Icon src={Icons.Reload} size="100" />
|
||||
</IconButton>
|
||||
</Box>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -5,7 +5,12 @@ import { useMatrixClient } from './useMatrixClient';
|
||||
export type UserProfile = {
|
||||
avatarUrl?: string;
|
||||
displayName?: string;
|
||||
|
||||
pronouns?: any[]; // io.fsky.nyx.pronouns
|
||||
timezone?: string; // us.cloke.msc4175.tz
|
||||
extended?: Record<string, any>; // any other fields
|
||||
};
|
||||
|
||||
export const useUserProfile = (userId: string): UserProfile => {
|
||||
const mx = useMatrixClient();
|
||||
|
||||
@@ -32,15 +37,34 @@ export const useUserProfile = (userId: string): UserProfile => {
|
||||
}));
|
||||
};
|
||||
|
||||
mx.getProfileInfo(userId).then((info) =>
|
||||
setProfile({
|
||||
mx.getProfileInfo(userId).then((info: any) => {
|
||||
const normalized: UserProfile = {
|
||||
avatarUrl: info.avatar_url,
|
||||
displayName: info.displayname,
|
||||
})
|
||||
);
|
||||
pronouns: info['io.fsky.nyx.pronouns'],
|
||||
timezone: info['us.cloke.msc4175.tz'],
|
||||
extended: {},
|
||||
};
|
||||
|
||||
const knownKeys = [
|
||||
'avatar_url',
|
||||
'displayname',
|
||||
'io.fsky.nyx.pronouns',
|
||||
'us.cloke.msc4175.tz',
|
||||
'm.tz'
|
||||
];
|
||||
Object.keys(info).forEach((key) => {
|
||||
if (!knownKeys.includes(key) && !key.startsWith('m.')) {
|
||||
normalized.extended![key] = info[key];
|
||||
}
|
||||
});
|
||||
|
||||
setProfile(normalized);
|
||||
});
|
||||
|
||||
user?.on(UserEvent.AvatarUrl, onAvatarChange);
|
||||
user?.on(UserEvent.DisplayName, onDisplayNameChange);
|
||||
|
||||
return () => {
|
||||
user?.removeListener(UserEvent.AvatarUrl, onAvatarChange);
|
||||
user?.removeListener(UserEvent.DisplayName, onDisplayNameChange);
|
||||
|
||||
Reference in new issue
Block a user