|
1 | 1 | import React, { useEffect, useState } from "react";
|
2 |
| -import { Alert } from "react-bootstrap"; |
3 | 2 | import { gitActionsContext } from "../../state/context";
|
4 |
| -import { remote } from "../../types"; |
| 3 | +import { branch, remote } from "../../types"; |
5 | 4 | import GitUIButton from "../buttons/gituibutton";
|
6 | 5 | import { gitPluginContext } from "../gitui";
|
7 | 6 | import { LocalBranchDetails } from "./branches/localbranchdetails";
|
8 | 7 | import { RemoteBranchDetails } from "./branches/remotebranchedetails";
|
9 | 8 | import { faSync } from "@fortawesome/free-solid-svg-icons";
|
10 | 9 | import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
11 | 10 |
|
| 11 | +const pageLength = 5; |
| 12 | + |
12 | 13 | export const Branches = () => {
|
13 | 14 | const context = React.useContext(gitPluginContext)
|
14 | 15 | const actions = React.useContext(gitActionsContext)
|
| 16 | + const [localBranchPage, setLocalBranchPage] = useState(1); |
| 17 | + const [remoteBranchPage, setRemoteBranchPage] = useState(1); |
15 | 18 | const [newBranch, setNewBranch] = useState({ value: "" });
|
| 19 | + const [localBranches, setLocalBranches] = useState<branch[]>([]); |
| 20 | + const [remoteBranches, setRemoteBranches] = useState<branch[]>([]); |
| 21 | + const [currentBranch, setCurrentBranch] = useState<branch | null>(null); |
16 | 22 |
|
17 | 23 | const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
18 | 24 | setNewBranch({ value: e.currentTarget.value });
|
19 | 25 | };
|
20 | 26 |
|
| 27 | + useEffect(() => { |
| 28 | + if (context.branches) { |
| 29 | + |
| 30 | + if (context.currentBranch && context.currentBranch.name !== '') { |
| 31 | + setCurrentBranch(context.branches.filter((branch, index) => !branch.remote && branch.name === context.currentBranch.name)[0]); |
| 32 | + setLocalBranches(context.branches.filter((branch, index) => !branch.remote && branch.name !== context.currentBranch.name)); |
| 33 | + } else { |
| 34 | + setLocalBranches(context.branches.filter((branch, index) => !branch.remote)); |
| 35 | + } |
| 36 | + if (context.upstream) { |
| 37 | + setRemoteBranches(context.branches.filter((branch, index) => branch.remote && branch.remote.name === context.upstream.name)); |
| 38 | + } else { |
| 39 | + setRemoteBranches([]); |
| 40 | + } |
| 41 | + } else { |
| 42 | + setLocalBranches([]); |
| 43 | + setRemoteBranches([]); |
| 44 | + } |
| 45 | + }, [context.branches, context.defaultRemote, context.upstream, context.currentBranch]); |
| 46 | + |
21 | 47 | return (
|
22 | 48 | <>
|
23 | 49 | <div data-id='branches-panel-content' className="pt-2">
|
24 | 50 | {context.branches && context.branches.length ?
|
25 | 51 | <div>
|
26 | 52 | <div data-id='branches-panel-content-local-branches'>
|
27 |
| - <label className="text-uppercase">local branches</label> |
28 |
| - {context.branches && context.branches.filter((branch, index) => !branch.remote).map((branch, index) => { |
| 53 | + <label className="text-uppercase">local branches </label><div className="badge badge-info badge-pill ml-2">{localBranches.length}</div> |
| 54 | + {currentBranch && <LocalBranchDetails branch={currentBranch}></LocalBranchDetails>} |
| 55 | + {context.branches && localBranches.slice(0, localBranchPage * pageLength).map((branch, index) => { |
29 | 56 | return (
|
30 | 57 | <LocalBranchDetails key={index} branch={branch}></LocalBranchDetails>
|
31 | 58 | );
|
32 | 59 | })}
|
| 60 | + {context.branches && localBranches.length > localBranchPage * pageLength && <GitUIButton className="btn btn-sm" onClick={() => { |
| 61 | + setLocalBranchPage(localBranchPage + 1) |
| 62 | + }}>Show more</GitUIButton>} |
33 | 63 | </div>
|
34 | 64 | <hr />
|
35 | 65 | {context.upstream ?
|
36 | 66 | <>
|
37 | 67 | <div data-id='branches-panel-content-remote-branches'>
|
38 |
| - <label className="text-uppercase">remote branches on {context.upstream ? context.upstream.name : null}</label> |
39 |
| - {context.branches && context.branches.filter((branch, index) => branch.remote && branch.remote.name === context.upstream.name).map((branch, index) => { |
40 |
| - return ( |
41 |
| - <RemoteBranchDetails allowCheckout={true} key={index} branch={branch}></RemoteBranchDetails> |
42 |
| - ); |
43 |
| - })} |
| 68 | + <label className="text-uppercase">remote branches on {context.upstream ? context.upstream.name : null}</label><div className="badge badge-info badge-pill ml-2">{remoteBranches.length}</div> |
| 69 | + {context.branches && remoteBranches |
| 70 | + .slice(0, remoteBranchPage * pageLength) |
| 71 | + .map((branch, index) => { |
| 72 | + return ( |
| 73 | + <RemoteBranchDetails allowCheckout={true} key={index} branch={branch}></RemoteBranchDetails> |
| 74 | + ); |
| 75 | + })} |
| 76 | + {context.branches && remoteBranches.length > remoteBranchPage * pageLength && <><GitUIButton className="btn btn-sm" onClick={() => { |
| 77 | + setRemoteBranchPage(remoteBranchPage + 1); |
| 78 | + }}>Show more</GitUIButton><br></br></>} |
44 | 79 | <GitUIButton data-id={`remote-sync-${context.upstream.name}`} className="btn btn-sm" onClick={async () => {
|
45 | 80 | await actions.fetch({
|
46 | 81 | remote: context.upstream
|
|
0 commit comments