| 1 |
import { useStatusStore } from '@agent/state/status'; |
| 2 |
import { useEffect, useMemo, useState } from '@wordpress/element'; |
| 3 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
|
| 6 |
const linesFor = (label) => ({ |
| 7 |
'calling-agent': __('Thinking...', 'extendify-local'), |
| 8 |
'agent-working': [ |
| 9 |
__('Working on it...', 'extendify-local'), |
| 10 |
__('Interpreting message...', 'extendify-local'), |
| 11 |
__('Formulating a response...', 'extendify-local'), |
| 12 |
__('Reviewing logic...', 'extendify-local'), |
| 13 |
], |
| 14 |
'workflow-tool-processing': __('Processing...', 'extendify-local'), |
| 15 |
'tool-started': label || __('Gathering data...', 'extendify-local'), |
| 16 |
'credits-exhausted': __('Usage limit reached', 'extendify-local'), |
| 17 |
'credits-restored': __('Usage limit restored', 'extendify-local'), |
| 18 |
}); |
| 19 |
|
| 20 |
// Randomized 3-5s beat so a long wait never looks stuck or syncs across sites. |
| 21 |
const useCurrentLine = (lines) => { |
| 22 |
const [index, setIndex] = useState(0); |
| 23 |
const isList = Array.isArray(lines); |
| 24 |
|
| 25 |
useEffect(() => setIndex(0), [lines]); |
| 26 |
|
| 27 |
useEffect(() => { |
| 28 |
if (!isList) return; |
| 29 |
const timer = setTimeout( |
| 30 |
() => setIndex((i) => (i + 1) % lines.length), |
| 31 |
3000 + Math.random() * 2000, |
| 32 |
); |
| 33 |
return () => clearTimeout(timer); |
| 34 |
}, [isList, lines, index]); |
| 35 |
|
| 36 |
return isList ? lines[index] : lines; |
| 37 |
}; |
| 38 |
|
| 39 |
export const StatusIndicator = () => { |
| 40 |
const leavingPage = useStatusStore((s) => s.leavingPage); |
| 41 |
const { type, label } = useStatusStore((s) => s.statuses.at(-1)) ?? {}; |
| 42 |
const lines = useMemo(() => linesFor(label)[type], [label, type]); |
| 43 |
const currentLine = useCurrentLine(lines); |
| 44 |
const text = leavingPage |
| 45 |
? __('Refreshing the page to show your changes…', 'extendify-local') |
| 46 |
: currentLine; |
| 47 |
|
| 48 |
if (!text) return null; |
| 49 |
|
| 50 |
return ( |
| 51 |
<div className="p-2 text-center text-xs italic text-gray-700"> |
| 52 |
{/* Reusing the node drops the new line mid-sweep. */} |
| 53 |
<span key={text} className="status-animation"> |
| 54 |
{decodeEntities(text)} |
| 55 |
</span> |
| 56 |
</div> |
| 57 |
); |
| 58 |
}; |
| 59 |
|