|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from 'react' |
| 4 | + |
| 5 | +const ASCII = [ |
| 6 | + ' ______ _ ______ _ _ _ _ ', |
| 7 | + ' / _____) | | (____ \\ (_) | | | (_) ', |
| 8 | + '| / ___ _ | | ____ ____) )_ _ _| | _ | | ____ ____ _ ___ ', |
| 9 | + '| | / _ \\ / || |/ _ ) __ (| | | | | |/ || |/ _ )/ ___) |/ _ \\ ', |
| 10 | + '| \\____| |_| ( (_| ( (/ /| |__) ) |_| | | ( (_| ( (/ /| | _| | |_| |', |
| 11 | + ' \\______)___/ \\____|\\____)______/ \\____|_|_|\\____|\\____)_| (_)_|\\___/ ', |
| 12 | +] |
| 13 | + |
| 14 | +const PROMPT = "404. The page you requested cannot be found right now. Try typing 'hack the world'." |
| 15 | +const MATRIX_TEXT = |
| 16 | + 'You are a slave. Like everyone else, you were born into bondage, born into a prison that you cannot smell or taste or touch. A prison...for your mind....Unfortunatly, no one can be..._told_ what the Matrix is...you have to see it for yourself.' |
| 17 | + |
| 18 | +export function CliOverlay() { |
| 19 | + const [open, setOpen] = useState(false) |
| 20 | + const [input, setInput] = useState('') |
| 21 | + const [lines, setLines] = useState<string[]>([PROMPT]) |
| 22 | + const [matrixEnabled, setMatrixEnabled] = useState(false) |
| 23 | + const [typingText, setTypingText] = useState('') |
| 24 | + const inputRef = useRef<HTMLInputElement>(null) |
| 25 | + const canvasRef = useRef<HTMLCanvasElement>(null) |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + const openHandler = (event: Event) => { |
| 29 | + const custom = event as CustomEvent<{ open?: boolean }> |
| 30 | + setOpen(custom.detail?.open ?? true) |
| 31 | + setInput('') |
| 32 | + setLines([PROMPT]) |
| 33 | + setTypingText('') |
| 34 | + setMatrixEnabled(false) |
| 35 | + } |
| 36 | + const closeHandler = () => setOpen(false) |
| 37 | + |
| 38 | + window.addEventListener('codebuilder:cli-overlay', openHandler as EventListener) |
| 39 | + window.addEventListener('codebuilder:cli-overlay-close', closeHandler) |
| 40 | + ;(window as Window & { CodeBuilderCliOverlay?: { open: () => void; close: () => void } }).CodeBuilderCliOverlay = { |
| 41 | + open: () => window.dispatchEvent(new CustomEvent('codebuilder:cli-overlay', { detail: { open: true } })), |
| 42 | + close: () => window.dispatchEvent(new Event('codebuilder:cli-overlay-close')), |
| 43 | + } |
| 44 | + |
| 45 | + return () => { |
| 46 | + window.removeEventListener('codebuilder:cli-overlay', openHandler as EventListener) |
| 47 | + window.removeEventListener('codebuilder:cli-overlay-close', closeHandler) |
| 48 | + } |
| 49 | + }, []) |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + if (!open) return |
| 53 | + inputRef.current?.focus() |
| 54 | + }, [open]) |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (!open || !matrixEnabled || !canvasRef.current) return |
| 58 | + const canvas = canvasRef.current |
| 59 | + const context = canvas.getContext('2d') |
| 60 | + if (!context) return |
| 61 | + |
| 62 | + const resize = () => { |
| 63 | + canvas.width = window.innerWidth |
| 64 | + canvas.height = window.innerHeight |
| 65 | + } |
| 66 | + |
| 67 | + resize() |
| 68 | + window.addEventListener('resize', resize) |
| 69 | + |
| 70 | + const chars = '田由甲申甴电甶男甸甹町画甼甽甾甿畀畁畂畃畄畅畆畇畈畉畊畋界畍畎畏畐畑'.split('') |
| 71 | + const fontSize = 12 |
| 72 | + const columns = Math.floor(window.innerWidth / fontSize) |
| 73 | + const drops = Array.from({ length: columns }, () => 1) |
| 74 | + |
| 75 | + const draw = () => { |
| 76 | + context.fillStyle = 'rgba(0, 0, 0, 0.05)' |
| 77 | + context.fillRect(0, 0, canvas.width, canvas.height) |
| 78 | + context.fillStyle = '#0F0' |
| 79 | + context.font = `${fontSize}px monospace` |
| 80 | + for (let i = 0; i < drops.length; i += 1) { |
| 81 | + const text = chars[Math.floor(Math.random() * chars.length)] |
| 82 | + context.fillText(text, i * fontSize, drops[i] * fontSize) |
| 83 | + if (drops[i] * fontSize > canvas.height && Math.random() > 0.975) { |
| 84 | + drops[i] = 0 |
| 85 | + } |
| 86 | + drops[i] += 1 |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const interval = window.setInterval(draw, 33) |
| 91 | + |
| 92 | + return () => { |
| 93 | + window.removeEventListener('resize', resize) |
| 94 | + window.clearInterval(interval) |
| 95 | + } |
| 96 | + }, [open, matrixEnabled]) |
| 97 | + |
| 98 | + useEffect(() => { |
| 99 | + if (!matrixEnabled || !open) return |
| 100 | + let index = 0 |
| 101 | + setTypingText('') |
| 102 | + const timer = window.setInterval(() => { |
| 103 | + index += 1 |
| 104 | + setTypingText(MATRIX_TEXT.slice(0, index)) |
| 105 | + if (index >= MATRIX_TEXT.length) { |
| 106 | + window.clearInterval(timer) |
| 107 | + } |
| 108 | + }, 28) |
| 109 | + return () => window.clearInterval(timer) |
| 110 | + }, [matrixEnabled, open]) |
| 111 | + |
| 112 | + if (!open) return null |
| 113 | + |
| 114 | + const onSubmit = (event: React.FormEvent<HTMLFormElement>) => { |
| 115 | + event.preventDefault() |
| 116 | + const value = input.trim().toLowerCase() |
| 117 | + if (value === 'hack the world') { |
| 118 | + setLines((prev) => [...prev, `> ${input}`]) |
| 119 | + setInput('') |
| 120 | + setMatrixEnabled(true) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + setLines((prev) => [...prev, `> ${input}`, 'Sorry that command is not recognized.']) |
| 125 | + setInput('') |
| 126 | + } |
| 127 | + |
| 128 | + return ( |
| 129 | + <div className="fixed inset-0 z-[200] bg-black"> |
| 130 | + <canvas ref={canvasRef} className="absolute inset-0 z-0 opacity-[0.45]" /> |
| 131 | + <div className="absolute inset-0 pointer-events-none bg-[repeating-linear-gradient(to_bottom,rgba(255,255,255,0.04)_0px,rgba(255,255,255,0.04)_1px,transparent_1px,transparent_3px)] opacity-[0.18]" /> |
| 132 | + |
| 133 | + <div className="relative z-10 flex min-h-screen items-center justify-center px-4 py-6"> |
| 134 | + <div className="shadow-[0_0_0_1px_rgba(31,240,66,0.14),0_28px_80px_rgba(0,0,0,0.66)] relative w-full max-w-6xl rounded-[8px] border border-[rgba(31,240,66,0.18)] bg-[rgba(0,0,0,0.82)] px-5 py-6 text-[#1ff042] md:px-8 md:py-8"> |
| 135 | + <div className="mb-4 flex items-center justify-between text-[12px] uppercase tracking-[0.18em] text-[#1ff042]/75"> |
| 136 | + <span className="inline-flex items-center gap-3"> |
| 137 | + <span className="inline-flex h-3 w-3 rounded-full bg-[#1ff042]" /> |
| 138 | + <span>codebuilder terminal</span> |
| 139 | + </span> |
| 140 | + <button |
| 141 | + type="button" |
| 142 | + onClick={() => setOpen(false)} |
| 143 | + className="rounded border border-[#1ff042]/30 px-3 py-1 text-[#1ff042]/85" |
| 144 | + > |
| 145 | + close |
| 146 | + </button> |
| 147 | + </div> |
| 148 | + |
| 149 | + <pre className="overflow-x-auto text-[12px] leading-[1.1] text-white md:text-[15px]">{ASCII.join('\n')}</pre> |
| 150 | + |
| 151 | + <div className="mt-5 space-y-3 font-mono text-[14px] font-bold uppercase tracking-[0.14em] text-[#1ff042]"> |
| 152 | + {lines.map((line, index) => ( |
| 153 | + <p key={`${line}-${index}`} className="drop-shadow-[0_0_3px_rgba(31,240,66,0.7)]"> |
| 154 | + > {line} |
| 155 | + </p> |
| 156 | + ))} |
| 157 | + {matrixEnabled ? <p className="drop-shadow-[0_0_3px_rgba(31,240,66,0.7)]">{typingText}</p> : null} |
| 158 | + </div> |
| 159 | + |
| 160 | + <form onSubmit={onSubmit} className="mt-4"> |
| 161 | + <label className="sr-only" htmlFor="cli-overlay-input"> |
| 162 | + Terminal input |
| 163 | + </label> |
| 164 | + <div className="flex items-center gap-3 font-mono text-[14px] font-bold uppercase tracking-[0.14em] text-[#1ff042]"> |
| 165 | + <span>></span> |
| 166 | + <input |
| 167 | + id="cli-overlay-input" |
| 168 | + ref={inputRef} |
| 169 | + value={input} |
| 170 | + onChange={(event) => setInput(event.target.value)} |
| 171 | + className="min-w-0 flex-1 border-0 bg-transparent p-0 text-[#1ff042] outline-none placeholder:text-[#1ff042]/40" |
| 172 | + placeholder="type a command" |
| 173 | + autoComplete="off" |
| 174 | + autoCorrect="off" |
| 175 | + spellCheck={false} |
| 176 | + /> |
| 177 | + </div> |
| 178 | + </form> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + ) |
| 183 | +} |
0 commit comments