/* Copyright 2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useEffect, useMemo, useState } from "react"; import { Room } from "matrix-js-sdk/src/models/room"; import { _t } from "../../../languageHandler"; import StyledRadioGroup from "../elements/StyledRadioGroup"; import QueryMatcher from "../../../autocomplete/QueryMatcher"; import SearchBox from "../../structures/SearchBox"; import AutoHideScrollbar from "../../structures/AutoHideScrollbar"; import { Entry } from "../dialogs/AddExistingToSpaceDialog"; import { filterBoolean } from "../../../utils/arrays"; enum Target { All = "All", Specific = "Specific", None = "None", } interface ISpecificChildrenPickerProps { filterPlaceholder: string; rooms: Room[]; selected: Set; onChange(selected: boolean, room: Room): void; } const SpecificChildrenPicker: React.FC = ({ filterPlaceholder, rooms, selected, onChange, }) => { const [query, setQuery] = useState(""); const lcQuery = query.toLowerCase().trim(); const filteredRooms = useMemo(() => { if (!lcQuery) { return rooms; } const matcher = new QueryMatcher(rooms, { keys: ["name"], funcs: [(r) => filterBoolean([r.getCanonicalAlias(), ...r.getAltAliases()])], shouldMatchWordsOnly: false, }); return matcher.match(lcQuery); }, [rooms, lcQuery]); return (
{filteredRooms.map((room) => { return ( { onChange(checked, room); }} /> ); })} {filteredRooms.length < 1 ? ( {_t("No results")} ) : undefined}
); }; interface IProps { space: Room; spaceChildren: Room[]; selected: Set; noneLabel?: string; allLabel: string; specificLabel: string; onChange(rooms: Room[]): void; } const SpaceChildrenPicker: React.FC = ({ space, spaceChildren, selected, onChange, noneLabel, allLabel, specificLabel, }) => { const [state, setState] = useState(noneLabel ? Target.None : Target.All); useEffect(() => { if (state === Target.All) { onChange(spaceChildren); } else { onChange([]); } }, [onChange, state, spaceChildren]); return ( <>
d.label)} />
{state === Target.Specific && ( { if (isSelected) { onChange([room, ...selected]); } else { onChange([...selected].filter((r) => r !== room)); } }} /> )} ); }; export default SpaceChildrenPicker;