把 components/admin/* 和 components/platform/* 共 11 个文件
移到对应 features/<name>/ 下,components/ 只保留跨特性共享的
common/ 子目录(features/admin/* 之前和 components/admin/* 同名
共存,迫使 features/admin/AdminPages.tsx 出现 barrel,顺带消除了
barrel 副作用 CSS 丢失的隐患)。
新的目录约定:
- features/<name>/ = 特性模块,所有页面 / Modal / state / hooks
/ routes 都在内,无 components/<name>/ 并列
- components/common/ = 仅放 ≥2 个特性共用的 widgets
(Icon、Sidebar、Toast、Topbar、WelcomePanel)
改动:
- 11 文件 git mv (components/{admin,platform}/* → features/*)
- 8 文件的 Icon 相对路径 '../common/Icon' → '../../components/common/Icon'
- 2 文件补 admin.css side-effect import(UserManagementPage /
ProjectManagementPage 之前依赖 AdminPages barrel)
- 3 文件(引用方)更新 import:SystemAdminPage、DashboardRoute、
ScriptsPage
- 删除 features/admin/AdminPages.tsx barrel(无消费者)
- 删除空目录 components/admin、components/platform
验证:
- pnpm typecheck 通过
- pnpm build 通过,CSS 体积 35.11 kB 与重构前一致(无样式增减)
- routes.ts / routes/platform.tsx 路径未动,route id 不变
踩坑:
第一轮把 '../common/Icon' 错改成 '../../../components/common/Icon',
typecheck 报 11 个 Cannot find module。原因:features/admin/ 和
components/admin/ 都是 depth 2,'../../X' 在两边都解析到 app/X,
只有 '../X' 才需要多一层。正确改法是 '../../components/common/Icon'。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
317 lines
10 KiB
TypeScript
317 lines
10 KiB
TypeScript
import { type ChangeEvent, type MouseEvent as ReactMouseEvent, type RefObject, useMemo } from "react";
|
|
import Icon from "../../components/common/Icon";
|
|
import { WorkspaceTreeGroup } from "~/features/platform/WorkspaceTree";
|
|
import { useScriptWorkspaceStore } from "~/features/platform/state/scriptWorkspaceStore";
|
|
import type { ScriptItem, WorkspaceDirectory } from "~/services/api";
|
|
import { type AuthUser } from "~/context/AuthContext";
|
|
import type { ResourceItem } from "~/services/api";
|
|
|
|
type ScriptExplorerProps = {
|
|
scripts: ScriptItem[];
|
|
filteredScripts: ScriptItem[];
|
|
directories: WorkspaceDirectory[];
|
|
dataResources: ResourceItem[];
|
|
user: AuthUser | null;
|
|
selectedId: string | null;
|
|
loading: boolean;
|
|
refreshing: boolean;
|
|
uploading: boolean;
|
|
keyword: string;
|
|
onKeywordChange: (keyword: string) => void;
|
|
onRefresh: () => void;
|
|
onUpload: () => void;
|
|
onOpenCreateDialog: (parentPath?: string, scriptType?: "notebook" | "python") => void;
|
|
onOpenFolderDialog: (parentPath?: string) => void;
|
|
onChooseUpload: (parentPath?: string) => void;
|
|
onContextMenu: (
|
|
event: ReactMouseEvent,
|
|
target: { kind: "root" | "directory" | "file"; path: string; script?: ScriptItem }
|
|
) => void;
|
|
onSelect: (scriptId: string) => void;
|
|
onCopyResourcePath: (jupyterPath: string) => void;
|
|
uploadInputRef: RefObject<HTMLInputElement | null>;
|
|
onHandleUpload: (event: ChangeEvent<HTMLInputElement>) => void;
|
|
};
|
|
|
|
export function ScriptExplorer({
|
|
scripts,
|
|
filteredScripts,
|
|
directories,
|
|
dataResources,
|
|
user,
|
|
selectedId,
|
|
loading,
|
|
refreshing,
|
|
uploading,
|
|
keyword,
|
|
onKeywordChange,
|
|
onRefresh,
|
|
onUpload,
|
|
onOpenCreateDialog,
|
|
onOpenFolderDialog,
|
|
onChooseUpload,
|
|
onContextMenu,
|
|
onSelect,
|
|
onCopyResourcePath,
|
|
uploadInputRef,
|
|
onHandleUpload,
|
|
}: ScriptExplorerProps) {
|
|
const expandedPaths = useScriptWorkspaceStore((s) => s.expandedPaths);
|
|
const loadingChildrenPaths = useScriptWorkspaceStore(
|
|
(s) => s.loadingChildrenPaths,
|
|
);
|
|
const members = useScriptWorkspaceStore((s) => s.members);
|
|
const onToggle = useScriptWorkspaceStore((s) => s.toggleExpanded);
|
|
|
|
const dataByOwner = useMemo(() => {
|
|
const map = new Map<string, ResourceItem[]>();
|
|
for (const r of dataResources) {
|
|
const list = map.get(r.owner_user_id) ?? [];
|
|
list.push(r);
|
|
map.set(r.owner_user_id, list);
|
|
}
|
|
return map;
|
|
}, [dataResources]);
|
|
|
|
const memberScriptGroups = useMemo(() => {
|
|
const visibleScripts =
|
|
user?.is_system_admin === true
|
|
? filteredScripts
|
|
: filteredScripts.filter(
|
|
(item) =>
|
|
item.owner_user_id === user?.user_id ||
|
|
item.visibility === "workspace" ||
|
|
item.visibility === "public",
|
|
);
|
|
|
|
// 用工作区成员列表播种分组 —— 顶层"我 / user1 / user2 / …"折叠分组
|
|
// 的来源。即使某成员尚未加载任何脚本/数据(默认折叠、点击才拉取),
|
|
// 也作为空分组出现,保证目录树结构稳定可见(修"目录树结构消失")。
|
|
const byOwner = new Map<string, ScriptItem[]>();
|
|
for (const m of members) {
|
|
if (!byOwner.has(m.user_id)) byOwner.set(m.user_id, []);
|
|
}
|
|
// 当前用户兜底(members 未就绪时仍渲染"我"的分组)。
|
|
if (user?.user_id && !byOwner.has(user.user_id)) {
|
|
byOwner.set(user.user_id, []);
|
|
}
|
|
for (const item of visibleScripts) {
|
|
const list = byOwner.get(item.owner_user_id) ?? [];
|
|
list.push(item);
|
|
byOwner.set(item.owner_user_id, list);
|
|
}
|
|
// data-only owner(只有数据资源、没有 scripts 的用户,且不在 members 列表
|
|
// 里,如已移除成员遗留的资源)也要出现在分组里。
|
|
for (const ownerUserId of dataByOwner.keys()) {
|
|
if (!byOwner.has(ownerUserId)) {
|
|
byOwner.set(ownerUserId, []);
|
|
}
|
|
}
|
|
|
|
// 成员 id → display_name 优先取 members 列表(最准)。
|
|
const memberName = new Map(members.map((m) => [m.user_id, m.display_name]));
|
|
|
|
const groups: {
|
|
user: AuthUser | null;
|
|
scripts: ScriptItem[];
|
|
directories: WorkspaceDirectory[];
|
|
dataResources: ResourceItem[];
|
|
}[] = [];
|
|
for (const [ownerUserId, groupScripts] of byOwner.entries()) {
|
|
const groupDataResources = dataByOwner.get(ownerUserId) ?? [];
|
|
const displayName =
|
|
memberName.get(ownerUserId) ??
|
|
groupScripts[0]?.owner_display_name ??
|
|
groupDataResources[0]?.owner_display_name ??
|
|
(ownerUserId === user?.user_id ? user?.display_name : null) ??
|
|
`${ownerUserId.slice(-6)}…`;
|
|
const groupUser =
|
|
ownerUserId === user?.user_id
|
|
? user
|
|
: ({
|
|
user_id: ownerUserId,
|
|
username: ownerUserId,
|
|
display_name: displayName,
|
|
email: null,
|
|
status: "unknown",
|
|
role_code: null,
|
|
is_system_admin: false,
|
|
} as AuthUser);
|
|
const inferred = inferredDirectories(groupScripts, ownerUserId);
|
|
// directories flat 数组现在按 owner_user_id 标记,按 owner 切分后与
|
|
// inferred 合并(inferred 补全 fetched 目录行未覆盖的祖先路径)。
|
|
const ownerDirs = directories.filter(
|
|
(d) => d.owner_user_id === ownerUserId,
|
|
);
|
|
groups.push({
|
|
user: groupUser,
|
|
scripts: groupScripts,
|
|
directories: mergeDirectories(ownerDirs, inferred),
|
|
dataResources: groupDataResources,
|
|
});
|
|
}
|
|
|
|
groups.sort((a, b) => {
|
|
if (a.user?.user_id === user?.user_id) return -1;
|
|
if (b.user?.user_id === user?.user_id) return 1;
|
|
return (a.user?.display_name ?? a.user?.user_id ?? "").localeCompare(
|
|
b.user?.display_name ?? b.user?.user_id ?? "",
|
|
);
|
|
});
|
|
|
|
return groups;
|
|
}, [filteredScripts, directories, user, dataByOwner, members]);
|
|
|
|
return (
|
|
<aside className="explorer">
|
|
<div className="explorer__header">
|
|
<div>
|
|
<h2>脚本目录</h2>
|
|
</div>
|
|
<div className="explorer__actions">
|
|
<button
|
|
className="text-button"
|
|
type="button"
|
|
disabled={uploading}
|
|
onClick={onUpload}
|
|
>
|
|
<Icon name="upload" size={15} />
|
|
{uploading ? "上传中…" : "上传"}
|
|
</button>
|
|
<input
|
|
ref={uploadInputRef}
|
|
className="visually-hidden"
|
|
type="file"
|
|
accept=".py,.ipynb,.csv,.xlsx,.xls,.tsv,.json,.parquet,.txt"
|
|
multiple
|
|
onChange={onHandleUpload}
|
|
/>
|
|
<button
|
|
className="text-button"
|
|
type="button"
|
|
onClick={() => onOpenCreateDialog()}
|
|
>
|
|
<Icon name="plus" size={16} />
|
|
新建
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="search-box">
|
|
<Icon name="search" size={17} />
|
|
<input
|
|
aria-label="搜索脚本"
|
|
placeholder="搜索脚本名称"
|
|
value={keyword}
|
|
onChange={(event) => onKeywordChange(event.target.value)}
|
|
/>
|
|
<button
|
|
className={refreshing ? "is-spinning" : ""}
|
|
type="button"
|
|
aria-label="刷新脚本"
|
|
onClick={onRefresh}
|
|
>
|
|
<Icon name="refresh" size={16} />
|
|
</button>
|
|
</div>
|
|
|
|
<div className="tree-scroll">
|
|
{loading ? (
|
|
<div className="tree-skeleton">
|
|
<span /><span /><span /><span />
|
|
</div>
|
|
) : (
|
|
<>
|
|
{memberScriptGroups.map((group) => {
|
|
const ownerKey = group.user?.user_id ?? "anon";
|
|
return (
|
|
<WorkspaceTreeGroup
|
|
key={ownerKey}
|
|
groupKey={`__group__${ownerKey}`}
|
|
ownerUserId={ownerKey}
|
|
title={`${group.user?.display_name}`}
|
|
scripts={group.scripts}
|
|
directories={group.directories}
|
|
dataResources={group.dataResources}
|
|
selectedId={selectedId}
|
|
onSelect={onSelect}
|
|
onContextMenu={
|
|
group.user?.user_id === user?.user_id
|
|
? onContextMenu
|
|
: undefined
|
|
}
|
|
readOnly={group.user?.user_id !== user?.user_id}
|
|
expandedPaths={expandedPaths}
|
|
onToggle={onToggle}
|
|
loadingChildrenPaths={loadingChildrenPaths}
|
|
onCopyResourcePath={onCopyResourcePath}
|
|
/>
|
|
);
|
|
})}
|
|
{filteredScripts.length === 0 && directories.length === 0 && (
|
|
<div className="tree-empty">
|
|
<span className="tree-empty__icon">
|
|
<Icon name="script" size={24} />
|
|
</span>
|
|
<strong>{keyword ? "没有匹配脚本" : "还没有构建脚本"}</strong>
|
|
<p>
|
|
{keyword
|
|
? "换个关键词试试"
|
|
: "新建 Notebook 或 Python 脚本开始实验"}
|
|
</p>
|
|
{!keyword && (
|
|
<button type="button" onClick={() => onOpenCreateDialog()}>
|
|
<Icon name="plus" size={15} />
|
|
新建脚本
|
|
</button>
|
|
)}
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
</aside>
|
|
);
|
|
}
|
|
|
|
function parentOf(path: string) {
|
|
const parts = path.split("/");
|
|
parts.pop();
|
|
return parts.join("/");
|
|
}
|
|
|
|
function ownedScriptPath(item: ScriptItem) {
|
|
return item.relative_path.replaceAll("\\", "/").split("/").slice(2).join("/");
|
|
}
|
|
|
|
function inferredDirectories(
|
|
items: ScriptItem[],
|
|
ownerUserId: string,
|
|
): WorkspaceDirectory[] {
|
|
const result = new Map<string, WorkspaceDirectory>();
|
|
for (const item of items) {
|
|
const parts = parentOf(ownedScriptPath(item)).split("/").filter(Boolean);
|
|
let parentPath = "";
|
|
for (const name of parts) {
|
|
const path = parentPath ? `${parentPath}/${name}` : name;
|
|
result.set(path, {
|
|
path,
|
|
name,
|
|
parent_path: parentPath,
|
|
owner_user_id: ownerUserId,
|
|
});
|
|
parentPath = path;
|
|
}
|
|
}
|
|
return [...result.values()];
|
|
}
|
|
|
|
function mergeDirectories(
|
|
left: WorkspaceDirectory[],
|
|
right: WorkspaceDirectory[],
|
|
): WorkspaceDirectory[] {
|
|
return [...new Map(
|
|
[...left, ...right].map((item) => [item.path, item]),
|
|
).values()];
|
|
}
|