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.

feat: updated useSectionCollapse methods (#4807)

Joaquin Coromina5151a96e8b2c
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index 437ffaf95f..ea0436d2a9 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -83,7 +83,6 @@ import { useRunAllCells } from "../cell/useRunCells";
import { settingDialogAtom } from "@/components/app-config/state";
import { AddDatabaseDialogContent } from "../database/add-database-form";
import { useHideAllMarkdownCode } from "./useHideAllMarkdownCode";
-import { useSectionCollapse } from "./useSectionCollapse";
import { Constants } from "@/core/constants";
const NOOP_HANDLER = (event?: Event) => {
@@ -99,13 +98,14 @@ export function useNotebookActions() {
const [viewState] = useAtom(viewStateAtom);
const kioskMode = useAtomValue(kioskModeAtom);
const hideAllMarkdownCode = useHideAllMarkdownCode();
- const { collapseAllSections, expandAllSections } = useSectionCollapse();
const {
updateCellConfig,
undoDeleteCell,
clearAllCellOutputs,
upsertSetupCell,
+ collapseAllCells,
+ expandAllCells,
} = useCellActions();
const restartKernel = useRestartKernel();
const runAllCells = useRunAllCells();
@@ -381,12 +381,12 @@ export function useNotebookActions() {
{
icon: <ChevronRightCircleIcon size={14} strokeWidth={1.5} />,
label: "Collapse all sections",
- handle: collapseAllSections,
+ handle: collapseAllCells,
},
{
icon: <ChevronDownCircleIcon size={14} strokeWidth={1.5} />,
label: "Expand all sections",
- handle: expandAllSections,
+ handle: expandAllCells,
},
{
icon: <DiamondPlusIcon size={14} strokeWidth={1.5} />,
diff --git a/frontend/src/components/editor/actions/useSectionCollapse.ts b/frontend/src/components/editor/actions/useSectionCollapse.ts
deleted file mode 100644
index 09caf028ec..0000000000
--- a/frontend/src/components/editor/actions/useSectionCollapse.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-/* Copyright 2024 Marimo. All rights reserved. */
-import { useCellActions } from "@/core/cells/cells";
-import { getNotebook } from "@/core/cells/cells";
-import { canCollapseOutline } from "@/core/dom/outline";
-import useEvent from "react-use-event-hook";
-
-/**
- * Hooks to collapse and expand all sections in the notebook.
- */
-
-export const useSectionCollapse = () => {
- const { collapseCell, expandCell } = useCellActions();
-
- const processAllSections = async (action: "collapse" | "expand") => {
- const notebook = getNotebook();
- const cellIds = notebook.cellIds.inOrderIds;
-
- // Find all markdown cells that aren't already hidden
- for (const cellId of cellIds) {
- const outline = notebook.cellRuntime[cellId].outline;
- // Check if the cell is a markdown cell with a TOC outline
- if (!outline) {
- continue;
- }
- // Check if the cell is a collapsible header
- if (!canCollapseOutline(outline)) {
- continue;
- }
- // Collapse or expand the cell based on the action
- action === "collapse" ? collapseCell({ cellId }) : expandCell({ cellId });
- }
- };
-
- return {
- collapseAllSections: useEvent(() => processAllSections("collapse")),
- expandAllSections: useEvent(() => processAllSections("expand")),
- };
-};