Add a developer tools view for public profile fields.
Trigger cinny-mobile / dispatch (push) Successful in 1s
Trigger cinny-mobile / dispatch (push) Successful in 1s
This commit is contained in:
@@ -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<string | null>();
|
||||
const [profileExpand, setProfileExpand] = useState(false);
|
||||
const [profileUserId, setProfileUserId] = useState(mx.getUserId() ?? '');
|
||||
const [profileFields, setProfileFields] = useState<Record<string, unknown>>({});
|
||||
const [profileSupported, setProfileSupported] = useState(false);
|
||||
const [profileFieldKey, setProfileFieldKey] = useState<string | null>();
|
||||
|
||||
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<string, unknown>),
|
||||
);
|
||||
} 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 (
|
||||
<Page>
|
||||
<PageHeader outlined={false}>
|
||||
@@ -109,6 +177,18 @@ export function DeveloperTools({ requestClose }: DeveloperToolsProps) {
|
||||
</SequenceCard>
|
||||
)}
|
||||
</Box>
|
||||
{developerTools && (
|
||||
<ProfileFields
|
||||
userId={profileUserId}
|
||||
fields={profileFields}
|
||||
editable={profileEditable}
|
||||
expand={profileExpand}
|
||||
onUserIdChange={setProfileUserId}
|
||||
onExpandToggle={handleProfileExpand}
|
||||
onSelect={setProfileFieldKey}
|
||||
onDelete={deleteProfileField}
|
||||
/>
|
||||
)}
|
||||
{developerTools && (
|
||||
<AccountData
|
||||
expand={expand}
|
||||
@@ -120,6 +200,38 @@ export function DeveloperTools({ requestClose }: DeveloperToolsProps) {
|
||||
</PageContent>
|
||||
</Scroll>
|
||||
</Box>
|
||||
{profileFieldKey !== undefined && (
|
||||
<Overlay open backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
clickOutsideDeactivates: true,
|
||||
onDeactivate: closeProfileField,
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Modal size="500" variant="Surface">
|
||||
{profileEditable ? (
|
||||
<AccountDataEditor
|
||||
type={profileFieldKey}
|
||||
content={profileFields[profileFieldKey] as object}
|
||||
submitChange={submitProfileField}
|
||||
requestClose={closeProfileField}
|
||||
/>
|
||||
) : (
|
||||
<TextViewer
|
||||
name={profileFieldKey}
|
||||
text={JSON.stringify(profileFields[profileFieldKey], null, 2)}
|
||||
langName="json"
|
||||
requestClose={closeProfileField}
|
||||
/>
|
||||
)}
|
||||
</Modal>
|
||||
</FocusTrap>
|
||||
</OverlayCenter>
|
||||
</Overlay>
|
||||
)}
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user