import { Button } from '@wordpress/components' import { useRef, useState, useEffect } from '@wordpress/element' import { __ } from '@wordpress/i18n' import { Icon, close } from '@wordpress/icons' import { Dialog } from '@headlessui/react' import classNames from 'classnames' import { getExitQuestions } from '@onboarding/api/DataApi' import { updateOption } from '@onboarding/api/WPApi' import { useGlobalStore } from '@onboarding/state/Global' import { useUserSelectionStore } from '@onboarding/state/UserSelections' import { Checkmark } from '@onboarding/svg' export const ExitModal = () => { const { exitModalOpen, closeExitModal, hoveredOverExitButton } = useGlobalStore() const { setExitFeedback } = useUserSelectionStore() const [value, setValue] = useState() const [options, setOptions] = useState([]) const initialFocus = useRef() const closeLaunch = async () => { // Store when Launch is skipped. await updateOption( 'extendify_onboarding_skipped', new Date().toISOString(), ) location.href = window.extOnbData.adminUrl } useEffect(() => { if (!hoveredOverExitButton) return // Intentionally not using SWR so we only try once. getExitQuestions() .then(({ data }) => { if (!Array.isArray(data) || !data[0]?.key) { throw new Error('Invalid data') } setOptions(data) }) .catch(() => setOptions([ { key: 'I still want it, just disabling temporary', label: __( 'I still want it, just disabling temporary', 'extendify', ), }, { key: "I don't really need it", label: __("I don't really need it", 'extendify'), }, { key: 'I plan on using my own theme or builder', label: __( 'I plan on using my own theme or builder', 'extendify', ), }, { key: "The theme designs don't look great", label: __( "The theme designs don't look great", 'extendify', ), }, { key: 'Other', label: __('Other', 'extendify') }, ]), ) .finally(() => { initialFocus.current?.focus() }) }, [hoveredOverExitButton]) useEffect(() => { setExitFeedback(value) }, [value, setExitFeedback]) return (
) } const LabeledCheckbox = ({ label, slug, setValue, value, initialFocus }) => { const { setExitFeedback } = useUserSelectionStore() const needsInput = slug === 'Other' const checked = value === slug const id = slug .toLowerCase() .replace(/ /g, '-') .replace(/[^\w-]+/g, '') return ( <> setValue(slug)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { setValue(slug) } }} role="radio" aria-labelledby={id} aria-checked={checked} data-value={slug} // Will focus on the first element in the list ref={initialFocus} tabIndex={0} className="w-5 h-5 relative mr-2"> setValue(slug)} id={id}> {label} {needsInput && checked ? (