11import Component from " @glimmer/component" ;
22import { tracked } from " @glimmer/tracking" ;
3- import { getOwner } from " @ember/application" ;
43import { action } from " @ember/object" ;
54import { service } from " @ember/service" ;
65import DButton from " discourse/components/d-button" ;
76import { ajax } from " discourse/lib/ajax" ;
87import { popupAjaxError } from " discourse/lib/ajax-error" ;
9- import { i18n } from " discourse-i18n" ;
108
119export default class SimscoreButton extends Component {
1210 @service currentUser;
11+ @service siteSettings;
12+ @service dialog;
1313 @tracked loading = false ;
14-
15- get logger () {
16- return getOwner (this ).lookup (" service:logger" );
17- }
14+ @tracked ideas = [];
1815
1916 @action
2017 async calculateSimScore () {
18+ // eslint-disable-next-line no-console
19+ console .info (" Debug: args received:" , {
20+ args: this .args ,
21+ outletArgs: this .args ? .outletArgs ,
22+ topic: this .args ? .topic ,
23+ });
24+
25+ // eslint-disable-next-line no-console
26+ console .info (" Debug: raw args:" , this .args );
27+
2128 try {
22- this .logger .debug (
23- " SimScore: Starting calculation for topic" ,
24- this .args .topic .id
25- );
26- this .loading = true ;
27- const response = await ajax (` /simscore/analyze/${ this .args .topic .id } ` , {
28- type: " POST" ,
29+ if (! this .args ? .topic ? .id ) {
30+ // eslint-disable-next-line no-console
31+ console .error (" ❌ SimScore: No topic ID found" );
32+ this .dialog .alert ({
33+ message: " Error: Cannot find topic ID" ,
34+ });
35+ return ;
36+ }
37+
38+ const topicId = this .args .topic .id ;
39+ const username = this .currentUser ? .username ;
40+
41+ // eslint-disable-next-line no-console
42+ console .info (" SimScore: Button pressed" , {
43+ topicId,
44+ username,
45+ timestamp: new Date ().toISOString (),
46+ topicTitle: this .args .topic ? .title ,
47+ postCount: this .args .topic ? .posts_count ,
48+ });
49+
50+ // Show a notification to the user
51+ this .dialog .alert ({
52+ message: ` Starting SimScore analysis for topic ${ this .args .topic .title } ...` ,
2953 });
3054
31- this .logger .debug (" SimScore: Received response" , response);
32-
33- if (response .success ) {
34- this .logger .debug (
35- " SimScore: Calculation successful, post created with ID:" ,
36- response .post_id
37- );
38- this .args .showSuccess ? .(i18n (" simscore.success" ));
39- } else {
40- this .logger .error (" SimScore: API returned error:" , response .errors );
41- this .args .showError ? .(i18n (" simscore.error" ));
55+ this .loading = true ;
56+ // eslint-disable-next-line no-console
57+ console .info (" 🔵 SimScore: Analysis started..." );
58+
59+ // Ensure we have at least 4 ideas as per API requirements
60+ // if (this.ideas.length < 4) {
61+ // this.dialog.alert({
62+ // message: "At least 4 ideas are required for analysis",
63+ // });
64+ // this.loading = false;
65+ // return;
66+ // }
67+
68+ const formattedIdeas = [
69+ ... this .ideas .map ((idea , index ) => ({
70+ id: String (index + 1 ),
71+ idea,
72+ })),
73+ {
74+ id: this .ideas .length + 1 ,
75+ idea: " Improve user engagement through gamification" ,
76+ },
77+ {
78+ id: this .ideas .length + 2 ,
79+ idea: " Implement a reward system for active contributors" ,
80+ },
81+ {
82+ id: this .ideas .length + 3 ,
83+ idea: " Add social sharing features" ,
84+ },
85+ {
86+ id: this .ideas .length + 4 ,
87+ idea: " Create a mobile-friendly interface" ,
88+ },
89+ ];
90+
91+ const payload = {
92+ ideas: formattedIdeas,
93+ advanced_features: {
94+ relationship_graph: false ,
95+ cluster_names: false ,
96+ pairwise_similarity_matrix: false ,
97+ },
98+ };
99+
100+ try {
101+ // eslint-disable-next-line no-console
102+ console .info (" 🔵 Debug payload:" , payload);
103+ const apiUrl = " https://simscore-api-dev.fly.dev/v1/rank_ideas" ;
104+ // eslint-disable-next-line no-console
105+ console .info (" 🔵 Making API request to:" , apiUrl);
106+
107+ const response = await ajax (apiUrl, {
108+ type: " POST" ,
109+ contentType: " application/json" ,
110+ dataType: " json" ,
111+ crossDomain: true ,
112+ xhrFields: {
113+ withCredentials: true ,
114+ },
115+ headers: {
116+ Accept: " application/json" ,
117+ " Access-Control-Allow-Origin" : " *" ,
118+ Authorization:
119+ " Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNWNhZWU2MTQtNTEzZS00NTljLTllN2UtNDM0MzYyMGFlMjlhIiwiZW1haWwiOiJtaWtoYWlsb3YuYW50QGdtYWlsLmNvbSIsImlzX2d1ZXN0IjpmYWxzZSwia2V5X2lkIjoiNWQ2YTVhYjEtNjI2Zi00MTFmLWE2ZWYtYTg3NjBjNTVlYTI5IiwidG9rZW5fdHlwZSI6ImFwaV9rZXkifQ.k3_qSLw1NbtT65S1yC1FQbVfMm8sRgL8x4FQnDkB8n0" ,
120+ },
121+ data: JSON .stringify (payload),
122+ });
123+
124+ // eslint-disable-next-line no-console
125+ console .info (" 🟢 API Response:" , response);
126+
127+ if (response .ranked_ideas ) {
128+ const score = response .ranked_ideas [0 ].similarity_score ;
129+ // eslint-disable-next-line no-console
130+ console .info (" ✅ SimScore: Analysis complete!" , score);
131+ this .dialog .alert ({
132+ message: ` SimScore analysis completed! Score: ${ (
133+ score * 100
134+ ).toFixed (1 )} %` ,
135+ });
136+ } else {
137+ throw new Error (" Invalid response format" );
138+ }
139+ } catch (error) {
140+ // eslint-disable-next-line no-console
141+ console .error (" ❌ API Error:" , error);
142+ // eslint-disable-next-line no-console
143+ console .error (" ❌ Error details:" , {
144+ message: error .message ,
145+ status: error .status ,
146+ stack: error .stack ,
147+ });
148+ throw error;
149+ } finally {
150+ // eslint-disable-next-line no-console
151+ console .info (" 🔵 SimScore: Operation completed" );
152+ this .loading = false ;
42153 }
43154 } catch (error) {
44- this .logger .error (" SimScore: Failed to calculate:" , error);
155+ // eslint-disable-next-line no-console
156+ console .error (" ❌ SimScore: Exception:" , error .message , error .stack );
157+ this .dialog .alert ({
158+ message: ` Error: ${ error .message } ` ,
159+ });
45160 popupAjaxError (error);
46- this .args .showError ? .(i18n (" simscore.error" ));
47- } finally {
48- this .loading = false ;
49161 }
50162 }
51163
@@ -54,7 +166,8 @@ export default class SimscoreButton extends Component {
54166 class =" btn-primary simscore-button"
55167 @ action ={{this .calculateSimScore }}
56168 @ disabled ={{this .loading }}
57- @ label =" simscore.calculate"
169+ @ icon =" calculator"
170+ @ translatedLabel =" Calculate SimScore"
58171 />
59172 </template >
60173}
0 commit comments