diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 39cbe6d6..5d4d11f4 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -659,6 +659,7 @@ export const RoomInput = forwardRef( id: perMessageProfile.id, displayname: perMessageProfile.name, avatar_url: perMessageProfile.avatarUrl, + 'io.fsky.nyx.pronouns': perMessageProfile.pronouns, }; } diff --git a/src/app/features/settings/Profiles/PerMessageProfileEditor.tsx b/src/app/features/settings/Profiles/PerMessageProfileEditor.tsx index 08999785..61682065 100644 --- a/src/app/features/settings/Profiles/PerMessageProfileEditor.tsx +++ b/src/app/features/settings/Profiles/PerMessageProfileEditor.tsx @@ -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(), onChange, onDelete, }: Readonly) { const useAuthentication = useMediaAuthentication(); const [currentDisplayName, setCurrentDisplayName] = useState(displayName ?? ''); + + // Pronouns + const [currentPronouns, setCurrentPronouns] = useState(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(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(); 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 ( + + + + + ) + } + /> {/* Rechte Spalte: Save Button */} { - 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 { - 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 { - 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; diff --git a/src/app/utils/pronouns.ts b/src/app/utils/pronouns.ts index c653d9fd..4af0d386 100644 --- a/src/app/utils/pronouns.ts +++ b/src/app/utils/pronouns.ts @@ -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 }[],