@@ -17,14 +17,16 @@ import {
1717 type ComboboxOption ,
1818 cn ,
1919 Loader ,
20+ toast ,
2021} from '@sim/emcn'
2122import { createLogger } from '@sim/logger'
2223import { getErrorMessage } from '@sim/utils/errors'
2324import { X } from 'lucide-react'
2425import { useParams } from 'next/navigation'
25- import { useForm } from 'react-hook-form'
26+ import { type FieldErrors , useForm } from 'react-hook-form'
2627import { z } from 'zod'
2728import type { StrategyOptions } from '@/lib/chunkers/types'
29+ import { KNOWLEDGE_BASE_DESCRIPTION_MAX_LENGTH } from '@/lib/knowledge/constants'
2830import { formatFileSize , validateKnowledgeBaseFile } from '@/lib/uploads/utils/file-utils'
2931import { ACCEPT_ATTRIBUTE } from '@/lib/uploads/utils/validation'
3032import { useKnowledgeUpload } from '@/app/workspace/[workspaceId]/knowledge/hooks/use-knowledge-upload'
@@ -58,7 +60,13 @@ const FormSchema = z
5860 . min ( 1 , 'Name is required' )
5961 . max ( 100 , 'Name must be less than 100 characters' )
6062 . refine ( ( value ) => value . trim ( ) . length > 0 , 'Name cannot be empty' ) ,
61- description : z . string ( ) . max ( 500 , 'Description must be less than 500 characters' ) . optional ( ) ,
63+ description : z
64+ . string ( )
65+ . max (
66+ KNOWLEDGE_BASE_DESCRIPTION_MAX_LENGTH ,
67+ `Description must be ${ KNOWLEDGE_BASE_DESCRIPTION_MAX_LENGTH } characters or less`
68+ )
69+ . optional ( ) ,
6270 minChunkSize : z
6371 . number ( )
6472 . min ( 1 , 'Min chunk size must be at least 1 character' )
@@ -223,6 +231,15 @@ export const CreateBaseModal = memo(function CreateBaseModal({
223231 const isSubmitting =
224232 createKnowledgeBaseMutation . isPending || deleteKnowledgeBaseMutation . isPending || isUploading
225233
234+ const onInvalid = ( formErrors : FieldErrors < FormInputValues > ) => {
235+ const firstMessage = Object . values ( formErrors ) . find (
236+ ( fieldError ) => typeof fieldError ?. message === 'string'
237+ ) ?. message
238+ toast . error (
239+ typeof firstMessage === 'string' ? firstMessage : 'Please fix the highlighted fields'
240+ )
241+ }
242+
226243 const onSubmit = async ( data : FormValues ) => {
227244 setSubmitStatus ( null )
228245
@@ -292,7 +309,7 @@ export const CreateBaseModal = memo(function CreateBaseModal({
292309 < ChipModal open = { open } onOpenChange = { handleClose } srTitle = 'Create Knowledge Base' size = 'lg' >
293310 < ChipModalHeader onClose = { ( ) => handleClose ( false ) } > Create Knowledge Base</ ChipModalHeader >
294311
295- < form onSubmit = { handleSubmit ( onSubmit ) } className = 'flex min-h-0 flex-1 flex-col' >
312+ < form onSubmit = { handleSubmit ( onSubmit , onInvalid ) } className = 'flex min-h-0 flex-1 flex-col' >
296313 < button type = 'submit' hidden disabled = { isSubmitting || ! nameValue ?. trim ( ) } />
297314 < ChipModalBody >
298315 < input
@@ -317,7 +334,7 @@ export const CreateBaseModal = memo(function CreateBaseModal({
317334 />
318335 </ ChipModalField >
319336
320- < ChipModalField type = 'custom' title = 'Description' >
337+ < ChipModalField type = 'custom' title = 'Description' error = { errors . description ?. message } >
321338 < ChipTextarea
322339 placeholder = 'Describe this knowledge base (optional)'
323340 rows = { 4 }
@@ -520,7 +537,7 @@ export const CreateBaseModal = memo(function CreateBaseModal({
520537 : 'Creating...'
521538 : 'Creating...'
522539 : 'Create' ,
523- onClick : handleSubmit ( onSubmit ) ,
540+ onClick : handleSubmit ( onSubmit , onInvalid ) ,
524541 disabled : isSubmitting || ! nameValue ?. trim ( ) ,
525542 } }
526543 />
0 commit comments