Skip to content

Commit

Permalink
fix: better component memozation (#3341)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick authored Jan 4, 2025
1 parent 2ecd53e commit b6834a9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
9 changes: 4 additions & 5 deletions frontend/src/components/editor/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
useRef,
useState,
} from "react";

import { saveCellConfig, sendRun, sendStdin } from "@/core/network/requests";
import { autocompletionKeymap } from "@/core/codemirror/cm";
import type { UserConfig } from "../../core/config/config-schema";
Expand Down Expand Up @@ -520,6 +519,10 @@ const CellComponent = (
: {}),
});

const handleRefactorWithAI = useEvent((opts: { prompt: string }) => {
setAiCompletionCell({ cellId, initialPrompt: opts.prompt });
});

if (!editing) {
const outputIsError = isErrorMime(output?.mimetype);
const hidden = errored || interrupted || stopped || outputIsError;
Expand All @@ -546,10 +549,6 @@ const CellComponent = (
return undefined;
};

const handleRefactorWithAI = (opts: { prompt: string }) => {
setAiCompletionCell({ cellId, initialPrompt: opts.prompt });
};

const cellStatusComponent = (
<CellStatusComponent
status={status}
Expand Down
83 changes: 56 additions & 27 deletions frontend/src/components/editor/output/ConsoleOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
return null;
}

const renderText = (text: string) => {
return (
<span dangerouslySetInnerHTML={{ __html: ansiUp.ansi_to_html(text) }} />
);
};

const reversedOutputs = [...consoleOutputs].reverse();

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

if (output.channel === "stdin") {
invariant(
typeof output.data === "string",
"Expected data to be a string",
);

const originalIdx = consoleOutputs.length - idx - 1;

if (output.response == null) {
return (
<div key={idx} className="flex gap-2 items-center">
{renderText(output.data)}
<Input
data-testid="console-input"
type="text"
autoComplete="off"
autoFocus={true}
className="m-0"
placeholder="stdin"
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
onSubmitDebugger(e.currentTarget.value, originalIdx);
}
}}
/>
</div>
<StdInput
key={idx}
output={output.data}
onSubmit={(text) => onSubmitDebugger(text, originalIdx)}
/>
);
}

return (
<div key={idx} className="flex gap-2 items-center">
{renderText(output.data)}
<span className="text-[var(--sky-11)]">{output.response}</span>
</div>
<StdInputWithResponse
key={idx}
output={output.data}
response={output.response}
/>
);
}

Expand All @@ -152,3 +138,46 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
</div>
);
};

const StdInput = (props: {
onSubmit: (text: string) => void;
output: string;
response?: string;
}) => {
return (
<div className="flex gap-2 items-center">
{renderText(props.output)}
<Input
data-testid="console-input"
type="text"
autoComplete="off"
autoFocus={true}
className="m-0"
placeholder="stdin"
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
props.onSubmit(e.currentTarget.value);
}
}}
/>
</div>
);
};

const StdInputWithResponse = (props: {
output: string;
response?: string;
}) => {
return (
<div className="flex gap-2 items-center">
{renderText(props.output)}
<span className="text-[var(--sky-11)]">{props.response}</span>
</div>
);
};

const renderText = (text: string) => {
return (
<span dangerouslySetInnerHTML={{ __html: ansiUp.ansi_to_html(text) }} />
);
};

0 comments on commit b6834a9

Please sign in to comment.