feat: multi-level space abbreviation inheritance + flat settings list
- 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 - <SpaceName>' chip badge; supports arbitrary nesting depth (mirrors the useImagePackRooms multi-level pattern)
This commit is contained in:
3 files changed
+191
-131
No files matched your search
@@ -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<RoomAbbreviationsContent>();
|
||||
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<RoomAbbreviationsContent>()
|
||||
: 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<SpaceEntryGroup[]>(
|
||||
(groups, parentId) => {
|
||||
const parentRoom = mx.getRoom(parentId);
|
||||
if (!parentRoom) return groups;
|
||||
const ev = getStateEvent(parentRoom, StateEvent.RoomAbbreviations);
|
||||
const c = ev?.getContent<RoomAbbreviationsContent>();
|
||||
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)
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{!isSpace && parentSpace && (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">
|
||||
{spaceEntries.length > 0
|
||||
? `Inherited from Space (${spaceEntries.length})`
|
||||
: 'Inherited from Space'}
|
||||
</Text>
|
||||
{spaceEntries.length === 0 ? (
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
>
|
||||
<Text size="T300" style={{ color: 'var(--mx-surface-variant-on)' }}>
|
||||
No abbreviations defined in {parentSpaceName}.
|
||||
</Text>
|
||||
</SequenceCard>
|
||||
) : (
|
||||
spaceEntries.map((entry, index) => (
|
||||
<SequenceCard
|
||||
// eslint-disable-next-line react/no-array-index-key
|
||||
key={index}
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Row"
|
||||
gap="400"
|
||||
alignItems="Center"
|
||||
>
|
||||
<Box grow="Yes" direction="Column" gap="100">
|
||||
<Box gap="200" alignItems="Center">
|
||||
<Text size="T300">
|
||||
<b>{entry.term}</b>
|
||||
</Text>
|
||||
<Chip variant="Primary" radii="Pill" size="300">
|
||||
<Text size="T200">Space</Text>
|
||||
</Chip>
|
||||
</Box>
|
||||
<Text size="T200" style={{ opacity: 0.7 }}>
|
||||
{entry.definition}
|
||||
</Text>
|
||||
</Box>
|
||||
</SequenceCard>
|
||||
))
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">
|
||||
{entries.length > 0
|
||||
? `${isSpace ? 'Space' : 'Room'} Abbreviations (${entries.length})`
|
||||
: `${isSpace ? 'Space' : 'Room'} Abbreviations`}
|
||||
</Text>
|
||||
{entries.length === 0 ? (
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
>
|
||||
<Text size="T300" style={{ color: 'var(--mx-surface-variant-on)' }}>
|
||||
No {isSpace ? 'space' : 'room'}-level abbreviations defined yet.
|
||||
{canEdit && ' Use the form above to add some.'}
|
||||
</Text>
|
||||
</SequenceCard>
|
||||
) : (
|
||||
entries.map((entry, index) => (
|
||||
<SequenceCard
|
||||
// eslint-disable-next-line react/no-array-index-key
|
||||
key={index}
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Row"
|
||||
gap="400"
|
||||
alignItems="Center"
|
||||
>
|
||||
<Box grow="Yes" direction="Column" gap="100">
|
||||
<Text size="T300">
|
||||
<b>{entry.term}</b>
|
||||
</Text>
|
||||
<Text size="T200" style={{ opacity: 0.7 }}>
|
||||
{entry.definition}
|
||||
</Text>
|
||||
</Box>
|
||||
{canEdit && (
|
||||
<Box shrink="No">
|
||||
<IconButton
|
||||
onClick={() => handleRemove(index)}
|
||||
variant="Background"
|
||||
size="300"
|
||||
radii="300"
|
||||
disabled={saving}
|
||||
aria-label={`Remove abbreviation ${entry.term}`}
|
||||
>
|
||||
<Icon src={Icons.Delete} size="100" />
|
||||
</IconButton>
|
||||
</Box>
|
||||
{(() => {
|
||||
const totalCount = entries.length + (isSpace ? 0 : allAncestorEntries.length);
|
||||
const label = isSpace ? 'Space' : 'Room';
|
||||
return (
|
||||
<>
|
||||
<Text size="L400">
|
||||
{totalCount > 0
|
||||
? `${label} Abbreviations (${totalCount})`
|
||||
: `${label} Abbreviations`}
|
||||
</Text>
|
||||
{totalCount === 0 ? (
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
>
|
||||
<Text size="T300" style={{ color: 'var(--mx-surface-variant-on)' }}>
|
||||
No {isSpace ? 'space' : 'room'}-level abbreviations defined yet.
|
||||
{canEdit && ' Use the form above to add some.'}
|
||||
</Text>
|
||||
</SequenceCard>
|
||||
) : (
|
||||
<>
|
||||
{entries.map((entry, index) => (
|
||||
<SequenceCard
|
||||
// eslint-disable-next-line react/no-array-index-key
|
||||
key={`room-${index}`}
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Row"
|
||||
gap="400"
|
||||
alignItems="Center"
|
||||
>
|
||||
<Box grow="Yes" direction="Column" gap="100">
|
||||
<Text size="T300">
|
||||
<b>{entry.term}</b>
|
||||
</Text>
|
||||
<Text size="T200" style={{ opacity: 0.7 }}>
|
||||
{entry.definition}
|
||||
</Text>
|
||||
</Box>
|
||||
{canEdit && (
|
||||
<Box shrink="No">
|
||||
<IconButton
|
||||
onClick={() => handleRemove(index)}
|
||||
variant="Background"
|
||||
size="300"
|
||||
radii="300"
|
||||
disabled={saving}
|
||||
aria-label={`Remove abbreviation ${entry.term}`}
|
||||
>
|
||||
<Icon src={Icons.Delete} size="100" />
|
||||
</IconButton>
|
||||
</Box>
|
||||
)}
|
||||
</SequenceCard>
|
||||
))}
|
||||
{!isSpace &&
|
||||
ancestorGroups.flatMap(({ spaceId, spaceName, entries: spEntries }) =>
|
||||
spEntries.map((entry, index) => (
|
||||
<SequenceCard
|
||||
// eslint-disable-next-line react/no-array-index-key
|
||||
key={`${spaceId}-${index}`}
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Row"
|
||||
gap="400"
|
||||
alignItems="Center"
|
||||
>
|
||||
<Box grow="Yes" direction="Column" gap="100">
|
||||
<Box gap="200" alignItems="Center">
|
||||
<Text size="T300">
|
||||
<b>{entry.term}</b>
|
||||
</Text>
|
||||
<Chip variant="Primary" radii="Pill" size="300">
|
||||
<Text size="T200">Space - {spaceName}</Text>
|
||||
</Chip>
|
||||
</Box>
|
||||
<Text size="T200" style={{ opacity: 0.7 }}>
|
||||
{entry.definition}
|
||||
</Text>
|
||||
</Box>
|
||||
</SequenceCard>
|
||||
))
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</SequenceCard>
|
||||
))
|
||||
)}
|
||||
</>
|
||||
);
|
||||
})()}
|
||||
</Box>
|
||||
</Box>
|
||||
</PageContent>
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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<string, string> = new Map();
|
||||
|
||||
@@ -20,24 +26,54 @@ export const useRoomAbbreviations = (room: Room): Map<string, string> => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 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<string, string> => {
|
||||
// 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<string, string> => {
|
||||
const mx = useMatrixClient();
|
||||
const roomToParents = useAtomValue(roomToParentsAtom);
|
||||
const [updateCount, forceUpdate] = useForceUpdate();
|
||||
|
||||
const spaceContent = space ? spaceStateEvent?.getContent<RoomAbbreviationsContent>() : undefined;
|
||||
const roomContent = roomStateEvent?.getContent<RoomAbbreviationsContent>();
|
||||
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<RoomAbbreviationsContent>();
|
||||
return Array.isArray(content?.entries) ? content.entries : [];
|
||||
});
|
||||
|
||||
if (spaceEntries.length === 0 && roomEntries.length === 0) return EMPTY_MAP;
|
||||
const roomContent = getStateEvent(
|
||||
room,
|
||||
StateEvent.RoomAbbreviations
|
||||
)?.getContent<RoomAbbreviationsContent>();
|
||||
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]);
|
||||
};
|
||||
Reference in new issue
Block a user