11import { IssuesData } from "../components/issues/IssuesList" ;
22import { TIssue , TIssuesPriority , TReference , TVersion } from "../types/redmine" ;
33
4- type GroupedIssues = Record <
4+ type IssueGroup = {
5+ type : "pinned" | "version" | "no-version" ;
6+ version ?: TVersion ;
7+ issues : TIssue [ ] ;
8+ } ;
9+
10+ type GroupedIssues = {
11+ project : TReference ;
12+ versions : TVersion [ ] ;
13+ groups : {
14+ type : "pinned" | "version" | "no-version" ;
15+ version ?: TVersion ;
16+ issues : TIssue [ ] ;
17+ } [ ] ;
18+ } [ ] ;
19+
20+ type GroupedIssuesHelper = Record <
521 number ,
622 {
23+ // The project reference
724 project : TReference ;
25+ // Pinned issues
26+ pinnedIssues : TIssue [ ] ;
27+ // All versions of project
828 versions : Record <
929 number ,
1030 {
31+ // The version reference
1132 version : TVersion ;
12- issues : TIssue [ ] ; // Issues of this version
33+ // Issues of this version
34+ issues : TIssue [ ] ;
35+ // Index for sorting
1336 sort : number ;
1437 }
1538 > ;
16- issues : TIssue [ ] ; // Issues without version
39+ // Issues without version
40+ issues : TIssue [ ] ;
41+ // Index for sorting
1742 sort : number ;
1843 }
1944> ;
@@ -36,16 +61,29 @@ export const getSortedIssues = (issues: TIssue[], issuePriorities: TIssuesPriori
3661 return sortedIssues ;
3762} ;
3863
39- export const getGroupedIssues = ( issues : TIssue [ ] , projectVersions : Record < number , TVersion [ ] > ) => {
40- const grouped = issues . reduce ( ( result : GroupedIssues , issue ) => {
64+ /**
65+ * Group issues
66+ * - group issues by project
67+ * - group pinned issues
68+ * - group issues by version
69+ */
70+ export const getGroupedIssues = ( issues : TIssue [ ] , projectVersions : Record < number , TVersion [ ] > , issuesData : IssuesData ) : GroupedIssues => {
71+ const grouped = issues . reduce ( ( result : GroupedIssuesHelper , issue ) => {
4172 if ( ! ( issue . project . id in result ) ) {
4273 result [ issue . project . id ] = {
4374 project : issue . project ,
75+ pinnedIssues : [ ] ,
4476 versions : { } ,
4577 issues : [ ] ,
4678 sort : Object . keys ( result ) . length ,
4779 } ;
4880 }
81+
82+ if ( issuesData [ issue . id ] ?. pinned ) {
83+ result [ issue . project . id ] . pinnedIssues . push ( issue ) ;
84+ return result ;
85+ }
86+
4987 if ( issue . fixed_version && projectVersions [ issue . project . id ] ) {
5088 if ( ! ( issue . fixed_version . id in result [ issue . project . id ] . versions ) ) {
5189 const version = projectVersions [ issue . project . id ] . find ( ( v ) => v . id === issue . fixed_version ?. id ) ;
@@ -62,6 +100,7 @@ export const getGroupedIssues = (issues: TIssue[], projectVersions: Record<numbe
62100 sort : projectVersions [ issue . project . id ] . indexOf ( version ) ,
63101 } ;
64102 }
103+
65104 result [ issue . project . id ] . versions [ issue . fixed_version . id ] . issues . push ( issue ) ;
66105 } else {
67106 result [ issue . project . id ] . issues . push ( issue ) ;
@@ -71,9 +110,29 @@ export const getGroupedIssues = (issues: TIssue[], projectVersions: Record<numbe
71110 } , { } ) ;
72111
73112 return Object . values ( grouped )
74- . map ( ( groupedByProject ) => ( {
75- ...groupedByProject ,
76- versions : Object . values ( groupedByProject . versions ) . sort ( ( a , b ) => a . sort - b . sort ) ,
77- } ) )
78- . sort ( ( a , b ) => a . sort - b . sort ) ;
113+ . sort ( ( a , b ) => a . sort - b . sort )
114+ . map ( ( { project, pinnedIssues, versions, issues } ) => ( {
115+ project : project ,
116+ versions : projectVersions [ project . id ] ?? [ ] ,
117+ groups : [
118+ {
119+ type : "pinned" ,
120+ issues : pinnedIssues ,
121+ } ,
122+ ...Object . values ( versions )
123+ . sort ( ( a , b ) => a . sort - b . sort )
124+ . map (
125+ ( version ) =>
126+ ( {
127+ type : "version" ,
128+ version : version . version ,
129+ issues : version . issues ,
130+ } ) satisfies IssueGroup
131+ ) ,
132+ {
133+ type : "no-version" ,
134+ issues : issues ,
135+ } ,
136+ ] ,
137+ } ) ) ;
79138} ;
0 commit comments