mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-22 16:10:50 +00:00
- 在脚本配置中添加 targetGroups 字段用于指定动态组路径 - 更新工具规范定义以支持动态组路径参数 - 实现组范围的连接脚本动态继承功能 - 更新 CLI 适配器以支持 --target-groups 参数 - 添加测试用例验证动态组路径映射和解析 - 更新国际化资源文件添加相关提示文本 - 重构脚本目标匹配逻辑分离显式主机和组路径匹配 - 实现组路径变更时的重映射和删除处理逻辑 - 更新 UI 组件支持组路径选择和显示功能 - 修改文档描述更新动态组路径使用说明
164 lines
4.9 KiB
TypeScript
164 lines
4.9 KiB
TypeScript
import { useCallback } from 'react';
|
|
|
|
import { hostTreeInlineGroupDeleteStore } from '../../application/state/hostTreeInlineGroupDeleteStore';
|
|
import { hostTreeInlineGroupEditStore } from '../../application/state/hostTreeInlineGroupEditStore';
|
|
import { hostTreeInlineHostEditStore } from '../../application/state/hostTreeInlineHostEditStore';
|
|
import {
|
|
allocateUnnamedGroupPath,
|
|
applyGroupPathRename,
|
|
ensureAncestorPathsExpanded,
|
|
groupDisplayName,
|
|
remapSnippetTargetGroupPaths,
|
|
} from '../../domain/hostGroupPathMutations';
|
|
import type { Host, ManagedSource, Snippet } from '../../types';
|
|
import { toast } from '../ui/toast';
|
|
|
|
type UseHostTreeInlineGroupActionsParams = {
|
|
customGroups: string[];
|
|
hosts: Host[];
|
|
snippets: Snippet[];
|
|
managedSources: ManagedSource[];
|
|
onUpdateCustomGroups: (groups: string[]) => void;
|
|
onUpdateHosts: (hosts: Host[]) => void;
|
|
onUpdateSnippets: (snippets: Snippet[]) => void;
|
|
onUpdateManagedSources: (sources: ManagedSource[]) => void;
|
|
selectedGroupPath: string | null;
|
|
setSelectedGroupPath: (path: string | null) => void;
|
|
ensurePathExpanded: (path: string) => void;
|
|
unnamedGroupLabel: string;
|
|
t: (key: string) => string;
|
|
};
|
|
|
|
export function useHostTreeInlineGroupActions({
|
|
customGroups,
|
|
hosts,
|
|
snippets,
|
|
managedSources,
|
|
onUpdateCustomGroups,
|
|
onUpdateHosts,
|
|
onUpdateSnippets,
|
|
onUpdateManagedSources,
|
|
selectedGroupPath,
|
|
setSelectedGroupPath,
|
|
ensurePathExpanded,
|
|
unnamedGroupLabel,
|
|
t,
|
|
}: UseHostTreeInlineGroupActionsParams) {
|
|
const startInlineNewGroup = useCallback((parentPath?: string) => {
|
|
hostTreeInlineHostEditStore.clear();
|
|
const parent = parentPath ?? null;
|
|
const { name, path } = allocateUnnamedGroupPath(customGroups, parent, unnamedGroupLabel);
|
|
onUpdateCustomGroups(Array.from(new Set([...customGroups, path])));
|
|
if (parent) {
|
|
ensureAncestorPathsExpanded(parent, ensurePathExpanded);
|
|
ensurePathExpanded(parent);
|
|
}
|
|
hostTreeInlineGroupEditStore.startEdit({
|
|
groupPath: path,
|
|
initialName: name,
|
|
isNew: true,
|
|
});
|
|
}, [customGroups, ensurePathExpanded, onUpdateCustomGroups, unnamedGroupLabel]);
|
|
|
|
const startInlineRenameGroup = useCallback((groupPath: string) => {
|
|
hostTreeInlineHostEditStore.clear();
|
|
hostTreeInlineGroupEditStore.startEdit({
|
|
groupPath,
|
|
initialName: groupDisplayName(groupPath),
|
|
isNew: false,
|
|
});
|
|
}, []);
|
|
|
|
const cancelInlineGroupEdit = useCallback(() => {
|
|
const edit = hostTreeInlineGroupEditStore.getEdit();
|
|
if (!edit) return;
|
|
if (edit.isNew) {
|
|
onUpdateCustomGroups(customGroups.filter((groupPath) => groupPath !== edit.groupPath));
|
|
}
|
|
hostTreeInlineGroupEditStore.clear();
|
|
}, [customGroups, onUpdateCustomGroups]);
|
|
|
|
const commitInlineGroupRename = useCallback((rawName: string) => {
|
|
const edit = hostTreeInlineGroupEditStore.getEdit();
|
|
if (!edit) return;
|
|
|
|
const result = applyGroupPathRename({
|
|
renameTargetPath: edit.groupPath,
|
|
nextName: rawName,
|
|
customGroups,
|
|
hosts,
|
|
managedSources,
|
|
});
|
|
|
|
if (result.ok === false) {
|
|
if (result.error === 'unchanged') {
|
|
hostTreeInlineGroupEditStore.clear();
|
|
return;
|
|
}
|
|
if (result.error === 'required') {
|
|
if (edit.isNew) {
|
|
cancelInlineGroupEdit();
|
|
return;
|
|
}
|
|
toast.error(t('vault.groups.errors.required'));
|
|
return;
|
|
}
|
|
if (result.error === 'invalidChars') {
|
|
toast.error(t('vault.groups.errors.invalidChars'));
|
|
return;
|
|
}
|
|
if (result.error === 'duplicatePath') {
|
|
toast.error(t('vault.groups.errors.duplicatePath'));
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (result.updatedManagedSources.some((source, index) => source !== managedSources[index])) {
|
|
onUpdateManagedSources(result.updatedManagedSources);
|
|
}
|
|
onUpdateCustomGroups(result.updatedGroups);
|
|
onUpdateHosts(result.updatedHosts);
|
|
const nextSnippets = remapSnippetTargetGroupPaths(snippets, edit.groupPath, result.nextPath);
|
|
if (nextSnippets !== snippets) onUpdateSnippets(nextSnippets);
|
|
|
|
if (
|
|
selectedGroupPath
|
|
&& (selectedGroupPath === edit.groupPath
|
|
|| selectedGroupPath.startsWith(`${edit.groupPath}/`))
|
|
) {
|
|
const suffix = selectedGroupPath === edit.groupPath
|
|
? ''
|
|
: selectedGroupPath.slice(edit.groupPath.length);
|
|
setSelectedGroupPath(result.nextPath + suffix);
|
|
}
|
|
|
|
hostTreeInlineGroupEditStore.clear();
|
|
}, [
|
|
cancelInlineGroupEdit,
|
|
customGroups,
|
|
hosts,
|
|
managedSources,
|
|
onUpdateCustomGroups,
|
|
onUpdateHosts,
|
|
onUpdateSnippets,
|
|
onUpdateManagedSources,
|
|
selectedGroupPath,
|
|
setSelectedGroupPath,
|
|
snippets,
|
|
t,
|
|
]);
|
|
|
|
const startInlineDeleteGroup = useCallback((groupPath: string) => {
|
|
hostTreeInlineGroupDeleteStore.open(groupPath);
|
|
}, []);
|
|
|
|
return {
|
|
startInlineNewGroup,
|
|
startInlineRenameGroup,
|
|
commitInlineGroupRename,
|
|
cancelInlineGroupEdit,
|
|
startInlineDeleteGroup,
|
|
};
|
|
}
|