import { Snackbar } from '@wordpress/components'; import { useEffect, useLayoutEffect, useState } from '@wordpress/element'; import { __, isRTL } from '@wordpress/i18n'; import { AnimatePresence, motion } from 'framer-motion'; import { NavigationButton } from '@launch/components/NavigationButton'; import { useCanLaunch } from '@launch/hooks/useCanLaunch'; import { PagesSelect, fetcher as pagesSelectFetcher, fetchData as pagesSelectData, state as pagesSelectState, } from '@launch/pages/PagesSelect'; import { useGlobalStore } from '@launch/state/Global'; import { usePagesStore } from '@launch/state/Pages'; import { useUserSelectionStore } from '@launch/state/user-selections'; import { RightCaret, LeftCaret } from '@launch/svg'; const PagesPageData = { component: PagesSelect, fetcher: pagesSelectFetcher, fetchData: pagesSelectData, state: pagesSelectState, }; export const PageControl = () => { const { currentPageIndex, setPage, addPage, removePage, replaceHistory, pushHistory, } = usePagesStore(); const { siteStructure } = useUserSelectionStore(); useLayoutEffect(() => { // If we later add more structures, consider having predefined paths if (siteStructure === 'multi-page') { addPage('page-select', PagesPageData, 'layout'); return; } removePage('page-select'); }, [siteStructure, addPage, removePage]); useEffect(() => { const replaceStateHistory = () => { history.state === null && replaceHistory(currentPageIndex); }; window.addEventListener('load', replaceStateHistory); const popstate = () => { const page = currentPageIndex - 1; if (page === -1) history.go(-1); setPage(page); pushHistory(page); }; window.addEventListener('popstate', popstate); return () => { window.removeEventListener('load', replaceStateHistory); window.removeEventListener('popstate', popstate); }; }, [setPage, replaceHistory, pushHistory, currentPageIndex]); return (
); }; const Steps = () => { const { currentPageIndex, pages } = usePagesStore(); const totalPages = usePagesStore((state) => state.count()); const pagesList = Array.from(pages.entries()); return (
{pagesList.map(([page], index) => { const bgColor = index < currentPageIndex ? 'bg-design-main' : 'bg-gray-200'; return (
{index !== currentPageIndex && (
)} {index === currentPageIndex && (
)} {index < totalPages - 1 && (
)}
); })}
); }; const PrevButton = () => { const { previousPage, currentPageIndex } = usePagesStore(); const onFirstPage = currentPageIndex === 0; if (onFirstPage) { return ( (window.location.href = `${window.extSharedData.adminUrl}admin.php?page=extendify-assist`) } className="border-gray-200 bg-white text-design-main hover:bg-gray-50 focus:bg-gray-50"> <> {isRTL() ? ( ) : ( )} {__('Exit Launch', 'extendify-local')} ); } return ( <> {isRTL() ? ( ) : ( )} {__('Back', 'extendify-local')} ); }; const NextButton = () => { const { nextPage, currentPageIndex, pages } = usePagesStore(); const totalPages = usePagesStore((state) => state.count()); const canLaunch = useCanLaunch(); const onLastPage = currentPageIndex === totalPages - 1; const currentPageKey = Array.from(pages.keys())[currentPageIndex]; const pageState = pages.get(currentPageKey).state; const [canProgress, setCanProgress] = useState(false); const [canSkip, setCanSkip] = useState(false); const [validation, setValidation] = useState({}); const [showValidationMessage, setShowValidationMessage] = useState(false); const nextPageOrComplete = () => { if (validation?.message) { setShowValidationMessage(true); const timeout = setTimeout(() => { setShowValidationMessage(false); }, 3000); return () => clearTimeout(timeout); } if (canLaunch && onLastPage) { useGlobalStore.setState({ generating: true }); } else { nextPage(); } }; useEffect(() => { const { ready, canSkip, validation } = pageState?.getState() || {}; setCanSkip(canSkip ?? false); setCanProgress(ready ?? false); setValidation(validation ?? {}); return pageState.subscribe((s) => { setCanSkip(s.canSkip); setCanProgress(s.ready); setValidation(s.validation); }); }, [pageState, currentPageIndex]); return ( <> {canSkip ? ( nextPageOrComplete()} data-test="back-button" className="mr-2 border-gray-200 bg-white text-design-main hover:bg-gray-50 focus:bg-gray-50"> <> {__('Skip', 'extendify-local')} {isRTL() ? ( ) : ( )} ) : ( <> {__('Next', 'extendify-local')} {isRTL() ? ( ) : ( )} )} {showValidationMessage && validation && (
{validation?.message}
)}
); };