From 62fe517b1d8d7db061fa22d4e4efab50a40f329e Mon Sep 17 00:00:00 2001 From: Delta Date: Sun, 9 Aug 2026 11:21:17 +0100 Subject: [PATCH] Fix persona picker displaying wrong in change persona menu & refactor --- src/app/components/message/modals/Options.tsx | 1 - src/app/features/room/RoomInput.tsx | 6 +- .../persona-picker/PersonaPicker.test.tsx | 10 +- .../room/persona-picker/PersonaPicker.tsx | 598 ++++++++++++------ 4 files changed, 395 insertions(+), 220 deletions(-) diff --git a/src/app/components/message/modals/Options.tsx b/src/app/components/message/modals/Options.tsx index 79e47e4cb..bf018c863 100644 --- a/src/app/components/message/modals/Options.tsx +++ b/src/app/components/message/modals/Options.tsx @@ -430,7 +430,6 @@ function OptionsReproxyPersonaPicker({ <> ( )} - {pmpPickerEnable && !editingEvent && ( - ({ getAll: vi.fn<(mx: MatrixClient) => Promise>(), @@ -128,7 +128,7 @@ const profiles: PerMessageProfileMsc4461[] = [ function renderPicker(mx = {} as MatrixClient, tab = PersonaPickerTab.Global) { return render( - { const view = renderPicker(firstClient); view.rerender( - void>()} @@ -230,7 +230,7 @@ describe('PersonaPicker async flows', () => { fireEvent.click(grabPersonaButton('First')); view.rerender( - { }); view.rerender( - void; + onPersonaSelect: (persona: PerMessageProfileMsc4461 | undefined) => void | Promise; + requestClose: () => void; + anchor?: RectCords; +}; -export function TemporaryPersonaPicker(props: PersonaPickerProps) { - return ( - - ); -} - -export function PersistentPersonaPicker(props: PersonaPickerProps) { - return ; -} - -function PersonaPickerMenu({ - tab: tabProp = PersonaPickerTab.Global, - mx, - roomId, - suppressEditorRefocus, - onTabChange, - latchedPersona, - onPersonaSelect, - anchor, - requestClose, -}: PersonaPickerProps & { presentation: PersonaPickerPresentation }) { +function useProfileCosmetics(mx: MatrixClient) { const useAuthentication = useMediaAuthentication(); - const [tab, setTab] = useState(tabProp); const activeTheme = useActiveTheme(); - const [AddPersonaMenuAnchor, setAddPersonaMenuAnchor] = useState(anchor); - const [profiles, setProfiles] = useState(undefined); - const [selectedGlobalPersona, setSelectedGlobalPersona] = - useState(null); - const [selectedRoomPersona, setSelectedRoomPersona] = useState( - latchedPersona ?? null - ); - const mountedRef = useRef(false); - const profileFetchGenerationRef = useRef(0); - // Bumped on each click so an in-flight sync cannot undo a fresher choice. Global and - // per-room selections are independent, so one must not invalidate the other's rollback. - const globalSelectionRef = useRef(0); - const roomSelectionRef = useRef(0); - const isPickerMenuItemSelected = (persona: PerMessageProfileMsc4461) => { - const selectedPersona = - tab === PersonaPickerTab.Global ? selectedGlobalPersona : selectedRoomPersona; - return persona.id === selectedPersona?.id ? true : undefined; - }; const nameColor = useCallback( (persona: PerMessageProfileMsc4461) => @@ -121,30 +90,143 @@ function PersonaPickerMenu({ [activeTheme] ); - const defactoPersona = () => selectedRoomPersona ?? selectedGlobalPersona; - - const searchInputRef = useRef(null); - - const scrollRef = useRef(null); - const [filteredProfiles, setFilteredProfiles] = useState( - undefined + const avatarUrl = useCallback( + (profile: PerMessageProfileMsc4461) => { + if (profile.avatar_url !== undefined) { + return mxcUrlToHttp(mx, profile.avatar_url, useAuthentication, 96, 96, 'crop') ?? undefined; + } else { + return undefined; + } + }, + [mx, useAuthentication] ); + return { nameColor, avatarUrl }; +} +function useProfiles( + mx: MatrixClient, + mountedRef: MutableRefObject, + profileFetchGenerationRef: MutableRefObject +) { + const [profiles, setProfiles] = useState(undefined); + + const fetchProfiles = useCallback(async () => { + const fetchGeneration = ++profileFetchGenerationRef.current; + try { + const fetchedProfiles = await new ProfileCatalog(mx).list(); + if (!mountedRef.current || fetchGeneration !== profileFetchGenerationRef.current) { + return; + } + setProfiles(fetchedProfiles); + } catch { + // Profile loading is best effort; keep the existing list when it fails. + } + }, [mountedRef, mx, profileFetchGenerationRef]); + + useEffect(() => { + void fetchProfiles(); + return () => { + profileFetchGenerationRef.current += 1; + }; + }, [fetchProfiles, profileFetchGenerationRef]); + + return { profiles, fetchProfiles }; +} + +function useFilteredProfiles( + profiles: Persona[], + mountedRef: MutableRefObject, + searchInputRef: RefObject, + profileFetchGenerationRef: MutableRefObject +) { + const [filteredProfiles, setFilteredProfiles] = useState( + profiles ?? undefined + ); + // useEffect(() => { mountedRef.current = true; return () => { mountedRef.current = false; profileFetchGenerationRef.current += 1; }; - }, []); + }, [mountedRef, profileFetchGenerationRef]); - const clearFilterInput = () => { + const clearFilter = () => { if (searchInputRef.current) { searchInputRef.current.value = ''; } setFilteredProfiles(profiles); }; + const filter = useCallback( + (e: FormEvent) => { + const term = (e.target as HTMLInputElement).value.toLocaleLowerCase(); + + const filtered = term + ? profiles?.filter((profile) => + searchInputRef.current + ? profile.displayname.toLocaleLowerCase().includes(searchInputRef.current?.value) || + profile.id.toLocaleLowerCase().includes(searchInputRef.current?.value) + : true + ) + : profiles; + + setFilteredProfiles(filtered); + }, + [profiles, searchInputRef] + ); + + useEffect(() => { + setFilteredProfiles(profiles); + }, [profiles]); + + return { filteredProfiles, filter, clearFilter }; +} + +function useSelectedProfiles( + mx: MatrixClient, + roomId: string, + latchedPersona: Persona | undefined, + mountedRef: MutableRefObject +) { + const [selectedGlobalPersona, setSelectedGlobalPersona] = + useState(null); + const [selectedRoomPersona, setSelectedRoomPersona] = useState( + latchedPersona ?? null + ); + + // Bumped on each click so an in-flight sync cannot undo a fresher choice. Global and + // per-room selections are independent, so one must not invalidate the other's rollback. + const globalSelectionRef = useRef(0); + const roomSelectionRef = useRef(0); + + const toggle = async (profile: Persona, isGlobal: boolean) => { + const previousPersona = isGlobal ? selectedGlobalPersona : selectedRoomPersona; + const disabling = profile.id === previousPersona?.id; + + const setPersona = isGlobal ? setSelectedGlobalPersona : setSelectedRoomPersona; + const generationRef = isGlobal ? globalSelectionRef : roomSelectionRef; + const selectionGeneration = ++generationRef.current; + + setPersona(disabling ? null : profile); + + try { + await new ProfileCatalog(mx).setSelection( + isGlobal ? 'account' : { roomId: roomId! }, + disabling ? undefined : profile.id, + undefined, + disabling + ); + } catch { + if (mountedRef.current && selectionGeneration === generationRef.current) { + setPersona(previousPersona); + } + } + }; + + const setGlobal = async (profile: Persona) => toggle(profile, true); + const setRoom = async (profile: Persona) => toggle(profile, false); + useEffect(() => { let cancelled = false; @@ -184,55 +266,56 @@ function PersonaPickerMenu({ }; }, [mx, roomId, latchedPersona, selectedRoomPersona]); - const fetchProfiles = useCallback(async (mx_: MatrixClient) => { - const fetchGeneration = ++profileFetchGenerationRef.current; - try { - const fetchedProfiles = await new ProfileCatalog(mx_).list(); - if (!mountedRef.current || fetchGeneration !== profileFetchGenerationRef.current) { - return; - } - setProfiles(fetchedProfiles); - setFilteredProfiles(fetchedProfiles); - } catch { - // Profile loading is best effort; keep the existing list when it fails. - } - }, []); + return { + room: selectedRoomPersona, + global: selectedGlobalPersona, + setRoom, + setGlobal, + }; +} - useEffect(() => { - void fetchProfiles(mx); - return () => { - profileFetchGenerationRef.current += 1; - }; - }, [fetchProfiles, mx]); +export function PersonaPicker({ + tab: tabProp = PersonaPickerTab.Global, + mx, + roomId, + suppressEditorRefocus, + onTabChange, + latchedPersona, + anchor, +}: PersonaPickerProps) { + const [tab, setTab] = useState(tabProp); + const [AddPersonaMenuAnchor, setAddPersonaMenuAnchor] = useState(anchor); + const mountedRef = useRef(false); + const profileFetchGenerationRef = useRef(0); - const filter = useCallback( - (e: FormEvent) => { - const term = (e.target as HTMLInputElement).value.toLocaleLowerCase(); + const { profiles } = useProfiles(mx, mountedRef, profileFetchGenerationRef); - const filtered = term - ? profiles?.filter((profile) => - searchInputRef.current - ? profile.displayname.toLocaleLowerCase().includes(searchInputRef.current?.value) || - profile.id.toLocaleLowerCase().includes(searchInputRef.current?.value) - : true - ) - : profiles; - - setFilteredProfiles(filtered); - }, - [profiles] + const searchInputRef = useRef(null); + const { filteredProfiles, filter, clearFilter } = useFilteredProfiles( + profiles ?? [], + mountedRef, + searchInputRef, + profileFetchGenerationRef ); - const avatarUrl = useCallback( - (profile: PerMessageProfileMsc4461) => { - if (profile.avatar_url !== undefined) { - return mxcUrlToHttp(mx, profile.avatar_url, useAuthentication, 96, 96, 'crop') ?? undefined; - } else { - return undefined; - } - }, - [mx, useAuthentication] - ); + const { + room: selectedRoomPersona, + global: selectedGlobalPersona, + setRoom, + setGlobal, + } = useSelectedProfiles(mx, roomId ?? '', latchedPersona, mountedRef); + + const isPickerMenuItemSelected = (persona: PerMessageProfileMsc4461) => { + const selectedPersona = + tab === PersonaPickerTab.Global ? selectedGlobalPersona : selectedRoomPersona; + return persona.id === selectedPersona?.id ? true : undefined; + }; + + const { nameColor, avatarUrl } = useProfileCosmetics(mx); + + const defactoPersona = () => selectedRoomPersona ?? selectedGlobalPersona; + + const scrollRef = useRef(null); return ( { setAddPersonaMenuAnchor(undefined); - clearFilterInput(); + clearFilter(); }} menu={ @@ -284,123 +367,92 @@ function PersonaPickerMenu({ + - <> - - - - {filteredProfiles?.map((profile) => ( - { - if (onPersonaSelect) { - await onPersonaSelect(profile); - requestClose?.(); - return; - } - const isGlobal = tab === PersonaPickerTab.Global; - const previousPersona = isGlobal - ? selectedGlobalPersona - : selectedRoomPersona; - const disabling = profile.id === previousPersona?.id; - const setPersona = isGlobal - ? setSelectedGlobalPersona - : setSelectedRoomPersona; - const generationRef = isGlobal ? globalSelectionRef : roomSelectionRef; - const selectionGeneration = ++generationRef.current; - - setPersona(disabling ? null : profile); - - try { - await new ProfileCatalog(mx).setSelection( - isGlobal ? 'account' : { roomId: roomId! }, - disabling ? undefined : profile.id, - undefined, - disabling - ); - } catch { - if (mountedRef.current && selectionGeneration === generationRef.current) { - setPersona(previousPersona); - } - } - }} - before={ - - ( - - {nameInitials(profile.displayname)} - - )} - alt={`Avatar for profile ${profile.id}`} - /> - - } - > - - - {profile.displayname} - - - {profile.id} - - - - ))} - - - Message will use your per-room persona. - - ) : selectedGlobalPersona ? ( - <> - Message will use your global persona. - - ) : ( - <>No persona chosen. - ) - } - /> - + + {filteredProfiles?.map((profile) => ( + { + const setPersona = tab === PersonaPickerTab.Global ? setGlobal : setRoom; + setPersona(profile); + }} + before={ + + ( + + {nameInitials(profile.displayname)} + + )} + alt={`Avatar for profile ${profile.id}`} + /> + + } + > + + + {profile.displayname} + + + {profile.id} + + + + ))} + + + Message will use your per-room persona. + + ) : selectedGlobalPersona ? ( + <> + Message will use your global persona. + + ) : ( + <>No persona chosen. + ) + } + /> } @@ -447,3 +499,127 @@ function PersonaPickerMenu({ ); } + +export function TemporaryPersonaPicker({ + mx, + onPersonaSelect, + anchor, + requestClose, +}: TemporaryPersonaPickerProps) { + const [AddPersonaMenuAnchor, setAddPersonaMenuAnchor] = useState(anchor); + // const [selectedGlobalPersona, setSelectedGlobalPersona] = + // useState(null); + // const [selectedRoomPersona, setSelectedRoomPersona] = useState( + // latchedPersona ?? null + // ); + const mountedRef = useRef(false); + const profileFetchGenerationRef = useRef(0); + + const { profiles } = useProfiles(mx, mountedRef, profileFetchGenerationRef); + + const searchInputRef = useRef(null); + const { filteredProfiles, filter, clearFilter } = useFilteredProfiles( + profiles ?? [], + mountedRef, + searchInputRef, + profileFetchGenerationRef + ); + + const { nameColor, avatarUrl } = useProfileCosmetics(mx); + + const scrollRef = useRef(null); + + return ( + { + setAddPersonaMenuAnchor(undefined); + clearFilter(); + requestClose(); + }} + menu={ + + + + + + {filteredProfiles?.map((profile) => ( + { + await onPersonaSelect(profile); + requestClose(); + return; + }} + before={ + + ( + + {nameInitials(profile.displayname)} + + )} + alt={`Avatar for profile ${profile.id}`} + /> + + } + > + + + {profile.displayname} + + + {profile.id} + + + + ))} + + + + } + > + ); +}