function getStateList()

in packages/dag-history-component/src/components/StateListContainer/index.tsx [16:96]


function getStateList(
	historyGraph: DagGraph<any>,
	commitPath: StateId[],
	bookmarks: any[],
	pinnedStateId: StateId,
	getSourceFromState: Function,
	chronological: boolean,
) {
	const { currentBranch, currentStateId } = historyGraph

	const activeBranchStartsAt = historyGraph.branchStartDepth(currentBranch)
	const isBookmarked = (id: StateId) =>
		bookmarks.map(b => b.stateId).includes(id)

	//
	// Transform state-ids into data that is used to render the states in the StateList view
	//
	const getStateData = (
		id: StateId,
		index: number,
		isSuccessor?: boolean,
		isCurrentBranchSuccessor?: boolean,
	) => {
		let branchType = index < activeBranchStartsAt ? 'legacy' : 'current'
		if (isSuccessor && !isCurrentBranchSuccessor) {
			branchType = 'unrelated'
		}
		const numChildren = historyGraph.childrenOf(id).length
		const label = historyGraph.stateName(id)
		const state = historyGraph.getState(id)
		const pinned = pinnedStateId === id
		const active = currentStateId === id
		const source = getSourceFromState(state)

		return {
			id,
			state,
			numChildren,
			label,
			source,
			bookmarked: isBookmarked(id),
			successor: isSuccessor,
			showContinuation: !chronological && !isSuccessor,
			pinned,
			active,
			branchType,
			historyGraph,
			getSourceFromState,
		} as any
	}

	//
	// If a state has been "pinned", then want to view it's successors. We will insert them after the pinned state ID, and
	// remove the current branch successor.
	//
	const commitPathData = commitPath.map((id, index) => getStateData(id, index))
	if (pinnedStateId !== undefined) {
		// If the pinned state doesn't have any children, don't bother
		const pinnedStateIndex = commitPath.indexOf(pinnedStateId)
		if (pinnedStateIndex < commitPath.length - 1) {
			// Get data for the children of the pinned state.
			const currentBranchSuccessor: StateId = commitPath[pinnedStateIndex + 1]
			const pinnedChildren = pinnedStateId
				? historyGraph.childrenOf(pinnedStateId)
				: []
			const pinnedChildrenData = pinnedChildren.map((id, index) =>
				getStateData(
					id,
					pinnedStateIndex + 1,
					true,
					currentBranchSuccessor === id,
				),
			)

			// Remove the natural child in the current branch's commit path,
			// since it will be presented as a child of the current state.
			commitPathData.splice(pinnedStateIndex + 1, 1, ...pinnedChildrenData)
		}
	}
	return commitPathData
}