orenccl commented on code in PR #6025:
URL: https://github.com/apache/gravitino/pull/6025#discussion_r1898542939


##########
web/web/src/app/metalakes/metalake/rightContent/LinkVersionDialog.js:
##########
@@ -0,0 +1,504 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+'use client'
+
+import { useState, forwardRef, useEffect, Fragment } from 'react'
+
+import {
+  Box,
+  Button,
+  Dialog,
+  DialogActions,
+  DialogContent,
+  Fade,
+  FormControl,
+  FormHelperText,
+  Grid,
+  IconButton,
+  InputLabel,
+  TextField,
+  Typography
+} from '@mui/material'
+
+import Icon from '@/components/Icon'
+
+import { useAppDispatch } from '@/lib/hooks/useStore'
+import { linkVersion } from '@/lib/store/metalakes'
+
+import * as yup from 'yup'
+import { useForm, Controller, useFieldArray } from 'react-hook-form'
+import { yupResolver } from '@hookform/resolvers/yup'
+
+import { groupBy } from 'lodash-es'
+import { keyRegex } from '@/lib/utils/regex'
+import { useSearchParams } from 'next/navigation'
+import { useAppSelector } from '@/lib/hooks/useStore'
+
+const defaultValues = {
+  uri: '',
+  aliases: [{ name: '' }],
+  comment: '',
+  propItems: []
+}
+
+const schema = yup.object().shape({
+  uri: yup.string().required(),
+  aliases: yup
+    .array()
+    .of(
+      yup.object().shape({
+        name: yup
+          .string()
+          .required('This aliase is required')
+          .test('not-number', 'Aliase cannot be a number or numeric string', 
value => {
+            return value === undefined || isNaN(Number(value))
+          })
+      })
+    )
+    .test('unique', 'Aliase must be unique', (aliases, ctx) => {
+      const values = aliases?.filter(a => !!a.name).map(a => a.name)
+      const duplicates = values.filter((value, index, self) => 
self.indexOf(value) !== index)
+
+      if (duplicates.length > 0) {
+        const duplicateIndex = values.lastIndexOf(duplicates[0])
+
+        return ctx.createError({
+          path: `aliases.${duplicateIndex}.name`,
+          message: 'This aliase is duplicated'
+        })
+      }
+
+      return true
+    }),
+  propItems: yup.array().of(
+    yup.object().shape({
+      required: yup.boolean(),
+      key: yup.string().required(),
+      value: yup.string().when('required', {
+        is: true,
+        then: schema => schema.required()
+      })
+    })
+  )
+})
+
+const Transition = forwardRef(function Transition(props, ref) {
+  return <Fade ref={ref} {...props} />
+})
+
+const LinkVersionDialog = props => {
+  const { open, setOpen, type = 'create', data = {} } = props
+  const searchParams = useSearchParams()
+  const metalake = searchParams.get('metalake')
+  const catalog = searchParams.get('catalog')
+  const schemaName = searchParams.get('schema')
+  const catalogType = searchParams.get('type')
+  const model = searchParams.get('model')
+  const [innerProps, setInnerProps] = useState([])
+  const dispatch = useAppDispatch()
+  const store = useAppSelector(state => state.metalakes)
+  const [cacheData, setCacheData] = useState()
+
+  const {
+    control,
+    reset,
+    watch,
+    setValue,
+    getValues,
+    handleSubmit,
+    trigger,
+    formState: { errors }
+  } = useForm({
+    defaultValues,
+    mode: 'all',
+    resolver: yupResolver(schema)
+  })
+
+  const handleFormChange = ({ index, event }) => {
+    let data = [...innerProps]
+    data[index][event.target.name] = event.target.value
+
+    if (event.target.name === 'key') {
+      const invalidKey = !keyRegex.test(event.target.value)
+      data[index].invalid = invalidKey
+    }
+
+    const nonEmptyKeys = data.filter(item => item.key.trim() !== '')
+    const grouped = groupBy(nonEmptyKeys, 'key')
+    const duplicateKeys = Object.keys(grouped).some(key => grouped[key].length 
> 1)

Review Comment:
   Just realize `groupBy` does not trim `key`.
   So " key1" and "key1" may not be detected as duplicates, right?
   
   ```javascript
   const nonEmptyKeys = data
     .filter(item => item.key.trim() !== '') // Filter out empty keys
     .map(item => ({ ...item, key: item.key.trim() })); // Trim the key
   
   const grouped = groupBy(nonEmptyKeys, 'key'); // Group by the trimmed key
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to