Skip to content

Commit 38a06d3

Browse files
committed
Fix process.env browser compatibility issues
1 parent efdc676 commit 38a06d3

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

src/components/QABot.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const QABotInternal = React.forwardRef((props, botRef) => {
5555
accessId
5656
} = props;
5757

58-
const finalApiKey = apiKey || process.env.REACT_APP_API_KEY;
58+
const finalApiKey = apiKey || ((typeof process !== 'undefined' && process.env) ? process.env.REACT_APP_API_KEY : null);
5959

6060
const [isBotLoggedIn, setIsBotLoggedIn] = useState(isLoggedIn !== undefined ? isLoggedIn : false);
6161
const sessionIdRef = useRef(getOrCreateSessionId());
@@ -128,12 +128,13 @@ const QABotInternal = React.forwardRef((props, botRef) => {
128128
feedbackForm,
129129
setFeedbackForm: updateFeedbackForm,
130130
formContext,
131+
apiKey: finalApiKey,
131132
userInfo: {
132133
email: userEmail || null,
133134
name: userName || null,
134135
accessId: accessId || null
135136
}
136-
}), [welcomeMessage, isBotLoggedIn, loginUrl, handleQuery, sessionId, currentQueryId, ticketForm, feedbackForm, updateTicketForm, updateFeedbackForm, formContext, userEmail, userName, accessId]);
137+
}), [welcomeMessage, isBotLoggedIn, loginUrl, handleQuery, sessionId, currentQueryId, ticketForm, feedbackForm, updateTicketForm, updateFeedbackForm, formContext, finalApiKey, userEmail, userName, accessId]);
137138

138139
useUpdateHeader(isBotLoggedIn, containerRef);
139140
useRingEffect(ringEffect, containerRef);

src/components/ThumbsUpThumbsDown.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DEFAULT_CONFIG } from '../config/constants';
1616
* @param {Function} [props.onOpenTicket] - Callback for "Open a ticket for assistance"
1717
* @returns {JSX.Element} Rendered thumbs up/down component
1818
*/
19-
const ThumbsUpThumbsDown = ({ sessionId, currentQueryId, className = '', style, onFeedbackChange }) => {
19+
const ThumbsUpThumbsDown = ({ sessionId, currentQueryId, apiKey, className = '', style, onFeedbackChange }) => {
2020
const [feedbackGiven, setFeedbackGiven] = React.useState(null); // null, 'positive', 'negative'
2121
const [hoveredButton, setHoveredButton] = React.useState(null); // 'up', 'down', or null
2222
const { injectMessage } = useMessages();
@@ -71,8 +71,6 @@ const ThumbsUpThumbsDown = ({ sessionId, currentQueryId, className = '', style,
7171
* @param {boolean} isPositive - true for thumbs up, false for thumbs down
7272
*/
7373
const sendFeedback = async (isPositive) => {
74-
const apiKey = process.env.REACT_APP_API_KEY;
75-
7674
if (!apiKey || !sessionId) {
7775
console.warn('Missing apiKey or sessionId for feedback');
7876
return;

src/config/constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const DEFAULT_CONFIG = {
88
//API_ENDPOINT: 'https://access-ai.ccs.uky.edu:543/api/query',
99

1010
// Netlify function URL
11-
netlifyBaseUrl: process.env.REACT_APP_NETLIFY_BASE_URL,
11+
netlifyBaseUrl: (typeof process !== 'undefined' && process.env) ? process.env.REACT_APP_NETLIFY_BASE_URL : 'https://access-ai.ccs.uky.edu/api/query',
1212

1313
ERRORS: {
1414
API_UNAVAILABLE: 'Unable to contact the Q&A Bot. Please try again later.',
@@ -43,5 +43,5 @@ export const buildWelcomeMessage = (isLoggedIn, welcomeMessage) => {
4343

4444
export const getApiKey = (providedApiKey) => {
4545
// Return provided API key if available, otherwise fall back to environment variable
46-
return providedApiKey || process.env.REACT_APP_API_KEY;
46+
return providedApiKey || ((typeof process !== 'undefined' && process.env) ? process.env.REACT_APP_API_KEY : null);
4747
};

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function ExampleApp() {
109109
open={chatOpen}
110110
onOpenChange={setChatOpen}
111111
loginUrl="/login"
112-
apiKey={process.env.REACT_APP_API_KEY}
112+
apiKey={(typeof process !== 'undefined' && process.env) ? process.env.REACT_APP_API_KEY : null}
113113
welcome="What can I help you with?"
114114
userEmail={email || undefined}
115115
userName={name || undefined}

src/utils/create-bot-flow.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function createBotFlow({
2121
// feedbackForm = {},
2222
// setFeedbackForm = () => {},
2323
formContext,
24+
apiKey,
2425
userInfo = {}
2526
}) {
2627
// Set the current form context for use in flow functions
@@ -39,7 +40,8 @@ function createBotFlow({
3940
? createQAFlow({
4041
fetchAndStreamResponse: handleQuery,
4142
sessionId,
42-
currentQueryId
43+
currentQueryId,
44+
apiKey
4345
})
4446
: {
4547
go_ahead_and_ask: {

src/utils/flows/qa-flow.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DEFAULT_CONFIG } from '../../config/constants';
1010
* @param {string} params.currentQueryId Current query ID
1111
* @returns {Object} Q&A flow configuration
1212
*/
13-
export const createQAFlow = ({ fetchAndStreamResponse, sessionId, currentQueryId }) => {
13+
export const createQAFlow = ({ fetchAndStreamResponse, sessionId, currentQueryId, apiKey }) => {
1414
return {
1515
go_ahead_and_ask: {
1616
message: "Please type your question.",
@@ -31,7 +31,6 @@ export const createQAFlow = ({ fetchAndStreamResponse, sessionId, currentQueryId
3131
chatDisabled: false,
3232
function: async (chatState) => {
3333
if (chatState.userInput === "👍 Yes" || chatState.userInput === "👎 No") {
34-
const apiKey = process.env.REACT_APP_API_KEY;
3534
if (apiKey && sessionId) {
3635
const isPositive = chatState.userInput === "👍 Yes";
3736
const headers = {

0 commit comments

Comments
 (0)