move online/offline listeners into tweet button

main
Mike Fix 6 years ago
parent f1fc330838
commit eb8b3bdd49

@ -45,7 +45,6 @@ class Editor extends React.Component {
...DEFAULT_SETTINGS, ...DEFAULT_SETTINGS,
loading: true, loading: true,
code: props.content, code: props.content,
online: true,
preset: DEFAULT_PRESET_ID preset: DEFAULT_PRESET_ID
} }
@ -87,8 +86,7 @@ class Editor extends React.Component {
...getSettings(localStorage), ...getSettings(localStorage),
// and then URL params // and then URL params
...initialState, ...initialState,
loading: false, loading: false
online: Boolean(window && window.navigator && window.navigator.onLine)
} }
// Makes sure the slash in 'application/X' is decoded // Makes sure the slash in 'application/X' is decoded
@ -98,27 +96,17 @@ class Editor extends React.Component {
this.updateState(newState) this.updateState(newState)
window.addEventListener('offline', this.setOffline)
window.addEventListener('online', this.setOnline)
this.isSafari = this.isSafari =
window.navigator && window.navigator &&
window.navigator.userAgent.indexOf('Safari') !== -1 && window.navigator.userAgent.indexOf('Safari') !== -1 &&
window.navigator.userAgent.indexOf('Chrome') === -1 window.navigator.userAgent.indexOf('Chrome') === -1
} }
componentWillUnmount() {
window.removeEventListener('offline', this.setOffline)
window.removeEventListener('online', this.setOnline)
}
updateState = updates => this.setState(updates, () => this.props.onUpdate(this.state)) updateState = updates => this.setState(updates, () => this.props.onUpdate(this.state))
updateCode = code => this.updateState({ code }) updateCode = code => this.updateState({ code })
updateAspectRatio = aspectRatio => this.updateState({ aspectRatio }) updateAspectRatio = aspectRatio => this.updateState({ aspectRatio })
updateTitleBar = titleBar => this.updateState({ titleBar }) updateTitleBar = titleBar => this.updateState({ titleBar })
setOffline = () => this.updateState({ online: false })
setOnline = () => this.updateState({ online: true })
async getCarbonImage( async getCarbonImage(
{ {
@ -293,7 +281,6 @@ class Editor extends React.Component {
backgroundImage, backgroundImage,
backgroundMode, backgroundMode,
aspectRatio, aspectRatio,
online,
titleBar, titleBar,
code, code,
exportSize exportSize
@ -348,7 +335,7 @@ class Editor extends React.Component {
getCarbonImage={this.getCarbonImage} getCarbonImage={this.getCarbonImage}
/> />
<div className="buttons"> <div className="buttons">
{this.props.api.tweet && online && <TweetButton onClick={this.upload} />} {this.props.api.tweet && <TweetButton onClick={this.upload} />}
<ExportMenu <ExportMenu
onChange={this.updateSetting} onChange={this.updateSetting}
export={this.export} export={this.export}

@ -3,8 +3,28 @@ import { useAsyncCallback } from '@dawnlabs/tacklebox'
import Button from './Button' import Button from './Button'
function useWindowListener(key, fn) {
React.useEffect(() => {
window.addEventListener(key, fn)
return () => window.removeEventListener(key, fn)
}, [])
}
function TweetButton(props) { function TweetButton(props) {
const [online, setOnline] = React.useState(true)
const [onClick, { loading }] = useAsyncCallback(props.onClick) const [onClick, { loading }] = useAsyncCallback(props.onClick)
React.useEffect(() => {
setOnline(Boolean(window && window.navigator && window.navigator.onLine))
}, [])
useWindowListener('offline', () => setOnline(false))
useWindowListener('online', () => setOnline(true))
if (!online) {
return null
}
return ( return (
<Button border large padding="0 16px" margin="0 8px 0 0" onClick={onClick} color="#57b5f9"> <Button border large padding="0 16px" margin="0 8px 0 0" onClick={onClick} color="#57b5f9">
{loading ? 'Loading...' : 'Tweet'} {loading ? 'Loading...' : 'Tweet'}

Loading…
Cancel
Save