| 1 |
import { RetryNotice } from '@page-creator/components/RetryNotice'; |
| 2 |
import { usePagesStore } from '@page-creator/state/pages'; |
| 3 |
import { useState } from '@wordpress/element'; |
| 4 |
import { SWRConfig } from 'swr'; |
| 5 |
|
| 6 |
export const MainPage = ({ insertPage }) => { |
| 7 |
const [retrying, setRetrying] = useState(false); |
| 8 |
const { component: CurrentPage } = usePagesStore((state) => |
| 9 |
state.getCurrentPageData(), |
| 10 |
); |
| 11 |
|
| 12 |
const page = () => { |
| 13 |
if (!CurrentPage) return null; |
| 14 |
return <CurrentPage insertPage={insertPage} />; |
| 15 |
}; |
| 16 |
|
| 17 |
return ( |
| 18 |
<SWRConfig |
| 19 |
value={{ |
| 20 |
errorRetryInterval: 1000, |
| 21 |
onErrorRetry: (error, _key, _config, revalidate, { retryCount }) => { |
| 22 |
console.error(error); |
| 23 |
if (error?.data?.status === 403) { |
| 24 |
// if they are logged out, we can't recover |
| 25 |
window.location.reload(); |
| 26 |
return; |
| 27 |
} |
| 28 |
if (retrying) return; |
| 29 |
setRetrying(true); |
| 30 |
setTimeout(() => { |
| 31 |
setRetrying(false); |
| 32 |
revalidate({ retryCount }); |
| 33 |
}, 5000); |
| 34 |
}, |
| 35 |
}} |
| 36 |
> |
| 37 |
{page()} |
| 38 |
<RetryNotice show={retrying} /> |
| 39 |
</SWRConfig> |
| 40 |
); |
| 41 |
}; |
| 42 |
|