11import {
2- deriveLogicalProjectKey ,
2+ buildProjectGroups ,
33 derivePhysicalProjectKey ,
44 deriveProjectGroupLabel ,
55} from "@t3tools/client-runtime/state/project-grouping" ;
@@ -37,10 +37,6 @@ export interface HomeProjectScope {
3737 readonly projectRefs : ReadonlyArray < ScopedProjectRef > ;
3838}
3939
40- function getProjectFreshnessTimestamp ( project : EnvironmentProject ) : number {
41- return toSortableTimestamp ( project . updatedAt ) ?? toSortableTimestamp ( project . createdAt ) ?? 0 ;
42- }
43-
4440function getProjectSortTimestamp (
4541 project : EnvironmentProject ,
4642 sortOrder : HomeProjectSortOrder ,
@@ -60,64 +56,19 @@ export function buildHomeProjectScopes(input: {
6056 const projects = input . projects . filter (
6157 ( project ) => input . environmentId === null || project . environmentId === input . environmentId ,
6258 ) ;
63- const projectsByPhysicalKey = new Map < string , EnvironmentProject [ ] > ( ) ;
64- for ( const project of projects ) {
65- const physicalKey = derivePhysicalProjectKey ( project ) ;
66- const existing = projectsByPhysicalKey . get ( physicalKey ) ;
67- if ( existing ) existing . push ( project ) ;
68- else projectsByPhysicalKey . set ( physicalKey , [ project ] ) ;
69- }
70-
71- const winnersByPhysicalKey = new Map <
72- string ,
73- { readonly key : string ; readonly project : EnvironmentProject }
74- > ( ) ;
75- for ( const [ physicalKey , members ] of projectsByPhysicalKey ) {
76- const project = members . reduce ( ( winner , candidate ) => {
77- const freshnessDelta =
78- getProjectFreshnessTimestamp ( candidate ) - getProjectFreshnessTimestamp ( winner ) ;
79- return freshnessDelta > 0 || ( freshnessDelta === 0 && candidate . id > winner . id )
80- ? candidate
81- : winner ;
82- } ) ;
83- const identitySource = members . find ( ( member ) => member . repositoryIdentity !== null ) ?? project ;
84- winnersByPhysicalKey . set ( physicalKey , {
85- key : deriveLogicalProjectKey ( identitySource , { groupingMode : input . projectGroupingMode } ) ,
86- project,
87- } ) ;
88- }
89-
90- const groups = new Map < string , EnvironmentProject [ ] > ( ) ;
91- for ( const { key, project } of winnersByPhysicalKey . values ( ) ) {
92- const existing = groups . get ( key ) ;
93- if ( existing ) existing . push ( project ) ;
94- else groups . set ( key , [ project ] ) ;
95- }
96-
97- const projectRefsByGroup = new Map < string , ScopedProjectRef [ ] > ( ) ;
98- const seenProjectRefs = new Set < string > ( ) ;
99- for ( const project of projects ) {
100- const refKey = scopedProjectKey ( project . environmentId , project . id ) ;
101- if ( seenProjectRefs . has ( refKey ) ) continue ;
102- seenProjectRefs . add ( refKey ) ;
103-
104- const key =
105- winnersByPhysicalKey . get ( derivePhysicalProjectKey ( project ) ) ?. key ??
106- deriveLogicalProjectKey ( project , { groupingMode : input . projectGroupingMode } ) ;
107- const refs = projectRefsByGroup . get ( key ) ;
108- const projectRef = { environmentId : project . environmentId , projectId : project . id } ;
109- if ( refs ) refs . push ( projectRef ) ;
110- else projectRefsByGroup . set ( key , [ projectRef ] ) ;
111- }
112-
113- return Array . from ( groups , ( [ key , projects ] ) => {
114- const representative = projects [ 0 ] ! ;
59+ return buildProjectGroups ( {
60+ projects,
61+ settings : {
62+ sidebarProjectGroupingMode : input . projectGroupingMode ,
63+ sidebarProjectGroupingOverrides : { } ,
64+ } ,
65+ } ) . map ( ( group ) => {
11566 return {
116- key,
117- title : deriveProjectGroupLabel ( { representative , members : projects } ) ,
118- representative,
119- projects,
120- projectRefs : projectRefsByGroup . get ( key ) ?? [ ] ,
67+ key : group . key ,
68+ title : group . label ,
69+ representative : group . representative ,
70+ projects : group . members . map ( ( member ) => member . project ) ,
71+ projectRefs : group . memberProjectRefs ,
12172 } ;
12273 } ) ;
12374}
@@ -264,9 +215,11 @@ export function buildHomeThreadGroups(input: {
264215} ) : ReadonlyArray < HomeThreadGroup > {
265216 const now = input . now ?? Date . now ( ) ;
266217 const groups = new Map < string , MutableHomeThreadGroup > ( ) ;
218+ const groupTitleByKey = new Map < string , string > ( ) ;
267219 const groupKeyByProjectKey = new Map < string , string > ( ) ;
268220
269221 for ( const scope of buildHomeProjectScopes ( input ) ) {
222+ groupTitleByKey . set ( scope . key , scope . title ) ;
270223 groups . set ( scope . key , {
271224 key : scope . key ,
272225 projects : [ ...scope . projects ] ,
@@ -345,7 +298,9 @@ export function buildHomeThreadGroups(input: {
345298 continue ;
346299 }
347300
348- const title = deriveProjectGroupLabel ( { representative, members : group . projects } ) ;
301+ const title =
302+ groupTitleByKey . get ( group . key ) ??
303+ deriveProjectGroupLabel ( { representative, members : group . projects } ) ;
349304 const groupMatches =
350305 query . length === 0 ||
351306 title . toLocaleLowerCase ( ) . includes ( query ) ||
0 commit comments