| 1 |
import { useSelect } from '@wordpress/data' |
| 2 |
import { useEffect, useState } from '@wordpress/element' |
| 3 |
import { SWRConfig, useSWRConfig } from 'swr' |
| 4 |
import { RetryNotice } from '@onboarding/components/RetryNotice' |
| 5 |
import { useDisableWelcomeGuide } from '@onboarding/hooks/useDisableWelcomeGuide' |
| 6 |
import { useBodyScrollLock } from '@onboarding/hooks/useScrollLock' |
| 7 |
import { CreatingSite } from '@onboarding/pages/CreatingSite' |
| 8 |
import { useGlobalStore } from '@onboarding/state/Global' |
| 9 |
import { usePagesStore } from '@onboarding/state/Pages' |
| 10 |
import { updateOption } from './api/WPApi' |
| 11 |
import { useSentry } from './hooks/useSentry' |
| 12 |
import { useTelemetry } from './hooks/useTelemetry' |
| 13 |
import { NeedsTheme } from './pages/NeedsTheme' |
| 14 |
|
| 15 |
export const Onboarding = () => { |
| 16 |
const [retrying, setRetrying] = useState(false) |
| 17 |
const CurrentPage = usePagesStore((state) => { |
| 18 |
const pageData = state.currentPageData() |
| 19 |
return pageData?.component |
| 20 |
}) |
| 21 |
const { fetcher, fetchData } = usePagesStore((state) => |
| 22 |
state.nextPageData(), |
| 23 |
) |
| 24 |
const { mutate } = useSWRConfig() |
| 25 |
const generating = useGlobalStore((state) => state.generating) |
| 26 |
const [show, setShow] = useState(false) |
| 27 |
const [needsTheme, setNeedsTheme] = useState(false) |
| 28 |
const theme = useSelect((select) => select('core').getCurrentTheme()) |
| 29 |
const { Sentry } = useSentry() |
| 30 |
useDisableWelcomeGuide() |
| 31 |
useBodyScrollLock() |
| 32 |
useTelemetry() |
| 33 |
|
| 34 |
const page = () => { |
| 35 |
if (needsTheme) return <NeedsTheme /> |
| 36 |
if (generating) return <CreatingSite /> |
| 37 |
if (!CurrentPage) return null |
| 38 |
return <CurrentPage /> |
| 39 |
} |
| 40 |
|
| 41 |
useEffect(() => { |
| 42 |
// Check that the textdomain came back and that it's extendable |
| 43 |
if (!theme?.textdomain) return |
| 44 |
if (theme?.textdomain === 'extendable') return |
| 45 |
setNeedsTheme(true) |
| 46 |
}, [theme]) |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
if (!show) return |
| 50 |
// If the library happens to be open, try to close it. |
| 51 |
const timeout = setTimeout(() => { |
| 52 |
window.dispatchEvent( |
| 53 |
new CustomEvent('extendify::close-library', { bubbles: true }), |
| 54 |
) |
| 55 |
}, 0) |
| 56 |
document.title = 'Extendify Launch' // Don't translate |
| 57 |
return () => clearTimeout(timeout) |
| 58 |
}, [show]) |
| 59 |
|
| 60 |
useEffect(() => { |
| 61 |
setShow(true) |
| 62 |
updateOption('extendify_launch_loaded', new Date().toISOString()) |
| 63 |
}, []) |
| 64 |
|
| 65 |
useEffect(() => { |
| 66 |
if (fetcher) { |
| 67 |
mutate(fetchData, fetcher) |
| 68 |
} |
| 69 |
}, [fetcher, mutate, fetchData]) |
| 70 |
|
| 71 |
if (!show) return null |
| 72 |
|
| 73 |
return ( |
| 74 |
<SWRConfig |
| 75 |
value={{ |
| 76 |
errorRetryInterval: 1000, |
| 77 |
onErrorRetry: ( |
| 78 |
error, |
| 79 |
key, |
| 80 |
config, |
| 81 |
revalidate, |
| 82 |
{ retryCount }, |
| 83 |
) => { |
| 84 |
if (error?.data?.status === 403) { |
| 85 |
// if they are logged out, we can't recover |
| 86 |
window.location.reload() |
| 87 |
return |
| 88 |
} |
| 89 |
if (retrying) return |
| 90 |
|
| 91 |
// TODO: Add back when we have something to show here |
| 92 |
// if (retryCount >= 5) { |
| 93 |
// console.error('Encountered unrecoverable error', error) |
| 94 |
// throw new Error(error?.message ?? 'Unknown error') |
| 95 |
// } |
| 96 |
console.error(key, error) |
| 97 |
Sentry.captureException( |
| 98 |
new Error(error?.message ?? 'Unknown error'), |
| 99 |
{ |
| 100 |
tags: { retrying: true }, |
| 101 |
extra: { cacheKey: key }, |
| 102 |
}, |
| 103 |
) |
| 104 |
|
| 105 |
setRetrying(true) |
| 106 |
setTimeout(() => { |
| 107 |
setRetrying(false) |
| 108 |
revalidate({ retryCount }) |
| 109 |
}, 5000) |
| 110 |
}, |
| 111 |
}}> |
| 112 |
<div |
| 113 |
style={{ zIndex: 99999 + 1 }} // 1 more than the library |
| 114 |
className="h-screen w-screen fixed inset-0 overflow-y-auto md:overflow-hidden bg-white"> |
| 115 |
{page()} |
| 116 |
</div> |
| 117 |
{retrying && <RetryNotice />} |
| 118 |
</SWRConfig> |
| 119 |
) |
| 120 |
} |
| 121 |
|