diff --git a/plugins/ui/src/js/src/elements/ListView.tsx b/plugins/ui/src/js/src/elements/ListView.tsx index 585b68b8e..d3e8ce0b0 100644 --- a/plugins/ui/src/js/src/elements/ListView.tsx +++ b/plugins/ui/src/js/src/elements/ListView.tsx @@ -1,5 +1,7 @@ import React, { ReactElement } from 'react'; import { useSelector } from 'react-redux'; +import { isElementOfType } from '@deephaven/react-hooks'; +import { getSettings, RootState } from '@deephaven/redux'; import { ListView as DHListView, ListViewProps as DHListViewProps, @@ -7,16 +9,13 @@ import { import { ListView as DHListViewJSApi, ListViewProps as DHListViewJSApiProps, - useTableClose, } from '@deephaven/jsapi-components'; -import { isElementOfType, usePromiseFactory } from '@deephaven/react-hooks'; -import { getSettings, RootState } from '@deephaven/redux'; import { SerializedListViewEventProps, useListViewProps, } from './useListViewProps'; import ObjectView, { ObjectViewProps } from './ObjectView'; -import { fetchReexportedTable } from './ElementUtils'; +import useReExportedTable from './useReExportedTable'; type WrappedDHListViewJSApiProps = Omit & { children: ReactElement; @@ -30,17 +29,7 @@ function ListView({ children, ...props }: ListViewProps) { const listViewProps = useListViewProps(props); const isObjectView = isElementOfType(children, ObjectView); - - const maybeExportedTable = - isObjectView && children.props.object.type === 'Table' - ? children.props.object - : null; - - const { data: table } = usePromiseFactory(fetchReexportedTable, [ - maybeExportedTable, - ]); - - useTableClose(table); + const table = useReExportedTable(children); if (isObjectView) { return ( diff --git a/plugins/ui/src/js/src/elements/Picker.tsx b/plugins/ui/src/js/src/elements/Picker.tsx index 7cc7f023f..be2d35352 100644 --- a/plugins/ui/src/js/src/elements/Picker.tsx +++ b/plugins/ui/src/js/src/elements/Picker.tsx @@ -7,13 +7,12 @@ import { import { Picker as DHPickerJSApi, PickerProps as DHPickerJSApiProps, - useTableClose, } from '@deephaven/jsapi-components'; -import { isElementOfType, usePromiseFactory } from '@deephaven/react-hooks'; +import { isElementOfType } from '@deephaven/react-hooks'; import { getSettings, RootState } from '@deephaven/redux'; import { SerializedPickerEventProps, usePickerProps } from './usePickerProps'; import ObjectView, { ObjectViewProps } from './ObjectView'; -import { fetchReexportedTable } from './ElementUtils'; +import useReExportedTable from './useReExportedTable'; type WrappedDHPickerJSApiProps = Omit & { children: ReactElement; @@ -27,17 +26,7 @@ function Picker({ children, ...props }: PickerProps) { const pickerProps = usePickerProps(props); const isObjectView = isElementOfType(children, ObjectView); - - const maybeExportedTable = - isObjectView && children.props.object.type === 'Table' - ? children.props.object - : null; - - const { data: table } = usePromiseFactory(fetchReexportedTable, [ - maybeExportedTable, - ]); - - useTableClose(table); + const table = useReExportedTable(children); if (isObjectView) { return ( diff --git a/plugins/ui/src/js/src/elements/useReExportedTable.ts b/plugins/ui/src/js/src/elements/useReExportedTable.ts new file mode 100644 index 000000000..a09134307 --- /dev/null +++ b/plugins/ui/src/js/src/elements/useReExportedTable.ts @@ -0,0 +1,25 @@ +import { ReactNode } from 'react'; +import { useTableClose } from '@deephaven/jsapi-components'; +import type { dh } from '@deephaven/jsapi-types'; +import { isElementOfType, usePromiseFactory } from '@deephaven/react-hooks'; +import ObjectView from './ObjectView'; +import { fetchReexportedTable } from './ElementUtils'; + +export function useReExportedTable(node: ReactNode): dh.Table | null { + const isObjectView = isElementOfType(node, ObjectView); + + const maybeExportedTable = + isObjectView && node.props.object.type === 'Table' + ? node.props.object + : null; + + const { data: table } = usePromiseFactory(fetchReexportedTable, [ + maybeExportedTable, + ]); + + useTableClose(table); + + return table; +} + +export default useReExportedTable;