PluginProbe
Extendify / 0.11.1
Extendify v0.11.1
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.11.1, at src/Onboarding/Onboarding.js

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