Skip to content

Commit

Permalink
Merge pull request opendatahub-io#1784 from DaoDaoNoCode/upstream-iss…
Browse files Browse the repository at this point in the history
…ue-1252

Limit the use of `useTableColumnSort` hook
  • Loading branch information
openshift-merge-robot authored Sep 29, 2023
2 parents 284aa98 + b9b9990 commit 640ec80
Show file tree
Hide file tree
Showing 36 changed files with 76 additions and 69 deletions.
8 changes: 8 additions & 0 deletions frontend/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
{
"group": ["~/api/**"],
"message": "Read from '~/api' instead."
},
{
"group": ["~/components/table/**", "!~/components/table/useTableColumnSort"],
"message": "Read from '~/components/table' instead."
},
{
"group": ["~/components/table/useTableColumnSort"],
"message": "The data will be sorted in the table, don't use this hook outside of '~/components/table' repo. For more information, please check the props of the Table component."
}
]
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
Td,
TbodyProps,
} from '@patternfly/react-table';
import useTableColumnSort, { SortableData } from '~/components/table/useTableColumnSort';
import { CHECKBOX_FIELD_ID } from '~/components/table/const';
import { EitherNotBoth } from '~/typeHelpers';
import useTableColumnSort from './useTableColumnSort';
import { CHECKBOX_FIELD_ID } from './const';
import { SortableData } from './types';

type TableProps<DataType> = {
data: DataType[];
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/table/const.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from './types';

export const CHECKBOX_FIELD_ID = 'checkbox';
export const KEBAB_FIELD_ID = 'kebab';
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/components/table/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './types';
export * from './const';

export { default as Table } from './Table';
export { default as useCheckboxTable } from './useCheckboxTable';

export { default as TableRowTitleDescription } from './TableRowTitleDescription';
export { default as CheckboxTd } from './CheckboxTd';
17 changes: 17 additions & 0 deletions frontend/src/components/table/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ThProps } from '@patternfly/react-table';

export type GetColumnSort = (columnIndex: number) => ThProps['sort'];

export type SortableData<T> = {
label: string;
field: string;
width?: ThProps['width'];
/**
* Set to false to disable sort.
* Set to true to handle string and number fields automatically (everything else is equal).
* Pass a function that will get the two results and what field needs to be matched.
* Assume ASC -- the result will be inverted internally if needed.
*/
sortable: boolean | ((a: T, b: T, keyField: string) => number);
info?: ThProps['info'];
};
2 changes: 1 addition & 1 deletion frontend/src/components/table/useCheckboxTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { xor } from 'lodash';
import Table from '~/components/table/Table';
import { useDeepCompareMemoize } from '~/utilities/useDeepCompareMemoize';
import type Table from './Table';

