Skip to content

Commit

Permalink
Use executionTime returned from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdentarikcan committed Dec 12, 2024
1 parent 0d8819c commit ded6c42
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions frontend/src/components/CodeSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/use-toast";
import { useExecuteCode } from "@/services/api/programmingForumComponents";
import { CodeExecution } from "@/services/api/programmingForumSchemas";
import React, { useState } from "react";
import React from "react";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import { a11yLight } from "react-syntax-highlighter/dist/esm/styles/hljs";

Expand All @@ -25,23 +25,14 @@ const languageUserFriendlyName = {

export const CodeSnippet: React.FC<CodeSnippetProps> = ({ code, language }) => {
const executeCode = useExecuteCode();
const [executionTime, setExecutionTime] = useState<number | null>(null);

const handleExecute = async (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
setExecutionTime(null); // Reset the timer
const execution: CodeExecution = {
code,
language: language as CodeExecution["language"],
};
const startTime = performance.now(); // Record start time

Check failure on line 34 in frontend/src/components/CodeSnippet.tsx

View workflow job for this annotation

GitHub Actions / Frontend Tests

'startTime' is assigned a value but never used
try {
await executeCode.mutateAsync({ body: execution });
const endTime = performance.now(); // Record end time
setExecutionTime(endTime - startTime); // Calculate and set execution time
} catch {
setExecutionTime(null); // Reset in case of error
}
executeCode.mutate({ body: execution });
};

return (
Expand Down Expand Up @@ -90,12 +81,8 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = ({ code, language }) => {
</Button>
{executeCode.isSuccess && (
<div className="mt-2 rounded border border-green-300 bg-green-100 p-2">
<h4 className="mb-4 text-sm font-bold">Output:</h4>
<h4 className="mb-4 text-sm font-bold">Output (in {executeCode.data.data.executionTime}s):</h4>
<pre className="bg-green-50 p-1">{executeCode.data.data.output}</pre>
<br></br>
{executionTime !== null && (
<p className="mb-4 text-sm font-bold">Execution Time: {(executionTime / 1000).toFixed(3)} seconds</p>
)}
</div>
)}
{executeCode.isError && (
Expand Down

0 comments on commit ded6c42

Please sign in to comment.