diff --git a/src/app/features/lobby/Lobby.tsx b/src/app/features/lobby/Lobby.tsx index dffb7304..c980db46 100644 --- a/src/app/features/lobby/Lobby.tsx +++ b/src/app/features/lobby/Lobby.tsx @@ -1,5 +1,25 @@ -import { MouseEventHandler, ReactElement, useCallback, useEffect, useMemo, useRef, useState } from 'react'; -import { Box, Chip, Icon, IconButton, Icons, Line, Scroll, Spinner, Text, color, config } from 'folds'; +import { + MouseEventHandler, + ReactElement, + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; +import { + Box, + Chip, + Icon, + IconButton, + Icons, + Line, + Scroll, + Spinner, + Text, + color, + config, +} from 'folds'; import { useVirtualizer, VirtualItem } from '@tanstack/react-virtual'; import { useAtom, useAtomValue } from 'jotai'; import { useNavigate } from 'react-router-dom'; @@ -13,7 +33,7 @@ import { import { produce } from 'immer'; import { useSpace } from '$hooks/useSpace'; import { Page, PageContent, PageContentCenter, PageHeroSection } from '$components/page'; -import { HierarchyItem, HierarchyItemSpace, SpaceHierarchy, useSpaceHierarchy } from '$hooks/useSpaceHierarchy'; +import { HierarchyItem, HierarchyItemSpace, useSpaceHierarchy } from '$hooks/useSpaceHierarchy'; import { VirtualTile } from '$components/virtualizer'; import { spaceRoomsAtom } from '$state/spaceRooms'; import { useSetting } from '$state/hooks/settings'; @@ -56,6 +76,7 @@ import { SpaceHierarchyItem } from './SpaceHierarchyItem'; import { CanDropCallback, useDnDMonitor } from './DnD'; import { LobbyHero } from './LobbyHero'; import { LobbyHeader } from './LobbyHeader'; +import { SpaceHierarchyNavItem } from './SpaceHierarchyNavItem'; const useCanDropLobbyItem = ( space: Room, @@ -273,11 +294,16 @@ export function Lobby() { return !Array.from(parentIds).some((id) => !getInClosedCategories(spaceId, id, roomId)); }; + const [subspaceHierarchyLimit] = useSetting(settingsAtom, 'subspaceHierarchyLimit'); const [draggingItem, setDraggingItem] = useState(); const hierarchy = useSpaceHierarchy( space.roomId, spaceRooms, getRoom, + useCallback( + (_childId, _spaceId, depth) => depth >= subspaceHierarchyLimit, + [subspaceHierarchyLimit] + ), useCallback( (childId) => getInClosedCategories(space.roomId, childId) || @@ -520,73 +546,76 @@ export function Lobby() { [mx, sidebarItems, sidebarSpaces] ); - const getPaddingTop = (hierarchy: SpaceHierarchy[], vItem: VirtualItem) => { + const getPaddingTop = (vItem: VirtualItem) => { if (vItem.index === 0) return 0; const prevDepth = hierarchy[vItem.index - 1]?.space.depth ?? 0; - const depth = hierarchy[vItem.index].space.depth; + const { depth } = hierarchy[vItem.index].space; if (depth !== 1 && depth >= prevDepth) return config.space.S200; return config.space.S500; }; - const getConnectorSVG = useCallback((hierarchy: SpaceHierarchy[], virtualizedItems: VirtualItem[]): ReactElement => { - const PADDING_LEFT_DEPTH_OFFSET = 15.75; - const PADDING_LEFT_DEPTH_OFFSET_START = -15; + const getConnectorSVG = useCallback( + (virtualizedItems: VirtualItem[]): ReactElement => { + const PADDING_LEFT_DEPTH_OFFSET = 15.75; + const PADDING_LEFT_DEPTH_OFFSET_START = -15; - var aY: number = 0; - // Holder for the paths - const pathHolder: ReactElement[] = []; - virtualizedItems.forEach((vItem) => { - const { depth } = hierarchy[vItem.index].space ?? {}; + let aY = 0; + // Holder for the paths + const pathHolder: ReactElement[] = []; + virtualizedItems.forEach((vItem) => { + const { depth } = hierarchy[vItem.index].space ?? {}; + + // We will render spaces at a level above their normal depth, since we want their children to be "under" them + // for the root items, we are not doing anything with it. + if (depth < 1) { + return; + } + // for the sub-root items, we will not draw any arcs from root to it. + // however, we should capture the aX and aY to draw starter arcs for next depths. + if (depth === 1) { + aY = vItem.end; + return; + } + + const pathStrings: string[] = []; + + for (let iDepth = 0; iDepth < depth; iDepth += 1) { + const X = iDepth * PADDING_LEFT_DEPTH_OFFSET + PADDING_LEFT_DEPTH_OFFSET_START; + + const bY = vItem.end; + + pathStrings.push(`M ${X} ${aY} L ${X} ${bY}`); + } + + pathHolder.push( + + ); - // We will render spaces at a level above their normal depth, since we want their children to be "under" them - // for the root items, we are not doing anything with it. - if (depth < 1) { - return; - } - // for the sub-root items, we will not draw any arcs from root to it. - // however, we should capture the aX and aY to draw starter arcs for next depths. - if (depth === 1) { aY = vItem.end; - return; - } + }); - var pathStrings: string[] = []; - - for (var iDepth = 0; iDepth < depth; iDepth++) { - var X = iDepth * PADDING_LEFT_DEPTH_OFFSET + PADDING_LEFT_DEPTH_OFFSET_START; - - var bY = vItem.end; - - pathStrings.push(`M ${X} ${aY} L ${X} ${bY}`); - } - - pathHolder.push( - + return ( + + {pathHolder} + ); - - aY = vItem.end; - }); - - return ( - - {pathHolder} - - ); - }, [hierarchy, vItems]); + }, + [hierarchy] + ); return ( @@ -641,39 +670,57 @@ export function Lobby() { - + {item.space.depth !== subspaceHierarchyLimit ? ( + + ) : ( + + )} ); })} - {getConnectorSVG(hierarchy, vItems)} + {getConnectorSVG(vItems)} {reordering && ( !subspaces.has(i.roomId)); - if (!spacePermissions?.stateEvent(StateEvent.SpaceChild, mx.getSafeUserId())) { - // hide unknown rooms for normal user - childItems = childItems?.filter((i) => { - const forbidden = error instanceof MatrixError ? error.errcode === 'M_FORBIDDEN' : false; - const inaccessibleRoom = !rooms.get(i.roomId) && !fetching && (error ? forbidden : true); - return !inaccessibleRoom; - }); - } + const [childItems, setChildItems] = useState([] as HierarchyItemRoom[] | undefined); + useEffect(() => { + let childItemsMut = roomItems?.filter((i) => !subspaces.has(i.roomId)); + if (!spacePermissions?.stateEvent(StateEvent.SpaceChild, mx.getSafeUserId())) { + // hide unknown rooms for normal user + childItemsMut = childItems?.filter((i) => { + const forbidden = error instanceof MatrixError ? error.errcode === 'M_FORBIDDEN' : false; + const inaccessibleRoom = !rooms.get(i.roomId) && !fetching && (error ? forbidden : true); + return !inaccessibleRoom; + }); + } + setChildItems(childItemsMut); + }, [childItems, spacePermissions, mx, roomItems, subspaces, fetching, error, rooms]); return ( @@ -204,7 +208,7 @@ export const SpaceHierarchyItem = forwardRef ) : ( childItems && ( - + ; + roomsPowerLevels: Map; + categoryId: string; + draggingItem?: HierarchyItem; + onDragging: (item?: HierarchyItem) => void; + canDrop: CanDropCallback; + disabledReorder?: boolean; + nextSpaceId?: string; + pinned: boolean; + togglePinToSidebar: (roomId: string) => void; + getRoom: (roomId: string) => Room | undefined; +}; +export const SpaceHierarchyNavItem = forwardRef( + ( + { + summary, + spaceItem, + allJoinedRooms, + roomsPowerLevels, + categoryId, + draggingItem, + onDragging, + canDrop, + disabledReorder, + nextSpaceId, + pinned, + togglePinToSidebar, + getRoom, + }, + ref + ) => { + const mx = useMatrixClient(); + + const spacePowerLevels = roomsPowerLevels.get(spaceItem.roomId); + + const draggingSpace = + draggingItem?.roomId === spaceItem.roomId && draggingItem.parentId === spaceItem.parentId; + + const { parentId } = spaceItem; + const parentPowerLevels = parentId ? roomsPowerLevels.get(parentId) : undefined; + const parentCreators = parentId ? getRoomCreatorsForRoomId(mx, parentId) : undefined; + const parentPermissions = + parentCreators && + parentPowerLevels && + getRoomPermissionsAPI(parentCreators, parentPowerLevels); + + return ( + + + ) + } + getRoom={getRoom} + after={ + + } + onDragging={onDragging} + data-dragging={draggingSpace} + /> + + ); + } +); diff --git a/src/app/features/lobby/SpaceItem.tsx b/src/app/features/lobby/SpaceItem.tsx index 9ddee738..586f9dcc 100644 --- a/src/app/features/lobby/SpaceItem.tsx +++ b/src/app/features/lobby/SpaceItem.tsx @@ -1,25 +1,5 @@ -import { MouseEventHandler, ReactNode, useCallback, useRef, useState } from 'react'; -import { - Box, - Avatar, - Text, - Chip, - Icon, - Icons, - as, - Badge, - toRem, - Spinner, - PopOut, - Menu, - MenuItem, - RectCords, - config, - IconButton, - TooltipProvider, - Tooltip, -} from 'folds'; -import FocusTrap from 'focus-trap-react'; +import { MouseEventHandler, ReactNode, useCallback, useRef } from 'react'; +import { Box, Avatar, Text, Chip, Icon, Icons, as, Badge, toRem, Spinner } from 'folds'; import classNames from 'classnames'; import { MatrixError, Room, IHierarchyRoom } from '$types/matrix-sdk'; import { HierarchyItem } from '$hooks/useSpaceHierarchy'; @@ -29,14 +9,8 @@ import { nameInitials } from '$utils/common'; import { LocalRoomSummaryLoader } from '$components/RoomSummaryLoader'; import { getRoomAvatarUrl } from '$utils/room'; import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; -import { stopPropagation } from '$utils/keyboard'; import { mxcUrlToHttp } from '$utils/matrix'; import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; -import { useOpenCreateRoomModal } from '$state/hooks/createRoomModal'; -import { useOpenCreateSpaceModal } from '$state/hooks/createSpaceModal'; -import { CreateRoomType } from '$components/create-room/types'; -import { AddExistingModal } from '$features/add-existing'; -import { BetaNoticeBadge } from '$components/BetaNoticeBadge'; import { useDraggableItem } from './DnD'; import * as styleCss from './style.css'; import * as css from './SpaceItem.css'; @@ -60,7 +34,7 @@ type InaccessibleSpaceProfileProps = { roomId: string; suggested?: boolean; }; -function InaccessibleSpaceProfile({ roomId, suggested }: InaccessibleSpaceProfileProps) { +export function InaccessibleSpaceProfile({ roomId, suggested }: InaccessibleSpaceProfileProps) { return ( (); - const openCreateRoomModal = useOpenCreateRoomModal(); - const [addExisting, setAddExisting] = useState(false); +// Keeping this dead code here until I figure out how to add it into the HierarchyItemMenu... +// function AddRoomButton({ item }: { item: HierarchyItem }) { +// const [cords, setCords] = useState(); +// const openCreateRoomModal = useOpenCreateRoomModal(); +// const [addExisting, setAddExisting] = useState(false); - const handleAddRoom: MouseEventHandler = (evt) => { - setCords(evt.currentTarget.getBoundingClientRect()); - }; +// const handleAddRoom: MouseEventHandler = (evt) => { +// setCords(evt.currentTarget.getBoundingClientRect()); +// }; - const handleCreateRoom = (type?: CreateRoomType) => { - openCreateRoomModal(item.roomId, type); - setCords(undefined); - }; +// const handleCreateRoom = (type?: CreateRoomType) => { +// openCreateRoomModal(item.roomId, type); +// setCords(undefined); +// }; - const handleAddExisting = () => { - setAddExisting(true); - setCords(undefined); - }; +// const handleAddExisting = () => { +// setAddExisting(true); +// setCords(undefined); +// }; - return ( - setCords(undefined), - clickOutsideDeactivates: true, - isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', - isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', - escapeDeactivates: stopPropagation, - }} - > - - handleCreateRoom(CreateRoomType.TextRoom)} - > - Chat Room - - handleCreateRoom(CreateRoomType.VoiceRoom)} - after={} - > - Voice Room - - - Existing Room - - - - } - > - {item.parentId === undefined ? ( - } - onClick={handleAddRoom} - aria-pressed={!!cords} - > - Add Room - - ) : ( - - Add Room - - } - > - {(triggerRef) => ( - - - - )} - - )} - {addExisting && ( - setAddExisting(false)} /> - )} - - ); -} +// return ( +// setCords(undefined), +// clickOutsideDeactivates: true, +// isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', +// isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', +// escapeDeactivates: stopPropagation, +// }} +// > +// +// handleCreateRoom(CreateRoomType.TextRoom)} +// > +// Chat Room +// +// handleCreateRoom(CreateRoomType.VoiceRoom)} +// after={} +// > +// Voice Room +// +// +// Existing Room +// +// +// +// } +// > +// {item.parentId === undefined ? ( +// } +// onClick={handleAddRoom} +// aria-pressed={!!cords} +// > +// Add Room +// +// ) : ( +// +// Add Room +// +// } +// > +// {(triggerRef) => ( +// +// +// +// )} +// +// )} +// {addExisting && ( +// setAddExisting(false)} /> +// )} +// +// ); +// } -function AddSpaceButton({ item }: { item: HierarchyItem }) { - const [cords, setCords] = useState(); - const openCreateSpaceModal = useOpenCreateSpaceModal(); - const [addExisting, setAddExisting] = useState(false); +// function AddSpaceButton({ item }: { item: HierarchyItem }) { +// const [cords, setCords] = useState(); +// const openCreateSpaceModal = useOpenCreateSpaceModal(); +// const [addExisting, setAddExisting] = useState(false); - const handleAddSpace: MouseEventHandler = (evt) => { - setCords(evt.currentTarget.getBoundingClientRect()); - }; +// const handleAddSpace: MouseEventHandler = (evt) => { +// setCords(evt.currentTarget.getBoundingClientRect()); +// }; - const handleCreateSpace = () => { - openCreateSpaceModal(item.roomId as any); - setCords(undefined); - }; +// const handleCreateSpace = () => { +// openCreateSpaceModal(item.roomId as any); +// setCords(undefined); +// }; - const handleAddExisting = () => { - setAddExisting(true); - setCords(undefined); - }; - return ( - setCords(undefined), - clickOutsideDeactivates: true, - isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', - isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', - escapeDeactivates: stopPropagation, - }} - > - - - New Space - - - Existing Space - - - - } - > - {item.parentId === undefined ? ( - } - onClick={handleAddSpace} - aria-pressed={!!cords} - > - Add Space - - ) : ( - - Add Space - - } - > - {(triggerRef) => ( - - - - )} - - )} - {addExisting && ( - setAddExisting(false)} /> - )} - - ); -} +// const handleAddExisting = () => { +// setAddExisting(true); +// setCords(undefined); +// }; +// return ( +// setCords(undefined), +// clickOutsideDeactivates: true, +// isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', +// isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', +// escapeDeactivates: stopPropagation, +// }} +// > +// +// +// New Space +// +// +// Existing Space +// +// +// +// } +// > +// {item.parentId === undefined ? ( +// } +// onClick={handleAddSpace} +// aria-pressed={!!cords} +// > +// Add Space +// +// ) : ( +// +// Add Space +// +// } +// > +// {(triggerRef) => ( +// +// +// +// )} +// +// )} +// {addExisting && ( +// setAddExisting(false)} /> +// )} +// +// ); +// } type SpaceItemCardProps = { summary: IHierarchyRoom | undefined; @@ -556,12 +531,6 @@ export const SpaceItemCard = as<'div', SpaceItemCardProps>( )} - {space && canEditChild && ( - - - - - )} {options} {after} diff --git a/src/app/features/lobby/SpaceNavItem.css.ts b/src/app/features/lobby/SpaceNavItem.css.ts new file mode 100644 index 00000000..c9d1b2c7 --- /dev/null +++ b/src/app/features/lobby/SpaceNavItem.css.ts @@ -0,0 +1,32 @@ +import { style } from '@vanilla-extract/css'; +import { config, toRem } from 'folds'; +import { recipe } from '@vanilla-extract/recipes'; + +export const SpaceItemCard = recipe({ + base: { + paddingBottom: config.space.S100, + borderBottom: `${config.borderWidth.B300} solid transparent`, + position: 'relative', + selectors: { + '&[data-dragging=true]': { + opacity: config.opacity.Disabled, + }, + }, + }, +}); +export const HeaderChip = style({ + paddingLeft: config.space.S200, + selectors: { + [`&[data-ui-before="true"]`]: { + paddingLeft: config.space.S100, + }, + }, +}); +export const HeaderChipPlaceholder = style([ + { + borderRadius: config.radii.R400, + paddingLeft: config.space.S100, + paddingRight: config.space.S300, + height: toRem(32), + }, +]); diff --git a/src/app/features/lobby/SpaceNavItem.tsx b/src/app/features/lobby/SpaceNavItem.tsx new file mode 100644 index 00000000..c07ce6cb --- /dev/null +++ b/src/app/features/lobby/SpaceNavItem.tsx @@ -0,0 +1,173 @@ +import { ReactNode, useRef } from 'react'; +import { Avatar, Badge, Box, Chip, Icon, Icons, as, Text } from 'folds'; +import classNames from 'classnames'; +import { IHierarchyRoom, MatrixClient, Room } from '$types/matrix-sdk'; +import { useMatrixClient } from '$hooks/useMatrixClient'; +import { getCanonicalAliasOrRoomId, mxcUrlToHttp } from '$utils/matrix'; +import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; +import { HierarchyItem } from '$hooks/useSpaceHierarchy'; +import { LocalRoomSummaryLoader } from '$components/RoomSummaryLoader'; +import { getRoomAvatarUrl } from '$utils/room'; +import { RoomAvatar } from '$components/room-avatar'; +import { nameInitials } from '$utils/common'; +import { useNavigate } from 'react-router-dom'; +import { getSpaceLobbyPath } from '$pages/pathUtils'; +import { InaccessibleSpaceProfile, UnjoinedSpaceProfile } from './SpaceItem'; +import * as css from './SpaceNavItem.css'; +import { useDraggableItem } from './DnD'; + +type SpaceProfileProps = { + roomId: string; + name: string; + avatarUrl?: string; + suggested?: boolean; + categoryId: string; + mx: MatrixClient; +}; +function SpaceNavProfile({ + roomId, + name, + avatarUrl, + suggested, + categoryId, + mx, +}: SpaceProfileProps) { + const navigate = useNavigate(); + return ( + navigate(getSpaceLobbyPath(getCanonicalAliasOrRoomId(mx, roomId)))} + before={ + + ( + + {nameInitials(name)} + + )} + /> + + } + after={} + > + + + {name} + + {suggested && ( + + Suggested + + )} + + + ); +} + +type SpaceNavItemCardProps = { + summary: IHierarchyRoom | undefined; + item: HierarchyItem; + joined?: boolean; + categoryId: string; + options?: ReactNode; + before?: ReactNode; + after?: ReactNode; + canReorder: boolean; + getRoom: (roomId: string) => Room | undefined; + onDragging: (item?: HierarchyItem) => void; +}; +export const SpaceNavItemCard = as<'div', SpaceNavItemCardProps>( + ( + { + className, + summary, + joined, + categoryId, + item, + options, + before, + after, + canReorder, + onDragging, + getRoom, + ...props + }, + ref + ) => { + const mx = useMatrixClient(); + const useAuthentication = useMediaAuthentication(); + const { roomId, content } = item; + // const spaceNav = getRoom(roomId); + const targetRef = useRef(null); + useDraggableItem(item, targetRef, onDragging); + const space = getRoom(item.roomId); + + return ( + + {before} + + + {space ? ( + + {(localSummary) => ( + + )} + + ) : ( + <> + {!summary && ( + + )} + {summary && ( + + )} + + )} + + + {options} + {after} + + ); + } +); diff --git a/src/app/hooks/useSpaceHierarchy.ts b/src/app/hooks/useSpaceHierarchy.ts index 3b6fd5aa..b5c08875 100644 --- a/src/app/hooks/useSpaceHierarchy.ts +++ b/src/app/hooks/useSpaceHierarchy.ts @@ -114,12 +114,13 @@ const getSpaceHierarchy = ( rootSpaceId: string, spaceRooms: Set, getRoom: (roomId: string) => Room | undefined, + excludeRoom: (parentId: string, roomId: string, depth: number) => boolean, closedCategory: (spaceId: string) => boolean ): SpaceHierarchy[] => { const spaceItems: HierarchyItemSpace[] = getHierarchySpaces( rootSpaceId, getRoom, - () => false, + excludeRoom, spaceRooms ); @@ -161,19 +162,20 @@ export const useSpaceHierarchy = ( spaceId: string, spaceRooms: Set, getRoom: (roomId: string) => Room | undefined, + excludeRoom: (parentId: string, roomId: string, depth: number) => boolean, closedCategory: (spaceId: string) => boolean ): SpaceHierarchy[] => { const mx = useMatrixClient(); const roomToParents = useAtomValue(roomToParentsAtom); const [hierarchyAtom] = useState(() => - atom(getSpaceHierarchy(spaceId, spaceRooms, getRoom, closedCategory)) + atom(getSpaceHierarchy(spaceId, spaceRooms, getRoom, excludeRoom, closedCategory)) ); const [hierarchy, setHierarchy] = useAtom(hierarchyAtom); useEffect(() => { - setHierarchy(getSpaceHierarchy(spaceId, spaceRooms, getRoom, closedCategory)); - }, [mx, spaceId, spaceRooms, setHierarchy, getRoom, closedCategory]); + setHierarchy(getSpaceHierarchy(spaceId, spaceRooms, getRoom, excludeRoom, closedCategory)); + }, [mx, spaceId, spaceRooms, setHierarchy, getRoom, closedCategory, excludeRoom]); useStateEventCallback( mx, @@ -184,10 +186,12 @@ export const useSpaceHierarchy = ( if (!eventRoomId) return; if (spaceId === eventRoomId || getAllParents(roomToParents, eventRoomId).has(spaceId)) { - setHierarchy(getSpaceHierarchy(spaceId, spaceRooms, getRoom, closedCategory)); + setHierarchy( + getSpaceHierarchy(spaceId, spaceRooms, getRoom, excludeRoom, closedCategory) + ); } }, - [spaceId, roomToParents, setHierarchy, spaceRooms, getRoom, closedCategory] + [spaceId, roomToParents, setHierarchy, spaceRooms, getRoom, closedCategory, excludeRoom] ) );