| 1 |
import { registerCoreBlocks } from '@wordpress/block-library'; |
| 2 |
import { useSelect, useDispatch } from '@wordpress/data'; |
| 3 |
import { useEffect, useState, useRef } from '@wordpress/element'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { safeParseJson } from '@shared/lib/parsing'; |
| 6 |
import { SWRConfig, useSWRConfig } from 'swr'; |
| 7 |
import { updateOption } from '@launch/api/WPApi'; |
| 8 |
import { RestartLaunchModal } from '@launch/components/RestartLaunchModal'; |
| 9 |
import { RetryNotice } from '@launch/components/RetryNotice'; |
| 10 |
import { useTelemetry } from '@launch/hooks/useTelemetry'; |
| 11 |
import { CreatingSite } from '@launch/pages/CreatingSite'; |
| 12 |
import { NeedsTheme } from '@launch/pages/NeedsTheme'; |
| 13 |
import { useGlobalStore } from '@launch/state/Global'; |
| 14 |
import { usePagesStore } from '@launch/state/Pages'; |
| 15 |
|
| 16 |
export const LaunchPage = () => { |
| 17 |
const { updateSettings } = useDispatch('core/block-editor'); |
| 18 |
const [retrying, setRetrying] = useState(false); |
| 19 |
const { component: CurrentPage, state } = usePagesStore((state) => |
| 20 |
state.getCurrentPageData(), |
| 21 |
); |
| 22 |
const { fetcher, fetchData } = usePagesStore((state) => |
| 23 |
state.getNextPageData(), |
| 24 |
); |
| 25 |
const { setPage } = usePagesStore(); |
| 26 |
const { mutate } = useSWRConfig(); |
| 27 |
const { generating } = useGlobalStore(); |
| 28 |
const [show, setShow] = useState(false); |
| 29 |
const [needsTheme, setNeedsTheme] = useState(false); |
| 30 |
const theme = useSelect((select) => select('core').getCurrentTheme()); |
| 31 |
useTelemetry(); |
| 32 |
const once = useRef(false); |
| 33 |
|
| 34 |
const page = () => { |
| 35 |
if (needsTheme) return <NeedsTheme />; |
| 36 |
if (generating) return <CreatingSite />; |
| 37 |
if (!CurrentPage) return null; |
| 38 |
return ( |
| 39 |
<> |
| 40 |
<RestartLaunchModal setPage={setPage} /> |
| 41 |
<CurrentPage /> |
| 42 |
</> |
| 43 |
); |
| 44 |
}; |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
if (once.current) return; |
| 48 |
once.current = true; |
| 49 |
// on page load, if we are on a page without nav, go to the first page |
| 50 |
if (state.getState().useNav) return; |
| 51 |
setPage(0); |
| 52 |
}, [state, setPage]); |
| 53 |
|
| 54 |
useEffect(() => { |
| 55 |
// translators: Launch is a noun. |
| 56 |
document.title = __('Launch - AI-Powered Web Creation', 'extendify-local'); |
| 57 |
}, []); |
| 58 |
|
| 59 |
useEffect(() => { |
| 60 |
// Add editor styles to use for live previews |
| 61 |
updateSettings(safeParseJson(window.extOnbData.editorStyles)); |
| 62 |
}, [updateSettings]); |
| 63 |
|
| 64 |
useEffect(() => { |
| 65 |
// Keep an eye on this. If WP starts registering blocks when |
| 66 |
// importing the block-library module (as they likely should be doing) |
| 67 |
// then we will need to have a conditional here |
| 68 |
registerCoreBlocks(); |
| 69 |
}, []); |
| 70 |
|
| 71 |
useEffect(() => { |
| 72 |
// Check that the textdomain came back and that it's extendable |
| 73 |
if (!theme?.textdomain) return; |
| 74 |
if (theme?.textdomain === 'extendable') return; |
| 75 |
setNeedsTheme(true); |
| 76 |
}, [theme]); |
| 77 |
|
| 78 |
useEffect(() => { |
| 79 |
setShow(true); |
| 80 |
updateOption('extendify_launch_loaded', new Date().toISOString()); |
| 81 |
}, []); |
| 82 |
|
| 83 |
useEffect(() => { |
| 84 |
const fetchers = [].concat(fetcher); |
| 85 |
const fetchDataArrays = [].concat(fetchData); |
| 86 |
if (fetchers.length) { |
| 87 |
fetchers.forEach((fetcher, i) => { |
| 88 |
try { |
| 89 |
const data = |
| 90 |
typeof fetchDataArrays?.[i] === 'function' |
| 91 |
? fetchDataArrays[i]() |
| 92 |
: fetchDataArrays?.[i]; |
| 93 |
mutate(data, (last) => last || fetcher(data), { revalidate: false }); |
| 94 |
} catch (e) { |
| 95 |
// |
| 96 |
} |
| 97 |
}); |
| 98 |
} |
| 99 |
}, [fetcher, mutate, fetchData]); |
| 100 |
|
| 101 |
if (!show) return null; |
| 102 |
|
| 103 |
return ( |
| 104 |
<SWRConfig |
| 105 |
value={{ |
| 106 |
errorRetryInterval: 1000, |
| 107 |
onErrorRetry: (error, key, config, revalidate, { retryCount }) => { |
| 108 |
if (error?.data?.status === 403) { |
| 109 |
// if they are logged out, we can't recover |
| 110 |
window.location.reload(); |
| 111 |
return; |
| 112 |
} |
| 113 |
if (retrying) return; |
| 114 |
|
| 115 |
// TODO: Add back when we have something to show here |
| 116 |
// if (retryCount >= 5) { |
| 117 |
// console.error('Encountered unrecoverable error', error) |
| 118 |
// throw new Error(error?.message ?? 'Unknown error') |
| 119 |
// } |
| 120 |
console.error(key, error); |
| 121 |
setRetrying(true); |
| 122 |
setTimeout(() => { |
| 123 |
setRetrying(false); |
| 124 |
revalidate({ retryCount }); |
| 125 |
}, 5000); |
| 126 |
}, |
| 127 |
}}> |
| 128 |
<div |
| 129 |
style={{ zIndex: 99999 + 1 }} // 1 more than the library |
| 130 |
className="fixed inset-0 h-screen w-screen overflow-y-auto bg-white md:overflow-hidden"> |
| 131 |
{page()} |
| 132 |
</div> |
| 133 |
<RetryNotice show={retrying} /> |
| 134 |
</SWRConfig> |
| 135 |
); |
| 136 |
}; |
| 137 |
|