Skip to content

Commit

Permalink
fix: stale editor view in cell action buttons (marimo-team#1029)
Browse files Browse the repository at this point in the history
* fix: stale editor view in cell action buttons

* fix
  • Loading branch information
akshayka authored and Benni-Math committed Apr 16, 2024
1 parent e493bf1 commit 8d71b9c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions frontend/src/components/editor/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ const CellComponent = (
[editorView, prepareToRunEffects],
);

// Callback to get the editor view.
const getEditorView = useCallback(() => editorView.current, [editorView]);

const handleRun = useEvent(async () => {
if (loading) {
return;
Expand Down Expand Up @@ -385,7 +388,7 @@ const CellComponent = (
cellId={cellId}
config={cellConfig}
status={status}
editorView={editorView.current}
getEditorView={getEditorView}
hasOutput={hasOutput}
name={name}
>
Expand Down Expand Up @@ -459,7 +462,7 @@ const CellComponent = (
ref={cellActionDropdownRef}
cellId={cellId}
status={status}
editorView={editorView.current}
getEditorView={getEditorView}
name={name}
config={cellConfig}
hasOutput={hasOutput}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export interface CellActionButtonProps
extends Pick<CellData, "name" | "config"> {
cellId: CellId;
status: CellStatus;
editorView: EditorView | null;
hasOutput: boolean;
getEditorView: () => EditorView | null;
}

interface Props {
Expand All @@ -66,11 +66,11 @@ export function useCellActionButtons({ cell }: Props) {
const runCell = useRunCell(cell?.cellId);
const { openModal } = useImperativeModal();
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);

if (!cell) {
return [];
}
const { cellId, config, editorView, name, hasOutput, status } = cell;
const { cellId, config, getEditorView, name, hasOutput, status } = cell;
const editorView = getEditorView();

const toggleDisabled = async () => {
const newConfig = { disabled: !config.disabled };
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/editor/cell/cell-context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const CellActionsContextMenu = ({ children, ...props }: Props) => {
label: "Paste",
icon: <ClipboardPasteIcon size={13} strokeWidth={1.5} />,
handle: async () => {
const { editorView } = props;
const { getEditorView } = props;
const editorView = getEditorView();
if (!editorView) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/core/cells/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const lastFocusedCellAtom = atom<{
config: CellConfig;
cellId: CellId;
status: CellStatus;
editorView: EditorView | null;
getEditorView: () => EditorView | null;
hasOutput: boolean;
} | null>((get) => {
const cellId = get(lastFocusedCellIdAtom);
Expand All @@ -29,13 +29,14 @@ export const lastFocusedCellAtom = atom<{
if (!data || !runtime || !handle) {
return null;
}
const getEditorView = () => handle.editorView;

return {
cellId,
name: data.name,
config: data.config,
status: runtime.status,
editorView: handle.editorView,
getEditorView: getEditorView,
hasOutput: runtime.output !== null,
};
});

0 comments on commit 8d71b9c

Please sign in to comment.