1- // discourse/initializers/your-custom-sidebar.js
1+ import { tracked } from "@glimmer/tracking" ;
2+ import { ajax } from "discourse/lib/ajax" ;
23import { withPluginApi } from "discourse/lib/plugin-api" ;
4+ import { i18n } from "discourse-i18n" ;
5+ import AiBotSidebarNewConversation from "../discourse/components/ai-bot-sidebar-new-conversation" ;
36
47export default {
58 name : "ai-conversations-sidebar" ,
69
710 initialize ( ) {
811 withPluginApi ( "1.8.0" , ( api ) => {
12+ const currentUser = api . container . lookup ( "service:current-user" ) ;
13+ const appEvents = api . container . lookup ( "service:app-events" ) ;
14+ const messageBus = api . container . lookup ( "service:message-bus" ) ;
15+
16+ if ( ! currentUser ) {
17+ return ;
18+ }
19+
20+ // TODO: Replace
21+ const recentConversations = 10 ;
922 // Step 1: Add a custom sidebar panel
1023 api . addSidebarPanel (
1124 ( BaseCustomSidebarPanel ) =>
@@ -21,35 +34,122 @@ export default {
2134 }
2235 ) ;
2336
37+ //api.renderInOutlet("after-sidebar-sections", AiBotSidebarNewConversation);
38+
2439 // Step 2: Add a custom section to your panel
2540 api . addSidebarSection (
2641 ( BaseCustomSidebarSection , BaseCustomSidebarSectionLink ) => {
27- return class AiConversationsSidebarSection extends BaseCustomSidebarSection {
28- name = "your-custom-section" ;
29- text = "Your Section Title" ;
30-
31- get links ( ) {
32- return [
33- // Add your links here
34- //new (class extends BaseCustomSidebarSectionLink {
35- //name = "custom-link-1";
36- //route = "some.route";
37- //text = "First Custom Link";
38- //prefixType = "icon";
39- //prefixValue = "cog";
40- //})(),
41- //new (class extends BaseCustomSidebarSectionLink {
42- //name = "custom-link-2";
43- //route = "another.route";
44- //text = "Second Custom Link";
45- //prefixType = "icon";
46- //prefixValue = "bell";
47- //})()
48- ] ;
42+ return class extends BaseCustomSidebarSection {
43+ @tracked links = [ ] ;
44+ @tracked topics = [ ] ;
45+ isFetching = false ;
46+ totalTopicsCount = 0 ;
47+
48+ constructor ( ) {
49+ super ( ...arguments ) ;
50+ this . fetchMessages ( ) ;
51+
52+ appEvents . on ( "topic:created" , ( topic ) => {
53+ // when asking a new question
54+ this . addNewMessage ( topic ) ;
55+ this . watchForTitleUpdate ( topic ) ;
56+ } ) ;
57+ }
58+
59+ fetchMessages ( ) {
60+ if ( this . isFetching ) {
61+ return ;
62+ }
63+
64+ this . isFetching = true ;
65+
66+ ajax ( "/discourse-ai/ai-bot/conversations.json" )
67+ . then ( ( data ) => {
68+ this . topics = data . conversations . slice (
69+ 0 ,
70+ recentConversations
71+ ) ;
72+ this . isFetching = false ;
73+ this . buildSidebarLinks ( ) ;
74+ } )
75+ . catch ( ( e ) => {
76+ this . isFetching = false ;
77+ } ) ;
78+ }
79+
80+ addNewMessage ( newTopic ) {
81+ // the pm endpoint isn't fast enough include the newly created topic
82+ // so this adds the new topic to the existing list
83+ const builtTopic =
84+ new ( class extends BaseCustomSidebarSectionLink {
85+ name = newTopic . title ;
86+ route = "topic.fromParamsNear" ;
87+ models = [ newTopic . topic_slug , newTopic . topic_id , 0 ] ;
88+ title = newTopic . title ;
89+ text = newTopic . title ;
90+ prefixType = "icon" ;
91+ prefixValue = "robot" ;
92+ } ) ( ) ;
93+
94+ this . links = [ builtTopic , ...this . links ] ;
95+ }
96+
97+ buildSidebarLinks ( ) {
98+ this . links = this . topics . map ( ( topic ) => {
99+ return new ( class extends BaseCustomSidebarSectionLink {
100+ name = topic . title ;
101+ route = "topic.fromParamsNear" ;
102+ models = [
103+ topic . slug ,
104+ topic . id ,
105+ topic . last_read_post_number || 0 ,
106+ ] ;
107+ title = topic . title ;
108+ text = topic . title ;
109+ prefixType = "icon" ;
110+ prefixValue = "robot" ;
111+ } ) ( ) ;
112+ } ) ;
113+
114+ if ( this . totalTopicsCount > recentConversations ) {
115+ this . links . push (
116+ new ( class extends BaseCustomSidebarSectionLink {
117+ name = "View All" ;
118+ route = "userPrivateMessages.user.index" ;
119+ models = [ currentUser . username ] ;
120+ title = "View all..." ;
121+ text = "View all..." ;
122+ prefixType = "icon" ;
123+ prefixValue = "list" ;
124+ } ) ( )
125+ ) ;
126+ }
127+ }
128+
129+ watchForTitleUpdate ( topic ) {
130+ const channel = `/discourse-ai/ai-bot/topic/${ topic . topic_id } ` ;
131+ messageBus . subscribe ( channel , ( ) => {
132+ this . fetchMessages ( ) ;
133+ messageBus . unsubscribe ( channel ) ;
134+ } ) ;
135+ }
136+
137+ get name ( ) {
138+ return "custom-messages" ;
139+ }
140+
141+ get text ( ) {
142+ // TODO: FIX
143+ //return i18n(themePrefix("messages_sidebar.title"));
144+ return "Conversations" ;
145+ }
146+
147+ get displaySection ( ) {
148+ return this . links ?. length > 0 ;
49149 }
50150 } ;
51151 } ,
52- "ai-conversations" // Important: Attach to your panel
152+ "ai-conversations"
53153 ) ;
54154 } ) ;
55155 } ,
0 commit comments