|
| 1 | +import { useStore } from '@nanostores/react'; |
| 2 | +import { memo, useEffect, useRef } from 'react'; |
| 3 | +import { workbenchStore } from '~/lib/stores/workbench'; |
| 4 | +import { motion, AnimatePresence } from 'framer-motion'; |
| 5 | +import { classNames } from '~/utils/classNames'; |
| 6 | + |
| 7 | +export const LiveActionAlert = memo(() => { |
| 8 | + const alert = useStore(workbenchStore.actionAlert); |
| 9 | + const outputRef = useRef<HTMLDivElement>(null); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + if (outputRef.current && alert?.isStreaming) { |
| 13 | + outputRef.current.scrollTop = outputRef.current.scrollHeight; |
| 14 | + } |
| 15 | + }, [alert?.streamingOutput]); |
| 16 | + |
| 17 | + if (!alert || !alert.isStreaming) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + return ( |
| 22 | + <AnimatePresence> |
| 23 | + <motion.div |
| 24 | + initial={{ opacity: 0, y: 20, scale: 0.95 }} |
| 25 | + animate={{ opacity: 1, y: 0, scale: 1 }} |
| 26 | + exit={{ opacity: 0, y: 20, scale: 0.95 }} |
| 27 | + transition={{ duration: 0.2, ease: 'easeOut' }} |
| 28 | + className="fixed bottom-4 right-4 w-96 max-h-80 bg-codinit-elements-background-depth-1 |
| 29 | + border border-codinit-elements-borderColor rounded-lg shadow-2xl z-50 |
| 30 | + flex flex-col overflow-hidden" |
| 31 | + > |
| 32 | + <div className="p-3 border-b border-codinit-elements-borderColor flex items-center justify-between bg-codinit-elements-background-depth-2"> |
| 33 | + <div className="flex items-center gap-2 flex-1 min-w-0"> |
| 34 | + <motion.div |
| 35 | + animate={{ rotate: 360 }} |
| 36 | + transition={{ duration: 2, repeat: Infinity, ease: 'linear' }} |
| 37 | + className="flex-shrink-0" |
| 38 | + > |
| 39 | + <div className="i-svg-spinners:90-ring-with-bg text-blue-500 text-lg" /> |
| 40 | + </motion.div> |
| 41 | + <div className="flex-1 min-w-0"> |
| 42 | + <div className="font-medium text-sm text-codinit-elements-textPrimary truncate">{alert.title}</div> |
| 43 | + {alert.command && ( |
| 44 | + <div className="text-xs text-codinit-elements-textSecondary truncate font-mono">{alert.command}</div> |
| 45 | + )} |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + <button |
| 49 | + onClick={() => workbenchStore.actionAlert.set(undefined)} |
| 50 | + className={classNames( |
| 51 | + 'flex-shrink-0 ml-2 p-1 rounded hover:bg-codinit-elements-background-depth-3', |
| 52 | + 'text-codinit-elements-textSecondary hover:text-codinit-elements-textPrimary', |
| 53 | + 'transition-colors', |
| 54 | + )} |
| 55 | + title="Close" |
| 56 | + > |
| 57 | + <div className="i-ph:x text-sm" /> |
| 58 | + </button> |
| 59 | + </div> |
| 60 | + |
| 61 | + <div |
| 62 | + ref={outputRef} |
| 63 | + className="flex-1 p-3 overflow-y-auto overflow-x-hidden font-mono text-xs |
| 64 | + text-codinit-elements-textPrimary bg-codinit-elements-background-depth-1 |
| 65 | + scrollbar-thin scrollbar-thumb-codinit-elements-borderColor |
| 66 | + scrollbar-track-transparent" |
| 67 | + > |
| 68 | + <pre className="whitespace-pre-wrap break-words">{alert.streamingOutput || alert.content}</pre> |
| 69 | + </div> |
| 70 | + |
| 71 | + {alert.progress !== undefined && alert.progress >= 0 && ( |
| 72 | + <div className="p-2 border-t border-codinit-elements-borderColor bg-codinit-elements-background-depth-2"> |
| 73 | + <div className="flex items-center justify-between mb-1"> |
| 74 | + <span className="text-xs text-codinit-elements-textSecondary">Progress</span> |
| 75 | + <span className="text-xs font-medium text-codinit-elements-textPrimary"> |
| 76 | + {Math.round(alert.progress)}% |
| 77 | + </span> |
| 78 | + </div> |
| 79 | + <div className="h-1 bg-codinit-elements-background-depth-3 rounded-full overflow-hidden"> |
| 80 | + <motion.div |
| 81 | + className="h-full bg-blue-500" |
| 82 | + initial={{ width: 0 }} |
| 83 | + animate={{ width: `${alert.progress}%` }} |
| 84 | + transition={{ duration: 0.3, ease: 'easeOut' }} |
| 85 | + /> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + )} |
| 89 | + </motion.div> |
| 90 | + </AnimatePresence> |
| 91 | + ); |
| 92 | +}); |
| 93 | + |
| 94 | +LiveActionAlert.displayName = 'LiveActionAlert'; |
0 commit comments