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 / pages / SiteStyle.js

SiteStyle.js in Extendify 0.11.1, at src/Onboarding/pages/SiteStyle.js

139 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 useCallback,
3 useEffect,
4 useState,
5 useRef,
6 useMemo,
7 } from '@wordpress/element'
8 import { __ } from '@wordpress/i18n'
9 import { getStyles } from '@onboarding/api/DataApi'
10 import { SkeletonLoader } from '@onboarding/components/SkeletonLoader'
11 import { StylePreview } from '@onboarding/components/StyledPreview'
12 import { useFetch } from '@onboarding/hooks/useFetch'
13 import { useIsMountedLayout } from '@onboarding/hooks/useIsMounted'
14 import { PageLayout } from '@onboarding/layouts/PageLayout'
15 import { useUserSelectionStore } from '@onboarding/state/UserSelections'
16 import { pageState } from '@onboarding/state/factory'
17
18 export const fetcher = (params) => getStyles(params)
19 export const fetchData = (siteType) => {
20 siteType = siteType ?? useUserSelectionStore?.getState().siteType
21 return {
22 key: 'site-style',
23 siteType: siteType?.slug ?? 'default',
24 styles: siteType?.styles ?? [],
25 }
26 }
27 export const state = pageState('Design', (set, get) => ({
28 title: __('Design', 'extendify'),
29 default: undefined,
30 showInSidebar: true,
31 ready: false,
32 isDefault: () =>
33 useUserSelectionStore.getState().style?.slug === get().default?.slug,
34 }))
35 export const SiteStyle = () => {
36 const { data: styleData, loading } = useFetch(fetchData, fetcher)
37 const once = useRef(false)
38 const stylesRef = useRef()
39 const isMounted = useIsMountedLayout()
40 const [styles, setStyles] = useState([])
41
42 useEffect(() => {
43 state.setState({ ready: !loading })
44 }, [loading])
45
46 useEffect(() => {
47 if (!styleData?.length) return
48 ;(async () => {
49 for (const style of styleData) {
50 if (!isMounted.current) return
51 setStyles((styles) => [...styles, style])
52 await new Promise((resolve) => setTimeout(resolve, 1000))
53 }
54 })()
55 }, [styleData, isMounted])
56
57 useEffect(() => {
58 if (styles?.length && !useUserSelectionStore.getState().style) {
59 useUserSelectionStore.getState().setStyle(styles[0])
60 state.setState({ default: styles[0] })
61 }
62 }, [styles])
63
64 useEffect(() => {
65 if (!styles?.length || once.current) return
66 once.current = true
67 // Focus the first style
68 stylesRef?.current?.querySelector('[role=button]')?.focus()
69 }, [styles])
70
71 return (
72 <PageLayout>
73 <div>
74 <h1 className="text-3xl text-partner-primary-text mb-4 mt-0">
75 {__('Now pick a design for your new site.', 'extendify')}
76 </h1>
77 <p className="text-base opacity-70 mb-0">
78 {__('You can personalize this later.', 'extendify')}
79 </p>
80 </div>
81 <div className="w-full">
82 <h2 className="text-lg m-0 mb-4 text-gray-900">
83 {loading
84 ? __(
85 'Please wait a moment while we generate style previews...',
86 'extendify',
87 )
88 : __('Pick your style', 'extendify')}
89 </h2>
90 <div
91 ref={stylesRef}
92 className="flex gap-6 flex-wrap justify-center">
93 {styles?.map((style) => (
94 <StylePreviewWrapper
95 key={style.recordId}
96 style={style}
97 />
98 ))}
99 {styleData?.slice(styles?.length).map((data) => (
100 <div
101 key={data.slug}
102 style={{ height: 497, width: 352 }}
103 className="lg:flex gap-6 relative">
104 <SkeletonLoader context="style" />
105 </div>
106 ))}
107 </div>
108 </div>
109 </PageLayout>
110 )
111 }
112
113 const StylePreviewWrapper = ({ style }) => {
114 const onSelect = useCallback((style) => {
115 useUserSelectionStore.getState().setStyle(style)
116 }, [])
117 const context = useMemo(
118 () => ({
119 type: 'style',
120 detail: style.slug,
121 measure: true,
122 }),
123 [style],
124 )
125 return (
126 <div className="relative" style={{ height: 497, width: 352 }}>
127 <StylePreview
128 style={style}
129 context={context}
130 onSelect={onSelect}
131 active={
132 useUserSelectionStore.getState()?.style?.slug === style.slug
133 }
134 blockHeight={497}
135 />
136 </div>
137 )
138 }
139