forked from web3infra-foundation/mega
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewCodeView.tsx
More file actions
155 lines (135 loc) · 5.07 KB
/
NewCodeView.tsx
File metadata and controls
155 lines (135 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { useState } from 'react'
import toast from 'react-hot-toast'
import { Button } from '@gitmono/ui/Button'
import { Dialog } from '@gitmono/ui/Dialog'
import { Select, SelectTrigger, SelectValue } from '@gitmono/ui/Select'
import { useCreateEntry } from '@/hooks/useCreateEntry'
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser'
import MarkdownEditor from './MarkdownEditor'
import PathInput from './PathInput'
interface NewCodeViewProps {
currentPath?: string
onClose?: () => void
defaultType?: 'folder' | 'file'
}
const NewCodeView = ({ currentPath = '', onClose, defaultType = 'file' }: NewCodeViewProps) => {
const [path, setPath] = useState(currentPath)
const [name, setName] = useState('')
const [skipBuild, setSkipBuild] = useState(false)
const [fileType, setFileType] = useState<'folder' | 'file'>(defaultType)
const [dialogOpen, setDialogOpen] = useState(false)
const [content, setContent] = useState('')
const createEntryHook = useCreateEntry()
const { data: currentUser } = useGetCurrentUser()
const handleSubmit = () => {
const fullPath = path
const lastSlashIndex = fullPath.lastIndexOf('/')
const parentPath = lastSlashIndex > 0 ? fullPath.substring(0, lastSlashIndex) : lastSlashIndex === 0 ? '/' : ''
// Use explicit name if provided, otherwise extract from path
const fileName = name || (lastSlashIndex >= 0 ? fullPath.substring(lastSlashIndex + 1) : fullPath)
createEntryHook.mutate(
{
name: fileName,
path: parentPath,
is_directory: fileType === 'folder',
content: fileType === 'file' ? content : '',
author_email: currentUser?.email,
author_username: currentUser?.username,
mode: 'force_create',
skip_build: skipBuild
},
{
onSuccess: async () => {
toast.success('Create Change List Success!')
setDialogOpen(false)
onClose?.()
},
onError: (error: any) => {
// Try to read a useful message from the error object
const msg =
error?.message ||
(error?.response && error.response.data && error.response.data.message) ||
'Create failed. Please try again.'
toast.error(msg)
}
}
)
}
const handleCommitClick = () => {
setDialogOpen(true)
setSkipBuild(false)
}
const handleDialogClose = (open: boolean) => {
setDialogOpen(open)
if (!open) {
setSkipBuild(false)
}
}
return (
<div className='flex h-full w-full flex-col gap-2'>
<div className='flex min-h-14 w-full items-center justify-between pl-2 pr-4'>
<PathInput pathState={[path, setPath]} nameState={[name, setName]} />
<div className='flex gap-2'>
<Button disabled={name === ''} onClick={handleCommitClick}>
Create CL
</Button>
<Select
typeAhead
options={[
{ value: 'folder', label: 'Folder' },
{ value: 'file', label: 'File' }
]}
value={fileType}
onChange={(value) => {
setFileType(value as 'folder' | 'file')
}}
>
<SelectTrigger>
<SelectValue placeholder='Select Create Type' />
</SelectTrigger>
</Select>
</div>
</div>
{/*The second parameter of MarkdownEditor is to disable the editor, which is currently hidden directly. */}
{fileType === 'file' && (
<div className='w-full flex-1 overflow-y-auto'>
<MarkdownEditor contentState={[content, setContent]} disabled={false} />
</div>
)}
<Dialog.Root open={dialogOpen} onOpenChange={handleDialogClose}>
<Dialog.Content>
<Dialog.CloseButton />
<Dialog.Header>
<Dialog.Title>Create {fileType === 'folder' ? 'Folder' : 'File'}</Dialog.Title>
</Dialog.Header>
<div className='flex flex-col gap-4 py-4'>
<div className='flex items-center gap-2'>
<input
type='checkbox'
id='skipBuild_creat'
checked={skipBuild}
onChange={(e) => setSkipBuild(e.target.checked)}
className='h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500'
disabled={createEntryHook.isPending}
/>
<label htmlFor='skipBuild_creat' className='text-sm font-medium text-gray-700'>
Skip automatic build after commit
</label>
</div>
</div>
<Dialog.Footer>
<Dialog.TrailingActions>
<Button variant='flat' onClick={() => handleDialogClose(false)} disabled={createEntryHook.isPending}>
Cancel
</Button>
<Button onClick={handleSubmit} disabled={createEntryHook.isPending}>
{createEntryHook.isPending ? 'Creating...' : 'Confirm'}
</Button>
</Dialog.TrailingActions>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>
</div>
)
}
export default NewCodeView