add pronouns support to PerMessageProfile; update editor and hooks for pronoun management
This commit is contained in:
5 files changed
+113
-24
No files matched your search
@@ -659,6 +659,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
id: perMessageProfile.id,
|
||||
displayname: perMessageProfile.name,
|
||||
avatar_url: perMessageProfile.avatarUrl,
|
||||
'io.fsky.nyx.pronouns': perMessageProfile.pronouns,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import { UserAvatar } from '$components/user-avatar';
|
||||
import { CompactUploadCardRenderer } from '$components/upload-card';
|
||||
import { addOrUpdatePerMessageProfile, deletePerMessageProfile } from '$hooks/usePerMessageProfile';
|
||||
import { SequenceCardStyle } from '../styles.css';
|
||||
import { parsePronounsStringToPronounsSetArray, PronounSet } from '$utils/pronouns';
|
||||
|
||||
/**
|
||||
* the props we use for the per-message profile editor, which is used to edit a per-message profile. This is used in the settings page when the user wants to edit a profile.
|
||||
@@ -20,6 +21,7 @@ type PerMessageProfileEditorProps = {
|
||||
profileId: string;
|
||||
avatarMxcUrl?: string;
|
||||
displayName?: string;
|
||||
pronouns?: PronounSet[];
|
||||
onChange?: (profile: { id: string; name: string; avatarUrl?: string }) => void;
|
||||
onDelete?: (profileId: string) => void;
|
||||
};
|
||||
@@ -29,11 +31,29 @@ export function PerMessageProfileEditor({
|
||||
profileId,
|
||||
avatarMxcUrl,
|
||||
displayName,
|
||||
pronouns = Array<PronounSet>(),
|
||||
onChange,
|
||||
onDelete,
|
||||
}: Readonly<PerMessageProfileEditorProps>) {
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const [currentDisplayName, setCurrentDisplayName] = useState(displayName ?? '');
|
||||
|
||||
// Pronouns
|
||||
const [currentPronouns, setCurrentPronouns] = useState<PronounSet[]>(pronouns);
|
||||
const currentPronounsString = useMemo(() => {
|
||||
const pronounsString = Array.isArray(currentPronouns)
|
||||
? currentPronouns.map((p) => `${p.language ? `${p.language}:` : ''}${p.summary}`).join(', ')
|
||||
: '';
|
||||
return [pronounsString] as const;
|
||||
}, [currentPronouns]);
|
||||
const [newPronouns, setNewPronouns] = useState<PronounSet[]>(pronouns);
|
||||
const newPronounsString = useMemo(() => {
|
||||
const pronounsString = Array.isArray(newPronouns)
|
||||
? newPronouns.map((p) => `${p.language ? `${p.language}:` : ''}${p.summary}`).join(', ')
|
||||
: '';
|
||||
return pronounsString;
|
||||
}, [newPronouns]);
|
||||
|
||||
const [newDisplayName, setNewDisplayName] = useState(currentDisplayName);
|
||||
const [imageFile, setImageFile] = useState<File | undefined>();
|
||||
const [avatarMxc, setAvatarMxc] = useState(avatarMxcUrl);
|
||||
@@ -73,16 +93,20 @@ export function PerMessageProfileEditor({
|
||||
const [disableSetDisplayname, setDisableSetDisplayname] = useState(false);
|
||||
|
||||
const hasChanges = useMemo(
|
||||
() => newDisplayName !== (currentDisplayName ?? '') || !!imageFile,
|
||||
[newDisplayName, currentDisplayName, imageFile]
|
||||
() =>
|
||||
newDisplayName !== (currentDisplayName ?? '') ||
|
||||
newPronouns !== (currentPronouns ?? '') ||
|
||||
!!imageFile,
|
||||
[newDisplayName, currentDisplayName, newPronouns, currentPronouns, imageFile]
|
||||
);
|
||||
|
||||
// Reset handler for display name
|
||||
const handleReset = useCallback(() => {
|
||||
setNewDisplayName(currentDisplayName);
|
||||
setNewPronouns(currentPronouns);
|
||||
setChangingDisplayName(false);
|
||||
setDisableSetDisplayname(false);
|
||||
}, [currentDisplayName]);
|
||||
}, [currentDisplayName, currentPronouns]);
|
||||
|
||||
const handleSave = useCallback(() => {
|
||||
addOrUpdatePerMessageProfile(mx, {
|
||||
@@ -91,17 +115,23 @@ export function PerMessageProfileEditor({
|
||||
avatarUrl: avatarMxc,
|
||||
}).then(() => {
|
||||
setCurrentDisplayName(newDisplayName);
|
||||
setCurrentPronouns(newPronouns);
|
||||
});
|
||||
setChangingDisplayName(false);
|
||||
setDisableSetDisplayname(false);
|
||||
}, [mx, profileId, newDisplayName, avatarMxc]);
|
||||
}, [mx, profileId, newDisplayName, avatarMxc, newPronouns]);
|
||||
|
||||
const handleDelete = useCallback(() => {
|
||||
deletePerMessageProfile(mx, profileId).then(() => {
|
||||
setCurrentDisplayName('');
|
||||
setCurrentPronouns([]);
|
||||
if (onDelete) onDelete(profileId);
|
||||
});
|
||||
}, [mx, profileId]);
|
||||
}, [mx, profileId, onDelete]);
|
||||
|
||||
const handlePronounsChange = useCallback(() => {
|
||||
return setNewPronouns(parsePronounsStringToPronounsSetArray(newPronounsString));
|
||||
}, [newPronounsString]);
|
||||
|
||||
return (
|
||||
<Box
|
||||
@@ -150,7 +180,14 @@ export function PerMessageProfileEditor({
|
||||
alignItems="Center"
|
||||
justifyContent="Center"
|
||||
gap="100"
|
||||
style={{ minWidth: 80, maxWidth: 100, maxHeight: 100, flexShrink: 0, overflow: 'visible', marginTop: 20 }}
|
||||
style={{
|
||||
minWidth: 80,
|
||||
maxWidth: 100,
|
||||
maxHeight: 100,
|
||||
flexShrink: 0,
|
||||
overflow: 'visible',
|
||||
marginTop: 20,
|
||||
}}
|
||||
aria-label="Avatar and upload"
|
||||
>
|
||||
<Avatar
|
||||
@@ -261,6 +298,52 @@ export function PerMessageProfileEditor({
|
||||
)
|
||||
}
|
||||
/>
|
||||
<label
|
||||
htmlFor={`pronounsInput-${profileId}`}
|
||||
style={{ marginBottom: config.space.S200, alignSelf: 'flex-start' }}
|
||||
>
|
||||
<Text size="T300" style={{ marginTop: config.space.S100 }}>
|
||||
Pronouns:
|
||||
</Text>
|
||||
</label>
|
||||
<Input
|
||||
required
|
||||
name="pronounsInput"
|
||||
id={`pronounsInput-${profileId}`}
|
||||
value={newPronounsString}
|
||||
onChange={handlePronounsChange}
|
||||
variant="Secondary"
|
||||
radii="300"
|
||||
style={{
|
||||
flex: 1,
|
||||
minWidth: 0,
|
||||
width: '100%',
|
||||
maxWidth: 'clamp(200px, 60vw, 480px)',
|
||||
paddingRight: config.space.S200,
|
||||
fontSize: 16,
|
||||
height: 50,
|
||||
}}
|
||||
placeholder="Pronouns"
|
||||
readOnly={changingDisplayName || disableSetDisplayname}
|
||||
aria-label={`Pronouns for ${profileId}`}
|
||||
title={`Pronouns for ${profileId}`}
|
||||
after={
|
||||
hasChanges &&
|
||||
!changingDisplayName && (
|
||||
<IconButton
|
||||
type="reset"
|
||||
onClick={handleReset}
|
||||
size="300"
|
||||
radii="300"
|
||||
variant="Secondary"
|
||||
aria-label="Reset pronouns"
|
||||
title="Reset pronouns"
|
||||
>
|
||||
<Icon src={Icons.Cross} size="100" aria-label="Reset icon" />
|
||||
</IconButton>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</Box>
|
||||
{/* Rechte Spalte: Save Button */}
|
||||
<Box
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { useState, useEffect, ChangeEvent } from 'react';
|
||||
import { Input } from 'folds';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
import { parsePronounsInput } from '$utils/pronouns';
|
||||
|
||||
export type PronounSet = {
|
||||
summary: string;
|
||||
language?: string;
|
||||
grammatical_gender?: string;
|
||||
};
|
||||
import { parsePronounsInput, PronounSet } from '$utils/pronouns';
|
||||
|
||||
type PronounEditorProps = {
|
||||
title: string;
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { PronounSet } from '$utils/pronouns';
|
||||
import { MatrixClient } from 'matrix-js-sdk';
|
||||
|
||||
const ACCOUNT_DATA_PREFIX = 'fyi.cisnt.permessageprofile';
|
||||
|
||||
/**
|
||||
* a per message profile
|
||||
*/
|
||||
@@ -18,6 +21,7 @@ export type PerMessageProfile = {
|
||||
* the avatar url to use for messages using this profile.
|
||||
*/
|
||||
avatarUrl?: string;
|
||||
pronouns?: PronounSet[];
|
||||
};
|
||||
|
||||
type PerMessageProfileIndex = {
|
||||
@@ -48,36 +52,33 @@ export async function getPerMessageProfileById(
|
||||
mx: MatrixClient,
|
||||
id: string
|
||||
): Promise<PerMessageProfile | undefined> {
|
||||
const profile = await mx.getAccountData(`fyi.cisnt.permessageprofile.${id}` as any);
|
||||
const profile = await mx.getAccountData(`${ACCOUNT_DATA_PREFIX}.${id}` as any);
|
||||
return profile ? (profile.getContent() as unknown as PerMessageProfile) : undefined;
|
||||
}
|
||||
|
||||
export async function getAllPerMessageProfiles(mx: MatrixClient): Promise<PerMessageProfile[]> {
|
||||
const profileData = await mx.getAccountData('fyi.cisnt.permessageprofile.index' as any);
|
||||
const profileData = await mx.getAccountData(`${ACCOUNT_DATA_PREFIX}.index` as any);
|
||||
const profileIds = (profileData?.getContent() as PerMessageProfileIndex)?.profileIds || [];
|
||||
const profiles = await Promise.all(profileIds.map((id) => getPerMessageProfileById(mx, id)));
|
||||
return profiles.filter((profile): profile is PerMessageProfile => profile !== undefined);
|
||||
}
|
||||
|
||||
export function addOrUpdatePerMessageProfile(mx: MatrixClient, profile: PerMessageProfile) {
|
||||
const profileListIndex = mx.getAccountData('fyi.cisnt.permessageprofile.index' as any);
|
||||
const profileListIndex = mx.getAccountData(`${ACCOUNT_DATA_PREFIX}.index` as any);
|
||||
if (profileListIndex?.getContent()?.profileIds.includes(profile.id)) {
|
||||
// profile already exists, just update it
|
||||
return mx.setAccountData(`fyi.cisnt.permessageprofile.${profile.id}` as any, profile as any);
|
||||
return mx.setAccountData(`${ACCOUNT_DATA_PREFIX}.${profile.id}` as any, profile as any);
|
||||
}
|
||||
// profile doesn't exist, add it to the index and then add the profile data
|
||||
const newProfileIds = [...(profileListIndex?.getContent()?.profileIds || []), profile.id];
|
||||
return Promise.all([
|
||||
mx.setAccountData(
|
||||
'fyi.cisnt.permessageprofile.index' as any,
|
||||
{ profileIds: newProfileIds } as any
|
||||
),
|
||||
mx.setAccountData(`fyi.cisnt.permessageprofile.${profile.id}` as any, profile as any),
|
||||
mx.setAccountData(`${ACCOUNT_DATA_PREFIX}.index` as any, { profileIds: newProfileIds } as any),
|
||||
mx.setAccountData(`${ACCOUNT_DATA_PREFIX}.${profile.id}` as any, profile as any),
|
||||
]);
|
||||
}
|
||||
|
||||
export function deletePerMessageProfile(mx: MatrixClient, id: string) {
|
||||
return mx.setAccountData(`fyi.cisnt.permessageprofile.${id}` as any, {});
|
||||
return mx.setAccountData(`${ACCOUNT_DATA_PREFIX}.${id}` as any, {});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +91,7 @@ export async function getCurrentlyUsedPerMessageProfileForRoom(
|
||||
mx: MatrixClient,
|
||||
roomId: string
|
||||
): Promise<PerMessageProfile | undefined> {
|
||||
const accountData = mx.getAccountData(`fyi.cisnt.permessageprofile.roomassociation` as any);
|
||||
const accountData = mx.getAccountData(`${ACCOUNT_DATA_PREFIX}.roomassociation` as any);
|
||||
const content = accountData?.getContent()?.associations as
|
||||
| PerMessageProfileRoomAssociation[]
|
||||
| undefined;
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
export type PronounSet = {
|
||||
summary: string;
|
||||
language?: string;
|
||||
grammatical_gender?: string;
|
||||
};
|
||||
|
||||
// helper function to convert a comma-separated pronouns string into an array of objects with summary and optional language
|
||||
export function parsePronounsInput(pronouns: string): { summary: string; language?: string }[] {
|
||||
if (!pronouns || typeof pronouns !== 'string') return [];
|
||||
@@ -25,6 +31,10 @@ export function parsePronounsInput(pronouns: string): { summary: string; languag
|
||||
});
|
||||
}
|
||||
|
||||
export function parsePronounsStringToPronounsSetArray(pronouns: string): PronounSet[] {
|
||||
return parsePronounsInput(pronouns) as unknown as PronounSet[];
|
||||
}
|
||||
|
||||
// helper function to filter a list of pronouns based on the user's language settings
|
||||
export function filterPronounsByLanguage(
|
||||
pronouns: { summary: string; language?: string }[],
|
||||
|
||||
Reference in new issue
Block a user