1- import { Tool , ToolsApiResponse } from "./types" ;
1+ import { ToolsApiResponse } from "./types" ;
22import { fetchTools } from "./utils" ;
33
4- interface AnalyticsData {
5- toolName : string ;
6- repo : { owner : string ; repo : string } ;
7- timestamp : number ;
8- }
9-
104class BackgroundService {
11- private analytics : AnalyticsData [ ] = [ ] ;
12-
135 constructor ( ) {
146 this . init ( ) ;
157 }
168
179 private init ( ) : void {
18- // Load stored analytics
19- // this.loadAnalytics();
2010
2111 // Listen for messages from content script
2212 chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
@@ -35,38 +25,12 @@ class BackgroundService {
3525 } ) ;
3626 }
3727
38- // private async loadAnalytics(): Promise<void> {
39- // try {
40- // const result = await chrome.storage.local.get(["analytics"]);
41- // this.analytics = result.analytics || [];
42- // } catch (error) {
43- // console.error("Failed to load analytics:", error);
44- // }
45- // }
46-
47- // private async saveAnalytics(): Promise<void> {
48- // try {
49- // await chrome.storage.local.set({analytics: this.analytics});
50- // } catch (error) {
51- // console.error("Failed to save analytics:", error);
52- // }
53- // }
54-
5528 private handleMessage (
5629 message : any ,
5730 sender : chrome . runtime . MessageSender ,
5831 sendResponse : ( response ?: any ) => void
5932 ) : void {
6033 switch ( message . type ) {
61- case "TRACK_TOOL_USAGE" :
62- this . trackToolUsage ( message . data ) ;
63- sendResponse ( { success : true } ) ;
64- break ;
65-
66- case "GET_ANALYTICS" :
67- sendResponse ( { analytics : this . analytics } ) ;
68- break ;
69-
7034 case "FETCH_TOOLS" :
7135 this . handleFetchTools ( {
7236 owner : message . data . owner ,
@@ -81,58 +45,11 @@ class BackgroundService {
8145 ) ;
8246 break ;
8347
84- case "GET_REPO_INFO" :
85- this . getRepositoryInfo ( message . data . owner , message . data . repo )
86- . then ( ( info ) => sendResponse ( { success : true , data : info } ) )
87- . catch ( ( error ) =>
88- sendResponse ( { success : false , error : error . message } )
89- ) ;
90- break ;
91-
9248 default :
9349 sendResponse ( { success : false , error : "Unknown message type" } ) ;
9450 }
9551 }
9652
97- private trackToolUsage ( data : {
98- toolName : string ;
99- repo : { owner : string ; repo : string } ;
100- } ) : void {
101- const analyticsEntry : AnalyticsData = {
102- toolName : data . toolName ,
103- repo : data . repo ,
104- timestamp : Date . now ( ) ,
105- } ;
106-
107- this . analytics . push ( analyticsEntry ) ;
108-
109- // Keep only last 1000 entries
110- if ( this . analytics . length > 1000 ) {
111- this . analytics = this . analytics . slice ( - 1000 ) ;
112- }
113-
114- // this.saveAnalytics();
115- }
116-
117- private async getRepositoryInfo ( owner : string , repo : string ) : Promise < any > {
118- try {
119- // In a real implementation, you would use GitHub API with proper authentication
120- // This is a mock implementation
121- return {
122- owner,
123- repo,
124- stars : Math . floor ( Math . random ( ) * 10000 ) ,
125- forks : Math . floor ( Math . random ( ) * 1000 ) ,
126- issues : Math . floor ( Math . random ( ) * 100 ) ,
127- pullRequests : Math . floor ( Math . random ( ) * 50 ) ,
128- language : "JavaScript" ,
129- lastUpdated : new Date ( ) . toISOString ( ) ,
130- } ;
131- } catch ( error ) {
132- throw new Error ( `Failed to fetch repository info: ${ error } ` ) ;
133- }
134- }
135-
13653 private async handleFetchTools ( data : {
13754 owner : string ;
13855 repo : string ;
@@ -164,41 +81,6 @@ class BackgroundService {
16481 // url: chrome.runtime.getURL("welcome.html"),
16582 // });
16683 }
167-
168- public getPopularTools ( ) : { name : string ; count : number } [ ] {
169- const toolCounts = new Map < string , number > ( ) ;
170-
171- this . analytics . forEach ( ( entry ) => {
172- const current = toolCounts . get ( entry . toolName ) || 0 ;
173- toolCounts . set ( entry . toolName , current + 1 ) ;
174- } ) ;
175-
176- return Array . from ( toolCounts . entries ( ) )
177- . map ( ( [ name , count ] ) => ( { name, count } ) )
178- . sort ( ( a , b ) => b . count - a . count )
179- . slice ( 0 , 5 ) ;
180- }
181-
182- public getRecentRepositories ( ) : {
183- owner : string ;
184- repo : string ;
185- lastUsed : number ;
186- } [ ] {
187- const repoMap = new Map < string , number > ( ) ;
188-
189- this . analytics . forEach ( ( entry ) => {
190- const key = `${ entry . repo . owner } /${ entry . repo . repo } ` ;
191- repoMap . set ( key , Math . max ( repoMap . get ( key ) || 0 , entry . timestamp ) ) ;
192- } ) ;
193-
194- return Array . from ( repoMap . entries ( ) )
195- . map ( ( [ repo , lastUsed ] ) => {
196- const [ owner , repoName ] = repo . split ( "/" ) ;
197- return { owner, repo : repoName , lastUsed } ;
198- } )
199- . sort ( ( a , b ) => b . lastUsed - a . lastUsed )
200- . slice ( 0 , 10 ) ;
201- }
20284}
20385
20486// Initialize background service
0 commit comments