1+ import { IDENTIFIER_PATTERN } from "@dafthunk/types" ;
12import { ColumnDef } from "@tanstack/react-table" ;
23import MoreHorizontal from "lucide-react/icons/more-horizontal" ;
34import PlusCircle from "lucide-react/icons/plus-circle" ;
@@ -34,6 +35,7 @@ import {
3435 deleteDatabase ,
3536 useDatabases ,
3637} from "@/services/database-service" ;
38+ import { cn } from "@/utils/utils" ;
3739
3840function useDatabaseActions ( ) {
3941 const { mutateDatabases } = useDatabases ( ) ;
@@ -151,6 +153,7 @@ function createColumns(
151153
152154export function DatabasesPage ( ) {
153155 const [ isCreateDialogOpen , setIsCreateDialogOpen ] = useState ( false ) ;
156+ const [ newDatabaseName , setNewDatabaseName ] = useState ( "" ) ;
154157 const { setBreadcrumbs } = usePageBreadcrumbs ( [ ] ) ;
155158 const { organization } = useAuth ( ) ;
156159 const orgId = organization ?. id || "" ;
@@ -206,17 +209,22 @@ export function DatabasesPage() {
206209 description : "Create a new database to get started." ,
207210 } }
208211 />
209- < Dialog open = { isCreateDialogOpen } onOpenChange = { setIsCreateDialogOpen } >
212+ < Dialog
213+ open = { isCreateDialogOpen }
214+ onOpenChange = { ( open ) => {
215+ setIsCreateDialogOpen ( open ) ;
216+ if ( ! open ) setNewDatabaseName ( "" ) ;
217+ } }
218+ >
210219 < DialogContent >
211220 < DialogHeader >
212221 < DialogTitle > Create New Database</ DialogTitle >
213222 </ DialogHeader >
214223 < form
215224 onSubmit = { async ( e ) => {
216225 e . preventDefault ( ) ;
217- const formData = new FormData ( e . currentTarget ) ;
218- const name = formData . get ( "name" ) as string ;
219- await handleCreateDatabase ( name ) ;
226+ await handleCreateDatabase ( newDatabaseName . trim ( ) ) ;
227+ setNewDatabaseName ( "" ) ;
220228 } }
221229 className = "space-y-4"
222230 >
@@ -225,9 +233,22 @@ export function DatabasesPage() {
225233 < Input
226234 id = "name"
227235 name = "name"
236+ value = { newDatabaseName }
237+ onChange = { ( e ) => setNewDatabaseName ( e . target . value ) }
228238 placeholder = "Enter database name"
229- className = "mt-2"
239+ className = { cn (
240+ "mt-2" ,
241+ newDatabaseName . trim ( ) . length > 0 &&
242+ ! IDENTIFIER_PATTERN . test ( newDatabaseName . trim ( ) ) &&
243+ "border-destructive"
244+ ) }
230245 />
246+ { newDatabaseName . trim ( ) . length > 0 &&
247+ ! IDENTIFIER_PATTERN . test ( newDatabaseName . trim ( ) ) && (
248+ < p className = "text-xs text-destructive mt-1" >
249+ Letters, digits, and underscores only (e.g. my_database)
250+ </ p >
251+ ) }
231252 </ div >
232253 < DialogFooter >
233254 < Button
@@ -237,7 +258,15 @@ export function DatabasesPage() {
237258 >
238259 Cancel
239260 </ Button >
240- < Button type = "submit" > Create Database</ Button >
261+ < Button
262+ type = "submit"
263+ disabled = {
264+ newDatabaseName . trim ( ) . length === 0 ||
265+ ! IDENTIFIER_PATTERN . test ( newDatabaseName . trim ( ) )
266+ }
267+ >
268+ Create Database
269+ </ Button >
241270 </ DialogFooter >
242271 </ form >
243272 </ DialogContent >
0 commit comments