import { LoadingSpinner } from '@sourcegraph/react-loading-spinner';
import formatDistance from 'date-fns/formatDistance';
import { upperFirst } from 'lodash';
import CheckIcon from 'mdi-react/CheckIcon';
import CloudDownloadIcon from 'mdi-react/CloudDownloadIcon';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Subscription } from 'rxjs';
import { withLatestFrom } from 'rxjs/operators';
import { PageTitle } from '../components/PageTitle';
import { eventLogger } from '../tracking/eventLogger';
import { fetchSite, fetchSiteUpdateCheck } from './backend';
import { getUpdateChannel } from './configHelpers';
/**
 * A page displaying information about available updates for the server.
 */
export class SiteAdminUpdatesPage extends React.Component {
    constructor() {
        super(...arguments);
        this.state = {};
        this.subscriptions = new Subscription();
    }
    componentDidMount() {
        eventLogger.logViewEvent('SiteAdminUpdates');
        this.subscriptions.add(fetchSite()
            .pipe(withLatestFrom(fetchSiteUpdateCheck()))
            .subscribe(([site, { buildVersion, productVersion, updateCheck }]) => this.setState({
            channel: getUpdateChannel(site.configuration.effectiveContents),
            buildVersion,
            productVersion,
            updateCheck,
            error: undefined,
        }), error => this.setState({ error: error.message })));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        const autoUpdateCheckingEnabled = this.state.channel === 'release';
        return (React.createElement("div", { className: "site-admin-updates-page" },
            React.createElement(PageTitle, { title: "Updates - Admin" }),
            React.createElement("h2", null, "Updates"),
            this.state.error && (React.createElement("p", { className: "site-admin-updates-page__error" },
                "Error: ",
                upperFirst(this.state.error))),
            this.state.updateCheck &&
                (this.state.updateCheck.pending || this.state.updateCheck.checkedAt) && (React.createElement("div", null,
                this.state.updateCheck.pending && (React.createElement("div", { className: "site-admin-updates-page__alert alert alert-primary" },
                    React.createElement(LoadingSpinner, { className: "icon-inline" }),
                    " Checking for updates... (reload in a few seconds)")),
                !this.state.updateCheck.errorMessage &&
                    (this.state.updateCheck.updateVersionAvailable ? (React.createElement("div", { className: "site-admin-updates-page__alert alert alert-success" },
                        React.createElement(CloudDownloadIcon, { className: "icon-inline" }),
                        " Update available:",
                        ' ',
                        React.createElement("a", { href: "https://about.sourcegraph.com" }, this.state.updateCheck.updateVersionAvailable))) : (React.createElement("div", { className: "site-admin-updates-page__alert alert alert-success" },
                        React.createElement(CheckIcon, { className: "icon-inline" }),
                        " Up to date."))),
                this.state.updateCheck.errorMessage && (React.createElement("div", { className: "site-admin-updates-page__alert alert alert-danger" },
                    "Error checking for updates: ",
                    this.state.updateCheck.errorMessage)))),
            !autoUpdateCheckingEnabled && (React.createElement("div", { className: "site-admin-updates-page__alert alert alert-warning" }, "Automatic update checking is disabled.")),
            React.createElement("p", { className: "site-admin-updates_page__info" },
                React.createElement("small", null,
                    React.createElement("strong", null, "Current product version:"),
                    " ",
                    this.state.productVersion,
                    " (",
                    this.state.buildVersion,
                    ")"),
                React.createElement("br", null),
                React.createElement("small", null,
                    React.createElement("strong", null, "Last update check:"),
                    ' ',
                    this.state.updateCheck && this.state.updateCheck.checkedAt
                        ? formatDistance(this.state.updateCheck.checkedAt, new Date(), { addSuffix: true })
                        : 'never',
                    "."),
                React.createElement("br", null),
                React.createElement("small", null,
                    React.createElement("strong", null, "Automatic update checking:"),
                    " ",
                    autoUpdateCheckingEnabled ? 'on' : 'off',
                    ".",
                    ' ',
                    React.createElement(Link, { to: "/site-admin/configuration" }, "Configure"),
                    " ",
                    React.createElement("code", null, "update.channel"),
                    " to",
                    ' ',
                    autoUpdateCheckingEnabled ? 'disable' : 'enable',
                    ".")),
            React.createElement("p", null,
                React.createElement("a", { href: "https://about.sourcegraph.com/changelog", target: "_blank" }, "Changelog"))));
    }
}
//# sourceMappingURL=SiteAdminUpdatesPage.js.map