Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

(fix) local storage bug in code editor (#401)

bautistaaad3445255cba6
diff --git a/packages/monaco/src/index.tsx b/packages/monaco/src/index.tsx
index d01a543c..534e8de2 100644
--- a/packages/monaco/src/index.tsx
+++ b/packages/monaco/src/index.tsx
@@ -52,13 +52,11 @@ export function CodePanel(props: CodePanelProps) {
lzstring.decompressFromEncodedURIComponent(params.get('code') ?? '') ?? localStorageCode;
const getDefaultCode = () => {
- if (!localStorageCode) {
+ if (!defaultCode) {
return props.challenge.code;
}
- const [appendSolutionToThis, separator] = props.challenge.code.split(USER_CODE_START_REGEX);
-
- return `${appendSolutionToThis ?? ''}${separator ?? ''}${defaultCode}`;
+ return defaultCode;
};
const [code, setCode] = useState(() => getDefaultCode());
@@ -98,7 +96,7 @@ export function CodePanel(props: CodePanelProps) {
<div className="w-full flex-1">
<SplitEditor
tests={props.challenge.tests}
- challenge={props.challenge.code}
+ userCode={code}
onMount={{
tests: (editor) => {
setTestEditorState(editor);
@@ -114,7 +112,6 @@ export function CodePanel(props: CodePanelProps) {
setUserEditorState(editor);
const model = editor.getModel();
- console.info('MODEL', model?.uri);
if (!model) {
throw new Error();
@@ -132,11 +129,7 @@ export function CodePanel(props: CodePanelProps) {
user: async (code) => {
if (!monacoInstance) return null;
setCode(code ?? '');
- // we we only want to save whats after the comment
- const [, , storeThiseCode] = (code ?? '').split(USER_CODE_START_REGEX);
-
- // Wow this is just... remarkably jank.
-
+ setLocalStorageCode(code ?? '');
const getTsWorker = await monacoInstance.languages.typescript.getTypeScriptWorker();
const mm = monacoInstance.editor.getModel(monacoInstance.Uri.parse(TESTS_PATH));
@@ -152,7 +145,6 @@ export function CodePanel(props: CodePanelProps) {
] as const);
setTsErrors(errors);
- setLocalStorageCode(storeThiseCode ?? '');
},
}}
/>
diff --git a/packages/monaco/src/split-editor.tsx b/packages/monaco/src/split-editor.tsx
index 90f830d7..46ef4a28 100644
--- a/packages/monaco/src/split-editor.tsx
+++ b/packages/monaco/src/split-editor.tsx
@@ -13,7 +13,7 @@ export interface SplitEditorProps {
/** the classes applied to the container div */
className?: string;
tests: string;
- challenge: string;
+ userCode: string;
onValidate?: {
tests?: OnValidate;
user?: OnValidate;
@@ -31,7 +31,7 @@ export interface SplitEditorProps {
export default function SplitEditor({
className,
tests,
- challenge,
+ userCode,
onMount,
onValidate,
onChange,
@@ -88,8 +88,8 @@ export default function SplitEditor({
<CodeEditor
defaultPath={USER_CODE_PATH}
onMount={onMount?.user}
- defaultValue={challenge}
- value={challenge}
+ defaultValue={userCode}
+ value={userCode}
onValidate={onValidate?.user}
onChange={async (e, a) => {
// Most of this shamelessly stolen from Stackoverflow.