import { getExtendifyCodeRecommendation } from '@auto-launch/functions/extendify-code'; import { getAbTest } from '@auto-launch/functions/getAbTest'; import { fetchWithTimeout } from '@auto-launch/functions/helpers'; import { useInstallRequiredPlugins } from '@auto-launch/hooks/useInstallRequiredPlugins'; import { loaderThreeDots } from '@auto-launch/icons'; import { useLaunchDataStore } from '@auto-launch/state/launch-data'; import { AI_HOST } from '@constants'; import { reqDataBasics } from '@shared/lib/data'; import { useAIConsentStore } from '@shared/state/ai-consent'; import { forwardRef, useCallback, useEffect, useRef, useState, } from '@wordpress/element'; import { decodeEntities } from '@wordpress/html-entities'; import { __ } from '@wordpress/i18n'; import { chevronRight, Icon, pencil } from '@wordpress/icons'; import { isURL } from '@wordpress/url'; const getShowTitle = () => Boolean(window.extLaunchData?.showLaunchTitle); const getSubmitOutside = () => getAbTest('AutoLaunch.SubmitOutside').variant === 'B' || getShowTitle(); export const DescriptionGathering = () => { const { setData, descriptionBackup, urlParams } = useLaunchDataStore(); useInstallRequiredPlugins(); const [input, setInput] = useState( urlParams.description || (!getShowTitle() && urlParams.title) || descriptionBackup || '', ); const blogname = window.extSharedData?.siteTitle || ''; const titlePrefill = !blogname || isURL(blogname) ? '' : decodeEntities(blogname); const [title, setTitle] = useState(urlParams.title || titlePrefill); const [improving, setImproving] = useState(false); const [checking, setChecking] = useState(false); const [lastImproved, setLastImproved] = useState(null); const textareaRef = useRef(null); const { consentTerms } = useAIConsentStore(); // Showing the title field makes the description optional, so the submit // gate and the textarea autofocus both follow it. const showTitle = getShowTitle(); const submitDisabled = checking || (showTitle ? title.trim().length === 0 : input.trim().length === 0); const placeholder = useDescriptionPlaceholder(); // resize the height of the textarea based on the content const adjustHeight = useCallback(() => { const el = textareaRef.current; if (!el) return; const bottomPadding = 120; // tweak as needed // Reset to measure natural height el.style.height = 'auto'; const rect = el.getBoundingClientRect(); const viewportHeight = window.innerHeight; const maxAvailable = Math.max(0, viewportHeight - rect.top - bottomPadding); const desired = el.scrollHeight; const nextHeight = Math.min(desired, maxAvailable); el.style.height = `${nextHeight}px`; el.style.overflowY = desired > maxAvailable ? 'auto' : 'hidden'; // Notify others window.dispatchEvent(new Event('launch-textarea-resize')); }, []); const submitForm = async (e) => { e.preventDefault(); const trimmedTitle = title.trim(); const trimmedInput = input.trim(); if (showTitle) setData('title', trimmedTitle); setData('descriptionRaw', trimmedInput); // Only `showExtendifyCode` partners pay the classification latency; on a // `1` we divert to the connector screen instead of starting site creation. if (window.extSharedData?.showExtendifyCode) { setChecking(true); const recommend = await getExtendifyCodeRecommendation( trimmedInput || trimmedTitle, ); if (recommend === 1) { // Leave `checking` on so the loading state holds through the exit // transition instead of flashing the textarea before the connector. setData('showExtendifyCodeScreen', true); return; } setChecking(false); } setData('go', true); }; const handleImprove = async () => { setImproving(true); const url = `${AI_HOST}/api/prompt/improve`; const method = 'POST'; const headers = { 'Content-Type': 'application/json' }; const response = await fetchWithTimeout(url, { method, headers, body: JSON.stringify({ ...reqDataBasics, description: input.trim(), title: window.extSharedData.siteTitle, }), }) .then((res) => res.ok && res.json()) .catch(() => null); const nextValue = response?.improvedPrompt; setImproving(false); if (nextValue) { setLastImproved(nextValue); const el = textareaRef.current; if (!el) return setInput(nextValue); requestAnimationFrame(() => { // Preserve undo ability by using native events instead of React state el.focus(); el.select(); const ok = document.execCommand('insertText', false, nextValue); if (!ok) setInput(nextValue); }); } }; useEffect(() => { setData('descriptionBackup', input.trim()); const raf = requestAnimationFrame(() => { adjustHeight(); }); return () => cancelAnimationFrame(raf); }, [input, setData]); useEffect(() => { const controller = new AbortController(); const { signal } = controller; const handleResize = () => { adjustHeight(); const c = textareaRef.current; c?.scrollTo(0, c.scrollHeight); }; window.addEventListener('resize', handleResize, { signal }); window.addEventListener('orientationchange', handleResize, { signal }); adjustHeight(); return () => controller.abort(); }, [adjustHeight]); return ( <> {/* biome-ignore lint: allow onClick without keyboard */}
> ); }; const useDescriptionPlaceholder = () => getAbTest('AutoLaunch.DescriptionPlaceholderLaw').variant === 'B' ? __( 'E.g., A boutique law firm specializing in family law, estate planning, and real estate, offering trusted, personalized counsel to clients across the region.', 'extendify-local', ) : __( 'E.g., A personal photography portfolio featuring a collection of landscape, portrait, and street photography, capturing moments from around the world.', 'extendify-local', ); const TitleField = ({ title, setTitle, setData, improving }) => { if (!getShowTitle() || improving) return null; return ( <>