-
Notifications
You must be signed in to change notification settings - Fork 5
Add Salesforce chat widget to book details page #2873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OpenStaxClaude
wants to merge
12
commits into
main
Choose a base branch
from
add-chat-to-book-details
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3ecbb69
Add Salesforce chat widget to book details page
OpenStaxClaude ef0e6a3
Address Copilot review comments
RoyEJohnson a8f2103
Add support for additional chat feature flags
OpenStaxClaude 42138c3
Address Copilot review comments on initialization and field updates
OpenStaxClaude 84c9785
chat_logged_in_only -> chat_logged_in
RoyEJohnson e525469
Address final Copilot review comments
OpenStaxClaude 6032520
Hide/show chatElement by id
RoyEJohnson 411f534
Address final Copilot review comments (Review 11)
OpenStaxClaude a813084
Address final Copilot review comments (Review 13)
OpenStaxClaude 57d4412
Centralize isLoggedIn logic in useUserContext
OpenStaxClaude 79be4f7
Address final Copilot review comments
OpenStaxClaude 4a0decb
Use chat_logged_in_only to restrict to logged in users
RoyEJohnson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Salesforce chat widget styling | ||
| // The chat widget is injected by Salesforce and appears as a fixed overlay | ||
| // Add custom styling overrides here if needed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import React from 'react'; | ||
| import useUserContext from '~/contexts/user'; | ||
| import './chat.scss'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| embeddedservice_bootstrap?: { | ||
| settings: { | ||
| language: string; | ||
| }; | ||
| init: ( | ||
| orgId: string, | ||
| deploymentName: string, | ||
| baseUrl: string, | ||
| options: {scrt2URL: string} | ||
| ) => void; | ||
| prechatAPI?: { | ||
| setHiddenPrechatFields: (fields: Record<string, string>) => void; | ||
| }; | ||
| }; | ||
| __salesforceChatInitialized?: boolean; | ||
| } | ||
| } | ||
|
|
||
| const SALESFORCE_CONFIG = { | ||
| orgId: '00DU0000000Kwch', | ||
| deploymentName: 'Web_Messaging_Deployment', | ||
| baseUrl: 'https://openstax.my.site.com/ESWWebMessagingDeployme1716235390398', | ||
| scrt2URL: 'https://openstax.my.salesforce-scrt.com', | ||
| bootstrapScript: 'https://openstax.my.site.com/ESWWebMessagingDeployme1716235390398/assets/js/bootstrap.min.js' | ||
| }; | ||
|
|
||
| function initEmbeddedMessaging() { | ||
| try { | ||
| if (window.embeddedservice_bootstrap) { | ||
| window.embeddedservice_bootstrap.settings.language = 'en_US'; | ||
| window.embeddedservice_bootstrap.init( | ||
| SALESFORCE_CONFIG.orgId, | ||
| SALESFORCE_CONFIG.deploymentName, | ||
| SALESFORCE_CONFIG.baseUrl, | ||
| {scrt2URL: SALESFORCE_CONFIG.scrt2URL} | ||
| ); | ||
| } | ||
| } catch (err) { | ||
| console.error('Error initializing Salesforce chat:', err); | ||
| } | ||
| } | ||
|
|
||
| // eslint-disable-next-line complexity | ||
| export default function Chat() { | ||
| const userContext = useUserContext(); | ||
| const [scriptLoaded, setScriptLoaded] = React.useState(false); | ||
|
|
||
|
RoyEJohnson marked this conversation as resolved.
|
||
| // Derive stable user primitives | ||
| const userModel = userContext?.userModel; | ||
| const uuid = userModel?.uuid; | ||
| const firstName = userModel?.first_name; | ||
| const lastName = userModel?.last_name; | ||
| const email = userModel?.email; | ||
| const school = userModel?.accountsModel?.school_name; | ||
|
RoyEJohnson marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Load Salesforce script once | ||
| React.useEffect(() => { | ||
| const script = document.createElement('script'); | ||
|
|
||
|
RoyEJohnson marked this conversation as resolved.
|
||
| script.src = SALESFORCE_CONFIG.bootstrapScript; | ||
| script.type = 'text/javascript'; | ||
|
RoyEJohnson marked this conversation as resolved.
Outdated
|
||
| script.async = true; | ||
|
|
||
| script.onload = () => { | ||
| setScriptLoaded(true); | ||
| }; | ||
|
|
||
| script.onerror = () => { | ||
| console.error('Failed to load Salesforce chat script'); | ||
| }; | ||
|
|
||
| document.body.appendChild(script); | ||
|
|
||
| return () => { | ||
| if (document.body.contains(script)) { | ||
| document.body.removeChild(script); | ||
| } | ||
|
RoyEJohnson marked this conversation as resolved.
|
||
| // Hide the chat widget when component unmounts | ||
| // The Salesforce widget injects elements with these selectors | ||
| const chatElement = document.getElementById('embedded-messaging'); | ||
|
|
||
| if (chatElement) { | ||
| chatElement.style.display = 'none'; | ||
| } | ||
|
RoyEJohnson marked this conversation as resolved.
|
||
| // Note: Don't delete window.embeddedservice_bootstrap or __salesforceChatInitialized | ||
| // to maintain conversation state across component remounts | ||
| }; | ||
|
RoyEJohnson marked this conversation as resolved.
|
||
| }, []); | ||
|
|
||
| // Show chat widget when component mounts (if it was previously hidden) | ||
| React.useEffect(() => { | ||
| if (scriptLoaded && window.__salesforceChatInitialized) { | ||
| const chatElement = document.getElementById('embedded-messaging'); | ||
|
|
||
| if (chatElement) { | ||
| chatElement.style.removeProperty('display'); | ||
| } | ||
| } | ||
| }, [scriptLoaded]); | ||
|
|
||
| // Initialize chat widget once (on first mount or after refresh) | ||
| React.useEffect(() => { | ||
| if (!scriptLoaded || !window.embeddedservice_bootstrap) { | ||
| return; | ||
| } | ||
|
|
||
| // Only initialize if not already initialized (persists across component remounts) | ||
| if (!window.__salesforceChatInitialized) { | ||
| initEmbeddedMessaging(); | ||
| window.__salesforceChatInitialized = true; | ||
| } | ||
|
RoyEJohnson marked this conversation as resolved.
|
||
| }, [scriptLoaded]); | ||
|
|
||
| // Update pre-chat fields whenever user information changes | ||
| // This allows fields to update when a user logs in after chat is initialized | ||
| // eslint-disable-next-line complexity | ||
| React.useEffect(() => { | ||
| if (!scriptLoaded || !window.embeddedservice_bootstrap?.prechatAPI) { | ||
| return; | ||
| } | ||
|
RoyEJohnson marked this conversation as resolved.
|
||
|
|
||
| // Set sProduct for all users (logged in or not) | ||
| const hiddenFields: Record<string, string> = { | ||
| sProduct: 'Website' | ||
| }; | ||
|
|
||
| // Add user information if available | ||
| if (uuid) { | ||
| hiddenFields.OpenStax_UUID__c = uuid; // eslint-disable-line camelcase | ||
| } | ||
| if (firstName) { | ||
| hiddenFields.FirstName = firstName; | ||
| } | ||
| if (lastName) { | ||
| hiddenFields.LastName = lastName; | ||
| } | ||
| if (email) { | ||
| hiddenFields.Email = email; | ||
| } | ||
| if (school) { | ||
| hiddenFields.School = school; | ||
| } | ||
|
|
||
| window.embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields(hiddenFields); | ||
| }, [scriptLoaded, uuid, firstName, lastName, email, school]); | ||
|
|
||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.