feat: Integrate forum layout support across various components and enhance room handling logic
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import React, { FormEventHandler, useCallback, useEffect, useState } from 'react';
|
||||
import React, { FormEventHandler, MouseEventHandler, useCallback, useEffect, useState } from 'react';
|
||||
import { MatrixError, Room } from 'matrix-js-sdk';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
@@ -9,6 +10,10 @@ import {
|
||||
Icon,
|
||||
Icons,
|
||||
Input,
|
||||
Menu,
|
||||
MenuItem,
|
||||
PopOut,
|
||||
RectCords,
|
||||
Spinner,
|
||||
Switch,
|
||||
Text,
|
||||
@@ -38,7 +43,9 @@ import {
|
||||
RoomVersionSelector,
|
||||
useAdditionalCreators,
|
||||
} from '../../components/create-room';
|
||||
import { RoomType } from '../../../types/matrix/room';
|
||||
import { RoomType, StateEvent } from '../../../types/matrix/room';
|
||||
import { stopPropagation } from '../../utils/keyboard';
|
||||
import { shouldShowForumLobby } from '../../utils/room';
|
||||
|
||||
const getCreateSpaceKindToIcon = (kind: CreateRoomKind) => {
|
||||
if (kind === CreateRoomKind.Private) return Icons.SpaceLock;
|
||||
@@ -46,6 +53,11 @@ const getCreateSpaceKindToIcon = (kind: CreateRoomKind) => {
|
||||
return Icons.SpaceGlobe;
|
||||
};
|
||||
|
||||
const spaceTypeName: Record<RoomType.Space | RoomType.Forum, string> = {
|
||||
[RoomType.Space]: 'Space',
|
||||
[RoomType.Forum]: 'Forum Space',
|
||||
};
|
||||
|
||||
type CreateSpaceFormProps = {
|
||||
defaultKind?: CreateRoomKind;
|
||||
space?: Room;
|
||||
@@ -74,12 +86,22 @@ export function CreateSpaceForm({ defaultKind, space, onCreate }: CreateSpaceFor
|
||||
useAdditionalCreators();
|
||||
const [federation, setFederation] = useState(true);
|
||||
const [knock, setKnock] = useState(false);
|
||||
const [spaceType, setSpaceType] = useState<RoomType.Space | RoomType.Forum>(RoomType.Space);
|
||||
const [typeMenuAnchor, setTypeMenuAnchor] = useState<RectCords>();
|
||||
const [advance, setAdvance] = useState(false);
|
||||
|
||||
const parentIsForum = Boolean(space && shouldShowForumLobby(space));
|
||||
|
||||
const allowKnock = kind === CreateRoomKind.Private && knockSupported(selectedRoomVersion);
|
||||
const allowKnockRestricted =
|
||||
kind === CreateRoomKind.Restricted && knockRestrictedSupported(selectedRoomVersion);
|
||||
|
||||
useEffect(() => {
|
||||
if (parentIsForum && spaceType === RoomType.Forum) {
|
||||
setSpaceType(RoomType.Space);
|
||||
}
|
||||
}, [parentIsForum, spaceType]);
|
||||
|
||||
const handleRoomVersionChange = (version: string) => {
|
||||
if (!restrictedSupported(version)) {
|
||||
setKind(CreateRoomKind.Private);
|
||||
@@ -94,6 +116,15 @@ export function CreateSpaceForm({ defaultKind, space, onCreate }: CreateSpaceFor
|
||||
const error = createState.status === AsyncStatus.Error ? createState.error : undefined;
|
||||
const disabled = createState.status === AsyncStatus.Loading;
|
||||
|
||||
const handleOpenTypeMenu: MouseEventHandler<HTMLButtonElement> = (evt) => {
|
||||
setTypeMenuAnchor(evt.currentTarget.getBoundingClientRect());
|
||||
};
|
||||
|
||||
const handleTypeSelect = (type: RoomType.Space | RoomType.Forum) => {
|
||||
setSpaceType(type);
|
||||
setTypeMenuAnchor(undefined);
|
||||
};
|
||||
|
||||
const handleSubmit: FormEventHandler<HTMLFormElement> = (evt) => {
|
||||
evt.preventDefault();
|
||||
if (disabled) return;
|
||||
@@ -119,7 +150,7 @@ export function CreateSpaceForm({ defaultKind, space, onCreate }: CreateSpaceFor
|
||||
|
||||
create({
|
||||
version: selectedRoomVersion,
|
||||
type: RoomType.Space,
|
||||
type: spaceType,
|
||||
parent: space,
|
||||
kind,
|
||||
name: roomName,
|
||||
@@ -128,7 +159,21 @@ export function CreateSpaceForm({ defaultKind, space, onCreate }: CreateSpaceFor
|
||||
knock: roomKnock,
|
||||
allowFederation: federation,
|
||||
additionalCreators: allowAdditionalCreators ? additionalCreators : undefined,
|
||||
}).then((roomId) => {
|
||||
}).then(async (roomId) => {
|
||||
if (spaceType === RoomType.Forum) {
|
||||
try {
|
||||
await mx.sendStateEvent(
|
||||
roomId,
|
||||
StateEvent.PaarrotRoomKind,
|
||||
{ kind: 'forum_space' },
|
||||
''
|
||||
);
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('Failed to set forum space marker state event:', err);
|
||||
}
|
||||
}
|
||||
|
||||
if (alive()) {
|
||||
onCreate?.(roomId);
|
||||
}
|
||||
@@ -147,6 +192,84 @@ export function CreateSpaceForm({ defaultKind, space, onCreate }: CreateSpaceFor
|
||||
getIcon={getCreateSpaceKindToIcon}
|
||||
/>
|
||||
</Box>
|
||||
{!parentIsForum && (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Type</Text>
|
||||
<SequenceCard
|
||||
style={{ padding: config.space.S300 }}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
>
|
||||
<SettingTile
|
||||
title="Space Type"
|
||||
description="Choose standard space navigation or forum-style space behavior."
|
||||
after={
|
||||
<>
|
||||
<Button
|
||||
size="300"
|
||||
variant="Primary"
|
||||
outlined
|
||||
fill="Soft"
|
||||
radii="300"
|
||||
after={<Icon size="300" src={Icons.ChevronBottom} />}
|
||||
onClick={handleOpenTypeMenu}
|
||||
disabled={disabled}
|
||||
>
|
||||
<Text size="T300">{spaceTypeName[spaceType]}</Text>
|
||||
</Button>
|
||||
<PopOut
|
||||
anchor={typeMenuAnchor}
|
||||
offset={5}
|
||||
position="Bottom"
|
||||
align="End"
|
||||
content={
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
onDeactivate: () => setTypeMenuAnchor(undefined),
|
||||
clickOutsideDeactivates: true,
|
||||
isKeyForward: (evt: KeyboardEvent) =>
|
||||
evt.key === 'ArrowDown' || evt.key === 'ArrowRight',
|
||||
isKeyBackward: (evt: KeyboardEvent) =>
|
||||
evt.key === 'ArrowUp' || evt.key === 'ArrowLeft',
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Menu>
|
||||
<Box direction="Column" gap="100" style={{ padding: config.space.S100 }}>
|
||||
<MenuItem
|
||||
size="300"
|
||||
variant={spaceType === RoomType.Space ? 'Primary' : 'Surface'}
|
||||
radii="300"
|
||||
onClick={() => handleTypeSelect(RoomType.Space)}
|
||||
>
|
||||
<Text size="T300">Space</Text>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
size="300"
|
||||
variant={spaceType === RoomType.Forum ? 'Primary' : 'Surface'}
|
||||
radii="300"
|
||||
onClick={() => handleTypeSelect(RoomType.Forum)}
|
||||
>
|
||||
<Text size="T300">Forum Space</Text>
|
||||
</MenuItem>
|
||||
</Box>
|
||||
</Menu>
|
||||
</FocusTrap>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
</Box>
|
||||
)}
|
||||
{parentIsForum && (
|
||||
<Text size="T200" priority="300">
|
||||
Creates a forum category (subspace). Add topics inside it with Add Topic after selecting the
|
||||
category in the filter bar.
|
||||
</Text>
|
||||
)}
|
||||
<Box shrink="No" direction="Column" gap="100">
|
||||
<Text size="L400">Name</Text>
|
||||
<Input
|
||||
|
||||
Reference in New Issue
Block a user