import React, { useCallback } from 'react'; import { Box, config, Header, IconButton, Modal, Overlay, OverlayBackdrop, OverlayCenter, Scroll, Text, } from 'folds'; import FocusTrap from 'focus-trap-react'; import { useAtomValue } from 'jotai'; import { EventType } from 'matrix-js-sdk'; import type { RoomJoinRulesEventContent } from 'matrix-js-sdk/lib/@types/state_events'; import { Icon, Icons } from '../../components/icons'; import { useAllJoinedRoomsSet, useGetRoom } from '../../hooks/useGetRoom'; import { SpaceProvider } from '../../hooks/useSpace'; import { CreateRoomForm } from './CreateRoom'; import { useCloseCreateRoomModal, useCreateRoomModalState, } from '../../state/hooks/createRoomModal'; import { CreateRoomModalState } from '../../state/createRoomModal'; import { stopPropagation } from '../../utils/keyboard'; import { useMatrixClient } from '../../hooks/useMatrixClient'; import { StateEvent } from '../../../types/matrix/room'; import { PaarrotSubRoomsContent } from '../../hooks/useRoomSubRooms'; import { roomToParentsAtom } from '../../state/room/roomToParents'; import { isSpace } from '../../utils/room'; type CreateRoomModalProps = { state: CreateRoomModalState; }; function CreateRoomModal({ state }: CreateRoomModalProps) { const { spaceId, parentSubRoomId } = state; const mx = useMatrixClient(); const closeDialog = useCloseCreateRoomModal(); const roomToParents = useAtomValue(roomToParentsAtom); const allJoinedRooms = useAllJoinedRoomsSet(); const getRoom = useGetRoom(allJoinedRooms); const space = spaceId ? getRoom(spaceId) : undefined; const parentRoom = parentSubRoomId ? getRoom(parentSubRoomId) : undefined; const handleCreate = useCallback( async (newRoomId: string) => { // If creating a sub-room, add it to the parent's sub-rooms and set parent relationship if (parentSubRoomId && parentRoom) { try { // Extract server from parent room ID for via servers const serverName = parentSubRoomId.split(':')[1] || ''; const viaServers = serverName ? [serverName] : []; // Set m.space.parent on the new room pointing to parent await mx.sendStateEvent( newRoomId, EventType.SpaceParent, { canonical: true, via: viaServers, }, parentSubRoomId, ); // Set m.space.child on the parent room pointing to this sub-room await mx.sendStateEvent( parentSubRoomId, EventType.SpaceChild, { via: viaServers }, newRoomId, ); // Copy parent's join_rules to the sub-room const parentJoinRulesEvent = parentRoom.currentState.getStateEvents( StateEvent.RoomJoinRules, '', ); if (parentJoinRulesEvent) { const joinRulesContent = parentJoinRulesEvent.getContent(); await mx.sendStateEvent(newRoomId, EventType.RoomJoinRules, joinRulesContent, ''); } // Add new room to parent's sub-rooms list const stateEvent = parentRoom.currentState.getStateEvents(StateEvent.PaarrotSubRooms, ''); const currentContent = stateEvent?.getContent() as PaarrotSubRoomsContent | undefined; const currentChildren = currentContent?.children ?? []; const newContent: PaarrotSubRoomsContent = { children: [...currentChildren, newRoomId], }; await mx.sendStateEvent(parentSubRoomId, StateEvent.PaarrotSubRooms, newContent, ''); // Also add sub-room to parent's space(s) for joining/permissions // The UI will hide it from the top-level display and only show it nested const parentSpaces = roomToParents.get(parentSubRoomId); if (parentSpaces) { const spaceIds = Array.from(parentSpaces); await Promise.all( spaceIds.map(async (parentSpaceId) => { const parentSpaceRoom = mx.getRoom(parentSpaceId); // Only add to actual spaces (not other sub-room parents) if (parentSpaceRoom && isSpace(parentSpaceRoom)) { try { const spaceServerName = parentSpaceId.split(':')[1] || ''; const spaceViaServers = spaceServerName ? [spaceServerName] : []; // Add as m.space.child to the space await mx.sendStateEvent( parentSpaceId, EventType.SpaceChild, { via: spaceViaServers }, newRoomId, ); } catch (spaceError) { console.warn(`Failed to add sub-room to space ${parentSpaceId}:`, spaceError); } } }), ); } } catch (error) { console.error('Failed to set up sub-room relationship:', error); } } closeDialog(); }, [mx, parentSubRoomId, parentRoom, roomToParents, closeDialog], ); const modalTitle = parentSubRoomId ? 'New Sub-Room' : 'New Room'; return ( }>
{modalTitle}
); } export function CreateRoomModalRenderer() { const state = useCreateRoomModalState(); if (!state) return null; return ; }