|
1 | 1 | import { type FC } from 'react';
|
2 | 2 | import styles from './index.module.scss';
|
| 3 | +import { useState, useCallback, useEffect, useRef } from 'react'; |
3 | 4 |
|
4 | 5 | const VersionCard: FC = () => {
|
| 6 | + const [copied, setCopied] = useState(false); |
| 7 | + const [currentCommand, setCurrentCommand] = useState(0); |
| 8 | + const [isHovered, setIsHovered] = useState(false); |
| 9 | + const [isFading, setIsFading] = useState(false); |
| 10 | + const intervalRef = useRef<NodeJS.Timeout>(); |
| 11 | + |
| 12 | + const commands = [ |
| 13 | + 'curl https://aiscript.dev/install.sh | sh', |
| 14 | + 'cargo install aiscript' |
| 15 | + ]; |
| 16 | + |
| 17 | + const handleCopy = useCallback(() => { |
| 18 | + navigator.clipboard.writeText(commands[currentCommand]).then(() => { |
| 19 | + setCopied(true); |
| 20 | + setTimeout(() => setCopied(false), 2000); |
| 21 | + }); |
| 22 | + }, [currentCommand, commands]); |
| 23 | + |
| 24 | + const switchCommand = useCallback(() => { |
| 25 | + setIsFading(true); |
| 26 | + setTimeout(() => { |
| 27 | + setCurrentCommand((prev) => (prev + 1) % commands.length); |
| 28 | + setIsFading(false); |
| 29 | + }, 300); |
| 30 | + }, [commands.length]); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + if (!isHovered) { |
| 34 | + intervalRef.current = setInterval(switchCommand, 3000); |
| 35 | + } |
| 36 | + return () => { |
| 37 | + if (intervalRef.current) { |
| 38 | + clearInterval(intervalRef.current); |
| 39 | + } |
| 40 | + }; |
| 41 | + }, [isHovered, switchCommand]); |
| 42 | + |
| 43 | + const handleMouseEnter = useCallback(() => { |
| 44 | + setIsHovered(true); |
| 45 | + }, []); |
| 46 | + |
| 47 | + const handleMouseLeave = useCallback(() => { |
| 48 | + setIsHovered(false); |
| 49 | + }, []); |
| 50 | + |
5 | 51 | return (
|
6 | 52 | <div className={styles.versionCard}>
|
7 | 53 | <div className={styles.content}>
|
8 | 54 | <div className={styles.version}>
|
9 | 55 | <span className={styles.label}>Latest Version</span>
|
10 | 56 | <span className={styles.value}>v0.1.0</span>
|
11 | 57 | </div>
|
12 |
| - <div className={styles.command}> |
13 |
| - <code className={styles.commandText}> |
14 |
| - curl https://aiscript.dev/install.sh | sh |
| 58 | + <div |
| 59 | + className={`${styles.command} ${copied ? styles.copied : ''}`} |
| 60 | + onMouseEnter={handleMouseEnter} |
| 61 | + onMouseLeave={handleMouseLeave} |
| 62 | + > |
| 63 | + <code className={`${styles.commandText} ${isFading ? styles.fade : ''}`}> |
| 64 | + {commands[currentCommand]} |
15 | 65 | </code>
|
| 66 | + <button |
| 67 | + className={styles.copyButton} |
| 68 | + onClick={handleCopy} |
| 69 | + title="Copy to clipboard" |
| 70 | + > |
| 71 | + {copied ? 'Copied!' : 'Copy'} |
| 72 | + </button> |
16 | 73 | </div>
|
17 | 74 | </div>
|
18 | 75 | </div>
|
|
0 commit comments