1"use client"
2
3import { useEffect, useMemo, useState, type ReactElement } from "react"
4import { TerminalIcon } from "lucide-react"
5import { GoTerminal } from "react-icons/go"
6
7type TerminalStep = {
8 text: string
9 bold?: boolean
10}
11
12type TerminalProps = {
13 command: string
14 steps: TerminalStep[]
15 pulseInterval?: number
16 showLocalhost?: boolean
17 hostBarTitle?: string
18 hostMessage?: string
19}
20
21function MacControls() {
22 return (
23 <>
24 <GoTerminal className="text-muted-foreground mr-1 size-4" />
25 <div className="h-2 w-2 rounded-full bg-red-500" />
26 <div className="h-2 w-2 rounded-full bg-yellow-500" />
27 <div className="h-2 w-2 rounded-full bg-green-500" />
28 </>
29 )
30}
31
32function LocalHost({ title, message }: { title: string; message: string }) {
33 return (
34 <div className="bg-card animate-in fade-in slide-in-from-top-10 absolute right-4 bottom-5 z-10 overflow-hidden rounded-md border shadow-xl">
35 <div className="bg-muted text-muted-foreground relative flex h-6 flex-row items-center border-b px-4 text-xs">
36 <TerminalIcon className="absolute inset-2 size-3" />
37 <p className="absolute inset-x-0 text-center">{title}</p>
38 </div>
39 <div className="text-card-foreground p-4 text-sm">{message}</div>
40 </div>
41 )
42}
43
44const Terminal = ({
45 command,
46 steps,
47 pulseInterval = 100,
48 showLocalhost = true,
49 hostBarTitle = "localhost:3000",
50 hostMessage = "New App Created!",
51}: TerminalProps) => {
52 const typingLen = useMemo(() => command.length, [command])
53 const revealLen = useMemo(() => steps.length, [steps])
54 const finalCount = typingLen + revealLen + 1
55
56 const [counter, setCounter] = useState(finalCount)
57
58 useEffect(() => {
59 const interval = setInterval(() => {
60 setCounter((prev) => (prev >= finalCount ? prev : prev + 1))
61 }, pulseInterval)
62 return () => clearInterval(interval)
63 }, [pulseInterval, finalCount])
64
65 const elements: ReactElement[] = []
66
67 const typedChars = Math.min(counter, typingLen)
68 const isTyping = counter < typingLen
69
70 elements.push(
71 <span key="command" className="text-foreground">
72 {command.substring(0, typedChars)}
73 {isTyping && (
74 <div className="bg-foreground inline-block h-3 w-1 animate-pulse" />
75 )}
76 </span>
77 )
78
79 if (!isTyping) {
80 const shownSteps = Math.min(revealLen, counter - typingLen)
81 if (shownSteps > 0) {
82 elements.push(<span key="space"> </span>)
83 }
84 for (let i = 0; i < shownSteps; i++) {
85 const step = steps[i]
86 elements.push(
87 <span
88 key={`step-${i}`}
89 className={
90 step.bold ? "text-foreground font-bold" : "text-foreground"
91 }
92 >
93 {step.text}
94 </span>
95 )
96 }
97 }
98
99 const revealComplete = counter > typingLen + revealLen
100
101 return (
102 <div
103 className="relative"
104 onMouseEnter={() => {
105 if (counter >= finalCount) {
106 setCounter(0)
107 }
108 }}
109 >
110 {showLocalhost && revealComplete && (
111 <LocalHost title={hostBarTitle} message={hostMessage} />
112 )}
113
114 <pre className="bg-card w-full min-w-[320px] overflow-hidden rounded-xl border text-[11px] shadow-lg sm:min-w-[480px] sm:text-[12px] md:min-w-[600px] md:text-[13px]">
115 <div className="bg-muted flex flex-row items-center gap-2 border-b px-3 py-2 sm:px-4">
116 <MacControls />
117 </div>
118 <div className="from-background to-muted min-h-[150px] bg-gradient-to-b sm:min-h-[180px] md:min-h-[200px]">
119 <div className="grid p-3 whitespace-pre-wrap sm:p-4">{elements}</div>
120 </div>
121 </pre>
122 </div>
123 )
124}
125
126export default Terminal
127