Skip to content

Commit

Permalink
refactor: Swap effect for input handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Jun 29, 2024
1 parent 59cbbc0 commit 6d2ed9b
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/components/controllers/repl/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'preact/hooks';
import { useState } from 'preact/hooks';
import { Splitter } from '../../splitter';
import { EXAMPLES, getExample, loadExample } from './examples';
import { ErrorOverlay } from './error-overlay';
Expand Down Expand Up @@ -42,18 +42,22 @@ export function Repl({ code, slug }) {
});
};

useEffect(() => {
const onEditorInput = (value) => {
setEditorCode(value);

// Clears the example & code query params when a user
// begins to modify the code
if (!exampleSlug || !location.search) return;
const example = getExample(exampleSlug);
(async function () {
if (example) {
const code = await loadExample(example.url);
if (location.search && code !== editorCode) {
if (example) {
loadExample(example.url).then(exampleCode => {
if (exampleCode !== value) {
setExampleSlug('');
history.replaceState(null, null, '/repl');
}
}
})();
}, [editorCode]);
});
}
};

const share = () => {
if (!exampleSlug) {
Expand Down Expand Up @@ -138,7 +142,7 @@ export function Repl({ code, slug }) {
value={editorCode}
baseExampleSlug={exampleSlug}
error={error}
onInput={setEditorCode}
onInput={onEditorInput}
/>
</Splitter>
</div>
Expand Down

0 comments on commit 6d2ed9b

Please sign in to comment.