2024-10-21 13:57:45 -06:00
|
|
|
"use client"
|
2024-07-23 17:30:35 -04:00
|
|
|
|
2024-10-21 13:57:45 -06:00
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
import { usePreview } from "@/context/PreviewContext"
|
|
|
|
import { useTerminal } from "@/context/TerminalContext"
|
|
|
|
import { Sandbox } from "@/lib/types"
|
|
|
|
import { Play, StopCircle } from "lucide-react"
|
|
|
|
import { useEffect, useRef } from "react"
|
|
|
|
import { toast } from "sonner"
|
2024-11-24 00:22:10 -05:00
|
|
|
import { templateConfigs } from "@/lib/templates"
|
2024-07-23 17:30:35 -04:00
|
|
|
|
|
|
|
export default function RunButtonModal({
|
|
|
|
isRunning,
|
|
|
|
setIsRunning,
|
2024-08-19 20:39:04 -04:00
|
|
|
sandboxData,
|
2024-07-23 17:30:35 -04:00
|
|
|
}: {
|
2024-10-21 13:57:45 -06:00
|
|
|
isRunning: boolean
|
|
|
|
setIsRunning: (running: boolean) => void
|
|
|
|
sandboxData: Sandbox
|
2024-07-23 17:30:35 -04:00
|
|
|
}) {
|
2024-10-21 13:57:45 -06:00
|
|
|
const { createNewTerminal, closeTerminal, terminals } = useTerminal()
|
|
|
|
const { setIsPreviewCollapsed, previewPanelRef } = usePreview()
|
2024-09-22 00:23:12 -04:00
|
|
|
// Ref to keep track of the last created terminal's ID
|
2024-10-21 13:57:45 -06:00
|
|
|
const lastCreatedTerminalRef = useRef<string | null>(null)
|
2024-07-23 17:30:35 -04:00
|
|
|
|
2024-09-22 00:23:12 -04:00
|
|
|
// Effect to update the lastCreatedTerminalRef when a new terminal is added
|
|
|
|
useEffect(() => {
|
|
|
|
if (terminals.length > 0 && !isRunning) {
|
2024-10-21 13:57:45 -06:00
|
|
|
const latestTerminal = terminals[terminals.length - 1]
|
|
|
|
if (
|
|
|
|
latestTerminal &&
|
|
|
|
latestTerminal.id !== lastCreatedTerminalRef.current
|
|
|
|
) {
|
|
|
|
lastCreatedTerminalRef.current = latestTerminal.id
|
2024-07-23 17:30:35 -04:00
|
|
|
}
|
2024-09-22 00:23:12 -04:00
|
|
|
}
|
2024-10-21 13:57:45 -06:00
|
|
|
}, [terminals, isRunning])
|
2024-11-24 22:28:32 -05:00
|
|
|
// commands to run in the terminal
|
|
|
|
const COMMANDS = {
|
|
|
|
streamlit: "./venv/bin/streamlit run main.py --server.runOnSave true",
|
|
|
|
php: "echo http://localhost:80 && npx vite",
|
|
|
|
default: "npm run dev",
|
|
|
|
} as const
|
2024-09-22 00:23:12 -04:00
|
|
|
const handleRun = async () => {
|
2024-10-21 13:57:45 -06:00
|
|
|
if (isRunning && lastCreatedTerminalRef.current) {
|
|
|
|
await closeTerminal(lastCreatedTerminalRef.current)
|
|
|
|
lastCreatedTerminalRef.current = null
|
|
|
|
setIsPreviewCollapsed(true)
|
|
|
|
previewPanelRef.current?.collapse()
|
|
|
|
} else if (!isRunning && terminals.length < 4) {
|
2024-11-24 00:22:10 -05:00
|
|
|
const command = templateConfigs[sandboxData.type]?.runCommand || "npm run dev"
|
2024-10-21 13:57:45 -06:00
|
|
|
|
2024-09-22 00:23:12 -04:00
|
|
|
try {
|
|
|
|
// Create a new terminal with the appropriate command
|
2024-10-21 13:57:45 -06:00
|
|
|
await createNewTerminal(command)
|
|
|
|
setIsPreviewCollapsed(false)
|
|
|
|
previewPanelRef.current?.expand()
|
2024-09-22 00:23:12 -04:00
|
|
|
} catch (error) {
|
2024-10-21 13:57:45 -06:00
|
|
|
toast.error("Failed to create new terminal.")
|
|
|
|
console.error("Error creating new terminal:", error)
|
|
|
|
return
|
2024-09-22 00:23:12 -04:00
|
|
|
}
|
|
|
|
} else if (!isRunning) {
|
2024-10-21 13:57:45 -06:00
|
|
|
toast.error("You've reached the maximum number of terminals.")
|
|
|
|
return
|
2024-07-23 17:30:35 -04:00
|
|
|
}
|
2024-09-22 00:23:12 -04:00
|
|
|
|
2024-10-21 13:57:45 -06:00
|
|
|
setIsRunning(!isRunning)
|
|
|
|
}
|
2024-07-23 17:30:35 -04:00
|
|
|
|
|
|
|
return (
|
2024-09-22 00:23:12 -04:00
|
|
|
<Button variant="outline" onClick={handleRun}>
|
2024-10-21 13:57:45 -06:00
|
|
|
{isRunning ? (
|
|
|
|
<StopCircle className="w-4 h-4 mr-2" />
|
|
|
|
) : (
|
|
|
|
<Play className="w-4 h-4 mr-2" />
|
|
|
|
)}
|
|
|
|
{isRunning ? "Stop" : "Run"}
|
2024-09-22 00:23:12 -04:00
|
|
|
</Button>
|
2024-10-21 13:57:45 -06:00
|
|
|
)
|
|
|
|
}
|