Server IP : 66.29.132.124 / Your IP : 18.191.72.220 Web Server : LiteSpeed System : Linux business141.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : wavevlvu ( 1524) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/wavevlvu/cynthiaadediran.com/wp-content/plugins/extendify/src/Draft/hooks/ |
Upload File : |
import { useEffect, useState } from '@wordpress/element'; import { completion } from '@draft/api/Data'; export const useCompletion = ( prompt, promptType, systemMessageKey, details, ) => { const [result, setResult] = useState(''); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { let cancelled = false; let reader; const decoder = new TextDecoder(); if (!prompt) { setLoading(false); setResult(''); cancelled = true; return; } const fetchData = async () => { setResult(''); setError(false); setLoading(true); const response = await completion( prompt, promptType, systemMessageKey, details, ); reader = response.body.getReader(); let done = false; while (!done) { const { value, done: readerDone } = await reader.read(); done = readerDone; if (value && !cancelled) { const decodedValue = decoder.decode(value); setResult((prevResult) => prevResult + decodedValue); } } }; fetchData() .finally(() => { if (!cancelled) { setLoading(false); } }) .catch((error) => { if (!cancelled) { setError(error); } }); return () => { cancelled = true; if (reader) { reader.cancel(); } }; }, [prompt, systemMessageKey, promptType, details]); return { completion: result, error, loading }; };