@@ -46,25 +46,96 @@ export function mergeGeneratedSubscriptions({
4646 defaultEnabled = false ,
4747} ) {
4848 const now = new Date ( ) . toISOString ( ) ;
49- const existingById = new Map ( existingSubscriptions . map ( ( subscription ) => [ subscription . id , subscription ] ) ) ;
49+ const existingById = new Map ( ) ;
50+
51+ for ( const subscription of existingSubscriptions ) {
52+ const currentList = existingById . get ( subscription . id ) || [ ] ;
53+ currentList . push ( subscription ) ;
54+ existingById . set ( subscription . id , currentList ) ;
55+ }
56+
57+ const existingByRoutePath = new Map ( ) ;
58+
59+ for ( const subscription of existingSubscriptions ) {
60+ if ( ! subscription . routePath ) {
61+ continue ;
62+ }
63+
64+ const currentList = existingByRoutePath . get ( subscription . routePath ) || [ ] ;
65+ currentList . push ( subscription ) ;
66+ existingByRoutePath . set ( subscription . routePath , currentList ) ;
67+ }
68+
69+ const generatedRoutePaths = new Set (
70+ generatedSubscriptions . map ( ( subscription ) => normalizeText ( subscription . routePath ) ) . filter ( Boolean ) ,
71+ ) ;
5072 const manualSubscriptions = existingSubscriptions . filter (
51- ( subscription ) => ! subscription . id . startsWith ( `${ generatedIdPrefix } -` ) ,
73+ ( subscription ) =>
74+ ! subscription . id . startsWith ( `${ generatedIdPrefix } -` ) &&
75+ ! generatedRoutePaths . has ( normalizeText ( subscription . routePath ) ) ,
5276 ) ;
5377
78+ function findExistingSubscription ( subscription ) {
79+ const candidateIds = [ subscription . id , ...( subscription . previousIds || [ ] ) ] ;
80+
81+ for ( const candidateId of candidateIds ) {
82+ const candidates = existingById . get ( candidateId ) || [ ] ;
83+ const matchedByRoute = candidates . find ( ( candidate ) => candidate . routePath === subscription . routePath ) ;
84+
85+ if ( matchedByRoute ) {
86+ return matchedByRoute ;
87+ }
88+ }
89+
90+ for ( const candidateId of candidateIds ) {
91+ const candidates = existingById . get ( candidateId ) || [ ] ;
92+ const matchedByName = candidates . find ( ( candidate ) => candidate . name === subscription . name ) ;
93+
94+ if ( matchedByName ) {
95+ return matchedByName ;
96+ }
97+ }
98+
99+ for ( const candidateId of candidateIds ) {
100+ const candidates = existingById . get ( candidateId ) || [ ] ;
101+
102+ if ( candidates [ 0 ] ) {
103+ return candidates [ 0 ] ;
104+ }
105+ }
106+
107+ const routeCandidates = existingByRoutePath . get ( subscription . routePath ) || [ ] ;
108+ const matchedByRouteAndName = routeCandidates . find ( ( candidate ) => candidate . name === subscription . name ) ;
109+
110+ if ( matchedByRouteAndName ) {
111+ return matchedByRouteAndName ;
112+ }
113+
114+ if ( routeCandidates [ 0 ] ) {
115+ return routeCandidates [ 0 ] ;
116+ }
117+
118+ return null ;
119+ }
120+
54121 const mergedGeneratedSubscriptions = generatedSubscriptions . map ( ( subscription ) => {
55- const current = existingById . get ( subscription . id ) ;
122+ const current = findExistingSubscription ( subscription ) ;
56123 const nextCore = {
57124 category : subscription . category ,
125+ categories : subscription . categories ?. length ? subscription . categories : current ?. categories ,
58126 name : subscription . name ,
59127 routePath : subscription . routePath ,
128+ routeTemplate : subscription . routeTemplate ?? current ?. routeTemplate ,
60129 description : subscription . description ,
61130 } ;
62131
63132 const isUnchanged =
64133 current &&
65134 current . category === nextCore . category &&
135+ JSON . stringify ( current . categories || [ ] ) === JSON . stringify ( nextCore . categories || [ ] ) &&
66136 current . name === nextCore . name &&
67137 current . routePath === nextCore . routePath &&
138+ current . routeTemplate === nextCore . routeTemplate &&
68139 current . description === nextCore . description ;
69140
70141 return {
0 commit comments