diff --git a/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx b/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx index a9e7386f..08761c43 100644 --- a/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx +++ b/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx @@ -1,5 +1,17 @@ import { FormEventHandler, useCallback } from 'react'; -import { Box, Button, Icon, IconButton, Icons, Input, Scroll, Spinner, Text, config } from 'folds'; +import { + Box, + Button, + Chip, + Icon, + IconButton, + Icons, + Input, + Scroll, + Spinner, + Text, + config, +} from 'folds'; import { Page, PageContent, PageHeader } from '$components/page'; import { SequenceCard } from '$components/sequence-card'; import { SettingTile } from '$components/setting-tile'; @@ -9,6 +21,8 @@ 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 { StateEvent } from '$types/matrix/room'; import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; import { MatrixError } from '$types/matrix-sdk'; @@ -31,6 +45,17 @@ export function RoomAbbreviations({ requestClose }: 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 + : []; + const canEdit = permissions.stateEvent(StateEvent.RoomAbbreviations, userId); const [saveState, saveAbbreviations] = useAsyncCallback( @@ -56,7 +81,9 @@ export function RoomAbbreviations({ requestClose }: AbbreviationsProps) { const definition = definitionInput.value.trim(); if (!term || !definition) return; - const alreadyExists = entries.some((e) => e.term.toLowerCase() === term.toLowerCase()); + const alreadyExists = + entries.some((e) => e.term.toLowerCase() === term.toLowerCase()) || + spaceEntries.some((e) => e.term.toLowerCase() === term.toLowerCase()); if (alreadyExists) { termInput.setCustomValidity('This term already exists.'); termInput.reportValidity(); @@ -163,11 +190,58 @@ export function RoomAbbreviations({ requestClose }: AbbreviationsProps) { )} + {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 - ? `Defined Abbreviations (${entries.length})` - : 'Defined Abbreviations'} + ? `Room Abbreviations (${entries.length})` + : 'Room Abbreviations'} {entries.length === 0 ? ( - No abbreviations defined yet. + No room-level abbreviations defined yet. {canEdit && ' Use the form above to add some.'} diff --git a/src/app/features/room/Room.tsx b/src/app/features/room/Room.tsx index b7fca58e..1f7593ef 100644 --- a/src/app/features/room/Room.tsx +++ b/src/app/features/room/Room.tsx @@ -18,7 +18,8 @@ import { callChatAtom } from '$state/callEmbed'; import { roomIdToOpenThreadAtomFamily } from '$state/room/roomToOpenThread'; import { roomIdToThreadBrowserAtomFamily } from '$state/room/roomToThreadBrowser'; import { createDebugLogger } from '$utils/debugLogger'; -import { useRoomAbbreviations, RoomAbbreviationsContext } from '$hooks/useRoomAbbreviations'; +import { useMergedAbbreviations, RoomAbbreviationsContext } from '$hooks/useRoomAbbreviations'; +import { useSpaceOptionally } from '$hooks/useSpace'; import { RoomViewHeader } from './RoomViewHeader'; import { MembersDrawer } from './MembersDrawer'; import { RoomView } from './RoomView'; @@ -98,7 +99,8 @@ export function Room() { ); const callView = room.isCallRoom(); - const abbreviations = useRoomAbbreviations(room); + const parentSpace = useSpaceOptionally(); + const abbreviations = useMergedAbbreviations(room, parentSpace); // Log call view state useEffect(() => { diff --git a/src/app/features/space-settings/SpaceSettings.tsx b/src/app/features/space-settings/SpaceSettings.tsx index ddd1d5c7..87eb2a9c 100644 --- a/src/app/features/space-settings/SpaceSettings.tsx +++ b/src/app/features/space-settings/SpaceSettings.tsx @@ -16,6 +16,7 @@ import { EmojisStickers } from '$features/common-settings/emojis-stickers'; import { Members } from '$features/common-settings/members'; import { DeveloperTools } from '$features/common-settings/developer-tools'; import { Cosmetics } from '$features/common-settings/cosmetics/Cosmetics'; +import { RoomAbbreviations } from '$features/room-settings/abbreviations/RoomAbbreviations'; import { General } from './general'; import { Permissions } from './permissions'; @@ -50,6 +51,11 @@ const useSpaceSettingsMenuItems = (): SpaceSettingsMenuItem[] => icon: Icons.Alphabet, activeIcon: Icons.AlphabetUnderline, }, + { + page: SpaceSettingsPage.AbbreviationsPage, + name: 'Abbreviations', + icon: Icons.Info, + }, { page: SpaceSettingsPage.EmojisStickersPage, name: 'Emojis & Stickers', @@ -187,6 +193,9 @@ export function SpaceSettings({ initialPage, requestClose }: SpaceSettingsProps) {activePage === SpaceSettingsPage.DeveloperToolsPage && ( )} + {activePage === SpaceSettingsPage.AbbreviationsPage && ( + + )} ); } diff --git a/src/app/hooks/useRoomAbbreviations.ts b/src/app/hooks/useRoomAbbreviations.ts index c961cc3a..897e42b7 100644 --- a/src/app/hooks/useRoomAbbreviations.ts +++ b/src/app/hooks/useRoomAbbreviations.ts @@ -18,3 +18,26 @@ export const useRoomAbbreviations = (room: Room): Map => { if (!Array.isArray(content?.entries) || content.entries.length === 0) return EMPTY_MAP; return buildAbbreviationsMap(content.entries); }; + +/** + * 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. + */ +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); + + const spaceContent = space ? spaceStateEvent?.getContent() : undefined; + const roomContent = roomStateEvent?.getContent(); + + const spaceEntries = Array.isArray(spaceContent?.entries) ? spaceContent.entries : []; + const roomEntries = Array.isArray(roomContent?.entries) ? roomContent.entries : []; + + if (spaceEntries.length === 0 && roomEntries.length === 0) return EMPTY_MAP; + + // Space entries first; room entries are appended so they override duplicates. + return buildAbbreviationsMap([...spaceEntries, ...roomEntries]); +}; diff --git a/src/app/state/spaceSettings.ts b/src/app/state/spaceSettings.ts index 28464812..c33b5e95 100644 --- a/src/app/state/spaceSettings.ts +++ b/src/app/state/spaceSettings.ts @@ -8,6 +8,7 @@ export enum SpaceSettingsPage { DeveloperToolsPage, // Sable pages CosmeticsPage, + AbbreviationsPage, } export type SpaceSettingsState = {