Skip to content

Commit 91340c6

Browse files
committed
feat: add the output and error message based on the status
1 parent e4091b9 commit 91340c6

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/client/store/editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type State = {
1616
token: string | null;
1717
time: string | null;
1818
memory: number | null;
19+
compile_output: string | null;
1920
};
2021
};
2122

@@ -40,6 +41,7 @@ export const useEditor = create<State & Action>((set) => ({
4041
token: null,
4142
time: null,
4243
memory: null,
44+
compile_output: null,
4345
},
4446

4547
setcode: (code) => set((state) => ({ code })),

src/components/pages/home/client.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { useEditor } from '@/client/store/editor';
44
import { useEditorOptions } from '@/client/store/editor-options';
55
import { api } from '@/client/trpc';
66
import { Button } from '@/components/ui/button';
7+
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area';
78
import { Skeleton } from '@/components/ui/skeleton';
89
import { Textarea } from '@/components/ui/textarea';
910
import { languageOptions } from '@/constants/app';
11+
import { cn } from '@/lib/utils/cn';
1012
import Editor from '@monaco-editor/react';
1113
import { CSSProperties } from 'react';
1214
import { toast } from 'sonner';
@@ -63,10 +65,27 @@ function CodeEditorLoading() {
6365
export function CodeOutput() {
6466
const output = useEditor((state) => state.output);
6567

66-
// Change the theme depending of the error state
68+
function getOutput() {
69+
if (!output.status) return { className: '', val: null };
70+
71+
const { id } = output.status;
72+
// Compilation Error
73+
if (id === 6) return { className: 'text-red-500', val: output.compile_output ? atob(output.compile_output) : null };
74+
75+
// Accepted
76+
if (id === 3) return { className: 'text-green-500', val: output.stdout ? atob(output.stdout) : null };
77+
78+
// Time limit exceeded
79+
if (id === 5) return { className: 'text-red-500', val: 'Time Limit Exceeded' };
80+
81+
return { className: 'text-red-500', val: output.stderr ? atob(output.stderr) : null };
82+
}
6783

6884
return (
69-
<pre className='bg-muted rounded-lg my-2 p-3 text-sm min-h-56 text-muted-foreground'>{output.stdout ? atob(output.stdout) : 'Output panel'}</pre>
85+
<ScrollArea className='bg-muted h-56 rounded-lg py-2 my-2'>
86+
<pre className={cn('px-3 text-sm text-muted-foreground', getOutput()?.className)}>{getOutput()?.val}</pre>
87+
<ScrollBar className='px-3' scrollAreaThumbClassname='bg-muted-foreground/30' orientation='horizontal' />
88+
</ScrollArea>
7089
);
7190
}
7291

src/server/api/routers/code.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const codeRouter = createTRPCRouter({
6161
token: z.string(),
6262
time: z.string().nullable(),
6363
memory: z.number().nullable(),
64+
compile_output: z.string().nullable(),
6465
}),
6566
)
6667
.mutation(async ({ input }) => {
@@ -87,6 +88,7 @@ export const codeRouter = createTRPCRouter({
8788
token: response.data.token,
8889
time: response.data.time,
8990
memory: response.data.memory,
91+
compile_output: response.data.compile_output,
9092
};
9193
} catch (error) {
9294
throw new Error('Failed to process code');

0 commit comments

Comments
 (0)