import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import './SpaceAddExisting.scss'; import { twemojify } from '../../../util/twemojify'; import initMatrix from '../../../client/initMatrix'; import cons from '../../../client/state/cons'; import navigation from '../../../client/state/navigation'; import { joinRuleToIconSrc } from '../../../util/matrixUtil'; import { Debounce } from '../../../util/common'; import Text from '../../atoms/text/Text'; import RawIcon from '../../atoms/system-icons/RawIcon'; import Button from '../../atoms/button/Button'; import IconButton from '../../atoms/button/IconButton'; import Checkbox from '../../atoms/button/Checkbox'; import Input from '../../atoms/input/Input'; import Spinner from '../../atoms/spinner/Spinner'; import RoomSelector from '../room-selector/RoomSelector'; import Dialog from '../dialog/Dialog'; import CrossIC from '../../../../public/res/ic/outlined/cross.svg'; import SearchIC from '../../../../public/res/ic/outlined/search.svg'; function SpaceAddExistingContent({ roomId }) { const [debounce] = useState(new Debounce()); const [process, setProcess] = useState(null); const [selected, setSelected] = useState([]); const [searchIds, setSearchIds] = useState(null); const mx = initMatrix.matrixClient; const { spaces, rooms, directs, roomIdToParents, } = initMatrix.roomList; let allRoomIds = [...spaces, ...rooms, ...directs]; allRoomIds = allRoomIds.filter((rId) => ( rId !== roomId && !roomIdToParents.get(rId)?.has(roomId) )); const toggleSelection = (rId) => { if (process !== null) return; const newSelected = [...selected]; const selectedIndex = newSelected.indexOf(rId); if (selectedIndex > -1) { newSelected.splice(selectedIndex, 1); setSelected(newSelected); return; } newSelected.push(rId); setSelected(newSelected); }; const handleSearch = (ev) => { const term = ev.target.value.toLocaleLowerCase().replaceAll(' ', ''); if (term === '') { setSearchIds(null); return; } debounce._(() => { const searchedIds = allRoomIds.filter((rId) => { let name = mx.getRoom(rId)?.name; if (!name) return false; name = name.normalize('NFKC') .toLocaleLowerCase() .replaceAll(' ', ''); return name.includes(term); }); setSearchIds(searchedIds); }, 400)(); }; const handleAdd = async () => { setProcess(`Adding ${selected.length} items...`); }; return ( <>
{searchIds?.length === 0 &&