|
| 1 | +import React from 'react'; |
| 2 | +import {autobind} from 'core-decorators'; |
| 3 | + |
| 4 | +import RemotePrController from './remote-pr-controller'; |
| 5 | +import Repository from '../models/repository'; |
| 6 | +import GithubLoginModel from '../models/github-login-model'; |
| 7 | +import ObserveModel from '../decorators/observe-model'; |
| 8 | +import {RemotePropType} from '../prop-types'; |
| 9 | + |
| 10 | +class RemoteSelector extends React.Component { |
| 11 | + static propTypes = { |
| 12 | + remotes: React.PropTypes.arrayOf(RemotePropType).isRequired, |
| 13 | + currentBranch: React.PropTypes.string.isRequired, |
| 14 | + selectRemote: React.PropTypes.func.isRequired, |
| 15 | + } |
| 16 | + |
| 17 | + render() { |
| 18 | + const {remotes, currentBranch, selectRemote} = this.props; |
| 19 | + return ( |
| 20 | + <div className="github-RemoteSelector"> |
| 21 | + <p> |
| 22 | + This repository has multiple remotes hosted at GitHub.com. |
| 23 | + Select a remote to see pull requests associated |
| 24 | + with the <strong>{currentBranch}</strong> branch. |
| 25 | + </p> |
| 26 | + <ul> |
| 27 | + {remotes.map(remote => ( |
| 28 | + <li key={remote.name}> |
| 29 | + <a href="#" onClick={e => selectRemote(e, remote)}> |
| 30 | + {remote.name} ({remote.info.owner}/{remote.info.name}) |
| 31 | + </a> |
| 32 | + </li> |
| 33 | + ))} |
| 34 | + </ul> |
| 35 | + </div> |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +@ObserveModel({ |
| 42 | + getModel: props => props.repository, |
| 43 | + fetchData: async repo => { |
| 44 | + let remotes = await repo.getRemotes(); |
| 45 | + const currentBranch = await repo.getCurrentBranch(); |
| 46 | + const selectedRemote = await repo.getConfig('atomGithub.currentRemote'); |
| 47 | + remotes = remotes.map(({name, url}) => ({name, url, info: Repository.githubInfoFromRemote(url)})) |
| 48 | + .filter(remote => remote.info.githubRepo); |
| 49 | + return {remotes, currentBranch, selectedRemote}; |
| 50 | + }, |
| 51 | +}) |
| 52 | +export default class GithubController extends React.Component { |
| 53 | + static propTypes = { |
| 54 | + repository: React.PropTypes.object, |
| 55 | + remotes: React.PropTypes.arrayOf(RemotePropType.isRequired), |
| 56 | + currentBranch: React.PropTypes.string, |
| 57 | + selectedRemote: React.PropTypes.string, |
| 58 | + } |
| 59 | + |
| 60 | + static defaultProps = { |
| 61 | + remotes: null, |
| 62 | + currentBranch: '', |
| 63 | + selectedRemote: null, |
| 64 | + } |
| 65 | + |
| 66 | + constructor(props, context) { |
| 67 | + super(props, context); |
| 68 | + this.loginModel = new GithubLoginModel(); |
| 69 | + } |
| 70 | + |
| 71 | + render() { |
| 72 | + if (!this.props.repository || !this.props.remotes) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + let remote = this.props.remotes.find(r => r.name === this.props.selectedRemote); |
| 77 | + let remotesAvailable = false; |
| 78 | + if (!remote && this.props.remotes.length === 1) { |
| 79 | + remote = this.props.remotes[0]; |
| 80 | + } else if (!remote && this.props.remotes.length > 1) { |
| 81 | + remotesAvailable = true; |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <div className="github-GithubController"> |
| 86 | + <div className="github-GithubController-content"> |
| 87 | + {/* only supporting GH.com for now, hardcoded values */} |
| 88 | + {remote && |
| 89 | + <RemotePrController |
| 90 | + instance="github.com" |
| 91 | + endpoint="https://api.github.com/" |
| 92 | + loginModel={this.loginModel} |
| 93 | + remote={remote} |
| 94 | + currentBranch={this.props.currentBranch} |
| 95 | + /> |
| 96 | + } |
| 97 | + {!remote && remotesAvailable && |
| 98 | + <RemoteSelector |
| 99 | + remotes={this.props.remotes} |
| 100 | + currentBranch={this.props.currentBranch} |
| 101 | + selectRemote={this.handleRemoteSelect} |
| 102 | + /> |
| 103 | + } |
| 104 | + {!remote && !remotesAvailable && this.renderNoRemotes()} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + renderNoRemotes() { |
| 111 | + return ( |
| 112 | + <div className="github-GithubController-no-remotes"> |
| 113 | + This repository does not have any remotes hosted at GitHub.com. |
| 114 | + </div> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + componentWillUnmount() { |
| 119 | + this.loginModel.destroy(); |
| 120 | + } |
| 121 | + |
| 122 | + @autobind |
| 123 | + handleRemoteSelect(e, remote) { |
| 124 | + e.preventDefault(); |
| 125 | + this.props.repository.setConfig('atomGithub.currentRemote', remote.name); |
| 126 | + } |
| 127 | +} |
0 commit comments