Skip to content

Commit

Permalink
feat(sdk): render root-url in embed code if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Oct 24, 2023
1 parent 86eadae commit bd8cf39
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 1 addition & 5 deletions src/Bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import localforage from 'localforage'
import * as serviceWorkerRegistration from 'serviceWorkerRegistration'
import { StorageContext } from 'contexts/StorageContext'
import { SettingsContext } from 'contexts/SettingsContext'
import { routes } from 'config/routes'
import { homepageUrl, routes } from 'config/routes'
import { Home } from 'pages/Home'
import { About } from 'pages/About'
import { Disclaimer } from 'pages/Disclaimer'
Expand All @@ -29,10 +29,6 @@ export interface BootstrapProps {
getUuid?: typeof uuid
}

const homepageUrl = new URL(
process.env.REACT_APP_HOMEPAGE ?? 'https://chitchatter.im/'
)

const getConfigFromParent = () => {
const queryParams = new URLSearchParams(window.location.search)

Expand Down
4 changes: 4 additions & 0 deletions src/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export enum routes {
ROOT = '/',
SETTINGS = '/settings',
}

export const homepageUrl = new URL(
process.env.REACT_APP_HOMEPAGE ?? 'https://chitchatter.im/'
)
14 changes: 11 additions & 3 deletions src/pages/Home/EmbedCodeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Link from '@mui/material/Link'
import { CopyableBlock } from 'components/CopyableBlock/CopyableBlock'

import { iframeFeatureAllowList } from 'config/iframeFeatureAllowList'
import { homepageUrl } from 'config/routes'

interface EmbedCodeDialogProps {
showEmbedCode: boolean
Expand All @@ -26,6 +27,15 @@ export const EmbedCodeDialog = ({
const iframeSrc = new URL(`${window.location.origin}/public/${roomName}`)
iframeSrc.search = new URLSearchParams({ embed: '1' }).toString()

const needsRootUrlAttribute = window.location.origin !== homepageUrl.origin

// NOTE: The script src is inaccurate in the local development environment.
const sdkEmbedCode = `<script src="${window.location.origin}/sdk.js"></script>
<chat-room room="${roomName}" ${
needsRootUrlAttribute ? `root-url="${window.location.origin}/" ` : ''
}width="800" height="800" />`

return (
<Dialog open={showEmbedCode} onClose={handleEmbedCodeWindowClose}>
<DialogTitle>Room embed code</DialogTitle>
Expand Down Expand Up @@ -91,9 +101,7 @@ export const EmbedCodeDialog = ({
}}
wrapLines={true}
>
{`<script src="${process.env.REACT_APP_HOMEPAGE}sdk.js"></script>
<chat-room room="${roomName}" width="800" height="800" />`}
{sdkEmbedCode}
</SyntaxHighlighter>
</CopyableBlock>
</DialogContent>
Expand Down

0 comments on commit bd8cf39

Please sign in to comment.