Skip to content

Commit 8893b95

Browse files
authored
Merge pull request #71 from mongoosejs/codex/add-button-to-create-new-dashboard
Add Create Dashboard button in chat message
2 parents bc246f6 + 10f4c58 commit 8893b95

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

frontend/src/chat/chat-message-script/chat-message-script.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 8V4m0 0h4M4 4l5 5m11-1V4m0 0h-4m4 0l-5 5M4 16v4m0 0h4m-4 0l5-5m11 1v4m0 0h-4m4 0l-5-5" />
3030
</svg>
3131
</button>
32+
<button
33+
class="px-2 py-1 mr-1 text-xs bg-ultramarine-500 text-white border-none rounded cursor-pointer hover:bg-ultramarine-600 transition-colors flex items-center"
34+
@click="openCreateDashboardModal">
35+
Create Dashboard
36+
</button>
3237
<async-button
3338
class="px-2 py-1 text-xs bg-green-500 text-white border-none rounded cursor-pointer hover:bg-green-600 transition-colors disabled:bg-gray-400"
3439
@click="executeScript(message, script)">
@@ -53,4 +58,46 @@
5358
</div>
5459
</template>
5560
</modal>
61+
<modal v-if="showCreateDashboardModal">
62+
<template #body>
63+
<div class="modal-exit" @click="showCreateDashboardModal = false">&times;</div>
64+
<div>
65+
<div class="mt-4 text-gray-900 font-semibold">Create Dashboard</div>
66+
<div class="mt-4">
67+
<label class="block text-sm font-medium leading-6 text-gray-900">Title</label>
68+
<div class="mt-2">
69+
<div class="w-full flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-teal-600">
70+
<input type="text" v-model="newDashboardTitle" class="outline-none block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6" placeholder="My Dashboard">
71+
</div>
72+
</div>
73+
</div>
74+
<div class="my-4">
75+
<label class="block text-sm font-medium leading-6 text-gray-900">Code</label>
76+
<div class="border border-gray-200">
77+
<textarea class="p-2 h-[300px] w-full" ref="dashboardCodeEditor"></textarea>
78+
</div>
79+
</div>
80+
<async-button
81+
@click="createDashboardFromScript"
82+
class="rounded-md bg-teal-600 px-2.5 py-1.5 text-sm font-semibold text-white shadow-sm hover:bg-teal-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-teal-600">
83+
Submit
84+
</async-button>
85+
<div v-if="createErrors.length > 0" class="rounded-md bg-red-50 p-4 mt-1">
86+
<div class="flex">
87+
<div class="flex-shrink-0">
88+
<svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
89+
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
90+
</svg>
91+
</div>
92+
<div class="ml-3">
93+
<h3 class="text-sm font-medium text-red-800">Error</h3>
94+
<div class="mt-2 text-sm text-red-700">
95+
{{createError}}
96+
</div>
97+
</div>
98+
</div>
99+
</div>
100+
</div>
101+
</template>
102+
</modal>
56103
</div>

frontend/src/chat/chat-message-script/chat-message-script.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ const vanillatoasts = require('vanillatoasts');
77
module.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

Comments
 (0)