-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove all
useEffect
usages (#12659)
- Loading branch information
1 parent
2daa914
commit f537335
Showing
7 changed files
with
208 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,3 +128,6 @@ dist | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
# Wrangler | ||
api/.wrangler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
} |
Oops, something went wrong.