|
@@ -1,12 +1,45 @@
|
|
|
// 项目显示别名(用户自定义标签):权威数据在 settings JSON(AppSettings.workspaceAliases),
|
|
// 项目显示别名(用户自定义标签):权威数据在 settings JSON(AppSettings.workspaceAliases),
|
|
|
-// 响应式读经 useSettings 的 settingsStore 快照,写经 settingService.update 读改写整个映射。
|
|
|
|
|
|
|
+// 响应式读经 settingsStore 快照,写经 settingService.update 读改写整个映射。
|
|
|
// 仅展示层覆盖:不改 workspacePath / workspaceIdentity(AGENTS.md 身份规则),
|
|
// 仅展示层覆盖:不改 workspacePath / workspaceIdentity(AGENTS.md 身份规则),
|
|
|
// 项目移除重开别名不丢;空串/纯空白 = 清除别名回退文件夹名。
|
|
// 项目移除重开别名不丢;空串/纯空白 = 清除别名回退文件夹名。
|
|
|
-import { useCallback } from "react";
|
|
|
|
|
-import { useSettings } from "@/hooks/useSettingService.js";
|
|
|
|
|
|
|
+//
|
|
|
|
|
+// 容错说明(合并上游 v3.15.1 后补充):上游新增的 WorkspaceSidebarItem 单测会在
|
|
|
|
|
+// 不挂 ServiceProvider 的情况下直接渲染组件。这里不能走 useSettings(其内部
|
|
|
|
|
+// useServices 在无 Provider 时抛错),改用 useOptionalServices + useSyncExternalStore
|
|
|
|
|
+// 订阅同一份 settingsStore:无服务环境返回空别名与 no-op 写入,真实应用行为不变。
|
|
|
|
|
+import { useCallback, useEffect, useSyncExternalStore } from "react";
|
|
|
|
|
+import { useOptionalServices } from "@/hooks/useServices.js";
|
|
|
|
|
+import {
|
|
|
|
|
+ getSettingsStore,
|
|
|
|
|
+ refreshSettingsStore,
|
|
|
|
|
+} from "@/hooks/useSettingService.js";
|
|
|
|
|
|
|
|
export function useWorkspaceAliases() {
|
|
export function useWorkspaceAliases() {
|
|
|
- const { settings, update } = useSettings();
|
|
|
|
|
|
|
+ const services = useOptionalServices();
|
|
|
|
|
+ const settingService = services?.settingService;
|
|
|
|
|
+ const settingsStore = getSettingsStore(settingService);
|
|
|
|
|
+
|
|
|
|
|
+ const subscribe = useCallback(
|
|
|
|
|
+ (onStoreChange: () => void) => {
|
|
|
|
|
+ settingsStore.listeners.add(onStoreChange);
|
|
|
|
|
+ return () => {
|
|
|
|
|
+ settingsStore.listeners.delete(onStoreChange);
|
|
|
|
|
+ };
|
|
|
|
|
+ },
|
|
|
|
|
+ [settingsStore],
|
|
|
|
|
+ );
|
|
|
|
|
+ const getSnapshot = useCallback(
|
|
|
|
|
+ () => settingsStore.snapshot,
|
|
|
|
|
+ [settingsStore],
|
|
|
|
|
+ );
|
|
|
|
|
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
|
|
|
+
|
|
|
|
|
+ // 挂载时(及服务实例变化时)触发一次刷新;无服务环境 refreshSettingsStore 直接返回。
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ void refreshSettingsStore(settingService);
|
|
|
|
|
+ }, [settingService]);
|
|
|
|
|
+
|
|
|
|
|
+ const settings = snapshot.settings;
|
|
|
const aliases = settings?.workspaceAliases ?? {};
|
|
const aliases = settings?.workspaceAliases ?? {};
|
|
|
|
|
|
|
|
const resolveWorkspaceAlias = useCallback(
|
|
const resolveWorkspaceAlias = useCallback(
|
|
@@ -19,6 +52,9 @@ export function useWorkspaceAliases() {
|
|
|
|
|
|
|
|
const setWorkspaceAlias = useCallback(
|
|
const setWorkspaceAlias = useCallback(
|
|
|
async (workspaceKey: string, alias: string) => {
|
|
async (workspaceKey: string, alias: string) => {
|
|
|
|
|
+ if (!settingService) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
const trimmed = alias.trim();
|
|
const trimmed = alias.trim();
|
|
|
// 读改写整个映射:settingService.update 是浅 patch,必须带上完整 record,
|
|
// 读改写整个映射:settingService.update 是浅 patch,必须带上完整 record,
|
|
|
// 否则会把其它项目的别名整块冲掉。
|
|
// 否则会把其它项目的别名整块冲掉。
|
|
@@ -28,9 +64,11 @@ export function useWorkspaceAliases() {
|
|
|
} else {
|
|
} else {
|
|
|
delete next[workspaceKey];
|
|
delete next[workspaceKey];
|
|
|
}
|
|
}
|
|
|
- await update({ workspaceAliases: next });
|
|
|
|
|
|
|
+ await settingService.update({ workspaceAliases: next });
|
|
|
|
|
+ // 本窗口立即回读新快照;跨窗口同步走既有 settings 广播链路。
|
|
|
|
|
+ await refreshSettingsStore(settingService);
|
|
|
},
|
|
},
|
|
|
- [settings?.workspaceAliases, update],
|
|
|
|
|
|
|
+ [settingService, settings?.workspaceAliases],
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
return { resolveWorkspaceAlias, setWorkspaceAlias };
|
|
return { resolveWorkspaceAlias, setWorkspaceAlias };
|