Skip to content
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

Support snippet/code param in Rzk playground #118

Merged
merged 1 commit into from
Oct 1, 2023
Merged
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
16 changes: 12 additions & 4 deletions rzk-playground/components/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ export default function Editor({
editorHeight: number
}) {
const [existsSelection, setExistsSelection] = useState(false)
const params = new URLSearchParams(window.location.search);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a Next.js (not normal React.js) project, window should not be used outside useEffect since it is not available during SSR. I can see that the whole project is wrapped in next/dynamic so it shouldn't be an issue in this specific situation, but that's not a good practice in general.
I wonder why you didn't just use the Vite template for React since we don't need any Next.js features.


var snippet = example
if (params.has("snippet")) {
snippet = params.get("snippet")!;
} else if (params.has("code")) {
snippet = params.get("code")!;
}
Comment on lines +24 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, always use let instead of var. Second, I would simplify it to avoid the intermediate variable as such:

Suggested change
var snippet = example
if (params.has("snippet")) {
snippet = params.get("snippet")!;
} else if (params.has("code")) {
snippet = params.get("code")!;
}
if (params.has("snippet")) {
setText(params.get("snippet")!);
} else if (params.has("code")) {
setText(params.get("code")!);
} else {
setText(example);
}

preferable inside the same useEffect in which params should be defined.
Then you would also not need setText(snippet) inside onCreateEditor.

return <CodeMirror
value={example}
value={snippet}
height={`100vh`}
width={`100vw`}
onCreateEditor={(view) => {
setText(example)
setText(snippet)
view.dispatch({ effects: EditorView.scrollIntoView(0) })
}}
onUpdate={(update) => {
Expand All @@ -48,7 +56,7 @@ export default function Editor({
]),
scrollPastEnd(),
centerCursor(editorHeight),

// dynamic parts of the theme
EditorView.theme({
"& .cm-scroller": {
Expand All @@ -61,4 +69,4 @@ export default function Editor({
language
]}
/>;
}
}
Loading