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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Refactor code to use useShouldShowInterrupt hook for showing interrupt button in RunButton and Controls components (#278)
Myles Scolnick0338408bb561
diff --git a/frontend/src/editor/Controls.tsx b/frontend/src/editor/Controls.tsxindex 0b4b17d04c..95e32abf48 100644--- a/frontend/src/editor/Controls.tsx+++ b/frontend/src/editor/Controls.tsx@@ -18,11 +18,11 @@ import { Tooltip } from "../components/ui/tooltip";import { renderShortcut } from "../components/shortcuts/renderShortcut";import { useCellActions } from "../core/state/cells";import { AppConfigButton } from "../components/app-config/app-config-button";-import { useState, useEffect } from "react";import { LayoutSelect } from "./renderers/layout-select";import { NotebookMenuDropdown } from "@/editor/notebook-menu-dropdown";import { FindReplace } from "@/components/find-replace/find-replace";import { AppConfig } from "@/core/config/config";+import { useShouldShowInterrupt } from "./cell/useShouldShowInterrupt";interface ControlsProps {filename: string | null;@@ -163,21 +163,10 @@ const RunControlButton = ({onRun: () => void;onInterrupt: () => void;}) => {- // Start a timer when the run starts.- // After 200ms, show the interrupt button to avoid flickering.- const [hasRunLongEnough, setHasRunLongEnough] = useState(false);- useEffect(() => {- if (!running) {- return;- }- setHasRunLongEnough(false);- const timeout = setTimeout(() => {- setHasRunLongEnough(true);- }, 200);- return () => clearTimeout(timeout);- }, [running]);-- if (running && hasRunLongEnough) {+ // Show the interrupt button after 200ms to avoid flickering.+ const showInterrupt = useShouldShowInterrupt(running);++ if (showInterrupt) {return (<Tooltip content={renderShortcut("global.interrupt")}><Buttondiff --git a/frontend/src/editor/cell/RunButton.tsx b/frontend/src/editor/cell/RunButton.tsxindex df8dad433a..eb66630f84 100644--- a/frontend/src/editor/cell/RunButton.tsx+++ b/frontend/src/editor/cell/RunButton.tsx@@ -1,10 +1,12 @@/* Copyright 2023 Marimo. All rights reserved. */-import { HardDriveDownloadIcon, PlayIcon } from "lucide-react";+import { HardDriveDownloadIcon, PlayIcon, SquareIcon } from "lucide-react";import { Button } from "@/editor/inputs/Inputs";import { Tooltip } from "../../components/ui/tooltip";import { renderShortcut } from "../../components/shortcuts/renderShortcut";import { cn } from "../../lib/utils";import { CellConfig, CellStatus } from "../../core/model/cells";+import { sendInterrupt } from "@/core/network/requests";+import { useShouldShowInterrupt } from "./useShouldShowInterrupt";function computeColor(appClosed: boolean,@@ -22,6 +24,7 @@ function computeColor(return "hint-green";}}+export const RunButton = (props: {edited: boolean;status: CellStatus;@@ -38,6 +41,10 @@ export const RunButton = (props: {const inactive =appClosed || loading || (!config.disabled && blockedStatus && !edited);const color = computeColor(appClosed, needsRun, loading, inactive);+ const running = status === "running";++ // Show the interrupt button after 200ms to avoid flickering.+ const showInterrupt = useShouldShowInterrupt(running);if (config.disabled) {return (@@ -80,11 +87,26 @@ export const RunButton = (props: {);}+ if (showInterrupt) {+ return (+ <Tooltip content={renderShortcut("global.interrupt")} usePortal={false}>+ <Button+ className={cn(appClosed && "inactive-button")}+ onClick={sendInterrupt}+ color="yellow"+ shape="circle"+ size="small"+ data-testid="run-button"+ >+ <SquareIcon strokeWidth={1.5} />+ </Button>+ </Tooltip>+ );+ }+let tooltipMsg: React.ReactNode = "";if (appClosed) {tooltipMsg = "App disconnected";- } else if (status === "running") {- tooltipMsg = "This cell is already running";} else if (status === "queued") {tooltipMsg = "This cell is already queued to run";} else {@@ -96,8 +118,7 @@ export const RunButton = (props: {<ButtonclassName={cn(!needsRun && "hover-action",- inactive && "inactive-button",- loading && "running"+ inactive && "inactive-button")}onClick={onClick}color={color}diff --git a/frontend/src/editor/cell/useShouldShowInterrupt.ts b/frontend/src/editor/cell/useShouldShowInterrupt.tsnew file mode 100644index 0000000000..c57b528de1--- /dev/null+++ b/frontend/src/editor/cell/useShouldShowInterrupt.ts@@ -0,0 +1,26 @@+/* Copyright 2023 Marimo. All rights reserved. */+import { useState, useEffect } from "react";++const DELAY_MS = 200;++/**+ * Returns true if the interrupt button should be shown.+ * This is based on the cell's running state and how long it has been running.+ */+export function useShouldShowInterrupt(running: boolean) {+ // Start a timer when the run starts.+ // After 200ms, show the interrupt button to avoid flickering.+ const [hasRunLongEnough, setHasRunLongEnough] = useState(false);+ useEffect(() => {+ if (!running) {+ return;+ }+ setHasRunLongEnough(false);+ const timeout = setTimeout(() => {+ setHasRunLongEnough(true);+ }, DELAY_MS);+ return () => clearTimeout(timeout);+ }, [running]);++ return running && hasRunLongEnough;+}