-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-studio-context-menu.ts
More file actions
296 lines (262 loc) · 9.29 KB
/
use-studio-context-menu.ts
File metadata and controls
296 lines (262 loc) · 9.29 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { useCallback, useRef, useState } from 'react'
import type { TreeItemIndex } from 'react-complex-tree'
import { deleteFile, renameFile } from '~/services/file-service'
import { createFolderInProject } from '~/services/file-tree-service'
import { createAdapter, renameAdapter, deleteAdapter } from '~/services/adapter-service'
import { clearConfigurationCache, createConfiguration } from '~/services/configuration-service'
import useTabStore from '~/stores/tab-store'
import { showErrorToastFrom } from '~/components/toast'
import type { StudioItemData, StudioFolderData, StudioAdapterData } from './studio-files-data-provider'
export type StudioItemType = 'root' | 'folder' | 'configuration' | 'adapter'
export interface StudioContextMenuState {
position: { x: number; y: number }
itemId: TreeItemIndex
itemType: StudioItemType
path: string
folderPath: string
name: string
}
export interface NameDialogState {
title: string
initialValue?: string
onSubmit: (name: string) => void
}
export interface DeleteTargetState {
name: string
itemType: StudioItemType
path: string
}
export interface StudioDataProviderLike {
getTreeItem(itemId: TreeItemIndex): Promise<{ data: StudioItemData; isFolder?: boolean } | undefined>
reloadDirectory(itemId: TreeItemIndex): Promise<void>
getRootPath(): string
}
export function detectItemType(data: StudioItemData): StudioItemType {
if (typeof data === 'string') return 'root'
if ('adapterName' in data) return 'adapter'
if ('path' in data && (data as StudioFolderData).path.endsWith('.xml')) return 'configuration'
return 'folder'
}
export function getItemName(data: StudioItemData): string {
if (typeof data === 'string') return data
if ('adapterName' in data) return (data as StudioAdapterData).adapterName
if ('name' in data) return (data as StudioFolderData).name
return 'Unnamed'
}
function getParentDir(filePath: string): string {
const lastSep = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'))
return lastSep > 0 ? filePath.slice(0, lastSep) : filePath
}
function ensureXmlExtension(name: string): string {
if (name.includes('.')) return name
return `${name}.xml`
}
export function resolveItemPaths(
data: StudioItemData,
itemType: StudioItemType,
dataProvider: StudioDataProviderLike,
): { path: string; folderPath: string } {
if (typeof data === 'string') {
const rootPath = dataProvider.getRootPath()
return { path: rootPath, folderPath: rootPath }
}
if (itemType === 'adapter') {
const configPath = (data as StudioAdapterData).configPath
return { path: configPath, folderPath: getParentDir(configPath) }
}
const folderData = data as StudioFolderData
if (itemType === 'configuration') {
return { path: folderData.path, folderPath: getParentDir(folderData.path) }
}
return { path: folderData.path, folderPath: folderData.path }
}
function removeAdapterTab(configPath: string, adapterName: string): void {
const tabStore = useTabStore.getState()
const prefix = `${configPath}::${adapterName}::`
for (const tabId of Object.keys(tabStore.tabs)) {
if (tabId.startsWith(prefix)) {
tabStore.removeTabAndSelectFallback(tabId)
}
}
}
interface UseStudioContextMenuOptions {
projectName: string | undefined
dataProvider: StudioDataProviderLike | null
}
export function useStudioContextMenu({ projectName, dataProvider }: UseStudioContextMenuOptions) {
const [contextMenu, setContextMenu] = useState<StudioContextMenuState | null>(null)
const [nameDialog, setNameDialog] = useState<NameDialogState | null>(null)
const [deleteTarget, setDeleteTarget] = useState<DeleteTargetState | null>(null)
const contextMenuRef = useRef<StudioContextMenuState | null>(null)
const openContextMenu = useCallback(
async (e: React.MouseEvent, itemId: TreeItemIndex) => {
e.preventDefault()
e.stopPropagation()
if (!dataProvider) return
const item = await dataProvider.getTreeItem(itemId)
if (!item) return
const itemType = detectItemType(item.data)
const name = getItemName(item.data)
const { path, folderPath } = resolveItemPaths(item.data, itemType, dataProvider)
const state: StudioContextMenuState = {
position: { x: e.clientX, y: e.clientY },
itemId,
itemType,
path,
folderPath,
name,
}
contextMenuRef.current = state
setContextMenu(state)
},
[dataProvider],
)
const closeContextMenu = useCallback(() => {
contextMenuRef.current = null
setContextMenu(null)
}, [])
function resolveMenu(menuState?: StudioContextMenuState): StudioContextMenuState | null {
return menuState ?? contextMenuRef.current
}
const handleNewConfiguration = useCallback(
(menuState?: StudioContextMenuState) => {
const menu = resolveMenu(menuState)
if (!menu || !projectName || !dataProvider) return
closeContextMenu()
setNameDialog({
title: 'New Configuration',
onSubmit: async (name: string) => {
const fileName = ensureXmlExtension(name)
try {
await createConfiguration(projectName, `${menu.folderPath}/${fileName}`)
await dataProvider.reloadDirectory('root')
} catch (error) {
showErrorToastFrom('Failed to create configuration', error)
}
setNameDialog(null)
},
})
},
[projectName, dataProvider, closeContextMenu],
)
const handleNewAdapter = useCallback(
(menuState?: StudioContextMenuState) => {
const menu = resolveMenu(menuState)
if (!menu || !projectName || !dataProvider) return
closeContextMenu()
setNameDialog({
title: 'New Adapter',
onSubmit: async (name: string) => {
try {
await createAdapter(projectName, name, menu.path)
await dataProvider.reloadDirectory('root')
} catch (error) {
showErrorToastFrom('Failed to create adapter', error)
}
setNameDialog(null)
},
})
},
[projectName, dataProvider, closeContextMenu],
)
const handleNewFolder = useCallback(
(menuState?: StudioContextMenuState) => {
const menu = resolveMenu(menuState)
if (!menu || !projectName || !dataProvider) return
closeContextMenu()
setNameDialog({
title: 'New Folder',
onSubmit: async (name: string) => {
try {
await createFolderInProject(projectName, `${menu.folderPath}/${name}`)
await dataProvider.reloadDirectory('root')
} catch (error) {
showErrorToastFrom('Failed to create folder', error)
}
setNameDialog(null)
},
})
},
[projectName, dataProvider, closeContextMenu],
)
const handleRename = useCallback(
(menuState?: StudioContextMenuState) => {
const menu = resolveMenu(menuState)
if (!menu || !projectName || !dataProvider) return
const oldName = menu.name
closeContextMenu()
setNameDialog({
title: 'Rename',
initialValue: oldName,
onSubmit: async (newName: string) => {
if (newName === oldName) {
setNameDialog(null)
return
}
try {
if (menu.itemType === 'adapter') {
await renameAdapter(projectName, oldName, newName, menu.path)
} else {
const finalName = menu.itemType === 'configuration' ? ensureXmlExtension(newName) : newName
await renameFile(projectName, `${menu.path}/${oldName}`, `${menu.path}/${newName}`)
clearConfigurationCache(projectName, menu.path)
const newPath = `${getParentDir(menu.path)}/${finalName}`
useTabStore.getState().renameTabsForConfig(menu.path, newPath)
}
await dataProvider.reloadDirectory('root')
} catch (error) {
showErrorToastFrom('Failed to rename', error)
}
setNameDialog(null)
},
})
},
[projectName, dataProvider, closeContextMenu],
)
const handleDelete = useCallback(
(menuState?: StudioContextMenuState) => {
const menu = resolveMenu(menuState)
if (!menu) return
setDeleteTarget({
name: menu.name,
itemType: menu.itemType,
path: menu.path,
})
closeContextMenu()
},
[closeContextMenu],
)
const confirmDelete = useCallback(async () => {
if (!deleteTarget || !projectName || !dataProvider) return
try {
if (deleteTarget.itemType === 'adapter') {
await deleteAdapter(projectName, deleteTarget.name, deleteTarget.path)
removeAdapterTab(deleteTarget.path, deleteTarget.name)
} else {
await deleteFile(projectName, deleteTarget.path)
clearConfigurationCache(projectName, deleteTarget.path)
useTabStore.getState().removeTabsForConfig(deleteTarget.path)
}
await dataProvider.reloadDirectory('root')
} catch (error) {
showErrorToastFrom('Failed to delete', error)
}
setDeleteTarget(null)
}, [deleteTarget, projectName, dataProvider])
return {
contextMenu,
setContextMenu,
closeContextMenu,
nameDialog,
setNameDialog,
deleteTarget,
setDeleteTarget,
openContextMenu,
handleNewConfiguration,
handleNewAdapter,
handleNewFolder,
handleRename,
handleDelete,
confirmDelete,
}
}