import * as React from 'react';
import { map } from 'rxjs/operators';
import { gql, queryGraphQL } from '../../backend/graphql';
import { FilteredConnection } from '../../components/FilteredConnection';
import { PageTitle } from '../../components/PageTitle';
import { eventLogger } from '../../tracking/eventLogger';
import { createAggregateError } from '../../util/errors';
import { RepoHeaderBreadcrumbNavItem } from '../RepoHeaderBreadcrumbNavItem';
import { RepoHeaderContributionPortal } from '../RepoHeaderContributionPortal';
import { GitCommitNode } from './GitCommitNode';
export const gitCommitFragment = gql `
    fragment GitCommitFields on GitCommit {
        id
        oid
        abbreviatedOID
        message
        subject
        body
        author {
            ...SignatureFields
        }
        committer {
            ...SignatureFields
        }
        parents {
            oid
            abbreviatedOID
        }
        url
        canonicalURL
        externalURLs {
            url
            serviceType
        }
        tree(path: "") {
            canonicalURL
        }
    }

    fragment SignatureFields on Signature {
        person {
            avatarURL
            name
            email
            displayName
        }
        date
    }
`;
const fetchGitCommits = (args) => queryGraphQL(gql `
            query RepositoryGitCommits($repo: ID!, $revspec: String!, $first: Int, $query: String) {
                node(id: $repo) {
                    ... on Repository {
                        commit(rev: $revspec) {
                            ancestors(first: $first, query: $query) {
                                nodes {
                                    ...GitCommitFields
                                }
                                pageInfo {
                                    hasNextPage
                                }
                            }
                        }
                    }
                }
            }
            ${gitCommitFragment}
        `, args).pipe(map(({ data, errors }) => {
    if (!data || !data.node) {
        throw createAggregateError(errors);
    }
    const repo = data.node;
    if (!repo.commit || !repo.commit.ancestors) {
        throw createAggregateError(errors);
    }
    return repo.commit.ancestors;
}));
/** A page that shows a repository's commits at the current revision. */
export class RepositoryCommitsPage extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.queryCommits = (args) => fetchGitCommits(Object.assign({}, args, { repo: this.props.repo.id, revspec: this.props.commitID }));
    }
    componentDidMount() {
        eventLogger.logViewEvent('RepositoryCommits');
    }
    render() {
        return (React.createElement("div", { className: "repository-commits-page" },
            React.createElement(PageTitle, { title: "Commits" }),
            React.createElement(RepoHeaderContributionPortal, { position: "nav", element: React.createElement(RepoHeaderBreadcrumbNavItem, { key: "commits" }, "Commits"), repoHeaderContributionsLifecycleProps: this.props.repoHeaderContributionsLifecycleProps }),
            React.createElement(FilteredConnection, { className: "repository-commits-page__content", listClassName: "list-group list-group-flush", noun: "commit", pluralNoun: "commits", queryConnection: this.queryCommits, nodeComponent: GitCommitNode, nodeComponentProps: { repoName: this.props.repo.name, className: 'list-group-item' }, defaultFirst: 20, autoFocus: true, history: this.props.history, hideSearch: true, location: this.props.location })));
    }
}
//# sourceMappingURL=RepositoryCommitsPage.js.map