Skip to content

Commit 2494074

Browse files
committed
feat: convex updated for exec mutation
1 parent 596e566 commit 2494074

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

convex/_generated/api.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
FilterApi,
1414
FunctionReference,
1515
} from "convex/server";
16+
import type * as codeExecutions from "../codeExecutions.js";
1617
import type * as http from "../http.js";
1718
import type * as users from "../users.js";
1819

@@ -25,6 +26,7 @@ import type * as users from "../users.js";
2526
* ```
2627
*/
2728
declare const fullApi: ApiFromModules<{
29+
codeExecutions: typeof codeExecutions;
2830
http: typeof http;
2931
users: typeof users;
3032
}>;

convex/codeExecutions.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { ConvexError, v } from "convex/values";
2+
import { mutation } from "./_generated/server";
3+
4+
export const saveCodeExecution = mutation({
5+
args: {
6+
language: v.string(),
7+
code: v.string(),
8+
output: v.optional(v.string()),
9+
error: v.optional(v.string()),
10+
},
11+
12+
handler: async (ctx, args) => {
13+
const identity = await ctx.auth.getUserIdentity();
14+
if (!identity) {
15+
throw new ConvexError("User not authenticated");
16+
}
17+
18+
const user = await ctx.db
19+
.query("users")
20+
.withIndex("by_user_id")
21+
.filter((q) => q.eq(q.field("userId"), identity.subject))
22+
.first();
23+
24+
if (!user?.isPro && args.language !== "javascript") {
25+
throw new ConvexError(
26+
"Only Pro users can execute code in languages other than JavaScript"
27+
);
28+
}
29+
30+
await ctx.db.insert("codeExecutions", {
31+
...args,
32+
userId: identity.subject,
33+
});
34+
},
35+
});

src/app/(home)/_components/RunButton.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
"use client";
22

3-
import { useCodeEditorState } from "@/store/useCodeEditorStore";
3+
import {
4+
getExecutionResult,
5+
useCodeEditorState,
6+
} from "@/store/useCodeEditorStore";
47
import { useUser } from "@clerk/nextjs";
58
import { Loader2, Play } from "lucide-react";
69
import React from "react";
710
import { motion } from "framer-motion";
11+
import { useMutation } from "convex/react";
12+
import { api } from "../../../../convex/_generated/api";
813

914
function RunButton() {
1015
const { user } = useUser();
11-
const { runCode, language, isRunning, executionResult } =
12-
useCodeEditorState();
13-
16+
const { runCode, language, isRunning } = useCodeEditorState();
17+
const saveCodeExecution = useMutation(api.codeExecutions.saveCodeExecution);
1418
const handleRun = async () => {
1519
await runCode();
20+
const result = getExecutionResult();
1621

17-
if (user && executionResult) {
22+
if (user && result) {
1823
//convex saving
24+
await saveCodeExecution({
25+
language,
26+
code: result.code,
27+
output: result.output || undefined,
28+
error: result.error || undefined,
29+
});
1930
}
2031
};
2132

src/store/useCodeEditorStore.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,6 @@ export const useCodeEditorState = create<CodeEditorState>((set, get) => {
148148
},
149149
};
150150
});
151+
152+
153+
export const getExecutionResult = () => useCodeEditorState.getState().executionResult;

0 commit comments

Comments
 (0)