From 6f3a555e75efc20af682704652c8119e3df80625 Mon Sep 17 00:00:00 2001 From: hahahumble Date: Wed, 19 Apr 2023 14:29:54 +0800 Subject: [PATCH 1/5] chore: update version number --- src/components/Settings/AboutSection.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Settings/AboutSection.tsx b/src/components/Settings/AboutSection.tsx index 5cf366a..3435ef4 100644 --- a/src/components/Settings/AboutSection.tsx +++ b/src/components/Settings/AboutSection.tsx @@ -51,7 +51,7 @@ const AboutSection: React.FC = ({}) => { -
Version: v0.4.0
+
Version: v0.4.1
); From 44db739910ddbfbc942b69beb0281161cbf1ff24 Mon Sep 17 00:00:00 2001 From: hahahumble Date: Wed, 19 Apr 2023 14:30:30 +0800 Subject: [PATCH 2/5] chore: update i18n --- src/locales/en.json | 6 +++++- src/locales/es.json | 6 +++++- src/locales/zh-CN.json | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/locales/en.json b/src/locales/en.json index d52e935..1b7a63f 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -54,6 +54,9 @@ "setting": { "chat": { "title": "Chat", + "access-code": "Access Code", + "access-code-tooltip": "Enter the access code to use the application", + "code": "Code", "api-key": "API Key", "default-host-address": "chat.openai.com", "openai": "OpenAI", @@ -153,6 +156,7 @@ "azure-synthesis-error": "There was an error with Azure speech synthesis", "azure-recognition-error": "There was an error with Azure speech recognition", "polly-synthesis-error": "There was an error with Amazon Polly speech synthesis", - "cannot-be-empty": "This field cannot be empty" + "cannot-be-empty": "This field cannot be empty", + "invalid-access-code": "Invalid access code" } } diff --git a/src/locales/es.json b/src/locales/es.json index ac8682e..c39ff23 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -54,6 +54,9 @@ "setting": { "chat": { "title": "Chat", + "access-code": "Access Code", + "access-code-tooltip": "Enter the access code to use the application", + "code": "Code", "api-key": "Código de API", "default-host-address": "chat.openai.com", "openai": "OpenAI", @@ -153,6 +156,7 @@ "azure-synthesis-error": "Se ha producido un error con la síntesis de voz de Azure", "azure-recognition-error": "Se ha producido un error en el reconocimiento de voz de Azure", "polly-synthesis-error": "Se ha producido un error con la síntesis de voz de Amazon Polly", - "cannot-be-empty": "This field cannot be empty" + "cannot-be-empty": "This field cannot be empty", + "invalid-access-code": "Invalid access code" } } diff --git a/src/locales/zh-CN.json b/src/locales/zh-CN.json index 57a7feb..130ee61 100644 --- a/src/locales/zh-CN.json +++ b/src/locales/zh-CN.json @@ -54,6 +54,9 @@ "setting": { "chat": { "title": "对话", + "access-code": "访问密码", + "access-code-tooltip": "输入访问密码以使用应用", + "code": "Code", "api-key": "API Key", "default-host-address": "chat.openai.com", "openai": "OpenAI", @@ -153,6 +156,7 @@ "azure-synthesis-error": "Azure 语音合成错误", "azure-recognition-error": "Azure 语音识别错误", "polly-synthesis-error": "Amazon Polly 语音合成错误", - "cannot-be-empty": "不能为空" + "cannot-be-empty": "不能为空", + "invalid-access-code": "无效的访问密码" } } From f36f5258c2175a8ba5b469afac4aca9867cab33f Mon Sep 17 00:00:00 2001 From: hahahumble Date: Wed, 19 Apr 2023 14:31:02 +0800 Subject: [PATCH 3/5] chore: access code global store --- src/store/module/global.ts | 1 + src/store/reducer/global.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/store/module/global.ts b/src/store/module/global.ts index b4892e4..8ed79c3 100644 --- a/src/store/module/global.ts +++ b/src/store/module/global.ts @@ -22,6 +22,7 @@ const defaultGlobalState = { locale: navigator.language.split(/[-_]/)[0] || '', appearance: 'system', key: { + accessCode: '', openaiApiKey: '', openaiModel: '', openaiHost: '', diff --git a/src/store/reducer/global.ts b/src/store/reducer/global.ts index e8f9fc2..4ed7927 100644 --- a/src/store/reducer/global.ts +++ b/src/store/reducer/global.ts @@ -6,6 +6,7 @@ export const globalSlice = createSlice({ locale: navigator.language.split(/[-_]/)[0] || '', appearance: 'system', key: { + accessCode: '', openaiApiKey: '', openaiModel: '', openaiHost: '', From a11073a147fddf052cb9c75a19dca42943b993ec Mon Sep 17 00:00:00 2001 From: hahahumble Date: Wed, 19 Apr 2023 14:32:23 +0800 Subject: [PATCH 4/5] feat: access code support --- .env | 1 + src/components/AzureSpeechToText.tsx | 10 ++++++++++ src/components/Content.tsx | 18 ++++++++++++++++++ src/components/Notification.tsx | 6 ++++++ src/components/Settings/ChatSection.tsx | 14 ++++++++++++++ src/pages/Home.tsx | 2 +- 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 8fe2a07..2a5207f 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ +VITE_ACCESS_CODE=REPLACE_WITH_YOUR_OWN VITE_OPENAI_API_KEY=REPLACE_WITH_YOUR_OWN VITE_OPENAI_HOST=REPLACE_WITH_YOUR_OWN VITE_OPENAI_MODEL=REPLACE_WITH_YOUR_OWN diff --git a/src/components/AzureSpeechToText.tsx b/src/components/AzureSpeechToText.tsx index ae3fdbc..f4b24ba 100644 --- a/src/components/AzureSpeechToText.tsx +++ b/src/components/AzureSpeechToText.tsx @@ -1,5 +1,6 @@ import React, { useState } from 'react'; import * as sdk from 'microsoft-cognitiveservices-speech-sdk'; +import { existEnvironmentVariable, getEnvironmentVariable } from '../helpers/utils'; interface AzureSpeechToTextProps { subscriptionKey: string; @@ -10,6 +11,7 @@ interface AzureSpeechToTextProps { setIsListening: (update: ((prevIsListening: boolean) => boolean) | boolean) => void; setWaiting: (update: ((prevWaiting: boolean) => boolean) | boolean) => void; notify: any; + accessCode: string; } const AzureSpeechToText: React.FC = ({ @@ -21,6 +23,7 @@ const AzureSpeechToText: React.FC = ({ setTranscript, setWaiting, notify, + accessCode, }) => { const [recognizer, setRecognizer] = useState(null); @@ -37,6 +40,13 @@ const AzureSpeechToText: React.FC = ({ }, [isListening]); const startSpeechRecognition = () => { + if (accessCode !== getEnvironmentVariable('ACCESS_CODE')) { + notify.invalidAccessCodeNotify(); + setIsListening(false); + setWaiting(false); + return; + } + setWaiting(true); if (subscriptionKey === '' || region === '') { diff --git a/src/components/Content.tsx b/src/components/Content.tsx index 7e47ee2..f814990 100644 --- a/src/components/Content.tsx +++ b/src/components/Content.tsx @@ -109,6 +109,13 @@ const Content: React.FC = ({ notify }) => { if (disableSpeaker) { return; } + if (existEnvironmentVariable('ACCESS_CODE')) { + const accessCode = getEnvironmentVariable('ACCESS_CODE'); + if (accessCode !== key.accessCode) { + notify.invalidAccessCodeNotify(); + return; + } + } stopSpeechSynthesis(); setStatus('speaking'); setFinished(false); @@ -219,6 +226,16 @@ const Content: React.FC = ({ notify }) => { const openaiApiModel = existEnvironmentVariable('OPENAI_MODEL') ? getEnvironmentVariable('OPENAI_MODEL') : key.openaiModel; + + if (existEnvironmentVariable('ACCESS_CODE')) { + const accessCode = getEnvironmentVariable('ACCESS_CODE'); + if (accessCode !== key.accessCode) { + notify.invalidAccessCodeNotify(); + setStatus('idle'); + return; + } + } + sendRequest( conversationsToSent as any, openaiApiKey, @@ -465,6 +482,7 @@ const Content: React.FC = ({ notify }) => { setTranscript={setTranscript} setWaiting={setWaiting} notify={notify} + accessCode={existEnvironmentVariable('ACCESS_CODE') ? key.accessCode : ''} /> )}
diff --git a/src/components/Notification.tsx b/src/components/Notification.tsx index da5840a..b5cd0ec 100644 --- a/src/components/Notification.tsx +++ b/src/components/Notification.tsx @@ -24,6 +24,12 @@ export const resetNotify = () => { }); }; +export const invalidAccessCodeNotify = () => { + toast.error(i18next.t('notification.invalid-access-code') as string, { + style: notificationStyle, + }); +}; + // OpenAI export const invalidOpenAiKeyNotify = () => { toast.error(i18next.t('notification.invalid-openai-key') as string, { diff --git a/src/components/Settings/ChatSection.tsx b/src/components/Settings/ChatSection.tsx index 0211059..1e01ea7 100644 --- a/src/components/Settings/ChatSection.tsx +++ b/src/components/Settings/ChatSection.tsx @@ -69,6 +69,20 @@ const ChatSection: React.FC = ({}) => { /> )} + {existEnvironmentVariable('ACCESS_CODE') ? ( + setKey({ ...key, accessCode: e })} + placeholder={i18n.t('setting.chat.code') as string} + className={''} + /> + ) : ( + <> + )} diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index d9b40bc..048a994 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -5,7 +5,6 @@ import * as Notify from '../components/Notification'; import { browserName, isMobile } from 'react-device-detect'; import { useGlobalStore, useSessionStore } from '../store/module'; import { v4 as uuidv4 } from 'uuid'; -import { current } from '@reduxjs/toolkit'; import { useTranslation } from 'react-i18next'; function Home() { @@ -32,6 +31,7 @@ function Home() { awsErrorNotify: Notify.awsErrorNotify, emptyAzureKeyNotify: Notify.emptyAzureKeyNotify, cannotBeEmptyNotify: Notify.cannotBeEmptyNotify, + invalidAccessCodeNotify: Notify.invalidAccessCodeNotify, }; if (isMobile || browserName !== 'Chrome') { From 457953fc6c8a5fe35e889becc703ef9e82b02556 Mon Sep 17 00:00:00 2001 From: hahahumble Date: Wed, 19 Apr 2023 14:32:50 +0800 Subject: [PATCH 5/5] style: format code --- src/components/ConversationPanel.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/ConversationPanel.tsx b/src/components/ConversationPanel.tsx index b37236a..f673035 100644 --- a/src/components/ConversationPanel.tsx +++ b/src/components/ConversationPanel.tsx @@ -64,7 +64,7 @@ function ConversationPanel({ className="group relative rounded-lg hover:bg-gray-200 p-2 flex flex-row space-x-3 transition-colors duration-100" > -
+ className={`absolute right-2 top-2 group-hover:opacity-100 opacity-0 transition-colors duration-100 flex-row space-x-0.5 ${ + isHidden ? 'hidden' : 'flex' + }`} + > { generateSpeech(conversation.content); @@ -109,4 +109,4 @@ function ConversationPanel({ ); } -export default ConversationPanel; \ No newline at end of file +export default ConversationPanel;