Compare commits

..

13 Commits

Author SHA1 Message Date
0117e197bb fix: prevent duplicate tabs when clicking file paths
Modify selectFile function to check for existing tabs before creating new ones.
When clicking file paths in AIChat, it will now switch to the existing tab
instead of creating duplicates.
2025-01-01 22:31:23 -05:00
37ebe41526 fix: ctrl/cmd + z with applied code 2025-01-01 08:15:53 -05:00
ed367ed27c fix: close the SSH channel when a Dokku command is finished 2025-01-01 07:58:44 -05:00
9666401625 chore: add dokku-daemon instructions to README 2025-01-01 07:58:25 -05:00
22d638a090 feat: hide the deploy button when the Dokku server is not connected 2025-01-01 07:58:08 -05:00
0dd99cbc77 fix: apply button file save 2024-12-31 08:03:19 -05:00
093a4e9758 fix: correctly generate remoteFileId for paths without a leading slash 2024-12-09 13:59:47 -08:00
6a60f4d286 Update README.md 2024-12-06 14:32:17 -05:00
260110724e chore: update .env.example 2024-12-06 14:24:59 -05:00
ee51ae7a33 feat: correctly show whether a project has been deployed 2024-12-01 20:07:01 -08:00
0d0eed34b2 fix: apply code- discard button 2024-12-01 22:15:16 -05:00
4e1c5cac27 feat: Add clickable file paths in AI chat responses
- Detect file paths with dots in directory names (e.g. next/styles/SignIn.module.css)
- Create new files when path ends with "(new file)"
- Use existing socket connection and file management system
2024-12-01 18:52:28 -05:00
0ae89341d2 feat: file path above each code snippet 2024-12-01 14:29:23 -05:00
17 changed files with 313 additions and 115 deletions

View File

