Skip to content

Commit

Permalink
refactor: resource preset page
Browse files Browse the repository at this point in the history
  • Loading branch information
agatha197 committed Aug 22, 2024
1 parent 01c9e65 commit ef84be4
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 38 deletions.
6 changes: 5 additions & 1 deletion react/src/components/ContainerRegistryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ const ContainerRegistryList: React.FC<{
direction="row"
justify="end"
gap={'sm'}
style={{ padding: token.paddingSM }}
style={{
padding: token.paddingContentVertical,
paddingLeft: token.paddingContentHorizontalSM,
paddingRight: token.paddingContentHorizontalSM,
}}
>
<Tooltip title={t('button.Refresh')}>
<Button
Expand Down
2 changes: 2 additions & 0 deletions react/src/components/KeypairResourcePolicyList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ const KeypairResourcePolicyList: React.FC<KeypairResourcePolicyListProps> = (
<Flex direction="row" align="stretch">
<Button
type="text"
size="large"
icon={<SettingOutlined />}
style={{
color: token.colorInfo,
Expand Down Expand Up @@ -252,6 +253,7 @@ const KeypairResourcePolicyList: React.FC<KeypairResourcePolicyListProps> = (
>
<Button
type="text"
size="large"
icon={
<DeleteOutlined
style={{
Expand Down
172 changes: 172 additions & 0 deletions react/src/components/ResourcePresetList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { filterNonNullItems, localeCompare } from '../helper';
import { useUpdatableState } from '../hooks';
import Flex from './Flex';
import ResourceNumber from './ResourceNumber';
import { ResourcePresetListDeleteMutation } from './__generated__/ResourcePresetListDeleteMutation.graphql';
import {
ResourcePresetListQuery,
ResourcePresetListQuery$data,
} from './__generated__/ResourcePresetListQuery.graphql';
import {
ReloadOutlined,
PlusOutlined,
SettingOutlined,
DeleteOutlined,
} from '@ant-design/icons';
import { Tooltip, Button, theme, Table } from 'antd';
import graphql from 'babel-plugin-relay/macro';
import _ from 'lodash';
import React, { useState, useTransition } from 'react';
import { useTranslation } from 'react-i18next';
import { useLazyLoadQuery, useMutation } from 'react-relay';

type ResourcePresets = NonNullable<

Check warning on line 23 in react/src/components/ResourcePresetList.tsx

View workflow job for this annotation

GitHub Actions / coverage

'ResourcePresets' is defined but never used
ResourcePresetListQuery$data['resource_presets']
>;

interface ResourcePresetListProps {}

const ResourcePresetList: React.FC<ResourcePresetListProps> = () => {
const { t } = useTranslation();
const { token } = theme.useToken();
const [isRefetchPending, startRefetchTransition] = useTransition();
const [resourcePresetsFetchKey, updateResourcePresetsFetchKey] =
useUpdatableState('initial-fetch');
const [inFlightResourcePresetName, setInFlightResourcePresetName] =

Check warning on line 35 in react/src/components/ResourcePresetList.tsx

View workflow job for this annotation

GitHub Actions / coverage

'setInFlightResourcePresetName' is assigned a value but never used
useState<string>();
// const [editingResourcePreset, setEditingResourcePreset] =
// useState<ResourcePresetSettingModalFragment$key | null>();

const { resource_presets } = useLazyLoadQuery<ResourcePresetListQuery>(
graphql`
query ResourcePresetListQuery {
resource_presets {
name
resource_slots
}
}
`,
{},
{
fetchPolicy:
resourcePresetsFetchKey === 'initial-fetch'
? 'store-and-network'
: 'network-only',
fetchKey: resourcePresetsFetchKey,
},
);

const [commitDelete, isInflightDelete] =

Check warning on line 59 in react/src/components/ResourcePresetList.tsx

View workflow job for this annotation

GitHub Actions / coverage

'commitDelete' is assigned a value but never used
useMutation<ResourcePresetListDeleteMutation>(graphql`
mutation ResourcePresetListDeleteMutation($name: String!) {
delete_resource_preset(name: $name) {
ok
msg
}
}
`);

return (
<Flex direction="column" align="stretch">
<Flex
direction="row"
gap={'xs'}
justify="end"
wrap="wrap"
style={{
padding: token.paddingContentVertical,
paddingLeft: token.paddingContentHorizontalSM,
paddingRight: token.paddingContentHorizontalSM,
}}
>
<Flex direction="row" gap={'xs'} wrap="wrap" style={{ flexShrink: 1 }}>
<Tooltip title={t('button.Refresh')}>
<Button
icon={<ReloadOutlined />}
onClick={() => {
startRefetchTransition(() => {
updateResourcePresetsFetchKey();
});
}}
loading={isRefetchPending}
/>
</Tooltip>
<Button type="primary" icon={<PlusOutlined />} onClick={() => {}}>
{t('resourcePreset.CreatePreset')}
</Button>
</Flex>
</Flex>
<Table
rowKey={'name'}
dataSource={filterNonNullItems(resource_presets)}
scroll={{ x: 'max-content' }}
pagination={false}
sortDirections={['descend', 'ascend', 'descend']}
showSorterTooltip={false}
columns={[
{
title: t('resourcePreset.Name'),
dataIndex: 'name',
sorter: (a, b) => localeCompare(a.name, b.name),
},
{
title: t('resourcePreset.Resources'),
dataIndex: 'resource_slots',
render: (text) => (
<Flex gap="xxs">
{!_.isEmpty(text)
? _.map(JSON.parse(text), (value, key) => (
<ResourceNumber key={key} type={key} value={value} />
))
: '-'}
</Flex>
),
},
{
title: t('general.Control'),
key: 'control',
fixed: 'right',
render: (text, row) => (
<Flex align="stretch">
<Button
type="text"
size="large"
icon={<SettingOutlined />}
style={{
color: token.colorInfo,
}}
onClick={() => {
// setEditingResourcePreset(record);
}}
/>
<Button
type="text"
size="large"
icon={
<DeleteOutlined
style={{
color: token.colorError,
}}
/>
}
loading={
isInflightDelete &&
inFlightResourcePresetName ===
row?.name + resourcePresetsFetchKey
}
disabled={
isInflightDelete &&
inFlightResourcePresetName !==
row?.name + resourcePresetsFetchKey
}
/>
</Flex>
),
},
]}
/>
</Flex>
);
};

export default ResourcePresetList;
53 changes: 16 additions & 37 deletions react/src/pages/EnvironmentPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ContainerRegistryList from '../components/ContainerRegistryList';
import Flex from '../components/Flex';
import FlexActivityIndicator from '../components/FlexActivityIndicator';
import ResourcePresetList from '../components/ResourcePresetList';
import { useSuspendedBackendaiClient } from '../hooks';
import { theme } from 'antd';
import Card from 'antd/es/card/Card';
Expand Down Expand Up @@ -48,43 +49,21 @@ const EnvironmentPage = () => {
},
}}
>
<Flex
style={{
display: curTabKey === 'image' ? 'block' : 'none',
paddingTop: token.paddingContentVerticalSM,
}}
>
{/* @ts-ignore */}
<backend-ai-environment-list active={curTabKey === 'image'} />
</Flex>
<Flex
style={{
display: curTabKey === 'preset' ? 'block' : 'none',
paddingTop: token.paddingContentVerticalSM,
}}
>
{/* @ts-ignore */}
<backend-ai-resource-preset-list active={curTabKey === 'preset'} />
</Flex>

<Flex
style={{
display: curTabKey === 'registry' ? 'block' : 'none',
height: 'calc(100vh - 145px)',
// height: 'calc(100vh - 175px)',
}}
>
{isSupportContainerRegistryGraphQL ? (
curTabKey === 'registry' ? (
<Suspense>
<ContainerRegistryList />
</Suspense>
) : null
) : (
<Suspense fallback={<FlexActivityIndicator />}>
{curTabKey === 'image' ? (
// @ts-ignore
<backend-ai-registry-list active={curTabKey === 'registry'} />
)}
</Flex>
<backend-ai-environment-list active={curTabKey === 'image'} />
) : null}
{curTabKey === 'preset' && <ResourcePresetList />}
{curTabKey === 'registry' ? (
isSupportContainerRegistryGraphQL ? (
<ContainerRegistryList />
) : (
// @ts-ignore
<backend-ai-registry-list active />
)
) : null}
</Suspense>
</Card>
);
};
Expand Down

0 comments on commit ef84be4

Please sign in to comment.