n8n/packages/editor-ui/src/components/ChatEmbedModal.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

204 lines
5 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
import { computed, ref } from 'vue';
import type { EventBus } from 'n8n-design-system/utils';
import { createEventBus } from 'n8n-design-system/utils';
import Modal from './Modal.vue';
import { CHAT_EMBED_MODAL_KEY, CHAT_TRIGGER_NODE_TYPE, WEBHOOK_NODE_TYPE } from '../constants';
import { useRootStore } from '@/stores/root.store';
import { useWorkflowsStore } from '@/stores/workflows.store';
import HtmlEditor from '@/components/HtmlEditor/HtmlEditor.vue';
import JsEditor from '@/components/JsEditor/JsEditor.vue';
import { useI18n } from '@/composables/useI18n';
const props = withDefaults(
defineProps<{
modalBus?: EventBus;
}>(),
{
modalBus: () => createEventBus(),
},
);
const i18n = useI18n();
const rootStore = useRootStore();
const workflowsStore = useWorkflowsStore();
type ChatEmbedModalTabValue = 'cdn' | 'vue' | 'react' | 'other';
type ChatEmbedModalTab = {
label: string;
value: ChatEmbedModalTabValue;
};
const tabs = ref<ChatEmbedModalTab[]>([
{
label: 'CDN Embed',
value: 'cdn',
},
{
label: 'Vue Embed',
value: 'vue',
},
{
label: 'React Embed',
value: 'react',
},
{
label: 'Other',
value: 'other',
},
]);
const currentTab = ref<ChatEmbedModalTabValue>('cdn');
const webhookNode = computed(() => {
for (const type of [CHAT_TRIGGER_NODE_TYPE, WEBHOOK_NODE_TYPE]) {
const node = workflowsStore.workflow.nodes.find((node) => node.type === type);
if (node) {
// This has to be kept up-to-date with the mode in the Chat-Trigger node
if (type === CHAT_TRIGGER_NODE_TYPE && !node.parameters.public) {
continue;
}
return {
type,
node,
};
}
}
return null;
});
const webhookUrl = computed(() => {
const url = `${rootStore.webhookUrl}${
webhookNode.value ? `/${webhookNode.value.node.webhookId}` : ''
}`;
return webhookNode.value?.type === CHAT_TRIGGER_NODE_TYPE ? `${url}/chat` : url;
});
function indentLines(code: string, indent: string = ' ') {
return code
.split('\n')
.map((line) => `${indent}${line}`)
.join('\n');
}
const importCode = 'import'; // To avoid vite from parsing the import statement
const commonCode = computed(() => ({
fix: Run dev command only for core packages (no-changelog) (#7928) ## Summary Provide details about your pull request and what it adds, fixes, or changes. Photos and videos are recommended. - Filters out `@n8n/chat` and `n8n-design-system` from `pnpm run dev` - Fixes dev/build error appearing due to code string `import ... from '@n8n/chat'` #### How to test the change: 1. `pnpm run dev` should work as expected 2. The command should not run for the two packages above 3. The error regarding `@n8n/chat` should no longer appear ## Issues fixed Include links to Github issue or Community forum post or **Linear ticket**: > Important in order to close automatically and provide context to reviewers https://linear.app/n8n/issue/PAY-1089/run-dev-command-only-for-core-packages ## Review / Merge checklist - [x] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [x] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [x] Tests included. N/A > A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. > > *(internal)* You can use Slack commands to trigger [e2e tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227) or [deploy test instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce) or [deploy early access version on Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
2023-12-06 07:28:33 -08:00
import: `${importCode} '@n8n/chat/style.css';
${importCode} { createChat } from '@n8n/chat';`,
createChat: `createChat({
webhookUrl: '${webhookUrl.value}'
});`,
install: 'npm install @n8n/chat',
}));
const cdnCode = computed(
() => `<link href="https://cdn.jsdelivr.net/npm/@n8n/chat/dist/style.css" rel="stylesheet" />
<script type="module">
${importCode} { createChat } from 'https://cdn.jsdelivr.net/npm/@n8n/chat/dist/chat.bundle.es.js';
${commonCode.value.createChat}
</${'script'}>`,
);
const vueCode = computed(
() => `<script lang="ts" setup>
fix: Run dev command only for core packages (no-changelog) (#7928) ## Summary Provide details about your pull request and what it adds, fixes, or changes. Photos and videos are recommended. - Filters out `@n8n/chat` and `n8n-design-system` from `pnpm run dev` - Fixes dev/build error appearing due to code string `import ... from '@n8n/chat'` #### How to test the change: 1. `pnpm run dev` should work as expected 2. The command should not run for the two packages above 3. The error regarding `@n8n/chat` should no longer appear ## Issues fixed Include links to Github issue or Community forum post or **Linear ticket**: > Important in order to close automatically and provide context to reviewers https://linear.app/n8n/issue/PAY-1089/run-dev-command-only-for-core-packages ## Review / Merge checklist - [x] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [x] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [x] Tests included. N/A > A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. > > *(internal)* You can use Slack commands to trigger [e2e tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227) or [deploy test instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce) or [deploy early access version on Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
2023-12-06 07:28:33 -08:00
${importCode} { onMounted } from 'vue';
${commonCode.value.import}
onMounted(() => {
${indentLines(commonCode.value.createChat)}
});
</${'script'}>`,
);
const reactCode = computed(
fix: Run dev command only for core packages (no-changelog) (#7928) ## Summary Provide details about your pull request and what it adds, fixes, or changes. Photos and videos are recommended. - Filters out `@n8n/chat` and `n8n-design-system` from `pnpm run dev` - Fixes dev/build error appearing due to code string `import ... from '@n8n/chat'` #### How to test the change: 1. `pnpm run dev` should work as expected 2. The command should not run for the two packages above 3. The error regarding `@n8n/chat` should no longer appear ## Issues fixed Include links to Github issue or Community forum post or **Linear ticket**: > Important in order to close automatically and provide context to reviewers https://linear.app/n8n/issue/PAY-1089/run-dev-command-only-for-core-packages ## Review / Merge checklist - [x] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [x] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [x] Tests included. N/A > A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. > > *(internal)* You can use Slack commands to trigger [e2e tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227) or [deploy test instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce) or [deploy early access version on Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
2023-12-06 07:28:33 -08:00
() => `${importCode} { useEffect } from 'react';
${commonCode.value.import}
export const App = () => {
useEffect(() => {
${indentLines(commonCode.value.createChat, ' ')}
}, []);
return (<div></div>);
};
</${'script'}>`,
);
const otherCode = computed(
() => `${commonCode.value.import}
${commonCode.value.createChat}`,
);
function closeDialog() {
props.modalBus.emit('close');
}
</script>
<template>
<Modal
max-width="960px"
:title="i18n.baseText('chatEmbed.title')"
:event-bus="modalBus"
:name="CHAT_EMBED_MODAL_KEY"
:center="true"
>
<template #content>
<div :class="$style.container">
<n8n-tabs v-model="currentTab" :options="tabs" />
<div v-if="currentTab !== 'cdn'">
<div class="mb-s">
<n8n-text>
{{ i18n.baseText('chatEmbed.install') }}
</n8n-text>
</div>
<HtmlEditor :model-value="commonCode.install" is-read-only />
</div>
<div class="mb-s">
<n8n-text>
<i18n-t :keypath="`chatEmbed.paste.${currentTab}`">
<template #code>
<code>{{ i18n.baseText(`chatEmbed.paste.${currentTab}.file`) }}</code>
</template>
</i18n-t>
</n8n-text>
</div>
<HtmlEditor v-if="currentTab === 'cdn'" :model-value="cdnCode" is-read-only />
<HtmlEditor v-if="currentTab === 'vue'" :model-value="vueCode" is-read-only />
<JsEditor v-if="currentTab === 'react'" :model-value="reactCode" is-read-only />
<JsEditor v-if="currentTab === 'other'" :model-value="otherCode" is-read-only />
<n8n-text>
{{ i18n.baseText('chatEmbed.packageInfo.description') }}
<n8n-link :href="i18n.baseText('chatEmbed.url')" new-window bold>
{{ i18n.baseText('chatEmbed.packageInfo.link') }}
</n8n-link>
</n8n-text>
<n8n-info-tip class="mt-s">
{{ i18n.baseText('chatEmbed.chatTriggerNode') }}
</n8n-info-tip>
</div>
</template>
<template #footer>
<div class="action-buttons">
<n8n-button float="right" :label="i18n.baseText('chatEmbed.close')" @click="closeDialog" />
</div>
</template>
</Modal>
</template>
<style module lang="scss">
.container > * {
margin-bottom: var(--spacing-s);
overflow-wrap: break-word;
}
</style>