Skip to content

Commit

Permalink
feat(sdk): [wip] partially stand up config listener
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Oct 17, 2023
1 parent aa53709 commit bc53e4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion sdk/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QueryParamKeys } from '../src/models/shell'
import { iframeFeatureAllowList } from '../src/config/iframeFeatureAllowList'

export const defaultRoot = 'https://chitchatter.im/'
Expand Down Expand Up @@ -27,7 +28,7 @@ class ChatEmbed extends HTMLElement {
const roomName = encodeURIComponent(
this.getAttribute(ChatEmbedAttributes.ROOM_NAME) ?? window.location.href
)
const iframeSrc = `${rootUrl}public/${roomName}/?embed=1`
const iframeSrc = `${rootUrl}public/${roomName}/?${QueryParamKeys.IS_EMBEDDED}&${QueryParamKeys.WAIT_FOR_CONFIG}`

iframe.setAttribute('src', iframeSrc)
iframe.style.border = 'none'
Expand Down
41 changes: 40 additions & 1 deletion src/Bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { PublicRoom } from 'pages/PublicRoom'
import { PrivateRoom } from 'pages/PrivateRoom'
import { UserSettings } from 'models/settings'
import { PersistedStorageKeys } from 'models/storage'
import { QueryParamKeys } from 'models/shell'
import { Shell } from 'components/Shell'

export interface BootstrapProps {
Expand All @@ -31,6 +32,26 @@ const homepageUrl = new URL(
process.env.REACT_APP_HOMEPAGE ?? 'https://chitchatter.im/'
)

const waitForConfig = () => {
return new Promise<UserSettings>((resolve, reject) => {
const configWaitTimeout = 3000

setTimeout(reject, configWaitTimeout)

window.addEventListener('message', (event: MessageEvent) => {
// FIXME: Make this work
//if (event.origin !== window.location.origin) return

if (event.data?.name === 'config') {
console.log('got config')
// FIXME: Use a specific origin here
window.postMessage({ name: 'receivedConfig' }, '*')
resolve(event.data.payload)
}
})
})
}

function Bootstrap({
persistedStorage: persistedStorageProp = localforage.createInstance({
name: 'chitchatter',
Expand Down Expand Up @@ -69,7 +90,25 @@ function Bootstrap({
)

if (persistedUserSettings) {
setUserSettings({ ...userSettings, ...persistedUserSettings })
const queryParams = new URLSearchParams(window.location.search)

let overrideConfig = {}

try {
if (queryParams.has(QueryParamKeys.WAIT_FOR_CONFIG)) {
overrideConfig = await waitForConfig()
}
} catch (e) {
console.error(
'Chitchatter configuration from parent frame could not be loaded'
)
}

setUserSettings({
...userSettings,
...persistedUserSettings,
...overrideConfig,
})
} else {
await persistedStorageProp.setItem(
PersistedStorageKeys.USER_SETTINGS,
Expand Down
1 change: 1 addition & 0 deletions src/models/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type AlertOptions = Pick<AlertProps, 'severity'>

export enum QueryParamKeys {
IS_EMBEDDED = 'embed',
WAIT_FOR_CONFIG = 'waitForConfig',
}

0 comments on commit bc53e4a

Please sign in to comment.