Skip to content

Commit 152cd1f

Browse files
fix(plugin-gantt): a lookup parentField nests its tasks (#34455)
Fixes #34395 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:甘特图的「父任务字段」是查找(lookup)字段时,子任务重新挂到父任务下面显示,不再全部平铺。 - 为什么改:甘特图读记录时会自动展开查找字段,父字段拿到的是整条父记录而不是它的 id;任务树按 id 找父任务,于是一个都对不上。HotCRM 项目任务(`parent_task` 自查找)实测命中。 - 风险与回滚:风险低,只改一处取值:展开后的对象取 `id`(没有再取 `_id`),原本就是 id 的值原样通过,空值仍是顶层。回滚即还原这一个提交。 - 先看:`ui/packages/plugin-gantt/src/ObjectGantt.tsx` 的 `taskParentId` 及其唯一调用处(`parent:` 那一行);再看 `ObjectGantt.harness.test.tsx` 末尾新增的 `ObjectGantt.lookupParent-34395` 块。 - 两分钟验证:`pnpm --dir ui exec vitest run -t lookupParent packages/plugin-gantt/src/ObjectGantt.harness.test.tsx`,预期 4 个用例全部通过。 - 需要你做的:无,席位落地。 ## What changes `ObjectGantt` builds each task's `parent` from the record's `parentField`. The record query auto-`$expand`s every lookup, so for a lookup (or self-lookup) `parentField` the value is the related row, not its id; the task tree keys parents by id, so no child matched its parent and every task drew flat, on both the object-gantt block and the gantt list view. A new local helper, `taskParentId`, reads an object parent down to `id`, falling back to `_id`, the same read `normalizeDependencies` already makes of an expanded predecessor (and `ObjectTree`'s `parentIdOf` makes of the same rows). A scalar id passes through unchanged and a null or missing parent stays `null`, so a `parentField` that already held a bare id behaves exactly as before. No existing exported helper fit: `extractLookupId` in `@object-ui/plugin-detail` is not part of that package's public entry and also falls back to `.value` and maps arrays. The quick-filter ancestor walk in the same component reads `task.parent` too, so a quick filter on a lookup-parent gantt now keeps a matching child's ancestors as it already did for scalar parents. Changeset: `.changeset/34395-gantt-lookup-parent-nests.md` (`@object-ui/plugin-gantt` patch), at the repository root, where `ui/AGENTS.md` (Housekeeping) puts every new changeset; the root config does not read `ui/.changeset/`. ## Tests New top-level block `ObjectGantt.lookupParent-34395` in the shared harness `ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx` (a new `ObjectGantt` pin joins that file rather than opening a new one). It renders the real `ObjectGantt` over a `psa_task` object with a `parent_task` self-lookup and reads the tasks handed to the `GanttView` stand-in: - an expanded lookup parent (`{ id: 'p1', … }`) gives the child `parent === 'p1'`, the parent task's own id; the case also asserts the query asked for `$expand` of `parent_task`; - an expanded parent carrying only `_id` resolves through it; - CONTROL: a scalar id parent still nests; - CONTROL: a null and a missing parent stay top-level. That the tree nests a task whose `parent` equals another task's id is the other half, already pinned in `GanttView.tree.test.tsx`. <details><summary>Full harness file at 4028dbb: 58/58</summary> ``` pnpm --dir ui exec vitest run --maxWorkers=2 packages/plugin-gantt/src/ObjectGantt.harness.test.tsx Test Files 1 passed (1) Tests 58 passed (58) os-verify-lock: VERDICT command-exit 0 ``` </details> <details><summary>Ablation: the fix reverted, the two lookup cases go red, the controls stay green</summary> Prediction written before the run: the two expanded-lookup cases red, both CONTROLs green. Observed: exactly that. Mutation through `scripts/ablation-replace.mjs` (anchor hit 1 time, on-disk blob moved), restored by blob hash. `ObjectGantt` is imported by the test through its relative source path, so no build leg applies. ``` ablation-replace: anchor "parent: parentField ? taskParentId(record[parentField]) : undefined," x1 (before) ablation-replace: replace "parent: parentField ? record[parentField] ?? null : undefined," x0 -> x1 ablation-replace: ok mutation landed: anchor 1 -> 0, blob 40ac121 -> d81d034c3a3b × ObjectGantt.lookupParent-34395 > an expanded lookup parent hands the tree the parent task id, so the child nests × ObjectGantt.lookupParent-34395 > an expanded lookup parent that carries only `_id` resolves through it ✓ ObjectGantt.lookupParent-34395 > CONTROL: a scalar id parent still nests, passed through as it is ✓ ObjectGantt.lookupParent-34395 > CONTROL: a null or missing parent stays top-level AssertionError: expected { id: 'p1', subject: 'Phase 1' } to be 'p1' // Object.is equality AssertionError: expected { _id: 'p1', subject: 'Phase 1' } to be 'p1' // Object.is equality Tests 2 failed | 2 passed | 54 skipped (58) ablation-replace: ok restored: blob == HEAD (40ac121) and `git diff HEAD` is empty ``` </details> ## Acceptance notes - None filed. No out-of-scope defect found on the touched lines. ## Verification Merged `origin/main` (merge, no rebase) before the final push; the incoming commits touch no `ui/` path. <details><summary>Local subset at 4db9832</summary> - `pnpm --filter @object-ui/plugin-gantt type-check` (`tsc --noEmit && tsc -p tsconfig.test.json`, the second program includes `src/**/*.test.tsx`): exit 0. - `pnpm --dir ui exec eslint` on the two touched source files: 0 errors (warnings are the file's existing `no-explicit-any` class). - `pnpm --dir ui check:new-line-citations`: `0 new citation(s)`. - `node scripts/pm/dispatch-gates.mjs --commands`: 38 derived commands, all run under the shared verify lock, each exit recorded before any pipe; `--ran` reconciliation: `38 derived, 38 run, 0 NOT-MEASURED, 0 UNRUN`, every exit 0. - Not local, declared to CI: the five type-check programs `dispatch-gates` names as CI-only, `Test UI`, `Lint UI`, `Type Check UI`, `Build & E2E UI`. No full dogfood run: the title carries no `!` and no `(narrowing)` arm. </details> ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (not touched) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes. - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: its generated artifacts are regenerated and committed. (not touched) - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. - [x] The *Before Submitting* list in CONTRIBUTING.md is done. --- _Generated by [Claude Code](https://claude.ai/code/session_01FEkQ5obaLbqgzRUR9xXfft)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3a08833 commit 152cd1f

3 files changed

Lines changed: 134 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/plugin-gantt': patch
3+
---
4+
5+
fix(plugin-gantt): a Gantt whose `parentField` is a lookup nests its tasks again
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The Gantt reads each record with its lookups expanded, so the value under a lookup `parentField` (a self-lookup such as a task's `parent_task`) is the related row, not its id. The task tree matches a child to its parent by id, so every task drew flat: summary tasks did not nest their subtasks, on both the object-gantt block and the gantt list view. The Gantt now reads the parent's `id` (or `_id`) off an expanded row, the way it already reads predecessor links. A `parentField` that holds a plain id behaves as before, and a record with no parent stays at the top level.

‎ui/packages/plugin-gantt/src/ObjectGantt.harness.test.tsx‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,3 +2056,109 @@ describe('ObjectGantt.retiredApiProvider-12325', () => {
20562056
});
20572057
});
20582058
});
2059+
2060+
// ── ObjectGantt.lookupParent-34395 ──────────────────────────────────────────────
2061+
2062+
/**
2063+
* objectstack#34395 — a `parentField` that is a lookup nests its tasks.
2064+
*
2065+
* The record query auto-`$expand`s every lookup, so the value under a lookup
2066+
* `parentField` is the related ROW (`{ id, subject, … }`), not its id. The task
2067+
* tree keys parents by id (`GanttView.tree.test.tsx` pins that half), so the
2068+
* task's `parent` must be the parent task's `id` for the child to draw under
2069+
* it. These cases pin the half that lives here: what `ObjectGantt` hands the
2070+
* tree as `parent`, for an expanded lookup, a scalar id and no parent.
2071+
*/
2072+
describe('ObjectGantt.lookupParent-34395', () => {
2073+
let lastTasks: any[] = [];
2074+
2075+
beforeEach(() => {
2076+
lastTasks = [];
2077+
standIns.ganttView = ({ tasks }: any) => {
2078+
lastTasks = tasks;
2079+
return <div data-testid="gantt-view" data-task-count={String(tasks.length)} />;
2080+
};
2081+
});
2082+
2083+
const OBJECT_SCHEMA = {
2084+
name: 'psa_task',
2085+
fields: {
2086+
id: { name: 'id', type: 'text' },
2087+
subject: { name: 'subject', type: 'text' },
2088+
start_date: { name: 'start_date', type: 'date' },
2089+
end_date: { name: 'end_date', type: 'date' },
2090+
// A self-lookup: the shape a project/subtask hierarchy is authored in.
2091+
parent_task: { name: 'parent_task', type: 'lookup', reference: 'psa_task' },
2092+
},
2093+
};
2094+
2095+
const PARENT_ROW = { id: 'p1', subject: 'Phase 1', start_date: '2026-01-01', end_date: '2026-01-20' };
2096+
2097+
function childRow(parent_task: unknown) {
2098+
return { id: 'c1', subject: 'Design', start_date: '2026-01-02', end_date: '2026-01-08', parent_task };
2099+
}
2100+
2101+
async function tasksFor(rows: any[]) {
2102+
const dataSource = {
2103+
find: vi.fn(async () => ({ data: rows, total: rows.length })),
2104+
findOne: vi.fn(),
2105+
create: vi.fn(),
2106+
update: vi.fn(),
2107+
delete: vi.fn(),
2108+
getObjectSchema: vi.fn(async () => OBJECT_SCHEMA),
2109+
} as any;
2110+
const schema: any = {
2111+
type: 'object-gantt',
2112+
objectName: 'psa_task',
2113+
gantt: {
2114+
titleField: 'subject',
2115+
startDateField: 'start_date',
2116+
endDateField: 'end_date',
2117+
parentField: 'parent_task',
2118+
},
2119+
};
2120+
render(<ObjectGantt schema={schema} dataSource={dataSource} />);
2121+
let settled: any[] = [];
2122+
await waitFor(() => {
2123+
const tasks = lastTasks;
2124+
expect(tasks.map((t) => t.title)).toEqual(rows.map((r) => r.subject));
2125+
settled = tasks;
2126+
});
2127+
return { tasks: settled, dataSource };
2128+
}
2129+
2130+
function byId(tasks: any[], id: string) {
2131+
const task = tasks.find((t) => String(t.id) === id);
2132+
expect(task, `task ${id}`).toBeDefined();
2133+
return task;
2134+
}
2135+
2136+
it('an expanded lookup parent hands the tree the parent task id, so the child nests', async () => {
2137+
const { tasks, dataSource } = await tasksFor([PARENT_ROW, childRow({ id: 'p1', subject: 'Phase 1' })]);
2138+
// The query really asks for the expansion this case feeds back.
2139+
expect(dataSource.find.mock.calls[0][1].$expand).toContain('parent_task');
2140+
const parent = byId(tasks, 'p1');
2141+
const child = byId(tasks, 'c1');
2142+
expect(child.parent).toBe(parent.id);
2143+
expect(parent.parent).toBeNull();
2144+
});
2145+
2146+
it('an expanded lookup parent that carries only `_id` resolves through it', async () => {
2147+
const { tasks } = await tasksFor([PARENT_ROW, childRow({ _id: 'p1', subject: 'Phase 1' })]);
2148+
expect(byId(tasks, 'c1').parent).toBe(byId(tasks, 'p1').id);
2149+
});
2150+
2151+
it('CONTROL: a scalar id parent still nests, passed through as it is', async () => {
2152+
const { tasks } = await tasksFor([PARENT_ROW, childRow('p1')]);
2153+
expect(byId(tasks, 'c1').parent).toBe('p1');
2154+
expect(byId(tasks, 'c1').parent).toBe(byId(tasks, 'p1').id);
2155+
});
2156+
2157+
it('CONTROL: a null or missing parent stays top-level', async () => {
2158+
const missing = { id: 'c2', subject: 'Review', start_date: '2026-01-09', end_date: '2026-01-12' };
2159+
const { tasks } = await tasksFor([PARENT_ROW, childRow(null), missing]);
2160+
expect(byId(tasks, 'p1').parent).toBeNull();
2161+
expect(byId(tasks, 'c1').parent).toBeNull();
2162+
expect(byId(tasks, 'c2').parent).toBeNull();
2163+
});
2164+
});

