-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ARYPROGRAMMER/develop/home
feat: code exec engine-piston
- Loading branch information
Showing
5 changed files
with
190 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ConvexError, v } from "convex/values"; | ||
import { mutation } from "./_generated/server"; | ||
|
||
export const saveCodeExecution = mutation({ | ||
args: { | ||
language: v.string(), | ||
code: v.string(), | ||
output: v.optional(v.string()), | ||
error: v.optional(v.string()), | ||
}, | ||
|
||
handler: async (ctx, args) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
if (!identity) { | ||
throw new ConvexError("User not authenticated"); | ||
} | ||
|
||
const user = await ctx.db | ||
.query("users") | ||
.withIndex("by_user_id") | ||
.filter((q) => q.eq(q.field("userId"), identity.subject)) | ||
.first(); | ||
|
||
if (!user?.isPro && args.language !== "javascript") { | ||
throw new ConvexError( | ||
"Only Pro users can execute code in languages other than JavaScript" | ||
); | ||
} | ||
|
||
await ctx.db.insert("codeExecutions", { | ||
...args, | ||
userId: identity.subject, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,73 @@ | ||
import React from 'react' | ||
"use client"; | ||
|
||
import { | ||
getExecutionResult, | ||
useCodeEditorState, | ||
} from "@/store/useCodeEditorStore"; | ||
import { useUser } from "@clerk/nextjs"; | ||
import { Loader2, Play } from "lucide-react"; | ||
import React from "react"; | ||
import { motion } from "framer-motion"; | ||
import { useMutation } from "convex/react"; | ||
import { api } from "../../../../convex/_generated/api"; | ||
|
||
function RunButton() { | ||
const { user } = useUser(); | ||
const { runCode, language, isRunning } = useCodeEditorState(); | ||
const saveCodeExecution = useMutation(api.codeExecutions.saveCodeExecution); | ||
const handleRun = async () => { | ||
await runCode(); | ||
const result = getExecutionResult(); | ||
|
||
if (user && result) { | ||
//convex saving | ||
await saveCodeExecution({ | ||
language, | ||
code: result.code, | ||
output: result.output || undefined, | ||
error: result.error || undefined, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div>RunBuilding...</div> | ||
) | ||
<motion.button | ||
onClick={handleRun} | ||
disabled={isRunning} | ||
whileHover={{ scale: 1.02 }} | ||
whileTap={{ scale: 0.98 }} | ||
className={` | ||
group relative inline-flex items-center gap-2.5 px-5 py-2.5 | ||
disabled:cursor-not-allowed | ||
focus:outline-none | ||
`} | ||
> | ||
<div className="absolute inset-0 bg-gradient-to-r from-blue-600 to-blue-500 rounded-xl opacity-100 transition-opacity group-hover:opacity-90" /> | ||
|
||
<div className="relative flex items-center gap-2.5"> | ||
{isRunning ? ( | ||
<> | ||
<div className="relative"> | ||
<Loader2 className="w-4 h-4 animate-spin text-white/70" /> | ||
<div className="absolute inset-0 blur animate-pulse" /> | ||
</div> | ||
<span className="text-sm font-medium text-white/90"> | ||
Executing... | ||
</span> | ||
</> | ||
) : ( | ||
<> | ||
<div className="relative flex items-center justify-center w-4 h-4"> | ||
<Play className="w-4 h-4 text-white/90 transition-transform group-hover:scale-110 group-hover:text-white" /> | ||
</div> | ||
<span className="text-sm font-medium text-white/90 group-hover:text-white"> | ||
Execute Code | ||
</span> | ||
</> | ||
)} | ||
</div> | ||
</motion.button> | ||
); | ||
} | ||
|
||
export default RunButton | ||
export default RunButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters