Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: remove unecessary work in scoped state (#6160)

Myles Scolnick62a57d87e726
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 5b19bd296f..79e3fb05ff 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -2,8 +2,7 @@
import { closeCompletion, completionStatus } from "@codemirror/autocomplete";
import type { EditorView } from "@codemirror/view";
import clsx from "clsx";
-import { useAtom, useAtomValue, useSetAtom } from "jotai";
-import { ScopeProvider } from "jotai-scope";
+import { useAtomValue, useSetAtom } from "jotai";
import {
HelpCircleIcon,
MoreHorizontalIcon,
@@ -78,7 +77,10 @@ import { useRunCell } from "./cell/useRunCells";
import { HideCodeButton } from "./code/readonly-python-code";
import { cellDomProps } from "./common";
import { useCellNavigationProps } from "./navigation/navigation";
-import { temporarilyShownCodeAtom } from "./navigation/state";
+import {
+ useTemporarilyShownCode,
+ useTemporarilyShownCodeActions,
+} from "./navigation/state";
import { OutputArea } from "./Output";
import { ConsoleOutput } from "./output/ConsoleOutput";
import { CellDragHandle, SortableCell } from "./SortableCell";
@@ -157,9 +159,8 @@ function useCellHiddenLogic({
editorView: React.RefObject<EditorView | null>;
editorViewParentRef: React.RefObject<HTMLDivElement | null>;
}) {
- const [temporarilyVisible, setTemporarilyVisible] = useAtom(
- temporarilyShownCodeAtom,
- );
+ const temporarilyVisible = useTemporarilyShownCode(cellId);
+ const temporarilyShownCodeActions = useTemporarilyShownCodeActions();
const isUntouched = useAtomValue(
useMemo(() => createUntouchedCellAtom(cellId), [cellId]),
);
@@ -179,7 +180,7 @@ function useCellHiddenLogic({
// Default to true
const focus = opts?.focus ?? true;
- setTemporarilyVisible(true);
+ temporarilyShownCodeActions.add(cellId);
if (focus) {
editorView.current?.focus();
@@ -297,16 +298,14 @@ const CellComponent = (props: CellProps) => {
if (mode === "edit") {
return (
- <ScopeProvider atoms={[temporarilyShownCodeAtom]}>
- <EditableCellComponent
- {...props}
- cellId={cellId}
- editorView={editorView}
- setEditorView={(ev) => {
- editorView.current = ev;
- }}
- />
- </ScopeProvider>
+ <EditableCellComponent
+ {...props}
+ cellId={cellId}
+ editorView={editorView}
+ setEditorView={(ev) => {
+ editorView.current = ev;
+ }}
+ />
);
}
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index f10e148d44..4f8e909fec 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -33,7 +33,7 @@ import {
useCellSelectionActions,
useIsCellSelected,
} from "./selection";
-import { temporarilyShownCodeAtom } from "./state";
+import { useTemporarilyShownCodeActions } from "./state";
import { handleVimKeybinding } from "./vim-bindings";
interface HotkeyHandler {
@@ -85,7 +85,7 @@ function addSingleHandler(handler: HotkeyHandler["bulkHandle"]): HotkeyHandler {
function useCellFocusProps(cellId: CellId) {
const focusActions = useCellFocusActions();
const actions = useCellActions();
- const setTemporarilyShownCode = useSetAtom(temporarilyShownCodeAtom);
+ const temporarilyShownCodeActions = useTemporarilyShownCodeActions();
// This occurs at the cell level and descedants.
const { focusWithinProps } = useFocusWithin({
@@ -95,7 +95,7 @@ function useCellFocusProps(cellId: CellId) {
},
onBlurWithin: () => {
// On blur, hide the code if it was temporarily shown.
- setTemporarilyShownCode(false);
+ temporarilyShownCodeActions.remove(cellId);
actions.markTouched({ cellId });
focusActions.blurCell();
},
@@ -132,7 +132,7 @@ export function useCellNavigationProps(
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);
const actions = useCellActions();
const store = useStore();
- const setTemporarilyShownCode = useSetAtom(temporarilyShownCodeAtom);
+ const temporarilyShownCodeActions = useTemporarilyShownCodeActions();
const runCells = useRunCells();
const keymapPreset = useAtomValue(keymapPresetAtom);
const { copyCells, pasteAtCell } = useCellClipboard();
@@ -275,7 +275,7 @@ export function useCellNavigationProps(
},
// Enter will focus the cell editor.
Enter: () => {
- setTemporarilyShownCode(true);
+ temporarilyShownCodeActions.add(cellId);
focusCellEditor(store, cellId);
selectionActions.clear();
return true;
@@ -608,7 +608,7 @@ export function useCellEditorNavigationProps(
cellId: CellId,
editorView: React.RefObject<EditorView | null>,
) {
- const setTemporarilyShownCode = useSetAtom(temporarilyShownCodeAtom);
+ const temporarilyShownCodeActions = useTemporarilyShownCodeActions();
const keymapPreset = useAtomValue(keymapPresetAtom);
const hotkeys = useAtomValue(hotkeysAtom);
@@ -618,7 +618,7 @@ export function useCellEditorNavigationProps(
}, [hotkeys]);
const exitToCommandMode = () => {
- setTemporarilyShownCode(false);
+ temporarilyShownCodeActions.remove(cellId);
focusCell(cellId);
};
diff --git a/frontend/src/components/editor/navigation/state.ts b/frontend/src/components/editor/navigation/state.ts
index cb39b75730..d2f2e94756 100644
--- a/frontend/src/components/editor/navigation/state.ts
+++ b/frontend/src/components/editor/navigation/state.ts
@@ -1,5 +1,37 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { atom } from "jotai";
+import { atom, useAtomValue } from "jotai";
+import { useMemo } from "react";
+import type { CellId } from "@/core/cells/ids";
+import { createReducerAndAtoms } from "@/utils/createReducer";
-export const temporarilyShownCodeAtom = atom<boolean>(false);
+type TemporarilyShownCodeState = Set<CellId>;
+
+// This previously used jotai-scope, but unfortunately this bug creates unnecessary work:
+// https://github.com/jotaijs/jotai-scope/issues/25
+
+const {
+ valueAtom: temporarilyShownCodeAtom,
+ useActions: useTemporarilyShownCodeActions,
+} = createReducerAndAtoms(() => new Set<CellId>(), {
+ add: (state: TemporarilyShownCodeState, cellId: CellId) => {
+ const newState = new Set(state);
+ newState.add(cellId);
+ return newState;
+ },
+ remove: (state: TemporarilyShownCodeState, cellId: CellId) => {
+ const newState = new Set(state);
+ newState.delete(cellId);
+ return newState;
+ },
+});
+
+const createTemporarilyShownCodeAtom = (cellId: CellId) =>
+ atom((get) => get(temporarilyShownCodeAtom).has(cellId));
+
+export function useTemporarilyShownCode(cellId: CellId) {
+ const atom = useMemo(() => createTemporarilyShownCodeAtom(cellId), [cellId]);
+ return useAtomValue(atom);
+}
+
+export { temporarilyShownCodeAtom, useTemporarilyShownCodeActions };