模型库初版提交

This commit is contained in:
zhangshun
2026-05-25 08:49:29 +08:00
parent 5d08480921
commit c2ac560527
7 changed files with 17 additions and 6 deletions

View File

@@ -20,7 +20,7 @@ const updateSchema = z.object({
function buildTree(rows: FolderRow[]) {
const nodes = rows.map((row) => ({
id: String(row.id),
text: row.name,
text: `${row.name}(${row.model_count ?? 0})`,
parentId: row.parent_id,
path: row.path,
children: [] as unknown[]
@@ -53,7 +53,15 @@ function assertCanRenameRoot(folder: FolderRow) {
export async function folderRoutes(app: FastifyInstance) {
app.get("/api/folders", { preHandler: [requireAuth] }, async () => {
const rows = db.prepare("SELECT * FROM folders ORDER BY parent_id, name").all() as unknown as FolderRow[];
const rows = db.prepare(`
SELECT
f.*,
COUNT(m.id) AS model_count
FROM folders f
LEFT JOIN models m ON m.folder_id = f.id
GROUP BY f.id
ORDER BY f.parent_id, f.name
`).all() as unknown as FolderRow[];
return { folders: rows, tree: buildTree(rows) };
});

View File

@@ -22,6 +22,7 @@ export interface FolderRow {
parent_id: number | null;
name: string;
path: string;
model_count?: number;
created_at: string;
updated_at: string;
}

View File

@@ -32,7 +32,6 @@ export async function renderApp() {
<div class="content-toolbar">
<div>
<h2>模型列表</h2>
<p id="modelCount">0 个模型</p>
</div>
<div class="content-actions">
${isAdmin ? `<button id="manageUsersBtn" type="button">人员权限</button>` : ""}

View File

@@ -14,7 +14,7 @@ type JsTreeNode = {
function selectFolder(node: JsTreeNode) {
appState.selectedFolderId = Number(node.id);
appState.selectedFolderName = node.text;
appState.selectedFolderName = appState.folders.find((folder) => folder.id === Number(node.id))?.name ?? node.text;
}
async function createFolder(parentId: number | null) {
@@ -111,7 +111,7 @@ export async function loadFolders() {
}
}).on("select_node.jstree", async (_event: JQuery.Event, data: { node: { id: string; text: string } }) => {
appState.selectedFolderId = Number(data.node.id);
appState.selectedFolderName = data.node.text;
appState.selectedFolderName = appState.folders.find((folder) => folder.id === appState.selectedFolderId)?.name ?? data.node.text;
appState.page = 1;
await loadModels();
});

View File

@@ -91,7 +91,6 @@ export async function loadModels() {
appState.page -= 1;
return loadModels();
}
document.querySelector("#modelCount")!.textContent = `${result.total} 个模型`;
renderPagination({
container: document.querySelector<HTMLElement>("#modelPagination")!,
total: result.total,

View File

@@ -62,6 +62,9 @@ select {
.jstree-default .jstree-anchor {
height: 24px;
line-height: 24px;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}
.jstree-default .jstree-icon:empty {

View File

@@ -25,6 +25,7 @@ export type Folder = {
parent_id: number | null;
name: string;
path: string;
model_count: number;
};
export type ModelItem = {