From bb27305b7013db7fdf7a1fbdb6a7dff708a46be5 Mon Sep 17 00:00:00 2001 From: litruv Date: Sun, 1 Nov 2026 02:03:29 +1100 Subject: [PATCH] Add a developer tools view for public profile fields. --- .../settings/developer-tools/DevelopTools.tsx | 114 +++++++++++++++++- .../developer-tools/ProfileFields.tsx | 109 +++++++++++++++++ .../settings/settingsSearchCatalog.ts | 5 + 3 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 src/app/features/settings/developer-tools/ProfileFields.tsx diff --git a/src/app/features/settings/developer-tools/DevelopTools.tsx b/src/app/features/settings/developer-tools/DevelopTools.tsx index 4b7fcd8e..e4440dcb 100644 --- a/src/app/features/settings/developer-tools/DevelopTools.tsx +++ b/src/app/features/settings/developer-tools/DevelopTools.tsx @@ -1,5 +1,6 @@ import React, { useCallback, useState } from 'react'; -import { Box, Text, IconButton, Scroll, Switch, Button } from 'folds'; +import { Box, Text, IconButton, Scroll, Switch, Button, Modal, Overlay, OverlayBackdrop, OverlayCenter } from 'folds'; +import FocusTrap from 'focus-trap-react'; import { Icon, Icons } from '../../../components/icons'; import { Page, PageContent, PageHeader } from '../../../components/page'; import { SequenceCard } from '../../../components/sequence-card'; @@ -12,8 +13,12 @@ import { AccountDataEditor, AccountDataSubmitCallback, } from '../../../components/AccountDataEditor'; +import { TextViewer } from '../../../components/text-viewer'; import { copyToClipboard } from '../../../utils/dom'; +import { stopPropagation } from '../../../utils/keyboard'; +import { clearProfileCache } from '../../../utils/profileCache'; import { AccountData } from './AccountData'; +import { ProfileFields } from './ProfileFields'; type DeveloperToolsProps = { requestClose: () => void; @@ -23,6 +28,67 @@ export function DeveloperTools({ requestClose }: DeveloperToolsProps) { const [developerTools, setDeveloperTools] = useSetting(settingsAtom, 'developerTools'); const [expand, setExpend] = useState(false); const [accountDataType, setAccountDataType] = useState(); + const [profileExpand, setProfileExpand] = useState(false); + const [profileUserId, setProfileUserId] = useState(mx.getUserId() ?? ''); + const [profileFields, setProfileFields] = useState>({}); + const [profileSupported, setProfileSupported] = useState(false); + const [profileFieldKey, setProfileFieldKey] = useState(); + + const profileEditable = profileSupported && profileUserId.trim() === mx.getUserId(); + + const fetchProfileFields = useCallback(async (userId: string) => { + if (!userId) return; + + try { + const supported = await mx.doesServerSupportExtendedProfiles(); + setProfileSupported(supported); + setProfileFields( + supported + ? await mx.getExtendedProfile(userId) + : ((await mx.getProfileInfo(userId)) as Record), + ); + } catch (err) { + console.error('Failed to fetch profile fields:', err); + setProfileSupported(false); + setProfileFields({}); + } + }, [mx]); + + const submitProfileField: AccountDataSubmitCallback = useCallback( + async (key, content) => { + await mx.setExtendedProfileProperty(key, content); + setProfileFields((prev) => ({ ...prev, [key]: content })); + // The rest of the client reads through the shared profile cache + clearProfileCache(profileUserId.trim()); + }, + [mx, profileUserId] + ); + + const deleteProfileField = useCallback( + async (key: string) => { + try { + await mx.deleteExtendedProfileProperty(key); + setProfileFields((prev) => { + const next = { ...prev }; + delete next[key]; + return next; + }); + // The rest of the client reads through the shared profile cache + clearProfileCache(profileUserId.trim()); + } catch (err) { + console.error('Failed to delete profile field:', err); + } + }, + [mx, profileUserId] + ); + + const handleProfileExpand = useCallback( + (expand: boolean) => { + setProfileExpand(expand); + if (expand) fetchProfileFields(profileUserId.trim()); + }, + [fetchProfileFields, profileUserId] + ); const submitAccountData: AccountDataSubmitCallback = useCallback( async (type, content) => { @@ -42,6 +108,8 @@ export function DeveloperTools({ requestClose }: DeveloperToolsProps) { ); } + const closeProfileField = useCallback(() => setProfileFieldKey(undefined), []); + return ( @@ -109,6 +177,18 @@ export function DeveloperTools({ requestClose }: DeveloperToolsProps) { )} + {developerTools && ( + + )} {developerTools && ( + {profileFieldKey !== undefined && ( + }> + + + + {profileEditable ? ( + + ) : ( + + )} + + + + + )} ); } diff --git a/src/app/features/settings/developer-tools/ProfileFields.tsx b/src/app/features/settings/developer-tools/ProfileFields.tsx new file mode 100644 index 00000000..22ebb826 --- /dev/null +++ b/src/app/features/settings/developer-tools/ProfileFields.tsx @@ -0,0 +1,109 @@ +import { Box, Text, Button, Input, MenuItem, IconButton } from 'folds'; +import { Icon, Icons } from '../../../components/icons'; +import { SequenceCard } from '../../../components/sequence-card'; +import { SequenceCardStyle } from '../styles.css'; +import { SettingTile } from '../../../components/setting-tile'; +import { CutoutCard } from '../../../components/cutout-card'; + +type ProfileFieldsProps = { + userId: string; + fields: Record; + editable: boolean; + expand: boolean; + onUserIdChange: (userId: string) => void; + onExpandToggle: (expand: boolean) => void; + onSelect: (key: string) => void; + onDelete: (key: string) => void; +}; + +export function ProfileFields({ + userId, + fields, + editable, + expand, + onUserIdChange, + onExpandToggle, + onSelect, + onDelete, +}: ProfileFieldsProps) { + return ( + + Public Profile Fields + + onExpandToggle(!expand)} + variant="Secondary" + fill="Soft" + size="300" + radii="300" + outlined + before={ + + } + > + {expand ? 'Collapse' : 'Expand'} + + } + /> + onUserIdChange(e.target.value)} + placeholder="@user:server" + /> + {expand && ( + + + Fields + Total: {Object.keys(fields).length} + + + {Object.keys(fields).sort().map((key) => ( + + {editable && ( + onDelete(key)} + > + + + )} + + + } + onClick={() => onSelect(key)} + > + + + {key} + + + + ))} + + + )} + + + ); +} diff --git a/src/app/features/settings/settingsSearchCatalog.ts b/src/app/features/settings/settingsSearchCatalog.ts index dc519818..b168ea8f 100644 --- a/src/app/features/settings/settingsSearchCatalog.ts +++ b/src/app/features/settings/settingsSearchCatalog.ts @@ -126,6 +126,11 @@ export const SETTINGS_SEARCH_CATALOG: SettingsSearchEntry[] = [ }, { page: SettingsPages.DeveloperToolsPage, title: 'Access Token', keywords: ['auth', 'secret'] }, { page: SettingsPages.DeveloperToolsPage, title: 'Account Data', keywords: ['global', 'events'] }, + { + page: SettingsPages.DeveloperToolsPage, + title: 'Public Profile Fields', + keywords: ['profile', 'msc4133', 'json'], + }, // About { page: SettingsPages.AboutPage, title: 'Clear Cache & Reload', keywords: ['reset', 'storage'] },