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

Converted ConsoleInput to functional components #2409

Closed
wants to merge 3 commits into from

Conversation

rain2003
Copy link

@rain2003 rain2003 commented Sep 11, 2023

Ref #2358

Changes: I have changed the ConsoleInput.jsx file from legacy to functional component.

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test)
  • is from a uniquely-named feature branch and is up to date with the develop branch.
  • is descriptively named and links to an issue number, i.e. Fixes #123

Copy link
Collaborator

@lindapaiste lindapaiste left a comment

Choose a reason for hiding this comment

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

This code doesn't actually work, does it? If I type let x = 1 + 2 into the editor console and hit enter, it just creates extra codemirror instances! 😭

So definitely more work is needed here. I suspect it's the way that we are passing down the dispatchConsoleEvent prop, in which case it can be fixed by connecting this component to Redux directly. But I'm still working on figuring it out.

import CodeMirror from 'codemirror';
import { Encode } from 'console-feed';

import RightArrowIcon from '../../../images/right-arrow.svg';
import { dispatchMessage, MessageTypes } from '../../../utils/dispatcher';

// heavily inspired by
// https://github.com/codesandbox/codesandbox-client/blob/92a1131f4ded6f7d9c16945dc7c18aa97c8ada27/packages/app/src/app/components/Preview/DevTools/Console/Input/index.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would probably leave this in? Even though the code is not as similar anymore.

const cursorPos = this._cm.getDoc().getLine(0).length - 1;
this._cm.getDoc().setCursor({ line: 0, ch: cursorPos });
return { commandCursor: newCursor };
setCommandCursor((prevCursor) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love that you are using a prevState callback!

return { commandCursor: newCursor };
setCommandCursor((prevCursor) => {
const newCursor = Math.min(prevCursor + 1, commandHistory.length - 1);
cm.getDoc().setValue(commandHistory[newCursor] || '');
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems not quite right to be performing these side effects inside of a set state. But that's how it was before so let's leave it for now.

codemirrorRef.current.off('keydown', handleKeyDown);
codemirrorRef.current = null;
};
}, [theme, dispatchConsoleEvent, fontSize]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have to be careful here because we don't want to re-create the entire codemirror instance when theme or fontSize change.

I think (need to try it out to be sure) that you can remove the theme: p5-${theme}, and the setting of the fontSize from this main useEffect as they are already covered by your next useEffect.

Comment on lines +95 to +97
codemirrorRef.current.getWrapperElement().style[
'font-size'
] = `${fontSize}px`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This part of the code has always felt a bit wonky to me. I think that you can delete these lines from all useEffects and instead put style={{ fontSize }} on the <div ref={codemirrorContainerRef}> element

// https://github.com/codesandbox/codesandbox-client/blob/92a1131f4ded6f7d9c16945dc7c18aa97c8ada27/packages/app/src/app/components/Preview/DevTools/Console/Input/index.tsx
const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
const [commandHistory, setCommandHistory] = useState([]);
const [commandCursor, setCommandCursor] = useState(-1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's put a // eslint-disable-next-line no-unused-vars above this line to suppress the warning. It's definitely not the best code to have a state which isn't used but that's not your fault, that's how it is currently.

@lindapaiste
Copy link
Collaborator

I suspect it's the way that we are passing down the dispatchConsoleEvent prop, in which case it can be fixed by connecting this component to Redux directly. But I'm still working on figuring it out.

This is a very complicated component!

I confirmed that I can type and submit to the console if the only dependency of the useEffect is [dispatch]. However the arrow up and arrow down to navigate through previous statements still doesn't work and now I'm seeing why. Those functions depend on commandHistory, but we do not have commandHistory as a dependency of the useEffect and so we will always have a stale/outdated value of commandHistory inside the effect. We can't just add commandHistory as a dependency because then we would go back to having the problem where it keeps creating new CodeMirror instances.

You might be able to get it working if you use one useState where the state is an object with properties commandCursor and commandHistory like it was before. Then you can access the commandHistory inside your setState from the prevState.

@rain2003
Copy link
Author

rain2003 commented Sep 18, 2023

I tried to use the above method but still am not getting anywhere can you please tell what's wrong in this I have even tried visiting other websites which use a similar type of console box and looked at their code but still can't find out what's wrong with mine.
consoleinputfunctionaltxt.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants