@@ -7,7 +7,17 @@ const vanillatoasts = require('vanillatoasts');
77module . exports = app => app . component ( 'chat-message-script' , {
88 template,
99 props : [ 'message' , 'script' , 'language' ] ,
10- data : ( ) => ( { activeTab : 'code' , showDetailModal : false } ) ,
10+ data ( ) {
11+ return {
12+ activeTab : 'code' ,
13+ showDetailModal : false ,
14+ showCreateDashboardModal : false ,
15+ newDashboardTitle : '' ,
16+ dashboardCode : '' ,
17+ createError : null ,
18+ dashboardEditor : null
19+ } ;
20+ } ,
1121 computed : {
1222 styleForMessage ( ) {
1323 return this . message . role === 'user' ? 'bg-gray-100' : '' ;
@@ -25,6 +35,42 @@ module.exports = app => app.component('chat-message-script', {
2535 openDetailModal ( ) {
2636 this . showDetailModal = true ;
2737 } ,
38+ openCreateDashboardModal ( ) {
39+ this . newDashboardTitle = '' ;
40+ this . dashboardCode = this . script ;
41+ this . createErrors = [ ] ;
42+ this . showCreateDashboardModal = true ;
43+ this . $nextTick ( ( ) => {
44+ if ( this . dashboardEditor ) {
45+ this . dashboardEditor . toTextArea ( ) ;
46+ }
47+ this . $refs . dashboardCodeEditor . value = this . dashboardCode ;
48+ this . dashboardEditor = CodeMirror . fromTextArea ( this . $refs . dashboardCodeEditor , {
49+ mode : 'javascript' ,
50+ lineNumbers : true
51+ } ) ;
52+ this . dashboardEditor . on ( 'change' , ( ) => {
53+ this . dashboardCode = this . dashboardEditor . getValue ( ) ;
54+ } ) ;
55+ } ) ;
56+ } ,
57+ async createDashboardFromScript ( ) {
58+ this . dashboardCode = this . dashboardEditor . getValue ( ) ;
59+ const { dashboard } = await api . Dashboard . createDashboard ( {
60+ code : this . dashboardCode ,
61+ title : this . newDashboardTitle
62+ } ) . catch ( err => {
63+ if ( err . response ?. data ?. message ) {
64+ const message = err . response . data . message . split ( ': ' ) . slice ( 1 ) . join ( ': ' ) ;
65+ this . createError = message ;
66+ throw new Error ( err . response ?. data ?. message ) ;
67+ }
68+ throw err ;
69+ } ) ;
70+ this . createError = null ;
71+ this . showCreateDashboardModal = false ;
72+ this . $router . push ( '/dashboard/' + dashboard . _id ) ;
73+ } ,
2874 async copyOutput ( ) {
2975 await navigator . clipboard . writeText ( this . message . executionResult . output ) ;
3076 vanillatoasts . create ( {
@@ -36,6 +82,14 @@ module.exports = app => app.component('chat-message-script', {
3682 } ) ;
3783 }
3884 } ,
85+ watch : {
86+ showCreateDashboardModal ( val ) {
87+ if ( ! val && this . dashboardEditor ) {
88+ this . dashboardEditor . toTextArea ( ) ;
89+ this . dashboardEditor = null ;
90+ }
91+ }
92+ } ,
3993 mounted ( ) {
4094 Prism . highlightElement ( this . $refs . code ) ;
4195 if ( this . message . executionResult ?. output ) {
0 commit comments