diff --git a/frontend/javascripts/components/disable_generic_dnd.ts b/frontend/javascripts/components/disable_generic_dnd.ts index 9d3f618a4b..6647d82f64 100644 --- a/frontend/javascripts/components/disable_generic_dnd.ts +++ b/frontend/javascripts/components/disable_generic_dnd.ts @@ -1,22 +1,20 @@ import window from "libs/window"; import React from "react"; -import type { EmptyObject } from "types/globals"; -export default class DisableGenericDnd extends React.Component { - componentDidMount() { - window.addEventListener("dragover", this.preventDefault, false); - window.addEventListener("drop", this.preventDefault, false); - } - componentWillUnmount() { - window.removeEventListener("dragover", this.preventDefault); - window.removeEventListener("drop", this.preventDefault); - } +const preventDefault = (e: Event) => { + e.preventDefault(); +}; - preventDefault = (e: Event) => { - e.preventDefault(); - }; +export default function DisableGenericDnd() { + React.useEffect(() => { + window.addEventListener("dragover", preventDefault, false); + window.addEventListener("drop", preventDefault, false); - render() { - return null; - } + return () => { + window.removeEventListener("dragover", preventDefault); + window.removeEventListener("drop", preventDefault); + }; + }, []); + + return null; } diff --git a/frontend/javascripts/components/fixed_expandable_table.tsx b/frontend/javascripts/components/fixed_expandable_table.tsx index 372013692f..9c0a912c20 100644 --- a/frontend/javascripts/components/fixed_expandable_table.tsx +++ b/frontend/javascripts/components/fixed_expandable_table.tsx @@ -1,11 +1,8 @@ import { Button, Table, type TableProps } from "antd"; -import type { ColumnsType, GetRowKey } from "antd/lib/table/interface"; -import React from "react"; +import type { ColumnsType, ExpandableConfig, GetRowKey } from "antd/lib/table/interface"; +import type React from "react"; +import { useEffect, useState } from "react"; -type State = { - expandedRows: Array; - className?: string; -}; /** This is a wrapper for large tables that have fixed columns and support expanded rows. * This wrapper ensures that when rows are expanded no column is fixed as this creates rendering bugs. * If you are using this wrapper, you do not need to set the class "large-table" @@ -17,72 +14,73 @@ type OwnTableProps = TableProps & { columns: ColumnsType; }; -const EMPTY_ARRAY: string[] = [] as const; +const EMPTY_ARRAY: React.Key[] = [] as const; -export default class FixedExpandableTable extends React.PureComponent { - state: State = { - expandedRows: EMPTY_ARRAY, - }; +const getAllRowIds = ( + dataSource: readonly any[] | undefined, + rowKey: string | number | symbol | GetRowKey | undefined, +) => { + const canUseRowKey = typeof rowKey === "string"; + return dataSource != null && canUseRowKey ? dataSource.map((row) => row[rowKey]) : []; +}; + +export default function FixedExpandableTable({ + className, + expandable, + dataSource, + rowKey, + columns, + ...restProps +}: OwnTableProps) { + const [expandedRows, setExpandedRows] = useState(EMPTY_ARRAY); - getAllRowIds( - dataSource: readonly any[] | undefined, - rowKey: string | number | symbol | GetRowKey | undefined, - ) { - const canUseRowKey = typeof rowKey === "string"; - return dataSource != null && canUseRowKey ? dataSource.map((row) => row[rowKey]) : []; - } + // biome-ignore lint/correctness/useExhaustiveDependencies: Collapse all rows when source changes + useEffect(() => { + setExpandedRows(EMPTY_ARRAY); + }, [dataSource]); - componentDidUpdate(prevProps: Readonly>): void { - if (prevProps.dataSource !== this.props.dataSource) { - this.setState({ expandedRows: EMPTY_ARRAY }); - } - } + const areAllRowsExpanded = dataSource != null && expandedRows.length === dataSource?.length; - render() { - const { expandedRows } = this.state; - const { className, expandable, ...restProps } = this.props; - const { dataSource, rowKey } = this.props; - const areAllRowsExpanded = - dataSource != null && this.state.expandedRows.length === dataSource?.length; + const columnTitleCollapsed = ( +