From b55dc2891fefed3c073fc5ffcaa842401e7bcf1c Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Mon, 26 Oct 2026 00:14:33 +0000 Subject: [PATCH] Fix skill detail navigation and editing --- packages/api/src/postgres/skill-repository.ts | 2 +- .../api/test/protocol-api.postgres.test.ts | 3 ++ packages/app/src/features/skills/detail.tsx | 17 +++++++++++ packages/app/src/features/skills/library.tsx | 9 +++++- packages/app/src/features/skills/routes.tsx | 4 +-- packages/app/test/skills.browser.test.mjs | 28 +++++++++++++++++++ 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 packages/app/src/features/skills/detail.tsx create mode 100644 packages/app/test/skills.browser.test.mjs diff --git a/packages/api/src/postgres/skill-repository.ts b/packages/api/src/postgres/skill-repository.ts index 8f9532e6..1621395e 100644 --- a/packages/api/src/postgres/skill-repository.ts +++ b/packages/api/src/postgres/skill-repository.ts @@ -7,7 +7,7 @@ export class SkillRepository { constructor(private db: Database, private tenantId: string, private encryptionKey: string) {} private async map(row: SkillRow, usageCount = 0, body = false) { return { id: row.id, key: row.key, revision: row.revision, ...(body ? { body: await decrypt(row.encrypted_body, this.encryptionKey) } : {}), usageCount, createdAt: row.created_at.toISOString(), updatedAt: row.updated_at.toISOString() }; } async list(q = "", limit = 30, cursor?: string) { const result = await this.db.pool.query(`SELECT s.*, count(js.job_id)::text usage_count FROM app.skills s LEFT JOIN app.job_skills js ON js.tenant_id=s.tenant_id AND js.skill_id=s.id WHERE s.tenant_id=$1 AND ($2='' OR s.key ILIKE '%'||$2||'%') AND ($3::uuid IS NULL OR s.id < $3::uuid) GROUP BY s.tenant_id, s.id ORDER BY s.id DESC LIMIT $4`, [this.tenantId, q, cursor ?? null, limit + 1]); const page = result.rows.slice(0, limit); return { items: await Promise.all(page.map(row => this.map(row, Number(row.usage_count)))), nextCursor: result.rows.length > limit ? page.at(-1)!.id : null }; } - async get(idOrKey: string, includeBody = true) { const row = (await this.db.pool.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND (id=$2::uuid OR key=$2) LIMIT 1", [this.tenantId, idOrKey])).rows[0]; return row ? this.map(row, 0, includeBody) : null; } + async get(idOrKey: string, includeBody = true) { const row = (await this.db.pool.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND (id=$2::uuid OR key=$2::text) LIMIT 1", [this.tenantId, idOrKey])).rows[0]; return row ? this.map(row, 0, includeBody) : null; } async create(key: string, body: string) { try { const row = (await this.db.pool.query("INSERT INTO app.skills(tenant_id,id,key,encrypted_body) VALUES($1,$2,$3,$4) RETURNING *", [this.tenantId, crypto.randomUUID(), key, await encrypt(body, this.encryptionKey)])).rows[0]; return this.map(row, 0, true); } catch (e: any) { if (e.code === "23505") throw new SkillConflict("Skill key is already in use."); throw e; } } async update(id: string, input: { key?: string; body?: string; expectedRevision: number }) { const row = (await this.db.pool.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND id=$2 FOR UPDATE", [this.tenantId, id])).rows[0]; if (!row) return null; if (row.revision !== input.expectedRevision) throw new SkillConflict("Skill changed since it was loaded."); try { const updated = (await this.db.pool.query("UPDATE app.skills SET key=COALESCE($3,key),encrypted_body=COALESCE($4,encrypted_body),revision=revision+1,updated_at=now() WHERE tenant_id=$1 AND id=$2 RETURNING *", [this.tenantId, id, input.key ?? null, input.body === undefined ? null : await encrypt(input.body, this.encryptionKey)])).rows[0]; return this.map(updated, 0, true); } catch (e: any) { if (e.code === "23505") throw new SkillConflict("Skill key is already in use."); throw e; } } async remove(id: string, expectedRevision: number) { return this.db.transaction(async client => { const skill = (await client.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND id=$2 FOR UPDATE", [this.tenantId, id])).rows[0]; if (!skill) return false; if (skill.revision !== expectedRevision) throw new SkillConflict("Skill changed since it was loaded."); const refs = (await client.query("SELECT j.id,j.name,j.slug,j.enabled,j.project_id FROM app.jobs j JOIN app.job_skills js ON js.tenant_id=j.tenant_id AND js.job_id=j.id WHERE js.tenant_id=$1 AND js.skill_id=$2 ORDER BY j.name", [this.tenantId, id])).rows; if (refs.length) throw new SkillReferenced(refs); await client.query("DELETE FROM app.skills WHERE tenant_id=$1 AND id=$2", [this.tenantId, id]); return true; }); } diff --git a/packages/api/test/protocol-api.postgres.test.ts b/packages/api/test/protocol-api.postgres.test.ts index 0686b6be..bbd0bc90 100644 --- a/packages/api/test/protocol-api.postgres.test.ts +++ b/packages/api/test/protocol-api.postgres.test.ts @@ -21,6 +21,9 @@ describe.skipIf(!url)("cookie protocol APIs against PostgreSQL", () => { const created = await request({ key: "review", body: "Review carefully" }); expect(created.status).toBe(201); const skill = await created.json() as { id: string }; + const detail = await worker.fetch(new Request(f.env.APP_ORIGIN + `/api/v1/skills/${skill.id}`, { headers: { Cookie: f.cookie, Origin: f.env.APP_ORIGIN } }), f.env, {} as ExecutionContext); + expect(detail.status).toBe(200); + expect(await detail.json()).toMatchObject({ id: skill.id, key: "review", body: "Review carefully", revision: 1, usageCount: 0 }); await f.db.pool.query("INSERT INTO app.job_skills(tenant_id,job_id,skill_id,position) VALUES($1,$2,$3,0)", [f.tenantId, f.jobIds[0], skill.id]); const listed = await request(); expect(listed.status).toBe(200); diff --git a/packages/app/src/features/skills/detail.tsx b/packages/app/src/features/skills/detail.tsx new file mode 100644 index 00000000..b3c597ad --- /dev/null +++ b/packages/app/src/features/skills/detail.tsx @@ -0,0 +1,17 @@ +import { useState } from "react"; +import { Link, useParams } from "@tanstack/react-router"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { api } from "factorize-api-client"; +import { Breadcrumb, Button, Card, Input, Label, Page, PageHeader, Textarea } from "../../shared/ui"; +import { messageOf } from "../auth/session"; +import { skillQuery, skillsQuery } from "./queries"; + +export function SkillDetail() { + const { skillId } = useParams({ from: "/skills/$skillId" }); + const skill = useQuery(skillQuery(skillId)); + const cache = useQueryClient(); + const [editing, setEditing] = useState(false), [key, setKey] = useState(""), [body, setBody] = useState(""); + const edit = useMutation({ mutationFn: async () => { const result = await api.PUT("/api/v1/skills/{skillId}", { params: { path: { skillId } }, body: { key, body, expectedRevision: skill.data!.revision } }); if (!result.data || result.error) throw new Error(messageOf(result.error)); return result.data; }, onSuccess: async updated => { cache.setQueryData(skillQuery(skillId).queryKey, updated); await cache.invalidateQueries({ queryKey: skillsQuery().queryKey }); setEditing(false); } }); + const startEditing = () => { if (!skill.data) return; setKey(skill.data.key); setBody(skill.data.body); setEditing(true); }; + return Skills{skill.data?.key ?? "Skill"}{skill.isPending &&

Loading skill…

}{skill.error &&

{skill.error.message}

}{skill.data && <>{editing ?
{ event.preventDefault(); edit.mutate(); }}>