Skip to content

Commit b6834a9

Browse files
authored
fix: better component memozation (#3341)
1 parent 2ecd53e commit b6834a9

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

frontend/src/components/editor/Cell.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
useRef,
1212
useState,
1313
} from "react";
14-
1514
import { saveCellConfig, sendRun, sendStdin } from "@/core/network/requests";
1615
import { autocompletionKeymap } from "@/core/codemirror/cm";
1716
import type { UserConfig } from "../../core/config/config-schema";
@@ -520,6 +519,10 @@ const CellComponent = (
520519
: {}),
521520
});
522521

522+
const handleRefactorWithAI = useEvent((opts: { prompt: string }) => {
523+
setAiCompletionCell({ cellId, initialPrompt: opts.prompt });
524+
});
525+
523526
if (!editing) {
524527
const outputIsError = isErrorMime(output?.mimetype);
525528
const hidden = errored || interrupted || stopped || outputIsError;
@@ -546,10 +549,6 @@ const CellComponent = (
546549
return undefined;
547550
};
548551

549-
const handleRefactorWithAI = (opts: { prompt: string }) => {
550-
setAiCompletionCell({ cellId, initialPrompt: opts.prompt });
551-
};
552-
553552
const cellStatusComponent = (
554553
<CellStatusComponent
555554
status={status}

frontend/src/components/editor/output/ConsoleOutput.tsx

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
7575
return null;
7676
}
7777

78-
const renderText = (text: string) => {
79-
return (
80-
<span dangerouslySetInnerHTML={{ __html: ansiUp.ansi_to_html(text) }} />
81-
);
82-
};
83-
8478
const reversedOutputs = [...consoleOutputs].reverse();
8579

8680
return (
@@ -99,39 +93,31 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
9993
if (output.channel === "pdb") {
10094
return null;
10195
}
102-
const originalIdx = consoleOutputs.length - idx - 1;
10396

10497
if (output.channel === "stdin") {
10598
invariant(
10699
typeof output.data === "string",
107100
"Expected data to be a string",
108101
);
109102

103+
const originalIdx = consoleOutputs.length - idx - 1;
104+
110105
if (output.response == null) {
111106
return (
112-
<div key={idx} className="flex gap-2 items-center">
113-
{renderText(output.data)}
114-
<Input
115-
data-testid="console-input"
116-
type="text"
117-
autoComplete="off"
118-
autoFocus={true}
119-
className="m-0"
120-
placeholder="stdin"
121-
onKeyDown={(e) => {
122-
if (e.key === "Enter" && !e.shiftKey) {
123-
onSubmitDebugger(e.currentTarget.value, originalIdx);
124-
}
125-
}}
126-
/>
127-
</div>
107+
<StdInput
108+
key={idx}
109+
output={output.data}
110+
onSubmit={(text) => onSubmitDebugger(text, originalIdx)}
111+
/>
128112
);
129113
}
114+
130115
return (
131-
<div key={idx} className="flex gap-2 items-center">
132-
{renderText(output.data)}
133-
<span className="text-[var(--sky-11)]">{output.response}</span>
134-
</div>
116+
<StdInputWithResponse
117+
key={idx}
118+
output={output.data}
119+
response={output.response}
120+
/>
135121
);
136122
}
137123

@@ -152,3 +138,46 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
152138
</div>
153139
);
154140
};
141+
142+
const StdInput = (props: {
143+
onSubmit: (text: string) => void;
144+
output: string;
145+
response?: string;
146+
}) => {
147+
return (
148+
<div className="flex gap-2 items-center">
149+
{renderText(props.output)}
150+
<Input
151+
data-testid="console-input"
152+
type="text"
153+
autoComplete="off"
154+
autoFocus={true}
155+
className="m-0"
156+
placeholder="stdin"
157+
onKeyDown={(e) => {
158+
if (e.key === "Enter" && !e.shiftKey) {
159+
props.onSubmit(e.currentTarget.value);
160+
}
161+
}}
162+
/>
163+
</div>
164+
);
165+
};
166+
167+
const StdInputWithResponse = (props: {
168+
output: string;
169+
response?: string;
170+
}) => {
171+
return (
172+
<div className="flex gap-2 items-center">
173+
{renderText(props.output)}
174+
<span className="text-[var(--sky-11)]">{props.response}</span>
175+
</div>
176+
);
177+
};
178+
179+
const renderText = (text: string) => {
180+
return (
181+
<span dangerouslySetInnerHTML={{ __html: ansiUp.ansi_to_html(text) }} />
182+
);
183+
};

0 commit comments

Comments
 (0)