aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ServerStats.tsx
blob: 57363663d3df93b6678eccb174c80e19af0b3c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { action, computed, makeObservable, observable, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { MainViewModal } from '../views/MainViewModal';
import './SharingManager.scss';
import { PingManager } from './PingManager';
import { SettingsManager } from './SettingsManager';

@observer
export class ServerStats extends React.Component<{}> {
    // eslint-disable-next-line no-use-before-define
    public static Instance: ServerStats;
    @observable private isOpen = false; // whether the SharingManager modal is open or not

    @observable _stats: { [key: string]: any } | undefined = undefined;
    // private get linkVisible() {
    //     return this.targetDoc ? this.targetDoc['acl_' + PublicKey] !== SharingPermissions.None : false;
    // }

    constructor(props: {}) {
        super(props);
        makeObservable(this);
        ServerStats.Instance = this;
    }

    /**
     * @returns the main interface of the SharingManager.
     */
    @computed get sharingInterface() {
        return (
            <div
                style={{
                    display: 'flex',
                    height: '100%',
                    width: 400,
                    background: SettingsManager.userBackgroundColor,
                    opacity: 0.6,
                }}>
                <div style={{ width: 300, margin: 'auto', display: 'flex', flexDirection: 'column' }}>
                    {PingManager.Instance.IsBeating ? 'The server connection is active' : 'The server connection has been interrupted.NOTE: Any changes made will appear to persist but will be lost after a browser refreshes.'}

                    <br />
                    <span>Active users:{this._stats?.socketMap.length}</span>
                    {this._stats?.socketMap.map((user: any) => <p>{user.username}</p>)}
                </div>
            </div>
        );
    }

    // eslint-disable-next-line react/sort-comp
    public close = action(() => {
        this.isOpen = false;
    });
    public open = async () => {
        /**
         * Populates the list of users.
         */
        fetch('/stats').then((res: Response) =>
            res.text().then(
                action(stats => {
                    this._stats = JSON.parse(stats);
                })
            )
        );

        runInAction(() => {
            this.isOpen = true;
        });
    };

    render() {
        return <MainViewModal contents={this.sharingInterface} isDisplayed={this.isOpen} interactive closeOnExternalClick={this.close} />;
    }
}