/* Copyright 2020 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 from 'react'; import * as sdk from '../../../../index'; import PropTypes from 'prop-types'; import { _t } from '../../../../languageHandler'; import SettingsStore, {SettingLevel} from "../../../../settings/SettingsStore"; import Modal from '../../../../Modal'; import {formatBytes, formatCountLong} from "../../../../utils/FormattingUtils"; import EventIndexPeg from "../../../../indexing/EventIndexPeg"; /* * Allows the user to introspect the event index state and disable it. */ export default class ManageEventIndexDialog extends React.Component { static propTypes = { onFinished: PropTypes.func.isRequired, } constructor(props) { super(props); this.state = { eventIndexSize: 0, eventCount: 0, crawlingRoomsCount: 0, roomCount: 0, currentRoom: null, crawlerSleepTime: SettingsStore.getValueAt(SettingLevel.DEVICE, 'crawlerSleepTime'), }; } async updateCurrentRoom(room) { const eventIndex = EventIndexPeg.get(); const stats = await eventIndex.getStats(); let currentRoom = null; if (room) currentRoom = room.name; const roomStats = eventIndex.crawlingRooms(); const crawlingRoomsCount = roomStats.crawlingRooms.size; const roomCount = roomStats.totalRooms.size; this.setState({ eventIndexSize: stats.size, eventCount: stats.eventCount, crawlingRoomsCount: crawlingRoomsCount, roomCount: roomCount, currentRoom: currentRoom, }); } componentWillUnmount(): void { const eventIndex = EventIndexPeg.get(); if (eventIndex !== null) { eventIndex.removeListener("changedCheckpoint", this.updateCurrentRoom.bind(this)); } } async componentWillMount(): void { let eventIndexSize = 0; let crawlingRoomsCount = 0; let roomCount = 0; let eventCount = 0; let currentRoom = null; const eventIndex = EventIndexPeg.get(); if (eventIndex !== null) { eventIndex.on("changedCheckpoint", this.updateCurrentRoom.bind(this)); const stats = await eventIndex.getStats(); const roomStats = eventIndex.crawlingRooms(); eventIndexSize = stats.size; crawlingRoomsCount = roomStats.crawlingRooms.size; roomCount = roomStats.totalRooms.size; eventCount = stats.eventCount; const room = eventIndex.currentRoom(); if (room) currentRoom = room.name; } this.setState({ eventIndexSize, eventCount, crawlingRoomsCount, roomCount, currentRoom, }); } _onDisable = async () => { Modal.createTrackedDialogAsync("Disable message search", "Disable message search", import("./DisableEventIndexDialog"), null, null, /* priority = */ false, /* static = */ true, ); } _onDone = () => { this.props.onFinished(true); } _onCrawlerSleepTimeChange = (e) => { this.setState({crawlerSleepTime: e.target.value}); SettingsStore.setValue("crawlerSleepTime", null, SettingLevel.DEVICE, e.target.value); } render() { let crawlerState; if (this.state.currentRoom === null) { crawlerState = _t("Not currently downloading messages for any room."); } else { crawlerState = ( _t("Downloading mesages for %(currentRoom)s.", { currentRoom: this.state.currentRoom }) ); } const Field = sdk.getComponent('views.elements.Field'); const eventIndexingSettings = (