type UseCheckboxTable = {
selections: string[];
Expand Down
17 changes: 1 addition & 16 deletions frontend/src/components/table/useTableColumnSort.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import * as React from 'react';
import { ThProps } from '@patternfly/react-table';

export type GetColumnSort = (columnIndex: number) => ThProps['sort'];

export type SortableData<T> = {
label: string;
field: string;
width?: ThProps['width'];
/**
* Set to false to disable sort.
* Set to true to handle string and number fields automatically (everything else is equal).
* Pass a function that will get the two results and what field needs to be matched.
* Assume ASC -- the result will be inverted internally if needed.
*/
sortable: boolean | ((a: T, b: T, keyField: string) => number);
info?: ThProps['info'];
};
import { GetColumnSort, SortableData } from './types';

/**
* Using PF Composable Tables, this utility will help with handling sort logic.
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/concepts/pipelines/content/tables/columns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import {
SortableData,
checkboxTableColumn,
expandTableColumn,
kebabTableColumn,
} from '~/components/table';
import {
PipelineKF,
PipelineRunJobKF,
Expand All @@ -12,7 +17,6 @@ import {
getScheduledStateWeight,
getStatusWeight,
} from '~/concepts/pipelines/content/tables/utils';
import { checkboxTableColumn, expandTableColumn, kebabTableColumn } from '~/components/table/const';

export const pipelineColumns: SortableData<PipelineKF>[] = [
expandTableColumn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { TableVariant } from '@patternfly/react-table';
import { PipelineKF } from '~/concepts/pipelines/kfTypes';
import Table from '~/components/table/Table';
import { Table } from '~/components/table';
import PipelinesTableRow from '~/concepts/pipelines/content/tables/pipeline/PipelinesTableRow';
import { FetchStateRefreshPromise } from '~/utilities/useFetchState';
import { pipelineColumns } from '~/concepts/pipelines/content/tables/columns';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useNavigate } from 'react-router-dom';
import { PipelineKF } from '~/concepts/pipelines/kfTypes';
import { relativeTime } from '~/utilities/time';
import usePipelineRunsForPipeline from '~/concepts/pipelines/apiHooks/usePipelineRunsForPipeline';
import TableRowTitleDescription from '~/components/table/TableRowTitleDescription';
import { TableRowTitleDescription } from '~/components/table';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import PipelinesTableExpandedRow from '~/concepts/pipelines/content/tables/pipeline/PipelinesTableExpandedRow';
import { getLastRun } from '~/concepts/pipelines/content/tables/utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as React from 'react';
import { TableVariant } from '@patternfly/react-table';
import Table from '~/components/table/Table';
import { PipelineCoreResourceKF, PipelineRunKF } from '~/concepts/pipelines/kfTypes';
import { pipelineRunColumns } from '~/concepts/pipelines/content/tables/columns';
import PipelineRunTableRow from '~/concepts/pipelines/content/tables/pipelineRun/PipelineRunTableRow';
import useCheckboxTable from '~/components/table/useCheckboxTable';
import { useCheckboxTable, Table } from '~/components/table';
import EmptyTableView from '~/concepts/pipelines/content/tables/EmptyTableView';
import usePipelineRunFilter from '~/concepts/pipelines/content/tables/pipelineRun/usePipelineRunFilter';
import PipelineRunTableToolbar from '~/concepts/pipelines/content/tables/pipelineRun/PipelineRunTableToolbar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionsColumn, Td, Tr } from '@patternfly/react-table';
import { Link, useNavigate } from 'react-router-dom';
import { Skeleton } from '@patternfly/react-core';
import { PipelineRunKF, PipelineRunStatusesKF } from '~/concepts/pipelines/kfTypes';
import TableRowTitleDescription from '~/components/table/TableRowTitleDescription';
import { TableRowTitleDescription, CheckboxTd } from '~/components/table';
import {
RunCreated,
RunDuration,
Expand All @@ -12,7 +12,6 @@ import {
RunStatus,
} from '~/concepts/pipelines/content/tables/renderUtils';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import CheckboxTd from '~/components/table/CheckboxTd';
import { GetJobInformation } from '~/concepts/pipelines/content/tables/pipelineRun/useJobRelatedInformation';
import useNotification from '~/utilities/useNotification';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as React from 'react';
import { TableVariant } from '@patternfly/react-table';
import Table from '~/components/table/Table';
import { PipelineCoreResourceKF, PipelineRunJobKF } from '~/concepts/pipelines/kfTypes';
import { pipelineRunJobColumns } from '~/concepts/pipelines/content/tables/columns';
import useCheckboxTable from '~/components/table/useCheckboxTable';
import { useCheckboxTable, Table } from '~/components/table';
import PipelineRunJobTableRow from '~/concepts/pipelines/content/tables/pipelineRunJob/PipelineRunJobTableRow';
import PipelineRunJobTableToolbar from '~/concepts/pipelines/content/tables/pipelineRunJob/PipelineRunJobTableToolbar';
import usePipelineRunJobFilter from '~/concepts/pipelines/content/tables/pipelineRunJob/usePipelineRunJobFilter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { ActionsColumn, Td, Tr } from '@patternfly/react-table';
import { useNavigate } from 'react-router-dom';
import { PipelineRunJobKF } from '~/concepts/pipelines/kfTypes';
import TableRowTitleDescription from '~/components/table/TableRowTitleDescription';
import { TableRowTitleDescription, CheckboxTd } from '~/components/table';
import {
RunJobScheduled,
RunJobStatus,
Expand All @@ -11,7 +11,6 @@ import {
CoreResourcePipeline,
} from '~/concepts/pipelines/content/tables/renderUtils';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import CheckboxTd from '~/components/table/CheckboxTd';

type PipelineRunJobTableRowProps = {
isChecked: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, ToolbarItem } from '@patternfly/react-core';
import { TemplateKind } from '~/k8sTypes';
import { useDashboardNamespace } from '~/redux/selectors';
import useNotification from '~/utilities/useNotification';
import Table from '~/components/table/Table';
import { Table } from '~/components/table';
import useDraggableTable from '~/utilities/useDraggableTable';
import { patchDashboardConfigTemplateOrderBackend } from '~/services/dashboardService';
import { getServingRuntimeNameFromTemplate, getSortedTemplates } from './utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { TemplateKind } from '~/k8sTypes';

export const columns: SortableData<TemplateKind>[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import { Button } from '@patternfly/react-core';
import ManageInferenceServiceModal from '~/pages/modelServing/screens/projects/InferenceServiceModal/ManageInferenceServiceModal';
import Table from '~/components/table/Table';

import { Table } from '~/components/table';
import { InferenceServiceKind, ServingRuntimeKind } from '~/k8sTypes';
import { ProjectsContext } from '~/concepts/projects/ProjectsContext';
import InferenceServiceTableRow from './InferenceServiceTableRow';
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/modelServing/screens/global/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InferenceServiceKind, ProjectKind, SecretKind } from '~/k8sTypes';
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { getProjectDisplayName } from '~/pages/projects/utils';
import { getInferenceServiceDisplayName, getTokenDisplayName } from './utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import Table from '~/components/table/Table';
import { Table } from '~/components/table';
import { AccessReviewResourceAttributes, ServingRuntimeKind } from '~/k8sTypes';
import { ProjectDetailsContext } from '~/pages/projects/ProjectDetailsContext';
import { useAccessReview } from '~/api';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { HelperText, HelperTextItem } from '@patternfly/react-core';
import Table from '~/components/table/Table';
import { Table } from '~/components/table';
import { ProjectDetailsContext } from '~/pages/projects/ProjectDetailsContext';
import { tokenColumns } from '~/pages/modelServing/screens/global/data';
import { ServingRuntimeKind } from '~/k8sTypes';
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/modelServing/screens/projects/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ServingRuntimeKind } from '~/k8sTypes';
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';

export const columns: SortableData<ServingRuntimeKind>[] = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import * as React from 'react';
import { Alert, Stack, StackItem, Title } from '@patternfly/react-core';
import { Td, Tr } from '@patternfly/react-table';
import Table from '~/components/table/Table';

import useTableColumnSort from '~/components/table/useTableColumnSort';
import { Table } from '~/components/table';
import ExternalLink from '~/components/ExternalLink';
import ApplicationsPage from '~/pages/ApplicationsPage';
import StopServerModal from '~/pages/notebookController/screens/server/StopServerModal';
import { Notebook } from '~/types';
import { columns } from './const';
import { columns } from './data';
import StopAllServersButton from './StopAllServersButton';
import UserTableCellTransform from './UserTableCellTransform';
import useAdminUsers from './useAdminUsers';
import { AdminViewUserData } from './types';
import { NotebookAdminContext } from './NotebookAdminContext';

const NotebookAdminControl: React.FC = () => {
const [unsortedUsers, loaded, loadError] = useAdminUsers();
const { transformData } = useTableColumnSort<AdminViewUserData>(columns, 0);
const [users, loaded, loadError] = useAdminUsers();
const { serverStatuses, setServerStatuses } = React.useContext(NotebookAdminContext);

const users = transformData(unsortedUsers);

const onNotebooksStop = React.useCallback(
(didStop: boolean) => {
if (didStop) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { AdminViewUserData } from './types';

export const columns: SortableData<AdminViewUserData>[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import Table from '~/components/table/Table';
import { Table } from '~/components/table';
import { RoleBindingKind } from '~/k8sTypes';
import { ProjectDetailsContext } from '~/pages/projects/ProjectDetailsContext';
import { deleteRoleBinding, generateRoleBindingProjectSharing, createRoleBinding } from '~/api';
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/projects/projectSharing/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RoleBindingKind } from '~/k8sTypes';
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { firstSubject } from './utils';

export const columnsProjectSharing: SortableData<RoleBindingKind>[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import Table from '~/components/table/Table';

import { Table } from '~/components/table';
import { DataConnection } from '~/pages/projects/types';
import { columns } from './data';
import DataConnectionsTableRow from './DataConnectionsTableRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ConnectedNotebookNames from '~/pages/projects/notebook/ConnectedNotebookN
import { ConnectedNotebookContext } from '~/pages/projects/notebook/useRelatedNotebooks';
import { DataConnection } from '~/pages/projects/types';
import EmptyTableCellForAlignment from '~/pages/projects/components/EmptyTableCellForAlignment';
import TableRowTitleDescription from '~/components/table/TableRowTitleDescription';
import { TableRowTitleDescription } from '~/components/table';
import {
getDataConnectionDescription,
getDataConnectionDisplayName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { DataConnection } from '~/pages/projects/types';
import { getDataConnectionDisplayName } from './utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import Table from '~/components/table/Table';

import { Table } from '~/components/table';
import { NotebookKind } from '~/k8sTypes';
import DeleteNotebookModal from '~/pages/projects/notebook/DeleteNotebookModal';
import AddNotebookStorage from '~/pages/projects/pvc/AddNotebookStorage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import NotebookStatusToggle from '~/pages/projects/notebook/NotebookStatusToggle
import { NotebookKind } from '~/k8sTypes';
import NotebookImagePackageDetails from '~/pages/projects/notebook/NotebookImagePackageDetails';
import { ProjectDetailsContext } from '~/pages/projects/ProjectDetailsContext';
import TableRowTitleDescription from '~/components/table/TableRowTitleDescription';
import { TableRowTitleDescription } from '~/components/table';
import useNotebookDeploymentSize from './useNotebookDeploymentSize';
import useNotebookImage from './useNotebookImage';
import NotebookSizeDetails from './NotebookSizeDetails';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { getNotebookDisplayName, getNotebookStatusPriority } from '~/pages/projects/utils';
import { NotebookState } from '~/pages/projects/notebook/types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import Table from '~/components/table/Table';

import { Table } from '~/components/table';
import { PersistentVolumeClaimKind } from '~/k8sTypes';
import DeletePVCModal from '~/pages/projects/pvc/DeletePVCModal';
import StorageTableRow from './StorageTableRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { PersistentVolumeClaimKind } from '~/k8sTypes';
import StorageSizeBar from '~/pages/projects/components/StorageSizeBars';
import ConnectedNotebookNames from '~/pages/projects/notebook/ConnectedNotebookNames';
import { ConnectedNotebookContext } from '~/pages/projects/notebook/useRelatedNotebooks';
import TableRowTitleDescription from '~/components/table/TableRowTitleDescription';
import { TableRowTitleDescription } from '~/components/table';
import useIsRootVolume from './useIsRootVolume';
import StorageWarningStatus from './StorageWarningStatus';

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/projects/screens/detail/storage/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { getPvcDisplayName } from '~/pages/projects/utils';
import { PersistentVolumeClaimKind } from '~/k8sTypes';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import { Button, ButtonVariant, ToolbarItem } from '@patternfly/react-core';
import { useNavigate } from 'react-router-dom';
import Table from '~/components/table/Table';
import useTableColumnSort from '~/components/table/useTableColumnSort';
import { Table } from '~/components/table';
import SearchField, { SearchType } from '~/pages/projects/components/SearchField';
import { ProjectKind } from '~/k8sTypes';
import { getProjectDisplayName, getProjectOwner } from '~/pages/projects/utils';
Expand All @@ -25,8 +24,7 @@ const ProjectListView: React.FC<ProjectListViewProps> = ({ allowCreate }) => {
const navigate = useNavigate();
const [searchType, setSearchType] = React.useState<SearchType>(SearchType.NAME);
const [search, setSearch] = React.useState('');
const sort = useTableColumnSort<ProjectKind>(columns, 0);
const filteredProjects = sort.transformData(unfilteredProjects).filter((project) => {
const filteredProjects = unfilteredProjects.filter((project) => {
if (!search) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/projects/screens/projects/tableData.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortableData } from '~/components/table/useTableColumnSort';
import { SortableData } from '~/components/table';
import { ProjectKind } from '~/k8sTypes';
import { getProjectCreationTime, getProjectDisplayName } from '~/pages/projects/utils';

Expand Down

0 comments on commit 640ec80

Please sign in to comment.