@ -2,7 +2,7 @@
![2024-10-2307 17 42-ezgif com-resize](https://github.com/user-attachments/assets/a4057129-81a7-4a31-a093-c8bc8189ae72)
Sandbox is an open-source cloud-based code editing environment with custom AI code generation, live preview, real-time collaboration and AI chat.
Sandbox is an open-source cloud-based code editing environment with custom AI code generation, live preview, real-time collaboration, and AI chat.
For the latest updates, join our Discord server: [discord.gitwit.dev](https://discord.gitwit.dev/).
@ -10,11 +10,11 @@ For the latest updates, join our Discord server: [discord.gitwit.dev](https://di
Notes:
- Double check that whatever you change "SUPERDUPERSECRET" to, it's the same in all config files.
- Double-check that whatever you change "SUPERDUPERSECRET" to, it's the same in all config files.
### 0. Requirements
The application uses NodeJS for the backend, NextJS for the frontend and Cloudflare workers for additional backend tasks.
The application uses NodeJS for the backend, NextJS for the frontend, and Cloudflare workers for additional backend tasks.
Needed accounts to set up:
@ -42,7 +42,6 @@ Run `npm install` in:
/backend/database
/backend/storage
/backend/server
/backend/ai
```
### 2. Adding Clerk
@ -175,6 +174,15 @@ Setting up deployments first requires a separate domain (such as gitwit.app, whi
We then deploy Dokku on a separate server, according to this guide: https://dev.to/jamesmurdza/host-your-own-paas-platform-as-a-service-on-amazon-web-services-3f0d
And we install [dokku-daemon](https://github.com/dokku/dokku-daemon) with the following commands:
```
git clone https://github.com/dokku/dokku-daemon
cd dokku-daemon
sudo make install
systemctl start dokku-daemon
```
The Sandbox platform connects to the Dokku server via SSH, using SSH keys specifically generated for this connection. The SSH key is stored on the Sandbox server, and the following environment variables are set in /backend/server/.env:
```bash
@ -248,8 +256,7 @@ backend/
├── database/
│ ├── src
│ └── drizzle
── storage
└── ai
── storage
```
| Path | Description |

View File

@ -7,7 +7,6 @@ PORT=4000
WORKERS_KEY=
DATABASE_WORKER_URL=
STORAGE_WORKER_URL=
AI_WORKER_URL=
E2B_API_KEY=
DOKKU_HOST=
DOKKU_USERNAME=

View File

@ -92,7 +92,11 @@ export class FileManager {
// Convert local file path to remote path
private getRemoteFileId(localId: string): string {
return `projects/${this.sandboxId}${localId}`
return [
"projects",
this.sandboxId,
localId.startsWith("/") ? localId : localId,
].join("/")
}
// Convert remote file path to local file path
@ -322,25 +326,69 @@ export class FileManager {
// Save file content
async saveFile(fileId: string, body: string): Promise<void> {
if (!fileId) return // handles saving when no file is open
if (Buffer.byteLength(body, "utf-8") > MAX_BODY_SIZE) {
throw new Error("File size too large. Please reduce the file size.")
}
// Save to remote storage
await RemoteFileStorage.saveFile(this.getRemoteFileId(fileId), body)
// Update local file data cache
let file = this.fileData.find((f) => f.id === fileId)
if (file) {
file.data = body
} else {
// If the file wasn't in our cache, add it
file = {
id: fileId,
data: body,
}
this.fileData.push(file)
}
await this.sandbox.files.write(path.posix.join(this.dirName, fileId), body)
// Save to sandbox filesystem
const filePath = path.posix.join(this.dirName, fileId)
await this.sandbox.files.write(filePath, body)
// Instead of updating the entire file structure, just ensure this file exists in it
const parts = fileId.split('/').filter(Boolean)
let current = this.files
let currentPath = ''
// Navigate/create the path to the file
for (let i = 0; i < parts.length - 1; i++) {
currentPath += '/' + parts[i]
let folder = current.find(
(f) => f.type === 'folder' && f.name === parts[i]
) as TFolder
if (!folder) {
folder = {
id: currentPath,
type: 'folder',
name: parts[i],
children: [],
}
current.push(folder)
}
current = folder.children
}
// Add/update the file in the structure if it doesn't exist
const fileName = parts[parts.length - 1]
const existingFile = current.find(
(f) => f.type === 'file' && f.name === fileName
)
if (!existingFile) {
current.push({
id: fileId,
type: 'file',
name: fileName,
})
}
this.refreshFileList?.(this.files)
this.fixPermissions()
}

View File

@ -13,7 +13,10 @@ export class SSHSocketClient {
private conn: Client
private config: SSHConfig
private socketPath: string
private isConnected: boolean = false
private _isConnected: boolean = false
public get isConnected(): boolean {
return this._isConnected
}
// Constructor initializes the SSH client and sets up configuration
constructor(config: SSHConfig, socketPath: string) {
@ -34,7 +37,7 @@ export class SSHSocketClient {
private closeConnection() {
console.log("Closing SSH connection...")
this.conn.end()
this.isConnected = false
this._isConnected = false
process.exit(0)
}
@ -44,17 +47,17 @@ export class SSHSocketClient {
this.conn
.on("ready", () => {
console.log("SSH connection established")
this.isConnected = true
this._isConnected = true
resolve()
})
.on("error", (err) => {
console.error("SSH connection error:", err)
this.isConnected = false
this._isConnected = false
reject(err)
})
.on("close", () => {
console.log("SSH connection closed")
this.isConnected = false
this._isConnected = false
})
.connect(this.config)
})
@ -86,10 +89,13 @@ export class SSHSocketClient {
)
})
.on("data", (data: Buffer) => {
// Netcat remains open until it is closed, so we close the connection once we receive data.
resolve(data.toString())
stream.close()
})
.stderr.on("data", (data: Buffer) => {
reject(new Error(data.toString()))
stream.close()
})
}
)

View File

@ -170,8 +170,20 @@ export class Sandbox {
// Handle checking if an app exists
const handleAppExists: SocketHandler = async ({ appName }) => {
if (!this.dokkuClient)
throw new Error("Failed to check app existence: No Dokku client")
if (!this.dokkuClient) {
console.log("Failed to check app existence: No Dokku client")
return {
success: false,
}
}
if (!this.dokkuClient.isConnected) {
console.log(
"Failed to check app existence: The Dokku client is not connected"
)
return {
success: false,
}
}
return {
success: true,
exists: await this.dokkuClient.appExists(appName),

View File

@ -3,18 +3,21 @@ CLERK_SECRET_KEY=
NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_KEY=
LIVEBLOCKS_SECRET_KEY=
NEXT_PUBLIC_SERVER_URL=http://localhost:4000
NEXT_PUBLIC_SERVER_PORT=4000
NEXT_PUBLIC_APP_URL=http://localhost:3000
NEXT_PUBLIC_API_URL=http://localhost:4000
NEXT_PUBLIC_SERVER_URL=http://localhost:4000
# Set WORKER_URLs after deploying the workers.
# Set NEXT_PUBLIC_WORKERS_KEY to be the same as KEY in /backend/storage/wrangler.toml.
# These URLs should begin with https:// in production
NEXT_PUBLIC_DATABASE_WORKER_URL=
NEXT_PUBLIC_STORAGE_WORKER_URL=
NEXT_PUBLIC_AI_WORKER_URL=
NEXT_PUBLIC_WORKERS_KEY=
NEXT_PUBLIC_DATABASE_WORKER_URL=https://database.your-worker.workers.dev
NEXT_PUBLIC_STORAGE_WORKER_URL=https://storage.your-worker.workers.dev
NEXT_PUBLIC_WORKERS_KEY=SUPERDUPERSECRET
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/dashboard
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/dashboard
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/dashboard
ANTHROPIC_API_KEY=
OPENAI_API_KEY=

View File

@ -1,16 +1,22 @@
import { currentUser } from "@clerk/nextjs"
import { Anthropic } from "@anthropic-ai/sdk"
import { TIERS } from "@/lib/tiers"
import {
ignoredFiles,
ignoredFolders,
} from "@/components/editor/AIChat/lib/ignored-paths"
import { templateConfigs } from "@/lib/templates"
import { TIERS } from "@/lib/tiers"
import { TFile, TFolder } from "@/lib/types"
import { ignoredFiles, ignoredFolders } from "@/components/editor/AIChat/lib/ignored-paths"
import { Anthropic } from "@anthropic-ai/sdk"
import { currentUser } from "@clerk/nextjs"
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY!,
})
// Format file structure for context
function formatFileStructure(items: (TFile | TFolder)[] | undefined, prefix = ""): string {
// Format file structure for context
function formatFileStructure(
items: (TFile | TFolder)[] | undefined,
prefix = ""
): string {
if (!items || !Array.isArray(items)) {
return "No files available"
}
@ -23,15 +29,23 @@ function formatFileStructure(items: (TFile | TFolder)[] | undefined, prefix = ""
return sortedItems
.map((item) => {
if (item.type === "file" && !ignoredFiles.some(
(pattern) => item.name.endsWith(pattern.replace("*", "")) || item.name === pattern
)) {
if (
item.type === "file" &&
!ignoredFiles.some(
(pattern) =>
item.name.endsWith(pattern.replace("*", "")) ||
item.name === pattern
)
) {
return `${prefix}├── ${item.name}`
} else if (
item.type === "folder" &&
item.type === "folder" &&
!ignoredFolders.some((folder) => folder === item.name)
) {
const folderContent = formatFileStructure(item.children, `${prefix}`)
const folderContent = formatFileStructure(
item.children,
`${prefix}`
)
return `${prefix}├── ${item.name}/\n${folderContent}`
}
return null
@ -94,6 +108,7 @@ export async function POST(request: Request) {
line,
templateType,
files,
projectName,
} = await request.json()
// Get template configuration
@ -138,13 +153,23 @@ Instructions: ${messages[0].content}
Respond only with the modified code that can directly replace the existing code.`
} else {
systemMessage = `You are an intelligent programming assistant for a ${templateType} project. Please respond to the following request concisely. If your response includes code, please format it using triple backticks (\`\`\`) with the appropriate language identifier. For example:
systemMessage = `You are an intelligent programming assistant for a ${templateType} project. Please respond to the following request concisely. When providing code:
\`\`\`python
print("Hello, World!")
\`\`\`
Provide a clear and concise explanation along with any code snippets. Keep your response brief and to the point.
1. Format it using triple backticks (\`\`\`) with the appropriate language identifier.
2. Always specify the complete file path in the format:
${projectName}/filepath/to/file.ext
3. If creating a new file, specify the path as:
${projectName}/filepath/to/file.ext (new file)
4. Format your code blocks as:
${projectName}/filepath/to/file.ext
\`\`\`language
code here
\`\`\`
If multiple files are involved, repeat the format for each file. Provide a clear and concise explanation along with any code snippets. Keep your response brief and to the point.
This is the project template:
${templateContext}

View File

@ -19,7 +19,9 @@ export async function POST(request: Request) {
- Explanations or comments
- Markdown formatting
The output should be the exact code that will replace the existing code, nothing more and nothing less.`
The output should be the exact code that will replace the existing code, nothing more and nothing less.
Important: When merging, preserve the original code structure as much as possible. Only make necessary changes to integrate the new code while maintaining the original code's organization and style.`
const mergedCode = `Original file (${fileName}):\n${originalCode}\n\nNew code to merge:\n${newCode}`

View File

@ -8,7 +8,7 @@ interface ApplyButtonProps {
activeFileName: string
activeFileContent: string
editorRef: { current: any }
onApply: (mergedCode: string) => void
onApply: (mergedCode: string, originalCode: string) => void
}
export default function ApplyButton({
@ -48,7 +48,7 @@ export default function ApplyButton({
mergedCode += decoder.decode(value, { stream: true })
}
}
onApply(mergedCode.trim())
onApply(mergedCode.trim(), activeFileContent)
} catch (error) {
console.error("Error applying code:", error)
toast.error(

View File

@ -19,6 +19,7 @@ export default function ChatMessage({
editorRef,
mergeDecorationsCollection,
setMergeDecorationsCollection,
selectFile,
}: MessageProps) {
// State for expanded message index
const [expandedMessageIndex, setExpandedMessageIndex] = useState<
@ -115,8 +116,9 @@ export default function ChatMessage({
activeFileContent,
editorRef,
handleApplyCode,
selectFile,
mergeDecorationsCollection,
setMergeDecorationsCollection
setMergeDecorationsCollection,
)
return (

View File

@ -19,8 +19,10 @@ export default function AIChat({
files,
templateType,
handleApplyCode,
selectFile,
mergeDecorationsCollection,
setMergeDecorationsCollection,
projectName,
}: AIChatProps) {
// Initialize socket and messages
const { socket } = useSocket()
@ -152,7 +154,8 @@ export default function AIChat({
activeFileContent,
false,
templateType,
files
files,
projectName
)
}
@ -223,6 +226,7 @@ export default function AIChat({
editorRef={editorRef}
mergeDecorationsCollection={mergeDecorationsCollection}
setMergeDecorationsCollection={setMergeDecorationsCollection}
selectFile={selectFile}
/>
))}
{isLoading && <LoadingDots />}

View File

@ -1,4 +1,4 @@
import { TFolder, TFile } from "@/lib/types"
import { TFile, TFolder } from "@/lib/types"
import React from "react"
// Stringify content for chat message component
@ -93,7 +93,8 @@ export const handleSend = async (
activeFileContent: string,
isEditMode: boolean = false,
templateType: string,
files: (TFile | TFolder)[]
files: (TFile | TFolder)[],
projectName: string
) => {
// Return if input is empty and context is null
if (input.trim() === "" && !context) return
@ -144,7 +145,8 @@ export const handleSend = async (
activeFileContent: activeFileContent,
isEditMode: isEditMode,
templateType: templateType,
files: files,
files: files,
projectName: projectName,
}),
signal: abortControllerRef.current.signal,
})
@ -241,3 +243,11 @@ export const looksLikeCode = (text: string): boolean => {
return codeIndicators.some((pattern) => pattern.test(text))
}
// Add this new function after looksLikeCode function
export const isFilePath = (text: string): boolean => {
// Match patterns like next/styles/SignIn.module.css or path/to/file.ext (new file)
const pattern =
/^(?:[a-zA-Z0-9_.-]+\/)*[a-zA-Z0-9_.-]+\.[a-zA-Z0-9]+(\s+\(new file\))?$/
return pattern.test(text)
}

View File

@ -1,11 +1,13 @@
import { Check, CornerUpLeft, X } from "lucide-react"
import { useSocket } from "@/context/SocketContext"
import { TTab } from "@/lib/types"
import { Check, CornerUpLeft, FileText, X } from "lucide-react"
import monaco from "monaco-editor"
import { Components } from "react-markdown"
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism"
import { Button } from "../../../ui/button"
import ApplyButton from "../ApplyButton"
import { stringifyContent } from "./chatUtils"
import { isFilePath, stringifyContent } from "./chatUtils"
// Create markdown components for chat message component
export const createMarkdownComponents = (
@ -15,7 +17,8 @@ export const createMarkdownComponents = (
activeFileName: string,
activeFileContent: string,
editorRef: any,
handleApplyCode: (mergedCode: string) => void,
handleApplyCode: (mergedCode: string, originalCode: string) => void,
selectFile: (tab: TTab) => void,
mergeDecorationsCollection?: monaco.editor.IEditorDecorationsCollection,
setMergeDecorationsCollection?: (collection: undefined) => void
): Components => ({
@ -58,7 +61,7 @@ export const createMarkdownComponents = (
mergeDecorationsCollection &&
editorRef?.current
) {
mergeDecorationsCollection.clear()
mergeDecorationsCollection?.clear()
setMergeDecorationsCollection(undefined)
}
}}
@ -72,14 +75,15 @@ export const createMarkdownComponents = (
<div className="w-px bg-input"></div>
<Button
onClick={() => {
if (
setMergeDecorationsCollection &&
mergeDecorationsCollection &&
editorRef?.current
) {
editorRef.current.getModel()?.setValue(activeFileContent)
mergeDecorationsCollection.clear()
setMergeDecorationsCollection(undefined)
if (editorRef?.current && mergeDecorationsCollection) {
const model = editorRef.current.getModel()
if (model && (model as any).originalContent) {
editorRef.current?.setValue(
(model as any).originalContent
)
mergeDecorationsCollection.clear()
setMergeDecorationsCollection?.(undefined)
}
}
}}
size="sm"
@ -126,8 +130,62 @@ export const createMarkdownComponents = (
)
},
// Render markdown elements
p: ({ node, children, ...props }) =>
renderMarkdownElement({ node, children, ...props }),
p: ({ node, children, ...props }) => {
const content = stringifyContent(children)
const { socket } = useSocket()
if (isFilePath(content)) {
const isNewFile = content.endsWith("(new file)")
const filePath = (
isNewFile ? content.replace(" (new file)", "") : content
)
.split("/")
.filter((part, index) => index !== 0)
.join("/")
const handleFileClick = () => {
if (isNewFile) {
socket?.emit(
"createFile",
{
name: filePath,
},
(response: any) => {
if (response.success) {
const tab: TTab = {
id: filePath,
name: filePath.split("/").pop() || "",
saved: true,
type: "file",
}
selectFile(tab)
}
}
)
} else {
const tab: TTab = {
id: filePath,
name: filePath.split("/").pop() || "",
saved: true,
type: "file",
}
selectFile(tab)
}
}
return (
<div
onClick={handleFileClick}
className="group flex items-center gap-2 px-2 py-1 bg-secondary/50 rounded-md my-2 text-xs hover:bg-secondary cursor-pointer w-fit"
>
<FileText className="h-4 w-4" />
<span className="font-mono group-hover:underline">{content}</span>
</div>
)
}
return renderMarkdownElement({ node, children, ...props })
},
h1: ({ node, children, ...props }) =>
renderMarkdownElement({ node, children, ...props }),
h2: ({ node, children, ...props }) =>

View File

@ -1,5 +1,5 @@
import { TemplateConfig } from "@/lib/templates"
import { TFile, TFolder } from "@/lib/types"
import { TFile, TFolder, TTab } from "@/lib/types"
import * as monaco from "monaco-editor"
import { Socket } from "socket.io-client"
@ -56,9 +56,11 @@ export interface AIChatProps {
files: (TFile | TFolder)[]
templateType: string
templateConfig?: TemplateConfig
handleApplyCode: (mergedCode: string) => void
projectName: string
handleApplyCode: (mergedCode: string, originalCode: string) => void
mergeDecorationsCollection?: monaco.editor.IEditorDecorationsCollection
setMergeDecorationsCollection?: (collection: undefined) => void
selectFile: (tab: TTab) => void
}
// Chat input props interface
@ -108,12 +110,13 @@ export interface MessageProps {
) => void
setIsContextExpanded: (isExpanded: boolean) => void
socket: Socket | null
handleApplyCode: (mergedCode: string) => void
handleApplyCode: (mergedCode: string, originalCode: string) => void
activeFileName: string
activeFileContent: string
editorRef: any
mergeDecorationsCollection?: monaco.editor.IEditorDecorationsCollection
setMergeDecorationsCollection?: (collection: undefined) => void
selectFile: (tab: TTab) => void
}
// Context tabs props interface

View File

@ -384,12 +384,19 @@ export default function CodeEditor({
// handle apply code
const handleApplyCode = useCallback(
(mergedCode: string) => {
(mergedCode: string, originalCode: string) => {
if (!editorRef) return
const originalLines = activeFileContent.split("\n")
const mergedLines = mergedCode.split("\n")
const model = editorRef.getModel()
if (!model) return // Store original content
;(model as any).originalContent = originalCode
// Calculate the full range of the document
const fullRange = model.getFullModelRange()
// Create decorations before applying the edit
const originalLines = originalCode.split("\n")
const mergedLines = mergedCode.split("\n")
const decorations: monaco.editor.IModelDeltaDecoration[] = []
for (
@ -397,32 +404,33 @@ export default function CodeEditor({
i < Math.max(originalLines.length, mergedLines.length);
i++
) {
if (originalLines[i] !== mergedLines[i]) {
// Only highlight new lines (green highlights)
if (i >= originalLines.length || originalLines[i] !== mergedLines[i]) {
decorations.push({
range: new monaco.Range(i + 1, 1, i + 1, 1),
options: {
isWholeLine: true,
className:
i < originalLines.length
? "removed-line-decoration"
: "added-line-decoration",
glyphMarginClassName:
i < originalLines.length
? "removed-line-glyph"
: "added-line-glyph",
className: "added-line-decoration",
glyphMarginClassName: "added-line-glyph",
},
})
}
}
// Update editor content
editorRef.setValue(mergedCode)
// Execute the edit operation
editorRef.executeEdits("apply-code", [
{
range: fullRange,
text: mergedCode,
forceMoveMarkers: true,
},
])
// Apply decorations
// Apply decorations after the edit
const newDecorations = editorRef.createDecorationsCollection(decorations)
setMergeDecorationsCollection(newDecorations)
},
[editorRef, activeFileContent]
[editorRef]
)
// Generate widget effect
@ -785,31 +793,32 @@ export default function CodeEditor({
setGenerate((prev) => ({ ...prev, show: false }))
// Check if the tab already exists in the list of open tabs
const exists = tabs.find((t) => t.id === tab.id)
setTabs((prev) => {
if (exists) {
// If the tab exists, make it the active tab
setActiveFileId(exists.id)
return prev
}
// If the tab doesn't exist, add it to the list of tabs and make it active
return [...prev, tab]
})
const existingTab = tabs.find((t) => t.id === tab.id)
// If the file's content is already cached, set it as the active content
if (fileContents[tab.id]) {
setActiveFileContent(fileContents[tab.id])
if (existingTab) {
// If the tab exists, just make it active
setActiveFileId(existingTab.id)
if (fileContents[existingTab.id]) {
setActiveFileContent(fileContents[existingTab.id])
}
} else {
// Otherwise, fetch the content of the file and cache it
debouncedGetFile(tab.id, (response: string) => {
setFileContents((prev) => ({ ...prev, [tab.id]: response }))
setActiveFileContent(response)
})
// If the tab doesn't exist, add it to the list and make it active
setTabs((prev) => [...prev, tab])
// Fetch content if not cached
if (!fileContents[tab.id]) {
debouncedGetFile(tab.id, (response: string) => {
setFileContents((prev) => ({ ...prev, [tab.id]: response }))
setActiveFileContent(response)
})
} else {
setActiveFileContent(fileContents[tab.id])
}
}
// Set the editor language based on the file type
setEditorLanguage(processFileType(tab.name))
// Set the active file ID to the new tab
// Set the active file ID
setActiveFileId(tab.id)
}
@ -1284,9 +1293,11 @@ export default function CodeEditor({
lastCopiedRangeRef={lastCopiedRangeRef}
files={files}
templateType={sandboxData.type}
projectName={sandboxData.name}
handleApplyCode={handleApplyCode}
mergeDecorationsCollection={mergeDecorationsCollection}
setMergeDecorationsCollection={setMergeDecorationsCollection}
selectFile={selectFile}
/>
</ResizablePanel>
</>

View File

@ -21,12 +21,14 @@ export default function DeployButtonModal({
const { deploy, getAppExists } = useTerminal()
const [isDeploying, setIsDeploying] = useState(false)
const [isDeployed, setIsDeployed] = useState(false)
const [deployButtonVisible, setDeployButtonEnabled] = useState(false)
useEffect(() => {
const checkDeployment = async () => {
if (getAppExists) {
const exists = await getAppExists(data.id)
setIsDeployed(exists)
setDeployButtonEnabled(exists.success)
setIsDeployed((exists.success && exists.exists) ?? false)
}
}
checkDeployment()
@ -50,10 +52,12 @@ export default function DeployButtonModal({
<>
<Popover>
<PopoverTrigger asChild>
<Button variant="outline">
<Globe className="w-4 h-4 mr-2" />
Deploy
</Button>
{deployButtonVisible && (
<Button variant="outline">
<Globe className="w-4 h-4 mr-2" />
Deploy
</Button>
)}
</PopoverTrigger>
<PopoverContent
className="p-4 w-full max-w-xs sm:max-w-sm md:max-w-md lg:max-w-lg xl:max-w-xl rounded-lg shadow-lg"

View File

@ -20,7 +20,9 @@ interface TerminalContextType {
createNewTerminal: (command?: string) => Promise<void>
closeTerminal: (id: string) => void
deploy: (callback: () => void) => void
getAppExists: ((appName: string) => Promise<boolean>) | null
getAppExists:
| ((appName: string) => Promise<{ success: boolean; exists?: boolean }>)
| null
}
const TerminalContext = createContext<TerminalContextType | undefined>(
@ -92,16 +94,18 @@ export const TerminalProvider: React.FC<{ children: React.ReactNode }> = ({
})
}
const getAppExists = async (appName: string): Promise<boolean> => {
const getAppExists = async (
appName: string
): Promise<{ success: boolean; exists?: boolean }> => {
console.log("Is there a socket: " + !!socket)
if (!socket) {
console.error("Couldn't check if app exists: No socket")
return false
return { success: false }
}
const response: { exists: boolean } = await new Promise((resolve) =>
socket.emit("getAppExists", { appName }, resolve)
const response: { success: boolean; exists?: boolean } = await new Promise(
(resolve) => socket.emit("getAppExists", { appName }, resolve)
)
return response.exists
return response
}
const value = {