@@ -98,6 +98,20 @@ const buildPRDict = (pullRequests: Array<PullRequest>): PullRequestDict => {
9898 ) ;
9999} ;
100100
101+ /** Builds CSM nodes from commit nodes with PR integration. */
102+ const buildCSMNodesWithPR = (
103+ commitNodes : CommitNode [ ] ,
104+ commitDict : CommitDict ,
105+ stemDict : StemDict ,
106+ prDict : PullRequestDict
107+ ) : CSMNode [ ] => {
108+ return commitNodes . map ( ( commitNode ) => {
109+ const csmNode = buildCSMNode ( commitNode , commitDict , stemDict ) ;
110+ const pr = prDict . get ( csmNode . base . commit . id ) ;
111+ return pr ? buildCSMNodeWithPullRequest ( csmNode , pr ) : csmNode ;
112+ } ) ;
113+ } ;
114+
101115/**
102116 * Builds a CSM (Commit Summary Model) dictionary.
103117 * Creates CSM nodes for each commit in the base branch,
@@ -128,22 +142,18 @@ export const buildCSMDict = (
128142 // return {};
129143 }
130144
131- const prDictByMergedCommitSha = buildPRDict ( pullRequests ) ;
145+ const prDict = buildPRDict ( pullRequests ) ;
146+ const csmNodes = buildCSMNodesWithPR ( masterStem . nodes , commitDict , stemDict , prDict ) ;
132147
133- const csmDict : CSMDictionary = { } ;
134- const stemNodes = masterStem . nodes ; // start on latest-node
135- csmDict [ baseBranchName ] = stemNodes . map ( ( commitNode ) => {
136- const csmNode = buildCSMNode ( commitNode , commitDict , stemDict ) ;
137- const pr = prDictByMergedCommitSha . get ( csmNode . base . commit . id ) ;
138- return pr ? buildCSMNodeWithPullRequest ( csmNode , pr ) : csmNode ;
139- } ) ;
140-
141- return csmDict ;
148+ return {
149+ [ baseBranchName ] : csmNodes ,
150+ } ;
142151} ;
143152
144153/**
145154 * Builds a paginated CSM dictionary.
146- * Creates CSM nodes for a specific range of commits in the base branch.
155+ * Creates CSM nodes for a specific range of commits in the base branch,
156+ * enabling efficient lazy loading for large repositories.
147157 */
148158export const buildPaginatedCSMDict = (
149159 commitDict : CommitDict ,
@@ -158,38 +168,36 @@ export const buildPaginatedCSMDict = (
158168 throw new Error ( "perPage must be greater than 0" ) ;
159169 }
160170
161- // Get master stem
162- const masterStem = stemDict . get ( baseBranchName ) ;
163- if ( ! masterStem ) {
171+ // Validate stemDict
172+ if ( stemDict . size === 0 ) {
173+ throw new Error ( "no stem" ) ;
174+ }
175+
176+ // Get base branch stem
177+ const baseStem = stemDict . get ( baseBranchName ) ;
178+ if ( ! baseStem ) {
164179 throw new Error ( "no master-stem" ) ;
165180 }
166181
167- // Determine start index
182+ // Determine start index based on cursor
168183 let startIndex = 0 ;
169184 if ( lastCommitId ) {
170- const lastCommitIndex = masterStem . nodes . findIndex ( ( node ) => node . commit . id === lastCommitId ) ;
185+ const lastCommitIndex = baseStem . nodes . findIndex ( ( node ) => node . commit . id === lastCommitId ) ;
171186 if ( lastCommitIndex === - 1 ) {
172187 throw new Error ( "Invalid lastCommitId" ) ;
173188 }
174189 startIndex = lastCommitIndex + 1 ;
175190 }
176191
177- // Calculate end index
178- const endIndex = Math . min ( startIndex + perPage , masterStem . nodes . length ) ;
179-
180- // Extract nodes for current page
181- const pageNodes = masterStem . nodes . slice ( startIndex , endIndex ) ;
192+ // Calculate end index and extract page nodes
193+ const endIndex = Math . min ( startIndex + perPage , baseStem . nodes . length ) ;
194+ const pageNodes = baseStem . nodes . slice ( startIndex , endIndex ) ;
182195
183- // Build PR dictionary
184- const prDictByMergedCommitSha = buildPRDict ( pullRequests ) ;
196+ // Build CSM nodes with PR integration
197+ const prDict = buildPRDict ( pullRequests ) ;
198+ const csmNodes = buildCSMNodesWithPR ( pageNodes , commitDict , stemDict , prDict ) ;
185199
186- // Build CSM nodes for the page
187- const csmDict : CSMDictionary = { } ;
188- csmDict [ baseBranchName ] = pageNodes . map ( ( commitNode ) => {
189- const csmNode = buildCSMNode ( commitNode , commitDict , stemDict ) ;
190- const pr = prDictByMergedCommitSha . get ( csmNode . base . commit . id ) ;
191- return pr ? buildCSMNodeWithPullRequest ( csmNode , pr ) : csmNode ;
192- } ) ;
193-
194- return csmDict ;
200+ return {
201+ [ baseBranchName ] : csmNodes ,
202+ } ;
195203} ;
0 commit comments