import { useEffect, useState, useRef } from '@wordpress/element' import { __ } from '@wordpress/i18n' import { mutate } from 'swr' import { getSiteTypes } from '@onboarding/api/DataApi' import { updateOption } from '@onboarding/api/WPApi' import { useFetch } from '@onboarding/hooks/useFetch' import { PageLayout } from '@onboarding/layouts/PageLayout' import { usePagesStore } from '@onboarding/state/Pages' import { useUserSelectionStore } from '@onboarding/state/UserSelections' import { pageState } from '@onboarding/state/factory' import { SearchIcon, LeftArrowIcon } from '@onboarding/svg' import { fetcher as styleFetcher, fetchData as styleFetchData, } from './SiteStyle' export const fetcher = () => getSiteTypes() export const fetchData = () => ({ key: 'site-types' }) export const state = pageState('Site Industry', (set, get) => ({ title: __('Site Industry', 'extendify'), default: undefined, showInSidebar: true, ready: false, isDefault: () => useUserSelectionStore.getState()?.siteType?.slug === get().default?.slug, })) export const SiteTypeSelect = () => { const { nextPage } = usePagesStore() const siteType = useUserSelectionStore((state) => state.siteType) const feedback = useUserSelectionStore( (state) => state.feedbackMissingSiteType, ) const [search, setSearch] = useState('') const searchRef = useRef(null) const { data: siteTypes, loading } = useFetch(fetchData, fetcher) const visibleSiteTypes = siteTypes?.filter((option) => { const { title } = option const searchTerm = search?.toLowerCase() if (!searchTerm) return option?.featured if (title.toLowerCase().indexOf(searchTerm) > -1) return true }) const showMissingInput = () => window.extOnbData?.activeTests?.['remove-dont-see-inputs'] === 'A' useEffect(() => { state.setState({ ready: !loading }) }, [loading]) useEffect(() => { const raf = requestAnimationFrame(() => searchRef.current?.focus()) return () => cancelAnimationFrame(raf) }, [searchRef]) useEffect(() => { if (loading) return if (siteType?.slug) return const defaultSiteType = siteTypes?.find( (record) => record.slug === 'default', ) if (defaultSiteType) { const defaultS = { label: defaultSiteType.title, recordId: defaultSiteType.id, slug: defaultSiteType.slug, } useUserSelectionStore.getState().setSiteType(defaultS) state.setState({ default: defaultS }) } }, [loading, siteType?.slug, siteTypes]) useEffect(() => { if (!search) return const timer = setTimeout(() => { useUserSelectionStore.setState({ siteTypeSearch: [ ...useUserSelectionStore.getState().siteTypeSearch, search, ], }) }, 500) return () => clearTimeout(timer) }, [search]) const selectSiteType = async (optionValue) => { useUserSelectionStore.getState().setSiteType({ label: optionValue.title, recordId: optionValue.id, slug: optionValue.slug, styles: optionValue.styles, }) await updateOption('extendify_siteType', optionValue) nextPage() } return (

{__('Welcome to your WordPress site', 'extendify')}

{__( 'Design and launch your site with this guided experience, or head right into the WordPress dashboard if you know your way around.', 'extendify', )}

{__('What is your site about?', 'extendify')}

setSearch(e.target.value)} placeholder={__('Search...', 'extendify')} />
{loading &&

{__('Loading...', 'extendify')}

}
{visibleSiteTypes?.length > 0 && (
{visibleSiteTypes.map((option) => ( ))}
)} {!loading && visibleSiteTypes?.length === 0 && (
{__('No Results', 'extendify')}
{showMissingInput() && ( <>

{__( "Don't see what you're looking for?", 'extendify', )}

useUserSelectionStore .getState() .setFeedbackMissingSiteType( e.target.value, ) } placeholder={__( 'Describe your site...', 'extendify', )} />
)}
)}
) } const SelectButton = ({ option, selectSiteType }) => { const hoveringTimeout = useRef(0) return ( ) }