diff --git a/src/app/features/settings/PerMessageProfiles/PerMessageProfileEditor.tsx b/src/app/features/settings/PerMessageProfiles/PerMessageProfileEditor.tsx new file mode 100644 index 000000000..2672f5276 --- /dev/null +++ b/src/app/features/settings/PerMessageProfiles/PerMessageProfileEditor.tsx @@ -0,0 +1,190 @@ +import { SequenceCard } from '$components/sequence-card'; +import { Box, Button, Text, Avatar, config, Icon, IconButton, Icons, Input } from 'folds'; +// Try relative import for CompactUploadCardRenderer +import { MatrixClient } from 'matrix-js-sdk'; +import { useCallback, useMemo, useState } from 'react'; +import { mxcUrlToHttp } from '$utils/matrix'; +import { useFilePicker } from '$hooks/useFilePicker'; +import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; +import { useObjectURL } from '$hooks/useObjectURL'; +import { createUploadAtom } from '$state/upload'; +import { UserAvatar } from '$components/user-avatar'; +import { CompactUploadCardRenderer } from '$components/upload-card'; +import { SequenceCardStyle } from '../styles.css'; + +/** + * 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. + */ +type PerMessageProfileEditorProps = { + mx: MatrixClient; + profileId: string; + avatarMxcUrl?: string; + displayName?: string; + onChange?: (profile: { id: string; name: string; avatarUrl?: string }) => void; +}; + +export function PerMessageProfileEditor({ + mx, + profileId, + avatarMxcUrl, + displayName, + onChange, +}: Readonly) { + const useAuthentication = useMediaAuthentication(); + const [newDisplayName, setNewDisplayName] = useState(displayName ?? ''); + const [imageFile, setImageFile] = useState(); + const imageFileURL = useObjectURL(imageFile); + const avatarUrl = useMemo(() => { + if (imageFileURL) return imageFileURL; + if (avatarMxcUrl) { + return mxcUrlToHttp(mx, avatarMxcUrl, useAuthentication, 96, 96, 'crop') ?? undefined; + } + return undefined; + }, [imageFileURL, avatarMxcUrl, mx, useAuthentication]); + const uploadAtom = useMemo(() => { + if (imageFile) return createUploadAtom(imageFile); + return undefined; + }, [imageFile]); + const pickFile = useFilePicker(setImageFile, false); + const handleRemoveUpload = useCallback(() => { + setImageFile(undefined); + }, []); + const handleUploaded = useCallback( + (upload: { status: string; mxc: string }) => { + if (upload && upload.status === 'success' && onChange) { + onChange({ id: profileId, name: newDisplayName, avatarUrl: upload.mxc }); + } + setImageFile(undefined); + }, + [onChange, profileId, newDisplayName] + ); + const handleNameChange = useCallback((e: React.ChangeEvent) => { + setNewDisplayName(e.target.value); + }, []); + + // Added missing state and logic for display name editing + const [changingDisplayName, setChangingDisplayName] = useState(false); + const [disableSetDisplayname, setDisableSetDisplayname] = useState(false); + + // Determine if there are changes to the display name or avatar + const hasChanges = useMemo( + () => newDisplayName !== (displayName ?? '') || !!imageFile, + [newDisplayName, displayName, imageFile] + ); + + // Reset handler for display name + const handleReset = useCallback(() => { + setNewDisplayName(displayName ?? ''); + setChangingDisplayName(false); + setDisableSetDisplayname(false); + }, [displayName]); + + return ( + + + + + + + p} + /> + + + + + + {uploadAtom ? ( + + + + ) : ( + + + + + ) + } + /> + + )} + + + + + ); +} diff --git a/src/app/features/settings/PerMessageProfiles/PerMessageProfileOverview.tsx b/src/app/features/settings/PerMessageProfiles/PerMessageProfileOverview.tsx new file mode 100644 index 000000000..d90e20734 --- /dev/null +++ b/src/app/features/settings/PerMessageProfiles/PerMessageProfileOverview.tsx @@ -0,0 +1,35 @@ +import { useMatrixClient } from '$hooks/useMatrixClient'; +import { getAllPerMessageProfiles, PerMessageProfile } from '$hooks/usePerMessageProfile'; +import { useEffect, useState } from 'react'; +import { Box } from 'folds'; +import { PerMessageProfileEditor } from './PerMessageProfileEditor'; + +/** + * Renders a list of per-message profiles along with an editor. + * @returns rendering of per message profile list including editor + */ +export function PerMessageProfileOverview() { + const mx = useMatrixClient(); + const [profiles, setProfiles] = useState([]); + + useEffect(() => { + const fetchProfiles = async () => { + const fetchedProfiles = await getAllPerMessageProfiles(mx); + setProfiles(fetchedProfiles); + }; + fetchProfiles(); + }, [mx]); + + return ( + + {profiles.map((profile) => ( + + ))} + + ); +} diff --git a/src/app/features/settings/PerMessageProfiles/PerMessageProfilesPage.tsx b/src/app/features/settings/PerMessageProfiles/PerMessageProfilesPage.tsx new file mode 100644 index 000000000..55ff3c28c --- /dev/null +++ b/src/app/features/settings/PerMessageProfiles/PerMessageProfilesPage.tsx @@ -0,0 +1,31 @@ +import { Page, PageHeader } from '$components/page'; +import { Box, IconButton, Icon, Icons, Text } from 'folds'; +import { PerMessageProfileOverview } from './PerMessageProfileOverview'; + +type PerMessageProfilePageProps = { + requestClose: () => void; +}; + +export function PerMessageProfilePage({ requestClose }: PerMessageProfilePageProps) { + return ( + + + + + + Per Message Profiles + + + + + + + + + + + + + + ); +} diff --git a/src/app/features/settings/Settings.tsx b/src/app/features/settings/Settings.tsx index 34f350b3d..139af2b95 100644 --- a/src/app/features/settings/Settings.tsx +++ b/src/app/features/settings/Settings.tsx @@ -36,10 +36,12 @@ import { General } from './general'; import { Cosmetics } from './cosmetics/Cosmetics'; import { Experimental } from './experimental/Experimental'; import { KeyboardShortcuts } from './keyboard-shortcuts'; +import { PerMessageProfilePage } from './PerMessageProfiles/PerMessageProfilesPage'; export enum SettingsPages { GeneralPage, AccountPage, + PerMessageProfilesPage, // Renamed to avoid conflict NotificationPage, DevicesPage, EmojisStickersPage, @@ -70,6 +72,11 @@ const useSettingsMenuItems = (): SettingsMenuItem[] => name: 'Account', icon: Icons.User, }, + { + page: SettingsPages.PerMessageProfilesPage, + name: 'Per-Message Profiles', + icon: Icons.User, + }, { page: SettingsPages.CosmeticsPage, name: 'Appearance', @@ -247,6 +254,9 @@ export function Settings({ initialPage, requestClose }: SettingsProps) { {activePage === SettingsPages.AccountPage && ( )} + {activePage === SettingsPages.PerMessageProfilesPage && ( + + )} {activePage === SettingsPages.CosmeticsPage && ( )}