fix: loadChildren + loadScripts + loadDataResources
This commit is contained in:
@@ -283,28 +283,41 @@ export const createScriptsSlice: StateCreator<
|
||||
|
||||
loadDataResources: async (parentPath = "", ownerUserId) => {
|
||||
const api = requireApi();
|
||||
const cacheKey = ownerCacheKey(ownerUserId, parentPath);
|
||||
// 命中缓存:listResources 是非递归的,同一 (owner, parent_path) 拉过的
|
||||
// 内容不会自己变化;省去 toggleExpanded 重复展开同一目录时的网络往返。
|
||||
if (get().loadedDataResourcePaths.has(cacheKey)) return;
|
||||
set({ dataResourcesLoading: true });
|
||||
try {
|
||||
const list = await api.listResources(parentPath, { ownerUserId });
|
||||
const fresh = Array.isArray(list) ? list : [];
|
||||
set((state) => {
|
||||
// 按 owner 范围合并:丢弃该 owner 的旧资源再并入 fresh(fresh 覆盖
|
||||
// 同 id)。owner 缺省=我,故根加载/刷新我的数据时替换我的一级资源。
|
||||
// 按 (owner, parent_path) 局部替换:丢掉该 owner 在 parentPath 下的
|
||||
// 旧条目,保留该 owner 在其它路径下的条目,再并入 fresh。
|
||||
// 这样 toggleExpanded 在子目录展开时按需拉取不会把根已加载的数据
|
||||
// 资源擦掉(修"根加载后子目录展开丢数据 / 子目录数据本来不显示")。
|
||||
const targetOwner = ownerUserId ?? getCurrentUserId() ?? null;
|
||||
const kept = state.dataResources.filter(
|
||||
(r) => r.owner_user_id !== targetOwner,
|
||||
);
|
||||
const kept = state.dataResources.filter((r) => {
|
||||
if (r.owner_user_id !== targetOwner) return true;
|
||||
return parentPathOf(r.jupyter_accessible_path) !== parentPath;
|
||||
});
|
||||
const byId = new Map(kept.map((r) => [r.resource_id, r]));
|
||||
for (const item of fresh) byId.set(item.resource_id, item);
|
||||
return { dataResources: Array.from(byId.values()) };
|
||||
const nextLoaded = new Set(state.loadedDataResourcePaths);
|
||||
nextLoaded.add(cacheKey);
|
||||
return {
|
||||
dataResources: Array.from(byId.values()),
|
||||
loadedDataResourcePaths: nextLoaded,
|
||||
};
|
||||
});
|
||||
} catch {
|
||||
set((state) => {
|
||||
const targetOwner = ownerUserId ?? getCurrentUserId() ?? null;
|
||||
return {
|
||||
dataResources: state.dataResources.filter(
|
||||
(r) => r.owner_user_id !== targetOwner,
|
||||
),
|
||||
dataResources: state.dataResources.filter((r) => {
|
||||
if (r.owner_user_id !== targetOwner) return true;
|
||||
return parentPathOf(r.jupyter_accessible_path) !== parentPath;
|
||||
}),
|
||||
};
|
||||
});
|
||||
} finally {
|
||||
@@ -336,4 +349,13 @@ export const createScriptsSlice: StateCreator<
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
// 提取 jupyter-accessible 路径的父目录;用于 `loadDataResources` 局部替换时
|
||||
// 判断一条缓存资源是否落在目标 parent_path 下(list-resources 按 parent_path
|
||||
// 精确匹配,非递归)。
|
||||
function parentPathOf(path: string): string {
|
||||
const parts = path.split("/");
|
||||
parts.pop();
|
||||
return parts.join("/");
|
||||
}
|
||||
Reference in New Issue
Block a user