-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: decouple DataTableControl #20226
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React, { useCallback, useMemo } from 'react'; | ||
import React, { useMemo, useState, useEffect } from 'react'; | ||
import { | ||
css, | ||
GenericDataType, | ||
|
@@ -29,7 +29,6 @@ import { | |
import { Global } from '@emotion/react'; | ||
import { Column } from 'react-table'; | ||
import debounce from 'lodash/debounce'; | ||
import { useDispatch } from 'react-redux'; | ||
import { Space } from 'src/components'; | ||
import { Input } from 'src/components/Input'; | ||
import { | ||
|
@@ -45,10 +44,7 @@ import Popover from 'src/components/Popover'; | |
import { prepareCopyToClipboardTabularData } from 'src/utils/common'; | ||
import CopyToClipboard from 'src/components/CopyToClipboard'; | ||
import RowCountLabel from 'src/explore/components/RowCountLabel'; | ||
import { | ||
setOriginalFormattedTimeColumn, | ||
unsetOriginalFormattedTimeColumn, | ||
} from 'src/explore/actions/exploreActions'; | ||
import { getTimeColumns, setTimeColumns } from './utils'; | ||
|
||
export const CellNull = styled('span')` | ||
color: ${({ theme }) => theme.colors.grayscale.light1}; | ||
|
@@ -130,8 +126,8 @@ export const RowCount = ({ | |
}) => <RowCountLabel rowcount={data?.length ?? 0} loading={loading} />; | ||
|
||
enum FormatPickerValue { | ||
Formatted, | ||
Original, | ||
Formatted = 'formatted', | ||
Original = 'original', | ||
} | ||
|
||
const FormatPicker = ({ | ||
|
@@ -165,47 +161,26 @@ const FormatPickerLabel = styled.span` | |
|
||
const DataTableTemporalHeaderCell = ({ | ||
columnName, | ||
onTimeColumnChange, | ||
datasourceId, | ||
originalFormattedTimeColumnIndex, | ||
}: { | ||
columnName: string; | ||
onTimeColumnChange: ( | ||
columnName: string, | ||
columnType: FormatPickerValue, | ||
) => void; | ||
datasourceId?: string; | ||
originalFormattedTimeColumnIndex: number; | ||
}) => { | ||
const theme = useTheme(); | ||
const dispatch = useDispatch(); | ||
const isTimeColumnOriginalFormatted = originalFormattedTimeColumnIndex > -1; | ||
|
||
const onChange = useCallback( | ||
Comment on lines
-176
to
-179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same logic moves to parent's component. |
||
e => { | ||
if (!datasourceId) { | ||
return; | ||
} | ||
if ( | ||
e.target.value === FormatPickerValue.Original && | ||
!isTimeColumnOriginalFormatted | ||
) { | ||
dispatch(setOriginalFormattedTimeColumn(datasourceId, columnName)); | ||
} else if ( | ||
e.target.value === FormatPickerValue.Formatted && | ||
isTimeColumnOriginalFormatted | ||
) { | ||
dispatch( | ||
unsetOriginalFormattedTimeColumn( | ||
datasourceId, | ||
originalFormattedTimeColumnIndex, | ||
), | ||
); | ||
} | ||
}, | ||
[ | ||
originalFormattedTimeColumnIndex, | ||
columnName, | ||
datasourceId, | ||
dispatch, | ||
isTimeColumnOriginalFormatted, | ||
], | ||
const [isOriginalTimeColumn, setIsOriginalTimeColumn] = useState<boolean>( | ||
getTimeColumns(datasourceId).includes(columnName), | ||
); | ||
|
||
const onChange = (e: any) => { | ||
onTimeColumnChange(columnName, e.target.value); | ||
setIsOriginalTimeColumn(getTimeColumns(datasourceId).includes(columnName)); | ||
}; | ||
|
||
const overlayContent = useMemo( | ||
() => | ||
datasourceId ? ( // eslint-disable-next-line jsx-a11y/no-static-element-interactions | ||
|
@@ -222,14 +197,14 @@ const DataTableTemporalHeaderCell = ({ | |
<FormatPicker | ||
onChange={onChange} | ||
value={ | ||
isTimeColumnOriginalFormatted | ||
isOriginalTimeColumn | ||
? FormatPickerValue.Original | ||
: FormatPickerValue.Formatted | ||
} | ||
/> | ||
</FormatPickerContainer> | ||
) : null, | ||
[datasourceId, isTimeColumnOriginalFormatted, onChange], | ||
[datasourceId, isOriginalTimeColumn], | ||
); | ||
|
||
return datasourceId ? ( | ||
|
@@ -288,10 +263,45 @@ export const useTableColumns = ( | |
coltypes?: GenericDataType[], | ||
data?: Record<string, any>[], | ||
datasourceId?: string, | ||
originalFormattedTimeColumns: string[] = [], | ||
isVisible?: boolean, | ||
moreConfigs?: { [key: string]: Partial<Column> }, | ||
) => | ||
useMemo( | ||
) => { | ||
const [originalFormattedTimeColumns, setOriginalFormattedTimeColumns] = | ||
useState<string[]>(getTimeColumns(datasourceId)); | ||
|
||
const onTimeColumnChange = ( | ||
columnName: string, | ||
columnType: FormatPickerValue, | ||
) => { | ||
if (!datasourceId) { | ||
return; | ||
} | ||
if ( | ||
columnType === FormatPickerValue.Original && | ||
!originalFormattedTimeColumns.includes(columnName) | ||
) { | ||
const cols = getTimeColumns(datasourceId); | ||
cols.push(columnName); | ||
setTimeColumns(datasourceId, cols); | ||
setOriginalFormattedTimeColumns(cols); | ||
} else if ( | ||
columnType === FormatPickerValue.Formatted && | ||
originalFormattedTimeColumns.includes(columnName) | ||
) { | ||
const cols = getTimeColumns(datasourceId); | ||
cols.splice(cols.indexOf(columnName), 1); | ||
setTimeColumns(datasourceId, cols); | ||
setOriginalFormattedTimeColumns(cols); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (isVisible) { | ||
setOriginalFormattedTimeColumns(getTimeColumns(datasourceId)); | ||
} | ||
}, [datasourceId, isVisible]); | ||
|
||
return useMemo( | ||
() => | ||
colnames && data?.length | ||
? colnames | ||
|
@@ -313,9 +323,7 @@ export const useTableColumns = ( | |
<DataTableTemporalHeaderCell | ||
columnName={key} | ||
datasourceId={datasourceId} | ||
originalFormattedTimeColumnIndex={ | ||
originalFormattedTimeColumnIndex | ||
} | ||
onTimeColumnChange={onTimeColumnChange} | ||
/> | ||
) : ( | ||
key | ||
|
@@ -352,3 +360,4 @@ export const useTableColumns = ( | |
originalFormattedTimeColumns, | ||
], | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,34 @@ | |
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { useSelector } from 'react-redux'; | ||
import { ExplorePageState } from '../reducers/getInitialState'; | ||
import { ensureIsArray } from '@superset-ui/core'; | ||
import { | ||
LocalStorageKeys, | ||
setItem, | ||
getItem, | ||
} from 'src/utils/localStorageHelpers'; | ||
|
||
export const useOriginalFormattedTimeColumns = (datasourceId?: string) => | ||
useSelector<ExplorePageState, string[]>(state => | ||
datasourceId | ||
? state?.explore?.originalFormattedTimeColumns?.[datasourceId] ?? [] | ||
: [], | ||
export const getTimeColumns = (datasourceId?: string): string[] => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name suggests that the function gets all time columns, not only the time columns with epoch formatting in data table There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
const colsMap = getItem( | ||
LocalStorageKeys.explore__data_table_original_formatted_time_columns, | ||
{}, | ||
); | ||
if (datasourceId === undefined) { | ||
return []; | ||
} | ||
return ensureIsArray(colsMap[datasourceId]); | ||
}; | ||
|
||
export const setTimeColumns = (datasourceId: string, columns: string[]) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
const colsMap = getItem( | ||
LocalStorageKeys.explore__data_table_original_formatted_time_columns, | ||
{}, | ||
); | ||
setItem( | ||
LocalStorageKeys.explore__data_table_original_formatted_time_columns, | ||
{ | ||
...colsMap, | ||
[datasourceId]: columns, | ||
}, | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string enum type is more readable in the debugger.