Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(website): correct crash when switching to dark/light mode in playground #6496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions packages/website/src/components/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useColorMode } from '@docusaurus/theme-common';
import ASTViewerScope from '@site/src/components/ASTViewerScope';
import ConfigEslint from '@site/src/components/config/ConfigEslint';
import ConfigTypeScript from '@site/src/components/config/ConfigTypeScript';
Expand Down Expand Up @@ -60,7 +59,6 @@ function Playground(): JSX.Element {
tsconfig: defaultTsConfig,
eslintrc: defaultEslintConfig,
});
const { colorMode } = useColorMode();
const [esAst, setEsAst] = useState<TSESTree.Program | null>();
const [tsAst, setTsAST] = useState<SourceFile | null>();
const [scope, setScope] = useState<Record<string, unknown> | null>();
Expand Down Expand Up @@ -150,7 +148,6 @@ function Playground(): JSX.Element {
code={state.code}
tsconfig={state.tsconfig}
eslintrc={state.eslintrc}
darkTheme={colorMode === 'dark'}
sourceType={state.sourceType}
showAST={state.showAST}
onEsASTChange={setEsAst}
Expand Down
9 changes: 6 additions & 3 deletions packages/website/src/components/editor/LoadedEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useColorMode } from '@docusaurus/theme-common';
import type Monaco from 'monaco-editor';
import type React from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
Expand Down Expand Up @@ -31,7 +32,6 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
code,
tsconfig,
eslintrc,
darkTheme,
decoration,
jsx,
main,
Expand All @@ -47,6 +47,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
webLinter,
activeTab,
}) => {
const { colorMode } = useColorMode();
const [_, setDecorations] = useState<string[]>([]);

const codeActions = useRef(new Map<string, LintCodeAction[]>()).current;
Expand Down Expand Up @@ -333,8 +334,10 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
}, [eslintrc, tabs.eslintrc]);

useEffect(() => {
sandboxInstance.monaco.editor.setTheme(darkTheme ? 'vs-dark' : 'vs-light');
}, [darkTheme, sandboxInstance]);
sandboxInstance.monaco.editor.setTheme(
colorMode === 'dark' ? 'vs-dark' : 'vs-light',
);
}, [colorMode, sandboxInstance]);

useEffect(() => {
if (sandboxInstance.editor.getModel() === tabs.code) {
Expand Down
1 change: 0 additions & 1 deletion packages/website/src/components/editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { SourceFile } from 'typescript';
import type { ConfigModel, ErrorGroup, SelectedRange, TabType } from '../types';

export interface CommonEditorProps extends ConfigModel {
readonly darkTheme: boolean;
readonly activeTab: TabType;
readonly decoration: SelectedRange | null;
readonly onChange: (cfg: Partial<ConfigModel>) => void;
Expand Down
5 changes: 4 additions & 1 deletion packages/website/src/components/editor/useSandboxServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ export const useSandboxServices = (
model.dispose();
}
};
}, [props.ts, colorMode, props.jsx, onLoaded]);
// colorMode and jsx can't be reactive here because we don't want to force a recreation
// updating of colorMode and jsx is handled in LoadedEditor
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.ts, onLoaded]);

return services;
};