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
add uneccesary use-effects eslint plugin and remove some use-effects (#6454)
Shahmir Varqha7bb5e9f67a07
diff --git a/frontend/.eslintrc.cjs b/frontend/.eslintrc.cjsindex 5a6943ddf4..e43ddc1008 100644--- a/frontend/.eslintrc.cjs+++ b/frontend/.eslintrc.cjs@@ -13,6 +13,7 @@ module.exports = {"plugin:@typescript-eslint/recommended-type-checked","plugin:@typescript-eslint/stylistic-type-checked","plugin:@typescript-eslint/strict-type-checked",+ "plugin:react-you-might-not-need-an-effect/legacy-recommended",// React"plugin:react-hooks/recommended-legacy","plugin:react/recommended",diff --git a/frontend/package.json b/frontend/package.jsonindex 83f33beac9..d8fe6d2a6e 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -229,6 +229,7 @@"eslint-config-prettier": "^9.1.0","eslint-plugin-react": "^7.34.3","eslint-plugin-react-hooks": "^6.0.0-rc.1",+ "eslint-plugin-react-you-might-not-need-an-effect": "^0.5.1","eslint-plugin-ssr-friendly": "^1.3.0","eslint-plugin-storybook": "^9.0.14","eslint-plugin-unicorn": "^54.0.0",diff --git a/frontend/src/components/chat/markdown-renderer.tsx b/frontend/src/components/chat/markdown-renderer.tsxindex 2a26159007..55becff94e 100644--- a/frontend/src/components/chat/markdown-renderer.tsx+++ b/frontend/src/components/chat/markdown-renderer.tsx@@ -3,7 +3,7 @@import { EditorView } from "@codemirror/view";import { useAtomValue } from "jotai";import { BetweenHorizontalStartIcon } from "lucide-react";-import { memo, Suspense, useEffect, useState } from "react";+import { memo, Suspense, useState } from "react";import { Streamdown, type StreamdownProps } from "streamdown";import { Button, type ButtonProps } from "@/components/ui/button";import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";@@ -114,9 +114,9 @@ const CodeBlock = ({ code, language }: CodeBlockProps) => {const { theme } = useTheme();const [value, setValue] = useState(code);- useEffect(() => {+ if (value !== code) {setValue(code);- }, [code]);+ }const handleCopyCode = async () => {await copyToClipboard(value);@@ -150,18 +150,13 @@ const CodeBlock = ({ code, language }: CodeBlockProps) => {const CopyButton: React.FC<ButtonProps> = ({ onClick, ...props }) => {const [copied, setCopied] = useState(false);- useEffect(() => {- if (copied) {- setTimeout(() => setCopied(false), 1000);- }- }, [copied]);-return (<Button{...props}onClick={(e) => {onClick?.(e);setCopied(true);+ setTimeout(() => setCopied(false), 1000);}}>{copied ? "Copied" : "Copy"}@@ -175,10 +170,11 @@ const COMPONENTS: Components = {code: ({ children, className }) => {const language = className?.replace("language-", "");if (language && typeof children === "string") {+ const code = children.trim();return (<div><div className="text-xs text-muted-foreground pl-1">{language}</div>- <CodeBlock code={children.trim()} language={language} />+ <CodeBlock code={code} language={language} /></div>);}diff --git a/frontend/src/components/data-table/SearchBar.tsx b/frontend/src/components/data-table/SearchBar.tsxindex ba9a79624d..9c7091d996 100644--- a/frontend/src/components/data-table/SearchBar.tsx+++ b/frontend/src/components/data-table/SearchBar.tsx@@ -31,13 +31,14 @@ export const SearchBar = ({onSearch(debouncedSearch);}, [debouncedSearch, onSearch]);- useEffect(() => {- if (hidden) {- setInternalValue("");- } else {- ref.current?.focus();- }- }, [hidden]);+ if (!hidden) {+ ref.current?.focus();+ }++ // Reset internal value when hidden becomes true+ if (hidden && internalValue !== "") {+ setInternalValue("");+ }return (<divdiff --git a/frontend/src/components/data-table/charts/components/form-fields.tsx b/frontend/src/components/data-table/charts/components/form-fields.tsxindex a6d49d01a3..ffc6ddd6c3 100644--- a/frontend/src/components/data-table/charts/components/form-fields.tsx+++ b/frontend/src/components/data-table/charts/components/form-fields.tsx@@ -342,7 +342,7 @@ interface SliderFieldProps {fieldName: FieldName;label: string;className?: string;- value: number;+ defaultValue: number;start: number;stop: number;step?: number;@@ -351,53 +351,57 @@ interface SliderFieldProps {export const SliderField = ({fieldName,label,- value,+ defaultValue,start,stop,step,className,}: SliderFieldProps) => {- const [internalValue, setInternalValue] = React.useState(value);const form = useFormContext();- // Update internal value on prop change- React.useEffect(() => {- setInternalValue(value);- }, [value]);-return (<FormFieldcontrol={form.control}name={fieldName}- render={({ field }) => (- <FormItem- className={cn("flex flex-row items-center gap-2 w-1/2", className)}- >- <FormLabel>{label}</FormLabel>- <FormControl>- <Slider- {...field}- id={fieldName}- className="relative flex items-center select-none"- value={[internalValue]}- min={start}- max={stop}- step={step}- // Triggered on slider drag- onValueChange={([nextValue]) => {- setInternalValue(nextValue);- field.onChange(nextValue);- }}- // Triggered on mouse up- onValueCommit={([nextValue]) => {- field.onChange(nextValue);- form.setValue(fieldName, nextValue);- }}- valueMap={(value) => value}- />- </FormControl>- </FormItem>- )}+ render={({ field }) => {+ const currentValue = field.value ?? defaultValue;+ const numericValue = Number(currentValue);++ const saveValue = (value: number | string) => {+ if (typeof value === "string") {+ value = Number(value);+ }+ field.onChange(value);+ };++ return (+ <FormItem+ className={cn("flex flex-row items-center gap-2 w-1/2", className)}+ >+ <FormLabel>{label}</FormLabel>+ <FormControl>+ <Slider+ {...field}+ id={fieldName}+ className="relative flex items-center select-none"+ value={[numericValue]}+ min={start}+ max={stop}+ step={step}+ // Triggered on slider drag+ onValueChange={([nextValue]) => {+ saveValue(nextValue);+ }}+ // Triggered on mouse up+ onValueCommit={([nextValue]) => {+ saveValue(nextValue);+ }}+ valueMap={(value) => value}+ />+ </FormControl>+ </FormItem>+ );+ }}/>);};diff --git a/frontend/src/components/data-table/charts/forms/common-chart.tsx b/frontend/src/components/data-table/charts/forms/common-chart.tsxindex 9aee753756..3ca208577d 100644--- a/frontend/src/components/data-table/charts/forms/common-chart.tsx+++ b/frontend/src/components/data-table/charts/forms/common-chart.tsx@@ -102,7 +102,7 @@ export const StyleForm: React.FC = () => {<SliderFieldfieldName="xAxis.width"label="Width"- value={400}+ defaultValue={400}start={200}stop={800}/>@@ -118,7 +118,7 @@ export const StyleForm: React.FC = () => {<SliderFieldfieldName="yAxis.height"label="Height"- value={300}+ defaultValue={300}start={150}stop={600}/>diff --git a/frontend/src/components/datasources/datasources.tsx b/frontend/src/components/datasources/datasources.tsxindex d7f2dcf0de..08b9c3a9c1 100644--- a/frontend/src/components/datasources/datasources.tsx+++ b/frontend/src/components/datasources/datasources.tsx@@ -304,10 +304,12 @@ const DatabaseItem: React.FC<{}> = ({ hasSearch, engineName, database, children }) => {const [isExpanded, setIsExpanded] = React.useState(false);const [isSelected, setIsSelected] = React.useState(false);+ const [prevHasSearch, setPrevHasSearch] = React.useState(hasSearch);- React.useEffect(() => {+ if (prevHasSearch !== hasSearch) {+ setPrevHasSearch(hasSearch);setIsExpanded(hasSearch);- }, [hasSearch]);+ }return (<>@@ -399,14 +401,10 @@ const SchemaItem: React.FC<{children: React.ReactNode;hasSearch: boolean;}> = ({ databaseName, schema, children, hasSearch }) => {- const [isExpanded, setIsExpanded] = React.useState(false);+ const [isExpanded, setIsExpanded] = React.useState(hasSearch);const [isSelected, setIsSelected] = React.useState(false);const uniqueValue = `${databaseName}:${schema.name}`;- React.useEffect(() => {- setIsExpanded(hasSearch);- }, [hasSearch]);-if (isSchemaless(schema.name)) {return children;}@@ -693,11 +691,9 @@ const DatasetColumnItem: React.FC<{const closeAllColumns = useAtomValue(closeAllColumnsAtom);const setExpandedColumns = useSetAtom(expandedColumnsAtom);- React.useEffect(() => {- if (closeAllColumns) {- setIsExpanded(false);- }- }, [closeAllColumns]);+ if (closeAllColumns && isExpanded) {+ setIsExpanded(false);+ }if (isExpanded) {setExpandedColumns(diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a679cfb7cf..c2dd529d95 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -587,6 +587,9 @@ importers:eslint-plugin-react-hooks:specifier: ^6.0.0-rc.1version: 6.0.0([email protected])+ eslint-plugin-react-you-might-not-need-an-effect:+ specifier: ^0.5.1+ version: 0.5.1([email protected])eslint-plugin-ssr-friendly:specifier: ^1.3.0version: 1.3.0([email protected])@@ -5634,6 +5637,12 @@ packages:peerDependencies:eslint: ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0 || ^9.0.0+ resolution: {integrity: sha512-Gi2kfHLkXUT3j+IAwgb8TEhY10iMwsdwSsgbIxk98zPpuPW7M52ey9fU1oPZrWUlyekr5eXwUCjeTHekS6Isrw==}+ engines: {node: '>=14.0.0'}+ peerDependencies:+ eslint: '>=8.40.0'+resolution: {integrity: sha512-Qteup0SqU15kdocexFNAJMvCJEfa2xUKNV4CC1xsVMrIIqEy3SQ/rqyxCWNzfrd3/ldy6HMlD2e0JDVpDg2qIA==}engines: {node: '>=4'}@@ -5679,6 +5688,16 @@ packages:resolution: {integrity: sha512-dOt21O7lTMhDM+X9mB4GX+DZrZtCUJPL/wlcTqxyrx5IvO0IYtILdtrQGQp+8n5S0gwSVmOf9NQrjMOgfQZlIg==}engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}+ resolution: {integrity: sha512-uuQC43IGctw68pJA1RgbQS8/NP7rch6Cwd4j3ZBtgo4/8Flj4eGE7ZYSZRN3iq5pVUv6GPdW5Z1RFleo84uLDA==}+ engines: {node: ^10.0.0 || ^12.0.0 || >= 14.0.0}+ peerDependencies:+ eslint: '>=5'++ resolution: {integrity: sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw==}+ engines: {node: '>=10'}+resolution: {integrity: sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==}engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}@@ -6066,6 +6085,10 @@ packages:resolution: {integrity: sha512-7ACyT3wmyp3I61S4fG682L0VA2RGD9otkqGJIwNUMF1SWUombIIk+af1unuDYgMm082aHYwD+mzJvv9Iu8dsgg==}engines: {node: '>=18'}+ resolution: {integrity: sha512-ob/2LcVVaVGCYN+r14cnwnoDPUufjiYgSqRhiFD0Q1iI4Odora5RE8Iv1D24hAz5oMophRGkGz+yuvQmmUMnMw==}+ engines: {node: '>=18'}+resolution: {integrity: sha512-DpLKbNU4WylpxJykQujfCcwYWiV/Jhm50Goo0wrVILAv5jOr9d+H+UR3PhSCD2rCCEIg0uc+G+muBTwD54JhDQ==}engines: {node: '>= 0.4'}@@ -16068,6 +16091,12 @@ snapshots:transitivePeerDependencies:- supports-color+ dependencies:+ eslint: 8.57.1+ eslint-utils: 3.0.0([email protected])+ globals: 16.4.0+dependencies:array-includes: 3.1.9@@ -16147,6 +16176,13 @@ snapshots:esrecurse: 4.3.0estraverse: 5.3.0+ dependencies:+ eslint: 8.57.1+ eslint-visitor-keys: 2.1.0++ [email protected]: {}+@@ -16629,6 +16665,8 @@ snapshots:+ [email protected]: {}+dependencies:define-properties: 1.2.1