({
activeCompPath,
projectDir,
projectIdRef,
showToast,
domEditSelectionRef,
domEditSelection,
}: UseAskAgentModalParams)
| 20 | // ── Hook ── |
| 21 | |
| 22 | export function useAskAgentModal({ |
| 23 | activeCompPath, |
| 24 | projectDir, |
| 25 | projectIdRef, |
| 26 | showToast, |
| 27 | domEditSelectionRef, |
| 28 | domEditSelection, |
| 29 | }: UseAskAgentModalParams) { |
| 30 | // ── State ── |
| 31 | |
| 32 | const [agentPromptTagSnippet, setAgentPromptTagSnippet] = useState<string | undefined>(); |
| 33 | const [agentPromptSelectionContext, setAgentPromptSelectionContext] = useState< |
| 34 | string | undefined |
| 35 | >(); |
| 36 | const [agentModalAnchorPoint, setAgentModalAnchorPoint] = useState<AgentModalAnchorPoint | null>( |
| 37 | null, |
| 38 | ); |
| 39 | const [copiedAgentPrompt, setCopiedAgentPrompt] = useState(false); |
| 40 | const [agentModalOpen, setAgentModalOpen] = useState(false); |
| 41 | |
| 42 | // ── Refs ── |
| 43 | |
| 44 | const copiedAgentTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 45 | |
| 46 | // ── Callbacks ── |
| 47 | |
| 48 | const preloadAgentPromptSnippet = useCallback( |
| 49 | async (selection: DomEditSelection) => { |
| 50 | const pid = projectIdRef.current; |
| 51 | if (!pid) return; |
| 52 | |
| 53 | const targetPath = selection.sourceFile || activeCompPath || "index.html"; |
| 54 | try { |
| 55 | const response = await fetch( |
| 56 | `/api/projects/${pid}/files/${encodeURIComponent(targetPath)}`, |
| 57 | ); |
| 58 | if (!response.ok) return; |
| 59 | |
| 60 | const data = (await response.json()) as { content?: string }; |
| 61 | const html = data.content; |
| 62 | const tagSnippet = |
| 63 | typeof html === "string" ? readTagSnippetByTarget(html, selection) : undefined; |
| 64 | |
| 65 | setAgentPromptTagSnippet((current) => { |
| 66 | if (domEditSelectionRef.current !== selection) return current; |
| 67 | return tagSnippet; |
| 68 | }); |
| 69 | } catch { |
| 70 | // Runtime outerHTML is still available as a synchronous copy fallback. |
| 71 | } |
| 72 | }, |
| 73 | [activeCompPath, domEditSelectionRef, projectIdRef], |
| 74 | ); |
| 75 | |
| 76 | const handleAskAgent = useCallback(() => { |
| 77 | if (!domEditSelection) return; |
| 78 | setAgentPromptTagSnippet(undefined); |
| 79 | setAgentPromptSelectionContext(undefined); |
no test coverage detected