Skip to content

Commit

Permalink
Merge pull request #5 from sendbird/feat/move-env-back
Browse files Browse the repository at this point in the history
feat: move appId & botId env back for easy of use
  • Loading branch information
luke-cha authored Jul 14, 2023
2 parents 9b1786a + ee90f89 commit c0df02c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Vite prefix is required for Vite to load the env variables
# Plz modify below two env variables on your needs
VITE_CHAT_WIDGET_APP_ID=AE8F7EEA-4555-4F86-AD8B-5E0BD86BFE67
VITE_CHAT_WIDGET_BOT_ID=khan-academy-bot
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ package-lock.json
*.njsproj
*.sln
*.sw?
.env

4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import ChatAiWidget from './components/ChatAiWidget'; //import { ChatAiWidget }
import { Constant } from './const';

interface Props extends Partial<Constant> {
applicationId: string;
botId: string;
applicationId?: string;
botId?: string;
}

const App = (props: Props) => {
Expand Down
20 changes: 16 additions & 4 deletions src/components/ChatAiWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Constant } from '../const';
import { ConstantStateProvider } from '../context/ConstantContext';
import { ReactComponent as ArrowDownIcon } from '../icons/ic-arrow-down.svg';
import { ReactComponent as ChatBotIcon } from '../icons/icon-widget-chatbot.svg';
import { assert } from '../utils';

const StyledWidgetButtonWrapper = styled.button`
position: fixed;
Expand Down Expand Up @@ -103,9 +104,12 @@ const getCookie = (cookieName: string) => {
return cookies.filter((cookie) => cookie.includes(`${cookieName}=`));
};

const CHAT_WIDGET_APP_ID = import.meta.env.VITE_CHAT_WIDGET_APP_ID;
const CHAT_WIDGET_BOT_ID = import.meta.env.VITE_CHAT_WIDGET_BOT_ID;

interface Props extends Partial<Constant> {
applicationId: string;
botId: string;
applicationId?: string;
botId?: string;
}

const ChatAiWidget = ({ applicationId, botId, ...constantProps }: Props) => {
Expand All @@ -125,10 +129,18 @@ const ChatAiWidget = ({ applicationId, botId, ...constantProps }: Props) => {
setCookie('chatbot');
}
}, []);

assert(
applicationId !== null && botId !== null,
'applicationId and botId must be provided'
);

return (
<ConstantStateProvider
applicationId={applicationId}
botId={botId}
// If env is not provided, prop will be used instead.
// But Either should be provided.
applicationId={CHAT_WIDGET_APP_ID ?? applicationId}
botId={CHAT_WIDGET_BOT_ID ?? botId}
{...constantProps}
>
<Fragment>
Expand Down
8 changes: 1 addition & 7 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ import ReactDOM from 'react-dom/client';

import App from './App';

const CHAT_WIDGET_APP_ID = import.meta.env.VITE_CHAT_WIDGET_APP_ID;
const CHAT_WIDGET_BOT_ID = import.meta.env.VITE_CHAT_WIDGET_BOT_ID;

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App
applicationId={CHAT_WIDGET_APP_ID}
botId={CHAT_WIDGET_BOT_ID}
/>
<App />
</React.StrictMode>
);

0 comments on commit c0df02c

Please sign in to comment.