Skip to content

Commit

Permalink
add numberSorterWithInfinityValue
Browse files Browse the repository at this point in the history
  • Loading branch information
yomybaby committed May 27, 2024
1 parent f93c982 commit 15659aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
10 changes: 6 additions & 4 deletions react/src/components/KeypairResourcePolicyList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { localeCompare } from '../helper';
import { localeCompare, numberSorterWithInfinityValue } from '../helper';
import { useSuspendedBackendaiClient, useUpdatableState } from '../hooks';
import Flex from './Flex';
import KeypairResourcePolicySettingModal, {
Expand Down Expand Up @@ -158,9 +158,11 @@ const KeypairResourcePolicyList: React.FC<KeypairResourcePolicyListProps> = (
dataIndex: 'max_session_lifetime',
key: 'max_session_lifetime',
sorter: (a, b) =>
a?.max_session_lifetime && b?.max_session_lifetime
? a.max_session_lifetime - b.max_session_lifetime
: 1,
numberSorterWithInfinityValue(
a?.max_session_lifetime,
b?.max_session_lifetime,
0,
),
render: (text) => (text ? text : '∞'),
},
{
Expand Down
18 changes: 15 additions & 3 deletions react/src/components/UserResourcePolicyList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { bytesToGB, localeCompare } from '../helper';
import {
bytesToGB,
localeCompare,
numberSorterWithInfinityValue,
} from '../helper';
import { useSuspendedBackendaiClient, useUpdatableState } from '../hooks';
import Flex from './Flex';
import TableColumnsSettingModal from './TableColumnsSettingModal';
Expand Down Expand Up @@ -111,7 +115,11 @@ const UserResourcePolicyList: React.FC<UserResourcePolicyListProps> = () => {
dataIndex: 'max_vfolder_count',
render: (text) => (_.toNumber(text) === 0 ? '∞' : text),
sorter: (a, b) =>
(a?.max_vfolder_count ?? 0) - (b?.max_vfolder_count ?? 0),
numberSorterWithInfinityValue(
a?.max_vfolder_count,
b?.max_vfolder_count,
0,
),
},
supportMaxSessionCountPerModelSession && {
title: t('resourcePolicy.MaxSessionCountPerModelSession'),
Expand All @@ -125,7 +133,11 @@ const UserResourcePolicyList: React.FC<UserResourcePolicyListProps> = () => {
dataIndex: 'max_quota_scope_size',
render: (text) => (text === -1 ? '∞' : bytesToGB(text)),
sorter: (a, b) =>
(a?.max_quota_scope_size ?? 0) - (b?.max_quota_scope_size ?? 0),
numberSorterWithInfinityValue(
a?.max_quota_scope_size,
b?.max_quota_scope_size,
-1,
),
},
supportMaxCustomizedImageCount && {
title: t('resourcePolicy.MaxCustomizedImageCount'),
Expand Down
14 changes: 14 additions & 0 deletions react/src/helper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,17 @@ export function transformSorterToOrderString<T = any>(
: undefined;
}
}

export const numberSorterWithInfinityValue = (
a?: number | null,
b?: number | null,
infiniteValue?: number | null,
nullishFallbackValue: number = 0,
) => {
const transform = (value?: number | null) => {
if (value === infiniteValue) return Number.POSITIVE_INFINITY;
if (value === null || value === undefined) return nullishFallbackValue;
return value;
};
return transform(a) - transform(b);
};

0 comments on commit 15659aa

Please sign in to comment.