1+ import React from "react" ;
2+ import { Dialog , DialogClose , DialogContent , DialogPortal } from "../dialog/Dialog" ;
3+ import { DFlowFolderItemPathInput } from "./DFlowFolderItemPathInput" ;
4+ import { Flex } from "../flex/Flex" ;
5+ import { Button } from "../button/Button" ;
6+ import { DFlowFolderContextMenuGroupData , DFlowFolderContextMenuItemData } from "./DFlowFolderContextMenu" ;
7+ import { DFlowFolderProps } from "./DFlowFolder" ;
8+ import { useForm } from "../form" ;
9+
10+ export interface DFlowFolderRenameDialogProps extends DFlowFolderProps {
11+ contextData : DFlowFolderContextMenuGroupData | DFlowFolderContextMenuItemData
12+ open ?: boolean
13+ onOpenChange ?: ( open : boolean ) => void
14+ }
15+
16+ export const DFlowFolderRenameDialog : React . FC < DFlowFolderRenameDialogProps > = ( props ) => {
17+ const { open} = props
18+
19+ const [ renameDialogOpen , setRenameDialogOpen ] = React . useState ( open )
20+ const initialValues = React . useMemo ( ( ) => ( {
21+ path : props . contextData . name
22+ } ) , [ ] )
23+
24+ React . useEffect ( ( ) => {
25+ setRenameDialogOpen ( open )
26+ } , [ open ] )
27+
28+ const [ inputs , validate ] = useForm ( {
29+ initialValues : initialValues ,
30+ validate : {
31+ path : ( value ) => {
32+ return null
33+ }
34+ } ,
35+ onSubmit : ( values ) => {
36+ if ( props . contextData . type === "item" ) {
37+ props . onRename ?.( props . contextData . flow , values . path )
38+ } else if ( props . contextData . type === "group" ) {
39+ props . contextData . flows . forEach ( flow => {
40+ const newName = flow . name ?. replace ( props . contextData . name , values . path ) ?? flow . name
41+ props . onRename ?.( flow , newName ! )
42+ } )
43+ }
44+ }
45+ } )
46+
47+ return < Dialog open = { renameDialogOpen } onOpenChange = { ( open ) => {
48+ props . onOpenChange ?.( open )
49+ setRenameDialogOpen ( open )
50+ } } >
51+ < DialogPortal >
52+ < DialogContent autoFocus showCloseButton
53+ title = { props . contextData . type == "item" ? "Rename flow" : "Rename folder" } >
54+ < div >
55+ < DFlowFolderItemPathInput
56+ description = { "You can choose a new name here and only use alphanumeric names." }
57+ title = { props . contextData . type == "item" ? "Name of the flow" : "Name of the folder" }
58+ { ...inputs . getInputProps ( "path" ) } />
59+ </ div >
60+ < Flex justify = { "space-between" } align = { "center" } >
61+ < DialogClose asChild >
62+ < Button color = { "secondary" } > No, go back!</ Button >
63+ </ DialogClose >
64+ < DialogClose asChild >
65+ < Button color = { "success" } onClick = { validate } > Yes, save!</ Button >
66+ </ DialogClose >
67+ </ Flex >
68+ </ DialogContent >
69+ </ DialogPortal >
70+ </ Dialog >
71+ }
0 commit comments