SearchParams.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { FC, useCallback, useMemo } from 'react';
  2. import Box from '@mui/material/Box';
  3. import { INDEX_PARAMS_CONFIG } from '@/pages/databases/collections/schema/indexParamsConfig';
  4. import IndexParamField from '@/pages/databases/collections/schema/IndexParamField';
  5. import type { SearchParamsProps } from './Types';
  6. import type { IndexParamConfig } from '@/pages/databases/collections/schema/indexParamsConfig';
  7. const COMMON_PARAMS = ['level', 'radius', 'range_filter'] as const;
  8. const SearchParams: FC<SearchParamsProps> = ({
  9. indexType = '',
  10. searchParamsForm,
  11. handleFormChange,
  12. isManaged,
  13. sx = {},
  14. }) => {
  15. // Get search params and their configs based on index type
  16. const { paramList, paramConfigs } = useMemo(() => {
  17. // Common params that are always available
  18. const commonParams: Record<string, IndexParamConfig> = {
  19. radius: {
  20. label: 'radius',
  21. key: 'radius',
  22. type: 'number',
  23. required: false,
  24. // description: 'searchParams.radius.description',
  25. // helperText: 'searchParams.radius.helperText',
  26. },
  27. range_filter: {
  28. label: 'range filter',
  29. key: 'range_filter',
  30. type: 'number',
  31. required: false,
  32. // helperText: 'searchParams.range_filter.helperText',
  33. // description: 'searchParams.range_filter.description',
  34. },
  35. };
  36. if (indexType === 'AUTOINDEX' && isManaged) {
  37. commonParams.level = {
  38. label: 'level',
  39. key: 'level',
  40. type: 'number',
  41. required: false,
  42. min: 1,
  43. max: 10,
  44. defaultValue: '1',
  45. helperText: 'searchParams.level.description',
  46. };
  47. }
  48. // Get search params for the specific index type
  49. const indexParams: Record<string, IndexParamConfig> = {};
  50. Object.values(INDEX_PARAMS_CONFIG).forEach(dataTypeConfig => {
  51. const config = dataTypeConfig[indexType];
  52. if (config?.searchParams) {
  53. Object.entries(config.searchParams).forEach(([param, paramConfig]) => {
  54. indexParams[param] = paramConfig;
  55. });
  56. }
  57. });
  58. const allParams = { ...indexParams, ...commonParams };
  59. const allParamKeys = Object.keys(allParams);
  60. // Sort params to ensure common params are at the end
  61. const sortedParamKeys = allParamKeys.sort((a, b) => {
  62. const aIsCommon = COMMON_PARAMS.includes(
  63. a as (typeof COMMON_PARAMS)[number]
  64. );
  65. const bIsCommon = COMMON_PARAMS.includes(
  66. b as (typeof COMMON_PARAMS)[number]
  67. );
  68. if (aIsCommon && !bIsCommon) return 1;
  69. if (!aIsCommon && bIsCommon) return -1;
  70. if (aIsCommon && bIsCommon) {
  71. return (
  72. COMMON_PARAMS.indexOf(a as (typeof COMMON_PARAMS)[number]) -
  73. COMMON_PARAMS.indexOf(b as (typeof COMMON_PARAMS)[number])
  74. );
  75. }
  76. return 0;
  77. });
  78. return {
  79. paramList: sortedParamKeys,
  80. paramConfigs: allParams,
  81. };
  82. }, [indexType]);
  83. const handleInputChange = useCallback(
  84. (key: string, value: string | number | boolean) => {
  85. let form = { ...searchParamsForm };
  86. if (value === '') {
  87. delete form[key];
  88. } else {
  89. form = { ...searchParamsForm, [key]: value };
  90. }
  91. handleFormChange(form);
  92. },
  93. [handleFormChange, searchParamsForm]
  94. );
  95. return (
  96. <Box
  97. sx={{
  98. ...sx,
  99. display: 'flex',
  100. flexDirection: 'column',
  101. gap: 1.5,
  102. }}
  103. >
  104. {paramList.map(param => {
  105. const config = paramConfigs[param];
  106. if (!config) return null;
  107. const value = searchParamsForm[param] ?? config.defaultValue ?? '';
  108. return (
  109. <IndexParamField
  110. key={param}
  111. config={config}
  112. value={value}
  113. onChange={handleInputChange}
  114. error={''}
  115. />
  116. );
  117. })}
  118. </Box>
  119. );
  120. };
  121. export default SearchParams;