PluginProbe
Extendify / 0.10.2
Extendify v0.10.2
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Onboarding / Onboarding.js

Onboarding.js in Extendify 0.10.2, at src/Onboarding/Onboarding.js

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