You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

useComputingResource.ts 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-10-10 08:51:41
  4. * @Description: 资源规格 hook
  5. */
  6. import { getComputingResourceReq } from '@/services/pipeline';
  7. import { ComputingResource } from '@/types';
  8. import { to } from '@/utils/promise';
  9. import { type SelectProps } from 'antd';
  10. import { useCallback, useEffect, useState } from 'react';
  11. const computingResource: ComputingResource[] = [];
  12. /** 过滤资源规格 */
  13. export const filterResourceStandard: SelectProps<string, ComputingResource>['filterOption'] = (
  14. input: string,
  15. option?: ComputingResource,
  16. ) => {
  17. return (
  18. option?.computing_resource?.toLocaleLowerCase()?.includes(input.toLocaleLowerCase()) ?? false
  19. );
  20. };
  21. /** 资源规格字段 */
  22. export const resourceFieldNames = {
  23. label: 'description',
  24. value: 'id',
  25. };
  26. /** 获取资源规格 */
  27. export function useComputingResource() {
  28. const [resourceStandardList, setResourceStandardList] = useState<ComputingResource[]>([]);
  29. useEffect(() => {
  30. // 获取资源规格列表数据
  31. const getComputingResource = async () => {
  32. const params = {
  33. page: 0,
  34. size: 1000,
  35. resource_type: '',
  36. };
  37. const [res] = await to(getComputingResourceReq(params));
  38. if (res && res.data && Array.isArray(res.data.content)) {
  39. setResourceStandardList(res.data.content);
  40. computingResource.splice(0, computingResource.length, ...res.data.content);
  41. }
  42. };
  43. if (computingResource.length > 0) {
  44. setResourceStandardList(computingResource);
  45. } else {
  46. getComputingResource();
  47. }
  48. }, []);
  49. // 根据 standard 获取 description
  50. const getDescription = useCallback(
  51. (id?: string | number) => {
  52. if (!id) {
  53. return undefined;
  54. }
  55. return resourceStandardList.find((item) => Number(item.id) === Number(id))?.description;
  56. },
  57. [resourceStandardList],
  58. );
  59. return [resourceStandardList, getDescription] as const;
  60. }