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.

1.3 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { atom, useAtomValue } from "jotai";
import type { CellId } from "@/core/cells/ids";
import { createReducerAndAtoms } from "@/utils/createReducer";

interface PendingCutState {
  cellIds: Set<CellId>;
}

const initialState = (): PendingCutState => ({
  cellIds: new Set(),
});

const {
  valueAtom: pendingCutStateAtom,
  useActions: usePendingCutActionsInternal,
} = createReducerAndAtoms(initialState, {
  markForCut: (_state, action: { cellIds: CellId[] }) => {
    return {
      cellIds: new Set(action.cellIds),
    };
  },
  clear: () => {
    return initialState();
  },
});

export { pendingCutStateAtom };

export const pendingCutCellIdsAtom = atom(
  (get) => get(pendingCutStateAtom).cellIds,
);

export const clearPendingCutAtom = atom(null, (_get, set) => {
  set(pendingCutStateAtom, initialState());
});

export function usePendingCutActions() {
  return usePendingCutActionsInternal();
}

export function useIsPendingCut(cellId: CellId): boolean {
  const cellIds = useAtomValue(pendingCutCellIdsAtom);
  return cellIds.has(cellId);
}

export function usePendingCutState() {
  return useAtomValue(pendingCutStateAtom);
}

export function useHasPendingCut(): boolean {
  const state = useAtomValue(pendingCutStateAtom);
  return state.cellIds.size > 0;
}