Skip to content

Commit

Permalink
Revert "refactor(sdk): pick settings from parent frame"
Browse files Browse the repository at this point in the history
This reverts commit 538be41.
  • Loading branch information
jeremyckahn committed Oct 24, 2023
1 parent 711263a commit 37d9832
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 64 deletions.
49 changes: 22 additions & 27 deletions src/Bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Disclaimer } from 'pages/Disclaimer'
import { Settings } from 'pages/Settings'
import { PublicRoom } from 'pages/PublicRoom'
import { PrivateRoom } from 'pages/PrivateRoom'
import { ColorMode, pickUserSettings, UserSettings } from 'models/settings'
import { ColorMode, UserSettings } from 'models/settings'
import { PersistedStorageKeys } from 'models/storage'
import { QueryParamKeys } from 'models/shell'
import { Shell } from 'components/Shell'
Expand All @@ -33,35 +33,31 @@ const homepageUrl = new URL(
process.env.REACT_APP_HOMEPAGE ?? 'https://chitchatter.im/'
)

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

return new Promise<{ userSettings: Partial<UserSettings> }>(
(resolve, reject) => {
const configWaitTimeout = 3000
return new Promise<Partial<UserSettings>>((resolve, reject) => {
const configWaitTimeout = 3000

setTimeout(reject, configWaitTimeout)
setTimeout(reject, configWaitTimeout)

const { origin: parentFrameOrigin } = new URL(
decodeURIComponent(queryParams.get(QueryParamKeys.PARENT_DOMAIN) ?? '')
)

window.addEventListener('message', (event: MessageEvent) => {
if (event.origin !== parentFrameOrigin) return
if (!isPostMessageEvent(event)) return
if (event.data.name !== PostMessageEventName.CONFIG) return
const { origin: parentFrameOrigin } = new URL(
decodeURIComponent(queryParams.get(QueryParamKeys.PARENT_DOMAIN) ?? '')
)

window.parent.postMessage(
{ name: PostMessageEventName.CONFIG_RECEIVED },
parentFrameOrigin
)
window.addEventListener('message', (event: MessageEvent) => {
if (event.origin !== parentFrameOrigin) return
if (!isPostMessageEvent(event)) return
if (event.data.name !== PostMessageEventName.CONFIG) return

const userSettings = pickUserSettings(event.data.payload)
window.parent.postMessage(
{ name: PostMessageEventName.CONFIG_RECEIVED },
parentFrameOrigin
)

resolve({ userSettings })
})
}
)
resolve(event.data.payload)
})
})
}

function Bootstrap({
Expand Down Expand Up @@ -104,12 +100,11 @@ function Bootstrap({
if (persistedUserSettings) {
const queryParams = new URLSearchParams(window.location.search)

let userSettingsFromParentFrame: Partial<UserSettings> = {}
let overrideConfig = {}

try {
if (queryParams.has(QueryParamKeys.WAIT_FOR_CONFIG)) {
const { userSettings } = await getConfigFromParentFrame()
userSettingsFromParentFrame = userSettings
overrideConfig = await getConfigFromParent()
}
} catch (e) {
console.error(
Expand All @@ -120,7 +115,7 @@ function Bootstrap({
setUserSettings({
...userSettings,
...persistedUserSettings,
...userSettingsFromParentFrame,
...overrideConfig,
})
} else {
await persistedStorageProp.setItem(
Expand Down
43 changes: 6 additions & 37 deletions src/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,11 @@ export const isColorMode = (color: string): color is ColorMode => {
return ColorModeValues.map(String).includes(color)
}

enum UserSettingsKey {
COLOR_MODE = 'colorMode',
USER_ID = 'userId',
CUSTOM_USERNAME = 'customUsername',
PLAY_SOUND_ON_NEW_MESSAGE = 'playSoundOnNewMessage',
SHOW_NOTIFICATION_ON_NEW_MESSAGE = 'showNotificationOnNewMessage',
SHOW_ACTIVE_TYPING_STATUS = 'showActiveTypingStatus',
}

export interface UserSettings {
[UserSettingsKey.COLOR_MODE]: ColorMode
[UserSettingsKey.USER_ID]: string
[UserSettingsKey.CUSTOM_USERNAME]: string
[UserSettingsKey.PLAY_SOUND_ON_NEW_MESSAGE]: boolean
[UserSettingsKey.SHOW_NOTIFICATION_ON_NEW_MESSAGE]: boolean
[UserSettingsKey.SHOW_ACTIVE_TYPING_STATUS]: boolean
}

const userSettingsKeyStrings = Object.values(UserSettingsKey).map(String)

export const isUserSettingKey = (key: string): key is keyof UserSettings => {
return userSettingsKeyStrings.includes(key)
}

export const pickUserSettings = (
record: Record<string, any>
): Partial<UserSettings> => {
const userSettings: Partial<UserSettings> = {}

for (const [key, value] of Object.entries(record)) {
if (!isUserSettingKey(key)) continue

// TODO: Future improvement: Validate the type of the value (right now it
// is any).
userSettings[key] = value
}

return userSettings
colorMode: ColorMode
userId: string
customUsername: string
playSoundOnNewMessage: boolean
showNotificationOnNewMessage: boolean
showActiveTypingStatus: boolean
}

0 comments on commit 37d9832

Please sign in to comment.