import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { updateOption } from '@launch/api/WPApi';
import { LoadingIndicator } from '@launch/components/LoadingIndicator';
import { Title } from '@launch/components/Title';
import { useSiteTypes } from '@launch/hooks/useSiteTypes';
import { PageLayout } from '@launch/layouts/PageLayout';
import { usePagesStore } from '@launch/state/Pages';
import { pageState } from '@launch/state/factory';
import { useUserSelectionStore } from '@launch/state/user-selections';
import { Checkmark, RightCaret } from '@launch/svg';
export const state = pageState('Site Industry', () => ({
ready: false,
canSkip: false,
validation: null,
onRemove: () => {},
}));
export const SiteTypeSelect = () => {
const { loading } = useUserSelectionStore();
return (
);
};
const SiteTypeSelector = () => {
const { nextPage } = usePagesStore();
const { siteType, setSiteType, setSiteTypeSearch } = useUserSelectionStore();
const [search, setSearch] = useState('');
const [searchDebounced, setSearchDebounced] = useState('');
const { data, loading } = useSiteTypes(searchDebounced);
const { siteTypes } = data ?? {};
const handleSetSiteType = ({ slug, name, language }) => {
nextPage();
setSiteType({ slug, name, language });
updateOption('extendify_siteType', { slug, name, language });
};
useEffect(() => {
state.setState({ ready: !!siteType?.slug });
}, [siteType]);
useEffect(() => {
if (!search) return;
// Fetch data after 300ms but wait 1s to set the search history
const timer = setTimeout(() => setSearchDebounced(search), 300);
const timer2 = setTimeout(() => setSiteTypeSearch(search), 1000);
return () => {
clearTimeout(timer);
clearTimeout(timer2);
};
}, [search, setSiteTypeSearch]);
return (
<>
setSearch(event.target.value)}
/>
{loading && search && (
{__('Searching...', 'extendify-local')}
)}
{siteType?.name && (!loading || !search) && (
{siteType.name}
)}
{siteTypes?.map((item) => (
))}
>
);
};