Skip to content

Commit

Permalink
Remove all useEffect usages (#12659)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Aug 8, 2024
1 parent 2daa914 commit f537335
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 168 deletions.
3 changes: 3 additions & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Wrangler
api/.wrangler
2 changes: 1 addition & 1 deletion playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Finally, install TypeScript dependencies with `npm install`, and run the develop

To run the datastore, which is based on [Workers KV](https://developers.cloudflare.com/workers/runtime-apis/kv/),
install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/install-and-update/),
then run `npx wrangler dev --local` from the `./playground/db` directory. Note that the datastore is
then run `npx wrangler dev --local` from the `./playground/api` directory. Note that the datastore is
only required to generate shareable URLs for code snippets. The development datastore does not
require Cloudflare authentication or login, but in turn only persists data locally.

Expand Down
123 changes: 123 additions & 0 deletions playground/src/Editor/Chrome.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { useCallback, useMemo, useRef, useState } from "react";
import Header from "./Header";
import { persist, persistLocal, restore, stringify } from "./settings";
import { useTheme } from "./theme";
import { default as Editor, Source } from "./Editor";
import initRuff, { Workspace } from "../pkg/ruff_wasm";
import { loader } from "@monaco-editor/react";
import { setupMonaco } from "./setupMonaco";
import { DEFAULT_PYTHON_SOURCE } from "../constants";

export default function Chrome() {
const initPromise = useRef<null | Promise<void>>(null);
const [pythonSource, setPythonSource] = useState<null | string>(null);
const [settings, setSettings] = useState<null | string>(null);
const [revision, setRevision] = useState(0);
const [ruffVersion, setRuffVersion] = useState<string | null>(null);

const [theme, setTheme] = useTheme();

const handleShare = useCallback(() => {
if (settings == null || pythonSource == null) {
return;
}

persist(settings, pythonSource).catch((error) =>
console.error(`Failed to share playground: ${error}`),
);
}, [pythonSource, settings]);

if (initPromise.current == null) {
initPromise.current = startPlayground()
.then(({ sourceCode, settings, ruffVersion }) => {
setPythonSource(sourceCode);
setSettings(settings);
setRuffVersion(ruffVersion);
setRevision(1);
})
.catch((error) => {
console.error("Failed to initialize playground.", error);
});
}

const handleSourceChanged = useCallback(
(source: string) => {
setPythonSource(source);
setRevision((revision) => revision + 1);

if (settings != null) {
persistLocal({ pythonSource: source, settingsSource: settings });
}
},
[settings],
);

const handleSettingsChanged = useCallback(
(settings: string) => {
setSettings(settings);
setRevision((revision) => revision + 1);

if (pythonSource != null) {
persistLocal({ pythonSource: pythonSource, settingsSource: settings });
}
},
[pythonSource],
);

const source: Source | null = useMemo(() => {
if (pythonSource == null || settings == null) {
return null;
}

return { pythonSource, settingsSource: settings };
}, [settings, pythonSource]);

return (
<main className="flex flex-col h-full bg-ayu-background dark:bg-ayu-background-dark">
<Header
edit={revision}
theme={theme}
version={ruffVersion}
onChangeTheme={setTheme}
onShare={handleShare}
/>

<div className="flex flex-grow">
{source != null && (
<Editor
theme={theme}
source={source}
onSettingsChanged={handleSettingsChanged}
onSourceChanged={handleSourceChanged}
/>
)}
</div>
</main>
);
}

// Run once during startup. Initializes monaco, loads the wasm file, and restores the previous editor state.
async function startPlayground(): Promise<{
sourceCode: string;
settings: string;
ruffVersion: string;
}> {
await initRuff();
const monaco = await loader.init();

console.log(monaco);

setupMonaco(monaco);

const response = await restore();
const [settingsSource, pythonSource] = response ?? [
stringify(Workspace.defaultSettings()),
DEFAULT_PYTHON_SOURCE,
];

return {
sourceCode: pythonSource,
settings: settingsSource,
ruffVersion: Workspace.version(),
};
}
Loading

0 comments on commit f537335

Please sign in to comment.