From 3bf51a2eb265f048de03fbde1aae039ff375d4af Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Fri, 20 Mar 2026 08:36:57 -0400 Subject: [PATCH] feat: multi-level space abbreviation inheritance + flat settings list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - useMergedAbbreviations: drop space param; use getAllParents+roomToParentsAtom to traverse the full ancestor chain and subscribe to state events at every level (not just direct parent) via useStateEventCallback on the mx client - Room.tsx: drop parentSpace/useSpaceOptionally — hook handles it internally - RoomAbbreviations settings: replace two-section layout with a single flat list; room entries shown without badge, inherited entries show a 'Space - ' chip badge; supports arbitrary nesting depth (mirrors the useImagePackRooms multi-level pattern) --- .../abbreviations/RoomAbbreviations.tsx | 250 ++++++++++-------- src/app/features/room/Room.tsx | 4 +- src/app/hooks/useRoomAbbreviations.ts | 68 +++-- 3 files changed, 191 insertions(+), 131 deletions(-) diff --git a/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx b/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx index cedaf74a..c406cc6f 100644 --- a/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx +++ b/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx @@ -1,4 +1,5 @@ -import { FormEventHandler, useCallback } from 'react'; +import { FormEventHandler, useCallback, useMemo } from 'react'; +import { useAtomValue } from 'jotai'; import { Box, Button, @@ -21,12 +22,14 @@ import { usePowerLevels } from '$hooks/usePowerLevels'; import { useRoomCreators } from '$hooks/useRoomCreators'; import { useRoomPermissions } from '$hooks/useRoomPermissions'; import { useStateEvent } from '$hooks/useStateEvent'; -import { useSpaceOptionally } from '$hooks/useSpace'; -import { useRoomName } from '$hooks/useRoomMeta'; +import { useStateEventCallback } from '$hooks/useStateEventCallback'; +import { useForceUpdate } from '$hooks/useForceUpdate'; import { StateEvent } from '$types/matrix/room'; import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; import { MatrixError } from '$types/matrix-sdk'; import { AbbreviationEntry, RoomAbbreviationsContent } from '$utils/abbreviations'; +import { getAllParents, getStateEvent } from '$utils/room'; +import { roomToParentsAtom } from '$state/room/roomToParents'; import { SequenceCardStyle } from '$features/common-settings/styles.css'; type AbbreviationsProps = { @@ -46,16 +49,48 @@ export function RoomAbbreviations({ requestClose, isSpace }: AbbreviationsProps) const content = stateEvent?.getContent(); const entries: AbbreviationEntry[] = Array.isArray(content?.entries) ? content.entries : []; - // Parent space abbreviations (read-only, inherited) - const parentSpace = useSpaceOptionally(); - const parentSpaceName = useRoomName(parentSpace ?? room); - const spaceStateEvent = useStateEvent(parentSpace ?? room, StateEvent.RoomAbbreviations); - const spaceContent = parentSpace - ? spaceStateEvent?.getContent() - : undefined; - const spaceEntries: AbbreviationEntry[] = Array.isArray(spaceContent?.entries) - ? spaceContent.entries - : []; + // Ancestor space abbreviations (read-only, inherited) — full multi-level support + const roomToParents = useAtomValue(roomToParentsAtom); + const [ancestorUpdateCount, forceAncestorUpdate] = useForceUpdate(); + + useStateEventCallback( + mx, + useCallback( + (event) => { + if (event.getType() !== StateEvent.RoomAbbreviations) return; + const eventRoomId = event.getRoomId(); + if (eventRoomId && getAllParents(roomToParents, room.roomId).has(eventRoomId)) { + forceAncestorUpdate(); + } + }, + [room.roomId, roomToParents, forceAncestorUpdate] + ) + ); + + type SpaceEntryGroup = { spaceId: string; spaceName: string; entries: AbbreviationEntry[] }; + const ancestorGroups = useMemo( + (): SpaceEntryGroup[] => + Array.from(getAllParents(roomToParents, room.roomId)).reduce( + (groups, parentId) => { + const parentRoom = mx.getRoom(parentId); + if (!parentRoom) return groups; + const ev = getStateEvent(parentRoom, StateEvent.RoomAbbreviations); + const c = ev?.getContent(); + const parentEntries: AbbreviationEntry[] = Array.isArray(c?.entries) ? c.entries : []; + if (parentEntries.length > 0) { + groups.push({ spaceId: parentId, spaceName: parentRoom.name, entries: parentEntries }); + } + return groups; + }, + [] + ), + // eslint-disable-next-line react-hooks/exhaustive-deps + [mx, roomToParents, room.roomId, ancestorUpdateCount] + ); + const allAncestorEntries = useMemo( + () => ancestorGroups.flatMap((g) => g.entries), + [ancestorGroups] + ); const canEdit = permissions.stateEvent(StateEvent.RoomAbbreviations, userId); @@ -84,7 +119,7 @@ export function RoomAbbreviations({ requestClose, isSpace }: AbbreviationsProps) const alreadyExists = entries.some((e) => e.term.toLowerCase() === term.toLowerCase()) || - spaceEntries.some((e) => e.term.toLowerCase() === term.toLowerCase()); + allAncestorEntries.some((e) => e.term.toLowerCase() === term.toLowerCase()); if (alreadyExists) { termInput.setCustomValidity('This term already exists.'); termInput.reportValidity(); @@ -191,106 +226,97 @@ export function RoomAbbreviations({ requestClose, isSpace }: AbbreviationsProps) )} - {!isSpace && parentSpace && ( - - - {spaceEntries.length > 0 - ? `Inherited from Space (${spaceEntries.length})` - : 'Inherited from Space'} - - {spaceEntries.length === 0 ? ( - - - No abbreviations defined in {parentSpaceName}. - - - ) : ( - spaceEntries.map((entry, index) => ( - - - - - {entry.term} - - - Space - - - - {entry.definition} - - - - )) - )} - - )} - - - {entries.length > 0 - ? `${isSpace ? 'Space' : 'Room'} Abbreviations (${entries.length})` - : `${isSpace ? 'Space' : 'Room'} Abbreviations`} - - {entries.length === 0 ? ( - - - No {isSpace ? 'space' : 'room'}-level abbreviations defined yet. - {canEdit && ' Use the form above to add some.'} - - - ) : ( - entries.map((entry, index) => ( - - - - {entry.term} - - - {entry.definition} - - - {canEdit && ( - - handleRemove(index)} - variant="Background" - size="300" - radii="300" - disabled={saving} - aria-label={`Remove abbreviation ${entry.term}`} - > - - - + {(() => { + const totalCount = entries.length + (isSpace ? 0 : allAncestorEntries.length); + const label = isSpace ? 'Space' : 'Room'; + return ( + <> + + {totalCount > 0 + ? `${label} Abbreviations (${totalCount})` + : `${label} Abbreviations`} + + {totalCount === 0 ? ( + + + No {isSpace ? 'space' : 'room'}-level abbreviations defined yet. + {canEdit && ' Use the form above to add some.'} + + + ) : ( + <> + {entries.map((entry, index) => ( + + + + {entry.term} + + + {entry.definition} + + + {canEdit && ( + + handleRemove(index)} + variant="Background" + size="300" + radii="300" + disabled={saving} + aria-label={`Remove abbreviation ${entry.term}`} + > + + + + )} + + ))} + {!isSpace && + ancestorGroups.flatMap(({ spaceId, spaceName, entries: spEntries }) => + spEntries.map((entry, index) => ( + + + + + {entry.term} + + + Space - {spaceName} + + + + {entry.definition} + + + + )) + )} + )} - - )) - )} + + ); + })()} diff --git a/src/app/features/room/Room.tsx b/src/app/features/room/Room.tsx index 1f7593ef..918f978a 100644 --- a/src/app/features/room/Room.tsx +++ b/src/app/features/room/Room.tsx @@ -19,7 +19,6 @@ import { roomIdToOpenThreadAtomFamily } from '$state/room/roomToOpenThread'; import { roomIdToThreadBrowserAtomFamily } from '$state/room/roomToThreadBrowser'; import { createDebugLogger } from '$utils/debugLogger'; import { useMergedAbbreviations, RoomAbbreviationsContext } from '$hooks/useRoomAbbreviations'; -import { useSpaceOptionally } from '$hooks/useSpace'; import { RoomViewHeader } from './RoomViewHeader'; import { MembersDrawer } from './MembersDrawer'; import { RoomView } from './RoomView'; @@ -99,8 +98,7 @@ export function Room() { ); const callView = room.isCallRoom(); - const parentSpace = useSpaceOptionally(); - const abbreviations = useMergedAbbreviations(room, parentSpace); + const abbreviations = useMergedAbbreviations(room); // Log call view state useEffect(() => { diff --git a/src/app/hooks/useRoomAbbreviations.ts b/src/app/hooks/useRoomAbbreviations.ts index 897e42b7..e2641b34 100644 --- a/src/app/hooks/useRoomAbbreviations.ts +++ b/src/app/hooks/useRoomAbbreviations.ts @@ -1,8 +1,14 @@ -import { createContext, useContext } from 'react'; +import { createContext, useContext, useCallback, useMemo } from 'react'; +import { useAtomValue } from 'jotai'; import { Room } from '$types/matrix-sdk'; import { StateEvent } from '$types/matrix/room'; import { buildAbbreviationsMap, RoomAbbreviationsContent } from '$utils/abbreviations'; +import { getAllParents, getStateEvent } from '$utils/room'; +import { roomToParentsAtom } from '$state/room/roomToParents'; +import { useMatrixClient } from './useMatrixClient'; import { useStateEvent } from './useStateEvent'; +import { useStateEventCallback } from './useStateEventCallback'; +import { useForceUpdate } from './useForceUpdate'; const EMPTY_MAP: Map = new Map(); @@ -20,24 +26,54 @@ export const useRoomAbbreviations = (room: Room): Map => { }; /** - * Return a merged map of abbreviations from the parent space (if any) and the room. - * Room-level entries override space-level entries for the same term (case-insensitive). - * Pass `space` as the parent space Room, or null if there is none. - * Both hooks are always called unconditionally to satisfy the Rules of Hooks. + * Return a merged map of abbreviations from ALL ancestor spaces and the room. + * Nearest ancestor entries override farther ancestor entries; room entries override everything. + * Subscribes to abbreviation state changes across the full space hierarchy. */ -export const useMergedAbbreviations = (room: Room, space: Room | null): Map => { - // Always call with a valid Room — use `room` as a harmless fallback when space is null. - const spaceStateEvent = useStateEvent(space ?? room, StateEvent.RoomAbbreviations); - const roomStateEvent = useStateEvent(room, StateEvent.RoomAbbreviations); +export const useMergedAbbreviations = (room: Room): Map => { + const mx = useMatrixClient(); + const roomToParents = useAtomValue(roomToParentsAtom); + const [updateCount, forceUpdate] = useForceUpdate(); - const spaceContent = space ? spaceStateEvent?.getContent() : undefined; - const roomContent = roomStateEvent?.getContent(); + useStateEventCallback( + mx, + useCallback( + (event) => { + if (event.getType() !== StateEvent.RoomAbbreviations) return; + const eventRoomId = event.getRoomId(); + if (!eventRoomId) return; + if ( + eventRoomId === room.roomId || + getAllParents(roomToParents, room.roomId).has(eventRoomId) + ) { + forceUpdate(); + } + }, + [room.roomId, roomToParents, forceUpdate] + ) + ); - const spaceEntries = Array.isArray(spaceContent?.entries) ? spaceContent.entries : []; - const roomEntries = Array.isArray(roomContent?.entries) ? roomContent.entries : []; + return useMemo(() => { + const allParentIds = Array.from(getAllParents(roomToParents, room.roomId)); + const ancestorEntries = allParentIds.flatMap((parentId) => { + const parentRoom = mx.getRoom(parentId); + if (!parentRoom) return []; + const content = getStateEvent( + parentRoom, + StateEvent.RoomAbbreviations + )?.getContent(); + return Array.isArray(content?.entries) ? content.entries : []; + }); - if (spaceEntries.length === 0 && roomEntries.length === 0) return EMPTY_MAP; + const roomContent = getStateEvent( + room, + StateEvent.RoomAbbreviations + )?.getContent(); + const roomEntries = Array.isArray(roomContent?.entries) ? roomContent.entries : []; - // Space entries first; room entries are appended so they override duplicates. - return buildAbbreviationsMap([...spaceEntries, ...roomEntries]); + if (ancestorEntries.length === 0 && roomEntries.length === 0) return EMPTY_MAP; + // Ancestor entries first; room entries appended so they override duplicates. + return buildAbbreviationsMap([...ancestorEntries, ...roomEntries]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [mx, roomToParents, room, updateCount]); };