@@ -36,6 +36,41 @@ export default {
3636
3737 api . addSidebarSection (
3838 ( BaseCustomSidebarSection , BaseCustomSidebarSectionLink ) => {
39+ const AiConversationLink = class extends BaseCustomSidebarSectionLink {
40+ route = "topic.fromParamsNear" ;
41+ prefixType = "icon" ;
42+ prefixValue = "robot" ;
43+
44+ constructor ( topic ) {
45+ super ( ...arguments ) ;
46+ this . topic = topic ;
47+ }
48+
49+ get name ( ) {
50+ return this . topic . title ;
51+ }
52+
53+ get models ( ) {
54+ return [
55+ this . topic . slug ,
56+ this . topic . id ,
57+ this . topic . last_read_post_number || 0 ,
58+ ] ;
59+ }
60+
61+ get title ( ) {
62+ return this . topic . title ;
63+ }
64+
65+ get text ( ) {
66+ return this . topic . title ;
67+ }
68+
69+ get classNames ( ) {
70+ return `ai-conversation-${ this . topic . id } ` ;
71+ }
72+ } ;
73+
3974 return class extends BaseCustomSidebarSection {
4075 @tracked links = new TrackedArray ( ) ;
4176 @tracked topics = [ ] ;
@@ -48,17 +83,38 @@ export default {
4883 super ( ...arguments ) ;
4984 this . fetchMessages ( ) ;
5085
51- appEvents . on ( "topic:created" , ( topic ) => {
52- // when asking a new question
53- this . addNewMessage ( topic ) ;
54- this . watchForTitleUpdate ( topic ) ;
55- } ) ;
86+ appEvents . on ( "topic:created" , this , "addNewMessageToSidebar" ) ;
5687 }
5788
89+ @bind
5890 willDestroy ( ) {
5991 this . removeScrollListener ( ) ;
92+ appEvents . on ( "topic:created" , this , "addNewMessageToSidebar" ) ;
93+ }
94+
95+ get name ( ) {
96+ return "ai-conversations-history" ;
97+ }
98+
99+ get text ( ) {
100+ // TODO: FIX
101+ //return i18n(themePrefix("messages_sidebar.title"));
102+ return "Conversations" ;
103+ }
104+
105+ get sidebarElement ( ) {
106+ return document . querySelector (
107+ ".sidebar-wrapper .sidebar-sections"
108+ ) ;
60109 }
61110
111+ addNewMessageToSidebar ( topic ) {
112+ this . addNewMessage ( topic ) ;
113+ this . watchForTitleUpdate ( topic ) ;
114+ }
115+
116+
117+
62118 @bind
63119 removeScrollListener ( ) {
64120 const sidebar = this . sidebarElement ;
@@ -75,12 +131,6 @@ export default {
75131 }
76132 }
77133
78- get sidebarElement ( ) {
79- return document . querySelector (
80- ".sidebar-wrapper .sidebar-sections"
81- ) ;
82- }
83-
84134 @bind
85135 scrollHandler ( ) {
86136 const sidebarElement = this . sidebarElement ;
@@ -108,7 +158,7 @@ export default {
108158 this . isFetching = true ;
109159
110160 ajax ( "/discourse-ai/ai-bot/conversations.json" , {
111- data : { page : this . page , per_page : 40 } ,
161+ data : { page : this . page , per_page : 20 } ,
112162 } )
113163 . then ( ( data ) => {
114164 if ( isLoadingMore ) {
@@ -139,50 +189,16 @@ export default {
139189 this . fetchMessages ( true ) ;
140190 }
141191
142- addNewMessage ( newTopic ) {
143- // the pm endpoint isn't fast enough include the newly created topic
144- // so this adds the new topic to the existing list
145- const builtTopic =
146- new ( class extends BaseCustomSidebarSectionLink {
147- name = newTopic . title ;
148- route = "topic.fromParamsNear" ;
149- models = [ newTopic . topic_slug , newTopic . topic_id , 0 ] ;
150- title = newTopic . title ;
151- text = newTopic . title ;
152- prefixType = "icon" ;
153- prefixValue = "robot" ;
154- classNames = `ai-conversation-${ newTopic . topic_id } ` ;
155- } ) ( ) ;
156-
157- this . links = [ builtTopic , ...this . links ] ;
158- }
159-
160- createBotConversationLink ( SuperClass , topic ) {
161- return new ( class extends SuperClass {
162- name = topic . title ;
163- route = "topic.fromParamsNear" ;
164- models = [
165- topic . slug ,
166- topic . id ,
167- topic . last_read_post_number || 0 ,
168- ] ;
169- title = topic . title ;
170- text = topic . title ;
171- prefixType = "icon" ;
172- prefixValue = "robot" ;
173- classNames = `ai-conversation-${ topic . id } ` ;
174- } ) ( ) ;
175- }
176-
177192 buildSidebarLinks ( ) {
178- this . links = this . topics . map ( ( topic ) =>
179- this . createBotConversationLink (
180- BaseCustomSidebarSectionLink ,
181- topic
182- )
193+ this . links = this . topics . map (
194+ ( topic ) => new AiConversationLink ( topic )
183195 ) ;
184196 }
185197
198+ addNewMessage ( newTopic ) {
199+ this . links = [ new AiConversationLink ( newTopic ) , ...this . links ] ;
200+ }
201+
186202 watchForTitleUpdate ( topic ) {
187203 const channel = `/discourse-ai/ai-bot/topic/${ topic . topic_id } ` ;
188204 const topicId = topic . topic_id ;
@@ -201,16 +217,6 @@ export default {
201217 text . innerText = title ;
202218 }
203219 }
204-
205- get name ( ) {
206- return "ai-conversations-history" ;
207- }
208-
209- get text ( ) {
210- // TODO: FIX
211- //return i18n(themePrefix("messages_sidebar.title"));
212- return "Conversations" ;
213- }
214220 } ;
215221 } ,
216222 AI_CONVERSATIONS_PANEL
0 commit comments