import AddIcon from 'mdi-react/AddIcon';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { gql, queryGraphQL } from '../backend/graphql';
import { PageTitle } from '../components/PageTitle';
import { accessTokenFragment, AccessTokenNode, FilteredAccessTokenConnection, } from '../settings/tokens/AccessTokenNode';
import { eventLogger } from '../tracking/eventLogger';
import { userURL } from '../user';
import { createAggregateError } from '../util/errors';
/**
 * Displays a list of all access tokens on the site.
 */
export class SiteAdminTokensPage extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.state = {};
        this.accessTokenUpdates = new Subject();
        this.queryAccessTokens = (args) => queryGraphQL(gql `
                query AccessTokens($first: Int) {
                    site {
                        accessTokens(first: $first) {
                            nodes {
                                ...AccessTokenFields
                            }
                            totalCount
                            pageInfo {
                                hasNextPage
                            }
                        }
                    }
                }
                ${accessTokenFragment}
            `, args).pipe(map(({ data, errors }) => {
            if (!data || !data.site || !data.site.accessTokens || errors) {
                throw createAggregateError(errors);
            }
            return data.site.accessTokens;
        }));
        this.onDidUpdateAccessToken = () => this.accessTokenUpdates.next();
    }
    componentDidMount() {
        eventLogger.logViewEvent('SiteAdminTokens');
    }
    render() {
        const nodeProps = {
            showSubject: true,
            onDidUpdate: this.onDidUpdateAccessToken,
        };
        return (React.createElement("div", { className: "user-settings-tokens-page" },
            React.createElement(PageTitle, { title: "Access tokens - Admin" }),
            React.createElement("div", { className: "d-flex justify-content-between align-items-center" },
                React.createElement("h2", null, "Access tokens"),
                React.createElement(Link, { className: "btn btn-primary ml-2", to: `${userURL(this.props.user.username)}/account/tokens/new` },
                    React.createElement(AddIcon, { className: "icon-inline" }),
                    " Generate access token")),
            React.createElement("p", null, "Tokens may be used to access the Sourcegraph API with the full privileges of the token's creator."),
            React.createElement(FilteredAccessTokenConnection, { listClassName: "list-group list-group-flush", noun: "access token", pluralNoun: "access tokens", queryConnection: this.queryAccessTokens, nodeComponent: AccessTokenNode, nodeComponentProps: nodeProps, updates: this.accessTokenUpdates, hideSearch: true, noSummaryIfAllNodesVisible: true, history: this.props.history, location: this.props.location })));
    }
}
//# sourceMappingURL=SiteAdminTokensPage.js.map