Skip to content

Commit

Permalink
Backward compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvorozhe committed Oct 8, 2021
1 parent 492c333 commit a301bad
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions apps/gatsby/src/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ export default class Sidebar extends React.Component {

refreshPreview = () => {
const {
webhookUrl,
authToken
} = this.sdk.parameters.installation;

if (webhookUrl) {
callWebhook(webhookUrl, authToken);
const previewWebhookUrl = this.sdk.parameters.installation.previewWebhookUrl ||
this.sdk.parameters.installation.webhookUrl

if (previewWebhookUrl) {
callWebhook(previewWebhookUrl, authToken);
} else {
console.warn(`Please add a Preview Webhook URL to your Gatsby Cloud App settings.`)
}
Expand Down Expand Up @@ -171,7 +173,7 @@ export default class Sidebar extends React.Component {

// Contentful takes a few seconds to save. If we do not wait a bit for this, then the Gatsby preview may be started and finish before any content is even saved on the Contentful side
await new Promise(resolve => setTimeout(resolve, 3000))

this.refreshPreview();

let previewUrl = this.getPreviewUrl()
Expand All @@ -180,34 +182,35 @@ export default class Sidebar extends React.Component {

// Wait to see if Contentful saves new data async
const interval = setInterval(() => {
const newPreviewUrl = this.getPreviewUrl()
const newPreviewUrl = this.getPreviewUrl()

if (previewUrl !== newPreviewUrl) {
clearInterval(interval)
if (previewUrl !== newPreviewUrl) {
clearInterval(interval)

previewUrl = newPreviewUrl
previewUrl = newPreviewUrl

console.info(`new preview url ${newPreviewUrl}`)
window.open(previewUrl, GATSBY_PREVIEW_TAB_ID)
console.info(`new preview url ${newPreviewUrl}`)
window.open(previewUrl, GATSBY_PREVIEW_TAB_ID)

this.refreshPreview();
this.setState({ buttonDisabled: false })
}
this.refreshPreview();
this.setState({ buttonDisabled: false })
}
}, 1000)

// after 10 seconds stop waiting for Contentful to save data
setTimeout(() => {
clearInterval(interval)
this.setState({ buttonDisabled: false })
}, 10000)
}
}

render = () => {
let {
contentSyncUrl,
authToken,
previewUrl,
webhookUrl,
previewWebhookUrl,
} = this.sdk.parameters.installation;
const { slug } = this.state

Expand All @@ -216,18 +219,18 @@ export default class Sidebar extends React.Component {
return (
<div className="extension">
<div className="flexcontainer">
{(webhookUrl) ?
<>
<ExtensionUI
disabled={this.state.buttonDisabled}
disablePreviewOpen={!!contentSyncUrl}
contentSlug={!!slug && slug}
previewUrl={previewUrl}
authToken={authToken}
onOpenPreviewButtonClick={!!contentSyncUrl && this.handleContentSync}
/>
{!!this.state.buttonDisabled && <Spinner />}
</>
{(webhookUrl || previewWebhookUrl) ?
<>
<ExtensionUI
disabled={this.state.buttonDisabled}
disablePreviewOpen={!!contentSyncUrl}
contentSlug={!!slug && slug}
previewUrl={previewUrl}
authToken={authToken}
onOpenPreviewButtonClick={!!contentSyncUrl && this.handleContentSync}
/>
{!!this.state.buttonDisabled && <Spinner />}
</>
:
<HelpText style={STATUS_STYLE}>
<Icon icon="Warning" color="negative" style={ICON_STYLE} />
Expand Down

0 comments on commit a301bad

Please sign in to comment.