feat: inherit space abbreviations in rooms, add abbreviations to space settings
This commit is contained in:
5 files changed
+116
-7
No files matched your search
@@ -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<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
|
||||
: [];
|
||||
|
||||
const canEdit = permissions.stateEvent(StateEvent.RoomAbbreviations, userId);
|
||||
|
||||
const [saveState, saveAbbreviations] = useAsyncCallback<void, MatrixError, [AbbreviationEntry[]]>(
|
||||
@@ -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) {
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{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
|
||||
? `Defined Abbreviations (${entries.length})`
|
||||
: 'Defined Abbreviations'}
|
||||
? `Room Abbreviations (${entries.length})`
|
||||
: 'Room Abbreviations'}
|
||||
</Text>
|
||||
{entries.length === 0 ? (
|
||||
<SequenceCard
|
||||
@@ -176,7 +250,7 @@ export function RoomAbbreviations({ requestClose }: AbbreviationsProps) {
|
||||
direction="Column"
|
||||
>
|
||||
<Text size="T300" style={{ color: 'var(--mx-surface-variant-on)' }}>
|
||||
No abbreviations defined yet.
|
||||
No room-level abbreviations defined yet.
|
||||
{canEdit && ' Use the form above to add some.'}
|
||||
</Text>
|
||||
</SequenceCard>
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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 && (
|
||||
<DeveloperTools requestClose={handlePageRequestClose} />
|
||||
)}
|
||||
{activePage === SpaceSettingsPage.AbbreviationsPage && (
|
||||
<RoomAbbreviations requestClose={handlePageRequestClose} />
|
||||
)}
|
||||
</PageRoot>
|
||||
);
|
||||
}
|
||||
@@ -18,3 +18,26 @@ export const useRoomAbbreviations = (room: Room): Map<string, string> => {
|
||||
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<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);
|
||||
|
||||
const spaceContent = space ? spaceStateEvent?.getContent<RoomAbbreviationsContent>() : undefined;
|
||||
const roomContent = roomStateEvent?.getContent<RoomAbbreviationsContent>();
|
||||
|
||||
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]);
|
||||
};
|
||||
@@ -8,6 +8,7 @@ export enum SpaceSettingsPage {
|
||||
DeveloperToolsPage,
|
||||
// Sable pages
|
||||
CosmeticsPage,
|
||||
AbbreviationsPage,
|
||||
}
|
||||
|
||||
export type SpaceSettingsState = {
|
||||
|
||||
Reference in new issue
Block a user