11import { useEffect , useState } from "react" ;
2+ import { ComponentStatus } from "@components/component-card/ComponentCard" ;
3+ import { toSentenceCase , fetchAllIssueCounts } from "../../utils" ;
24import {
35 GoabTable ,
46 GoabTableSortHeader ,
@@ -461,7 +463,7 @@ const AllComponents = () => {
461463 ] ;
462464
463465 return initialCards . sort ( ( a , b ) => {
464- const statusOrder = [ "Published" , "In Progress" , "Not Published" ] ;
466+ const statusOrder : ComponentStatus [ ] = [ "Published" , "In Progress" , "Not Published" ] ;
465467 const statusComparison = statusOrder . indexOf ( a . status ) - statusOrder . indexOf ( b . status ) ;
466468 if ( statusComparison !== 0 ) return statusComparison ;
467469 return a . name . localeCompare ( b . name ) ;
@@ -486,7 +488,7 @@ const AllComponents = () => {
486488 const newDirection = sortDirection [ sortBy ] === 1 ? - 1 : 1 ;
487489 const sorted = [ ...cards ] . sort ( ( a , b ) => {
488490 if ( sortBy === "status" ) {
489- const statusOrder = [ "Published" , "In Progress" , "Not Published" ] ;
491+ const statusOrder : ComponentStatus [ ] = [ "Published" , "In Progress" , "Not Published" ] ;
490492 const statusComparison = statusOrder . indexOf ( a . status ) - statusOrder . indexOf ( b . status ) ;
491493 if ( statusComparison !== 0 ) return statusComparison * newDirection ;
492494 }
@@ -516,78 +518,20 @@ const AllComponents = () => {
516518 setSortDirection ( { [ sortBy ] : newDirection } ) ;
517519 } ;
518520
519- // Helper to convert to sentence case
520- const toSentenceCase = ( str : string ) => {
521- return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) . toLowerCase ( ) ;
522- } ;
523-
524521 // Helper to format the label query for REST URLs
525522 const getLabelQuery = ( name : string ) => {
526523 const formatted = toSentenceCase ( name ) ;
527524 return formatted . includes ( " " ) ? `"${ formatted } "` : formatted ;
528525 } ;
529526
530527 // Helper to format the label query for GraphQL (escaping inner quotes)
531- const getGraphQLLabelQuery = ( name : string ) => {
532- const formatted = toSentenceCase ( name ) ;
533- return formatted . includes ( " " ) ? `\\"${ formatted } \\"` : formatted ;
534- } ;
535-
536528 useEffect ( ( ) => {
537- const fetchIssueCountsGraphQL = async ( ) => {
538- const token =
539- import . meta. env . VITE_GITHUB_TOKEN ||
540- import . meta. env . VITE_GITHUB_TOKEN_ALTERNATE ;
541- if ( ! token ) {
542- console . error ( "GitHub token not provided" ) ;
543- return ;
544- }
545-
546- // Build a single GraphQL query for all cards
547- const queryFields = cards . map ( ( card ) => {
548- const alias = card . name . replace ( / \s + / g, "" ) . toLowerCase ( ) ;
549- const labelQuery = getGraphQLLabelQuery ( card . name ) ;
550- return `${ alias } : search(query: "is:issue is:open repo:GovAlta/ui-components label:${ labelQuery } ", type: ISSUE, first: 1) { issueCount }` ;
551- } ) . join ( "\n" ) ;
552-
553- const graphQLQuery = `
554- query {
555- ${ queryFields }
556- }
557- ` ;
558-
559- try {
560- const response = await fetch ( "https://api.github.com/graphql" , {
561- method : "POST" ,
562- headers : {
563- "Content-Type" : "application/json" ,
564- Authorization : `Bearer ${ token } ` ,
565- } ,
566- body : JSON . stringify ( { query : graphQLQuery } ) ,
567- } ) ;
568-
569- const result = await response . json ( ) ;
570- if ( result . errors ) {
571- console . error ( "GraphQL errors:" , result . errors ) ;
572- return ;
573- }
574-
575- const newIssueCounts : Record < string , number > = { } ;
576- cards . forEach ( ( card ) => {
577- const alias = card . name . replace ( / \s + / g, "" ) . toLowerCase ( ) ;
578- newIssueCounts [ card . name ] =
579- result . data [ alias ] && result . data [ alias ] . issueCount
580- ? result . data [ alias ] . issueCount
581- : 0 ;
582- } ) ;
583-
584- setIssueCounts ( newIssueCounts ) ;
585- } catch ( error ) {
586- console . error ( "Error fetching issue counts:" , error ) ;
587- }
529+ const loadIssueCounts = async ( ) => {
530+ const issueCounts = await fetchAllIssueCounts ( cards ) ;
531+ setIssueCounts ( issueCounts ) ;
588532 } ;
589533
590- fetchIssueCountsGraphQL ( ) ;
534+ loadIssueCounts ( ) ;
591535 } , [ cards ] ) ;
592536
593537 const renderTable = ( ) => (
0 commit comments