import { useCallback, useEffect, useState, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import classNames from 'classnames';
import { AnimatePresence, motion } from 'framer-motion';
import { getHomeTemplates } from '@launch/api/DataApi';
import { getThemeVariations } from '@launch/api/WPApi';
import { LoadingIndicator } from '@launch/components/LoadingIndicator';
import { SmallPreview } from '@launch/components/SmallPreview';
import { Title } from '@launch/components/Title';
import { useFetch } from '@launch/hooks/useFetch';
import { useIsMountedLayout } from '@launch/hooks/useIsMounted';
import { PageLayout } from '@launch/layouts/PageLayout';
import { pageState } from '@launch/state/factory';
import { usePagesSelectionStore } from '@launch/state/pages-selections';
import { useUserSelectionStore } from '@launch/state/user-selections';
import { Checkmark } from '@launch/svg';
export const fetcher = getHomeTemplates;
export const fetchData = () => {
const { siteType, siteStructure } = useUserSelectionStore?.getState() || {};
return {
key: 'home-pages-list',
siteType,
siteStructure,
};
};
export const state = pageState('Layout', () => ({
ready: false,
canSkip: false,
validation: null,
onRemove: () => {},
}));
export const HomeSelect = () => {
const { loading, data: homeTemplate } = useFetch(fetchData, fetcher);
return (
);
};
const DesignSelector = ({ homeTemplate }) => {
const { data: variations } = useFetch('variations', getThemeVariations);
const isMounted = useIsMountedLayout();
const [styles, setStyles] = useState([]);
const { setStyle, style: currentStyle } = usePagesSelectionStore();
const { setVariation, variation } = useUserSelectionStore();
const onSelect = useCallback(
(style) => {
setStyle(style);
setVariation(style?.variation);
},
[setStyle, setVariation],
);
const wrapperRef = useRef();
const once = useRef(false);
useEffect(() => {
state.setState({ ready: !!variation?.title });
}, [variation]);
useEffect(() => {
if (!homeTemplate || !variations) return;
if (styles.length) return;
setStyle(null);
setVariation(null);
(async () => {
const slicedEntries = Array.from(homeTemplate.entries());
for (const [index, style] of slicedEntries) {
if (!isMounted.current) return;
setStyles((styles) => [
...styles,
{
...style,
variation: variations[index % variations.length],
},
]);
// Delay between 350ms and 1s to make it less rigid
const random = Math.floor(Math.random() * (1000 - 150 + 1)) + 150;
await new Promise((resolve) => setTimeout(resolve, random));
}
})();
}, [
homeTemplate,
isMounted,
variations,
styles.length,
setStyle,
setVariation,
]);
useEffect(() => {
if (!currentStyle || !styles || once.current) return;
const currentButton = wrapperRef.current?.querySelector(
`#layout-style-${currentStyle.slug} [role="button"]`,
);
if (!currentButton) return;
once.current = true;
currentButton.focus();
}, [currentStyle, styles]);
return (
{styles?.map((style) => (
{currentStyle?.id === style.id ? (
) : null}
))}
{homeTemplate?.slice(styles?.length).map((_, i) => (
))}
);
};