/* Copyright 2024 New Vector Ltd. Copyright 2021 The Matrix.org Foundation C.I.C. SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only Please see LICENSE files in the repository root for full details. */ import React, { useEffect, useMemo, useState } from "react"; import { Room } from "matrix-js-sdk/src/matrix"; 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("common|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;