import type { Dispatch } from 'react'; import type { AppAction, SupportedLanguage } from '@/types'; const MAX_CODE_LENGTH = 10_000; export function useCodeExecution(dispatch: Dispatch) { async function execute(language: SupportedLanguage, code: string): Promise { if (code.trim().length === 0) return; if (code.length > MAX_CODE_LENGTH) { dispatch({ type: 'SET_ERROR', payload: 'Code exceeds the 10,000 character limit.' }); return; } // HTML is handled by HtmlPreview client-side if (language === 'html') { return; } dispatch({ type: 'EXECUTE_START' }); try { const res = await fetch('/api/execute', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ language, code }), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error ?? `Server error ${res.status}`); } const result = await res.json(); dispatch({ type: 'EXECUTE_DONE', payload: result }); } catch (err) { dispatch({ type: 'EXECUTE_DONE', payload: { stdout: '', stderr: '', exitCode: -1, timedOut: false, error: err instanceof Error ? err.message : 'Execution failed', }, }); } } return { execute }; }