Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Add localstorage to frontend #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions frontend/src/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import React, { useRef, useState } from "react";
import React, {useRef, useState} from "react";
import Editor, { Monaco } from "@monaco-editor/react";
import type { editor as EditorType } from "monaco-editor";
import { ChevronDownIcon } from "@heroicons/react/20/solid";
Expand All @@ -21,8 +21,9 @@ const CodeEditor = ({ authToken }: CodeEditorProps) => {
const [monacoInstance, setMonacoInstance] = useState<Monaco | null>(null);
const [output, setOutput] = useState<{ stdOut: string; stdErr: string; exitCode: number } | null>(null);
const [error, setError] = useState<{ message: string } | null>(null);

const [currentText] = useState(typeof window !== "undefined" ? localStorage.getItem("code") || '// Write your code here' : "// Write your code here");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since your are not using setCurrentText(), you don't really need useState() to begin with.

Just pass the text you want directly into the defaultValue prop.

const endpoint = "/api/execute"

const getValue = async () => {
const base64Code = convertToBase64(editorRef.current?.getValue() || '');
let response = await fetch(endpoint, {
Expand Down Expand Up @@ -115,10 +116,11 @@ const CodeEditor = ({ authToken }: CodeEditorProps) => {
<Editor
height="70vh"
defaultLanguage="javascript"
defaultValue="// your code here"
defaultValue={currentText}
theme="vs-dark"
onMount={handleEditorDidMount}
className="flex-grow"
onChange={(value) => localStorage.setItem('code', value || '')}
/>
{output && (
<div className="p-4 bg-gray-900 text-white">
Expand Down