public getBranchList()

in packages/dag-history-component/src/components/BranchListContainer/index.tsx [31:129]


	public getBranchList(historyGraph: DagGraph<any>, commitPath: string[]) {
		const { branches, currentBranch, currentStateId } = historyGraph
		const { pinnedStateId: pinnedState, onRenameBranch } = this.props
		const pinnedStateBranch = historyGraph.branchOf(pinnedState)

		// Determine what branches are on the commit path
		const branchPaths: { [key: string]: { start: number; end: number } } = {}
		const branchPath = commitPath.map(commit => historyGraph.branchOf(commit))
		branchPath.forEach((branch, index) => {
			if (branchPaths[branch]) {
				branchPaths[branch].end = index
			} else {
				branchPaths[branch] = { start: index, end: index }
			}
		})

		// This is a hash of branchId -> stateId
		const selectedSuccessorsByBranch: { [key: string]: StateId } = {}
		if (pinnedState !== undefined) {
			historyGraph.childrenOf(pinnedState).forEach(child => {
				const branch = historyGraph.branchOf(child)
				selectedSuccessorsByBranch[branch] = child
			})
		}

		const getSuccessorDepth = (branch: BranchId) => {
			const successorId = selectedSuccessorsByBranch[branch]
			return successorId ? historyGraph.depthIndexOf(branch, successorId) : null
		}

		const getPinnedStateDepth = (branch: BranchId) => {
			if (pinnedState !== undefined || pinnedStateBranch !== branch) {
				return null
			}
			return historyGraph.depthIndexOf(branch, pinnedState)
		}

		const activeStateBranch = historyGraph.branchOf(currentStateId)
		const activeStateIndex = historyGraph.depthIndexOf(
			activeStateBranch,
			currentStateId,
		)

		let maxDepth = 0
		const branchData: {
			[key: string]: { startsAt: number; endsAt: number; length: number }
		} = {}
		branches.forEach(branch => {
			const startsAt = historyGraph.branchStartDepth(branch)
			const endsAt = historyGraph.branchEndDepth(branch)
			const length = endsAt - startsAt
			maxDepth = Math.max(maxDepth, length)
			branchData[branch] = {
				startsAt,
				endsAt,
				length,
			}
		})

		return branches
			.sort((a, b) => parseInt(b, 10) - parseInt(a, 10))
			.map(branch => {
				const { startsAt, endsAt } = branchData[branch]
				const branchType =
					currentBranch === branch ? BranchType.CURRENT : BranchType.LEGACY
				const label = historyGraph.getBranchName(branch)
				const showActiveStateIndex =
					currentBranch === branch || activeStateBranch === branch

				// Figure out where this branch intersects the commit path
				const myBranchPath = branchPaths[branch]
				const currentBranchStart = myBranchPath ? myBranchPath.start : null
				const currentBranchEnd = myBranchPath ? myBranchPath.end : null
				const successorDepth =
					pinnedState === undefined ? undefined : getSuccessorDepth(branch)
				const pinnedStateIndex = getPinnedStateDepth(branch)

				return {
					id: branch,
					active: currentBranch === branch,
					label,
					activeStateIndex: showActiveStateIndex ? activeStateIndex : null,
					startsAt,
					endsAt,
					maxDepth,
					branchType,
					currentBranchStart,
					currentBranchEnd,
					successorDepth,
					pinnedStateIndex,
					onRename: (name: string) => onRenameBranch({ branch, name }),
				}
			})
			.filter(
				branch =>
					!pinnedState || branch.active || isNumber(branch.successorDepth),
			)
			.reverse()
	}