From 65f15c54f459f046a0ac3dbb03a0e4c54d36f8e1 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Wed, 6 Sep 2023 12:09:24 +0200 Subject: [PATCH] Fix chatbot --- src/commands/Chatbot.ts | 19 +++++++++++++++++-- .../components/Chatbot/Chatbot.tsx | 14 +++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/commands/Chatbot.ts b/src/commands/Chatbot.ts index f37288a1..c598d10b 100644 --- a/src/commands/Chatbot.ts +++ b/src/commands/Chatbot.ts @@ -1,9 +1,10 @@ import { Telemetry } from './../helpers/Telemetry'; -import { TelemetryEvent, PreviewCommands, SETTING_EXPERIMENTAL } from './../constants'; +import { TelemetryEvent, PreviewCommands, GeneralCommands } from './../constants'; import { join } from 'path'; import { commands, Uri, ViewColumn, window } from 'vscode'; import { Extension, Settings } from '../helpers'; import { WebviewHelper } from '@estruyf/vscode'; +import { getLocalizationFile } from '../utils/getLocalizationFile'; export class Chatbot { /** @@ -30,13 +31,27 @@ export class Chatbot { const cspSource = webView.webview.cspSource; - webView.webview.onDidReceiveMessage((message) => { + webView.webview.onDidReceiveMessage(async (message) => { switch (message.command) { case PreviewCommands.toVSCode.open: if (message.data) { commands.executeCommand('vscode.open', message.data); } return; + case GeneralCommands.toVSCode.getLocalization: + const { requestId } = message; + if (!requestId) { + return; + } + + const fileContents = await getLocalizationFile(); + + webView.webview.postMessage({ + command: GeneralCommands.toVSCode.getLocalization, + requestId, + payload: fileContents + }); + return; } }); diff --git a/src/dashboardWebView/components/Chatbot/Chatbot.tsx b/src/dashboardWebView/components/Chatbot/Chatbot.tsx index 56a68412..295cb9fa 100644 --- a/src/dashboardWebView/components/Chatbot/Chatbot.tsx +++ b/src/dashboardWebView/components/Chatbot/Chatbot.tsx @@ -9,6 +9,8 @@ import { useSettingsContext } from '../../providers/SettingsProvider'; import { AiInitResponse } from './models/AiInitResponse'; import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../../../localization'; +import { messageHandler } from '@estruyf/vscode/dist/client'; +import { GeneralCommands } from '../../../constants'; export interface IChatbotProps { } @@ -21,9 +23,19 @@ export const Chatbot: React.FunctionComponent = ({ }: React.Props const [answerIds, setAnswerIds] = React.useState([]); const [sources, setSources] = React.useState([]); const [loading, setLoading] = React.useState(false); + const [localeReady, setLocaleReady] = React.useState(false); const init = async () => { setLoading(true); + messageHandler.request(GeneralCommands.toVSCode.getLocalization).then((data) => { + if (data) { + l10n.config({ + contents: data + }); + } + setLocaleReady(true); + }); + const initResponse = await fetch(`${aiUrl}/api/ai-init`); if (!initResponse.ok) { @@ -54,7 +66,7 @@ export const Chatbot: React.FunctionComponent = ({ }: React.Props setSources(prev => [...prev, []]) setAnswerIds(prev => [...prev, 0]) - if (!company || !chatId) { + if (!company || !chatId || !localeReady) { return; }