‎ui/packages/plugin-gantt/src/ObjectGantt.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ export function normalizeDependencies(raw: unknown): GanttDependency[] {
299299
return out;
300300
}
301301

302+
/**
303+
* A task's parent id, read from the value under the configured `parentField`.
304+
*
305+
* The record query auto-`$expand`s lookup fields, so for a lookup (or
306+
* self-lookup) `parentField` the value is the related ROW, not its id. The task
307+
* tree keys parents by id, so the row is read down to its `id` (`_id` as the
308+
* fallback) — the same read {@link normalizeDependencies} makes of an expanded
309+
* predecessor. A scalar id passes through as it is; no value is `null`.
310+
*/
311+
function taskParentId(raw: unknown): string | number | null {
312+
if (raw != null && typeof raw === 'object') {
313+
const row = raw as { id?: string | number | null; _id?: string | number | null };
314+
return row.id ?? row._id ?? null;
315+
}
316+
return (raw as string | number | null | undefined) ?? null;
317+
}
318+
302319
/**
303320
* The two keys the fetch path reads off the node that the published
304321
* {@link ObjectGanttSchema} does not declare: the package-local hand-off
@@ -1545,7 +1562,7 @@ export const ObjectGantt: React.FC<ObjectGanttProps> = ({
15451562
hasOwnDates: !!(startDate && endDate),
15461563
progress: Math.min(100, Math.max(0, progress || 0)), // Clamp between 0-100
15471564
dependencies: normalizeDependencies(dependencies),
1548-
parent: parentField ? record[parentField] ?? null : undefined,
1565+
parent: parentField ? taskParentId(record[parentField]) : undefined,
15491566
type: typeField ? normalizeTaskType(record[typeField]) : undefined,
15501567
locked: lockField ? !!record[lockField] : undefined,
15511568
color,

0 commit comments

Comments
 (0)