-
Notifications
You must be signed in to change notification settings - Fork 1
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
Update assignable launch #2253
Update assignable launch #2253
Changes from 10 commits
09d8321
76bb683
77ed4a8
69bfa24
41eac9b
62b2eb4
566c8b0
1225172
a7701d5
421b552
9c996b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import React from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import styled, { css } from 'styled-components'; | ||
import { MessageEvent } from '@openstax/types/lib.dom'; | ||
import theme from '../../theme'; | ||
import { setTextSize } from '../actions'; | ||
import * as selectContent from '../selectors'; | ||
import { LinkedArchiveTreeSection } from '../types'; | ||
import { shadow, TopBarWrapper } from './Topbar/styled'; | ||
import { TextResizer } from './Topbar/TextResizer'; | ||
import { topbarDesktopHeight, topbarMobileHeight } from './constants'; | ||
import { TextResizerValue, textResizerValues } from '../constants'; | ||
import { useLaunchToken } from '../launchToken'; | ||
|
||
// tslint:disable-next-line:variable-name | ||
const StyledTopBarWrapper = styled(TopBarWrapper)` | ||
|
@@ -39,21 +42,60 @@ const StyledSectionTitle = styled.h2` | |
`)} | ||
`; | ||
|
||
declare const window: any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't notice any custom properties so it seems like you wouldn't need it this.... maybe prerendering related? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like an artifact of an earlier age |
||
|
||
const useTextResizeIntegration = (handleChange: (value: TextResizerValue) => void) => { | ||
const launchToken = useLaunchToken(); | ||
|
||
React.useEffect(() => { | ||
if (typeof window === 'undefined' || !window.parent || window.parent === window) { | ||
return; | ||
} | ||
|
||
const win = window; | ||
const referrer = new URL(win.document.referrer); | ||
|
||
const handler = (event: MessageEvent<any>) => { | ||
if ( | ||
event.data.type === 'TextSizeUpdate' && | ||
event.origin === referrer.origin && | ||
textResizerValues.includes(event.data.value as TextResizerValue) | ||
) { | ||
handleChange(event.data.value); | ||
} | ||
}; | ||
|
||
win.addEventListener('message', handler); | ||
return () => win.removeEventListener('message', handler); | ||
}, [handleChange]); | ||
|
||
return typeof launchToken.textSize === 'number'; | ||
}; | ||
|
||
// tslint:disable-next-line:variable-name | ||
export const AssignedTopBar = (props: { | ||
section: LinkedArchiveTreeSection; | ||
}) => { | ||
|
||
const bookTheme = useSelector(selectContent.bookTheme); | ||
const textSize = useSelector(selectContent.textSize); | ||
const dispatch = useDispatch(); | ||
|
||
const handleValueChange = React.useCallback((value: TextResizerValue) => { | ||
dispatch(setTextSize(value)); | ||
}, [dispatch]); | ||
|
||
const hasIntegration = useTextResizeIntegration(handleValueChange); | ||
|
||
if (hasIntegration) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<StyledTopBarWrapper> | ||
<StyledSectionTitle dangerouslySetInnerHTML={{ __html: props.section?.title }} /> | ||
<TextResizer | ||
bookTheme={bookTheme} | ||
setTextSize={(value) => dispatch(setTextSize(value))} | ||
setTextSize={handleValueChange} | ||
textSize={textSize} | ||
data-testid='text-resizer' | ||
mobileToolbarOpen={false} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that swapping this makes it synchronous, but I can't tell why at a glance. Is act not sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its possible that newer versions of react or the testing library don't have this problem, but there are a few places in the rex code that do this. the react issue is here facebook/react#14050 (comment)