@@ -10,6 +10,7 @@ import type {
1010import type { Notification } from '../../typesGitHub' ;
1111import { listNotificationsForAuthenticatedUser } from '../api/client' ;
1212import { determineFailureType } from '../api/errors' ;
13+ import { composeMergedQuery , extractFragments } from '../api/graphql/utils' ;
1314import { getHeaders } from '../api/request' ;
1415import { getGitHubGraphQLUrl , getNumberFromUrl } from '../api/utils' ;
1516import { rendererLogError , rendererLogWarn } from '../logger' ;
@@ -147,19 +148,10 @@ export async function enrichNotifications(
147148 } > = [ ] ;
148149
149150 const collectFragments = ( doc : string ) => {
150- const fragmentRegex =
151- / f r a g m e n t \s + [ A - Z a - z 0 - 9 _ ] + \s + o n [ \s \S ] * ?(? = (?: f r a g m e n t \s + [ A - Z a - z 0 - 9 _ ] + \s + o n ) | $ ) / g;
152- const nameRegex = / f r a g m e n t \s + ( [ A - Z a - z 0 - 9 _ ] + ) \s + o n / ;
153-
154- const matches = doc . match ( fragmentRegex ) ?? [ ] ;
155- for ( const match of matches ) {
156- const nameMatch = match . match ( nameRegex ) ;
157- if ( ! nameMatch ) {
158- continue ;
159- }
160- const name = nameMatch [ 1 ] ;
151+ const found = extractFragments ( doc ) ;
152+ for ( const [ name , frag ] of found . entries ( ) ) {
161153 if ( ! fragments . has ( name ) ) {
162- fragments . set ( name , match . trim ( ) ) ;
154+ fragments . set ( name , frag ) ;
163155 }
164156 }
165157 } ;
@@ -201,9 +193,17 @@ export async function enrichNotifications(
201193 if ( selections . length === 0 ) {
202194 // No handlers with mergeQueryConfig, just enrich individually
203195 return Promise . all (
204- notifications . map ( ( notification ) =>
205- enrichNotification ( notification , settings ) ,
206- ) ,
196+ notifications . map ( async ( notification ) => {
197+ const handler = createNotificationHandler ( notification ) ;
198+ const details = await handler . enrich ( notification , settings ) ;
199+ return {
200+ ...notification ,
201+ subject : {
202+ ...notification . subject ,
203+ ...details ,
204+ } ,
205+ } ;
206+ } ) ,
207207 ) ;
208208 }
209209
@@ -212,14 +212,13 @@ export async function enrichNotifications(
212212 ...Array . from ( extraVariableDefinitions . entries ( ) ) . map (
213213 ( [ name , type ] ) => `$${ name } : ${ type } ` ,
214214 ) ,
215- ] . join ( ', ' ) ;
215+ ] ;
216216
217- const mergedQuery = `query FetchMergedNotifications(${ combinedVariableDefinitions } ) {
218- ${ selections . join ( '\n' ) }
219- }
220-
221- ${ Array . from ( fragments . values ( ) ) . join ( '\n' ) }
222- ` ;
217+ const mergedQuery = composeMergedQuery (
218+ selections ,
219+ fragments ,
220+ combinedVariableDefinitions ,
221+ ) ;
223222
224223 const queryVariables = {
225224 ...variableValues ,
@@ -259,12 +258,14 @@ export async function enrichNotifications(
259258 const enrichedNotifications = await Promise . all (
260259 notifications . map ( async ( notification : Notification ) => {
261260 const target = targets . find ( ( item ) => item . notification === notification ) ;
261+ const handler =
262+ target ?. handler ?? createNotificationHandler ( notification ) ;
263+
264+ let fragment : unknown ;
262265 if ( mergedData && target ) {
263- // Try to find the first defined property in the repoData (pullRequest, issue, discussion, etc.)
264266 const repoData = mergedData [ target . alias ] as
265267 | Record < string , unknown >
266268 | undefined ;
267- let fragment : unknown ;
268269 if ( repoData ) {
269270 for ( const value of Object . values ( repoData ) ) {
270271 if ( value !== undefined ) {
@@ -273,29 +274,14 @@ export async function enrichNotifications(
273274 }
274275 }
275276 }
276- if ( fragment ) {
277- const details = await target . handler . enrich (
278- notification ,
279- settings ,
280- fragment ,
281- ) ;
282- return {
283- ...notification ,
284- subject : {
285- ...notification . subject ,
286- ...details ,
287- } ,
288- } ;
289- }
290277 }
291- // fallback
292- const handler = createNotificationHandler ( notification ) ;
293- const fetchedDetails = await handler . enrich ( notification , settings ) ;
278+
279+ const details = await handler . enrich ( notification , settings , fragment ) ;
294280 return {
295281 ...notification ,
296282 subject : {
297283 ...notification . subject ,
298- ...fetchedDetails ,
284+ ...details ,
299285 } ,
300286 } ;
301287 } ) ,
0 commit comments