diff --git a/.changeset/add_option_to_filter_user_pronouns_based_on_the_pronouns_language.md b/.changeset/add_option_to_filter_user_pronouns_based_on_the_pronouns_language.md new file mode 100644 index 00000000..390e3a43 --- /dev/null +++ b/.changeset/add_option_to_filter_user_pronouns_based_on_the_pronouns_language.md @@ -0,0 +1,5 @@ +--- +sable: minor +--- + +Add option to filter user pronouns based on the pronouns language diff --git a/src/app/components/user-profile/UserRoomProfile.tsx b/src/app/components/user-profile/UserRoomProfile.tsx index 68812698..0031943d 100644 --- a/src/app/components/user-profile/UserRoomProfile.tsx +++ b/src/app/components/user-profile/UserRoomProfile.tsx @@ -31,6 +31,8 @@ import { } from '$plugins/react-custom-html-parser'; import { useSpoilerClickHandler } from '$hooks/useSpoilerClickHandler'; import { RenderBody } from '$components/message'; +import { getSettings } from '$state/settings'; +import { filterPronounsByLanguage } from '$utils/pronouns'; import { CreatorChip } from './CreatorChip'; import { UserInviteAlert, UserBanAlert, UserModeration, UserKickAlert } from './UserModeration'; import { PowerChip } from './PowerChip'; @@ -74,7 +76,16 @@ function UserExtendedSection({ return String(val); }; - const pronouns = profile.pronouns?.map((p: any) => clamp(p.summary, 16)).join(', '); + const languageFilterEnabled = getSettings().filterPronounsBasedOnLanguage ?? false; + const languagesToFilterFor = getSettings().filterPronounsLanguages ?? ['en']; + + const pronouns = filterPronounsByLanguage( + profile.pronouns!, + languageFilterEnabled, + languagesToFilterFor + ) + .map((p) => p.summary) + .join(', '); const localTime = useMemo(() => { if (!profile.timezone) return null; diff --git a/src/app/features/room/message/Message.tsx b/src/app/features/room/message/Message.tsx index 65a88f9f..b9f81d54 100644 --- a/src/app/features/room/message/Message.tsx +++ b/src/app/features/room/message/Message.tsx @@ -50,7 +50,7 @@ import { } from '$components/message'; import { canEditEvent, getMemberAvatarMxc } from '$utils/room'; import { mxcUrlToHttp } from '$utils/matrix'; -import { MessageLayout, MessageSpacing, settingsAtom } from '$state/settings'; +import { getSettings, MessageLayout, MessageSpacing, settingsAtom } from '$state/settings'; import { nicknamesAtom, setNicknameAtom } from '$state/nicknames'; import { useMatrixClient } from '$hooks/useMatrixClient'; import { useRecentEmoji } from '$hooks/useRecentEmoji'; @@ -76,6 +76,7 @@ import { MessageReadReceiptItem } from '$components/message/modals/MessageReadRe import { MessageSourceCodeItem } from '$components/message/modals/MessageSource'; import { MessageDeleteItem } from '$components/message/modals/MessageDelete'; import { MessageReportItem } from '$components/message/modals/MessageReport'; +import { filterPronounsByLanguage } from '$utils/pronouns'; import { MessageEditor } from './MessageEditor'; import * as css from './styles.css'; @@ -261,17 +262,34 @@ const Pronouns = as< >(({ as: AsPronouns = 'span', pronouns, tagColor, ...props }, ref) => { if (!pronouns || pronouns.length === 0) return null; + const languageFilterEnabled = Boolean(getSettings().filterPronounsBasedOnLanguage ?? false); + // if no language is given use english + const selectedLanguages = (getSettings().filterPronounsLanguages ?? ['en']) + .map((lang) => lang.trim().toLowerCase()) + .filter(Boolean); + + const visiblePronouns = filterPronounsByLanguage( + pronouns, + languageFilterEnabled, + selectedLanguages + ); + const clamp = (str: string, len: number) => (str.length > len ? `${str.slice(0, len)}...` : str); const limit = mobileOrTablet() ? 1 : 3; + // if language specific pronouns can't be found matching the filter return unfiltered + if (visiblePronouns.length === 0) { + visiblePronouns.push(...pronouns); + } + return ( - {pronouns.slice(0, limit).map((p) => ( + {visiblePronouns.slice(0, limit).map((p) => ( {clamp(p.summary, 16)} ))} - {pronouns.length > limit && ...} + {visiblePronouns.length > limit && ...} ); }); diff --git a/src/app/features/settings/account/PronounEditor.tsx b/src/app/features/settings/account/PronounEditor.tsx index 9edfd1c9..29ee9467 100644 --- a/src/app/features/settings/account/PronounEditor.tsx +++ b/src/app/features/settings/account/PronounEditor.tsx @@ -1,6 +1,7 @@ import { useState, useEffect, ChangeEvent } from 'react'; import { Input } from 'folds'; import { SettingTile } from '$components/setting-tile'; +import { parsePronounsInput } from '$utils/pronouns'; type PronounSet = { summary: string; @@ -14,7 +15,9 @@ type PronounEditorProps = { }; export function PronounEditor({ current, onSave }: PronounEditorProps) { - const initialString = current.map((p) => p.summary).join(', '); + const initialString = current + .map((p) => `${p.language ? `${p.language}:` : ''}${p.summary}`) + .join(', '); const [val, setVal] = useState(initialString); useEffect(() => setVal(initialString), [initialString]); @@ -22,11 +25,7 @@ export function PronounEditor({ current, onSave }: PronounEditorProps) { 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), language: 'en' })); + const next = parsePronounsInput(safeVal); onSave(next); }; @@ -37,7 +36,9 @@ export function PronounEditor({ current, onSave }: PronounEditorProps) { return ( { + if (enabled === undefined) return false; + if (typeof enabled === 'boolean') return enabled; + const normalized = String(enabled).trim().toLowerCase(); + if (normalized === 'false' || normalized === '0' || normalized === 'off' || normalized === 'no') + return false; + if (normalized === 'true' || normalized === '1' || normalized === 'on' || normalized === 'yes') + return true; + return false; +}; + +// utility function to split a comma separated list of languages and trim whitespace +function splitAndTrimLanguages(languages: string): string[] { + return languages + .split(',') + .map((lang) => lang.trim()) + .filter((lang) => lang.length > 0); +} + +// MSC4247 allows users to set pronouns in different languages +export function LanguageSpecificPronouns() { + const [useLanguageSpecificPronouns, setEnabled] = useState(false); + const [languageList, setLanguageList] = useState(''); + + // common handler for saving changes to the language specific pronouns settings + const handleSave = (enabled: boolean, languages: string) => { + const currentSettings = getSettings(); + setSettings({ + ...currentSettings, + filterPronounsBasedOnLanguage: enabled, + filterPronounsLanguages: splitAndTrimLanguages(languages), + }); + }; + + // handler for when the language list input changes + const handleLanguageListChange = (e: React.ChangeEvent) => { + const val = e.target.value.trim(); + setLanguageList(val); + // save the new language list to the client config, keeping the enabled state unchanged + handleSave(getSettings().filterPronounsBasedOnLanguage ?? false, val); + }; + + useEffect(() => { + setEnabled( + resolveLanguageSpecificPronounsEnabled(getSettings().filterPronounsBasedOnLanguage ?? false) + ); + setLanguageList(getSettings().filterPronounsLanguages?.join(',') || ''); + }, []); + + // handler for toggling the enabled state of language specific pronouns + const handleSetEnabled = (enabled: boolean) => { + handleSave(enabled, getSettings().filterPronounsLanguages?.join(',') || ''); + setEnabled(enabled); + }; + + return ( + + Language Specific Pronouns + + + } + /> + {useLanguageSpecificPronouns && ( + + } + /> + )} + + + ); +} diff --git a/src/app/features/settings/experimental/Experimental.tsx b/src/app/features/settings/experimental/Experimental.tsx index 8148c92e..b9719357 100644 --- a/src/app/features/settings/experimental/Experimental.tsx +++ b/src/app/features/settings/experimental/Experimental.tsx @@ -1,6 +1,7 @@ import { Box, Text, IconButton, Icon, Icons, Scroll } from 'folds'; import { Page, PageContent, PageHeader } from '$components/page'; import { InfoCard } from '$components/info-card'; +import { LanguageSpecificPronouns } from '../cosmetics/LanguageSpecificPronouns'; import { Sync } from '../general'; type ExperimentalProps = { @@ -42,6 +43,9 @@ export function Experimental({ requestClose }: ExperimentalProps) { + + + diff --git a/src/app/hooks/useCommands.ts b/src/app/hooks/useCommands.ts index 4322c62b..ed2ad8cf 100644 --- a/src/app/hooks/useCommands.ts +++ b/src/app/hooks/useCommands.ts @@ -30,6 +30,7 @@ import { splitWithSpace } from '$utils/common'; import { useSetting } from '$state/hooks/settings'; import { settingsAtom } from '$state/settings'; import { createRoomEncryptionState } from '$components/create-room'; +import { parsePronounsInput } from '$utils/pronouns'; import { useRoomNavigate } from './useRoomNavigate'; import { enrichWidgetUrl } from './useRoomWidgets'; @@ -888,7 +889,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { [Command.Pronoun]: { name: Command.Pronoun, description: - 'Set your pronouns for this room. Example: /pronoun "they/them, it/its" | /pronoun reset', + 'Set your pronouns for this room. Example: /pronoun "en:they/them, de:sie/ihr" | /pronoun reset', exe: async (payload) => { const match = payload.trim().match(/^"(.*)"$/); const rawInput = match ? match[1].trim() : payload.trim(); @@ -917,11 +918,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { return; } - const pronounsArray = rawInput - .split(',') - .map((p) => p.trim()) - .filter((p) => p.length > 0) - .map((p) => ({ summary: p })); + const pronounsArray = parsePronounsInput(rawInput); await mx.sendStateEvent( room.roomId, @@ -929,7 +926,12 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { { pronouns: pronounsArray }, userId ); - sendFeedback(`Room pronouns set: ${rawInput}`); + + const feedbackString = pronounsArray + .map((p) => (p.language ? `for ${p.language} "${p.summary}" was set` : p.summary)) + .join(', '); + + sendFeedback(`Room pronouns set: ${feedbackString}`); } catch (e: any) { if (e.errcode === 'M_FORBIDDEN') { sendFeedback('Permission Denied. Could not update room pronouns.'); @@ -940,7 +942,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { [Command.GPronoun]: { name: Command.GPronoun, description: - 'Set your global pronouns for this space. Example: /gpronoun "they/them, it/its" | /gpronoun reset', + 'Set your global pronouns for this space. Example: /gpronoun "en:they/them, de:sie/ihr" | /gpronoun reset', exe: async (payload) => { const match = payload.trim().match(/^"(.*)"$/); const rawInput = match ? match[1].trim() : payload.trim(); @@ -977,11 +979,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { return; } - const pronounsArray = rawInput - .split(',') - .map((p) => p.trim()) - .filter((p) => p.length > 0) - .map((p) => ({ summary: p })); + const pronounsArray = parsePronounsInput(rawInput); await mx.sendStateEvent( targetSpaceId as any, @@ -989,7 +987,12 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { { pronouns: pronounsArray }, userId ); - sendFeedback(`Global space pronouns set: ${rawInput}`); + + const feedbackString = pronounsArray + .map((p) => (p.language ? `for ${p.language} "${p.summary}" was set` : p.summary)) + .join(', '); + + sendFeedback(`Global space pronouns set: ${feedbackString}`); } catch (e: any) { if (e.errcode === 'M_FORBIDDEN') { sendFeedback('Permission Denied. Could not update space pronouns.'); diff --git a/src/app/state/settings.ts b/src/app/state/settings.ts index 68e3eca8..835984b3 100644 --- a/src/app/state/settings.ts +++ b/src/app/state/settings.ts @@ -61,6 +61,8 @@ export interface Settings { privacyBlurEmotes: boolean; showPronouns: boolean; renderGlobalNameColors: boolean; + filterPronounsBasedOnLanguage?: boolean; + filterPronounsLanguages?: string[]; renderRoomColors: boolean; renderRoomFonts: boolean; diff --git a/src/app/utils/pronouns.ts b/src/app/utils/pronouns.ts new file mode 100644 index 00000000..a306543c --- /dev/null +++ b/src/app/utils/pronouns.ts @@ -0,0 +1,41 @@ +// 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 }[] { + return pronouns + .split(',') + .map((s) => s.trim()) + .filter(Boolean) + .map((s) => { + const parts = s.split(':'); + + if (parts.length === 1) { + return { + summary: parts[0].slice(0, 16), + language: 'en', + }; + } + + const [language, summary] = parts; + + return { + language: language || 'en', + summary: summary.slice(0, 16), + }; + }); +} + +// helper function to filter a list of pronouns based on the user's language settings +export function filterPronounsByLanguage( + pronouns: { summary: string; language?: string }[], + enabled: boolean, + languages: string[] +): { summary: string; language?: string }[] { + if (!enabled) return pronouns; + const normalizedLanguages = languages.map((lang) => lang.trim().toLowerCase()); + const filteredPronouns = pronouns.filter((p) => + normalizedLanguages.includes((p.language || 'en').trim().toLowerCase()) + ); + if (filteredPronouns.length === 0) { + return pronouns; + } + return filteredPronouns; +}