Skip to content

Commit 2173da2

Browse files
author
Eric Wheeler
committed
feat: Respect parent's bulk-expanded state for new child tasks recursively
When a new child task (or any of its descendants) is added to the history view, its initial expansion state will now correctly respect the 'bulk-expanded' status of its parent or any higher ancestor. If an ancestor is bulk-expanded, the new task and its own children will also be expanded automatically. This ensures a consistent and intuitive user experience when navigating hierarchical task histories, particularly when new sub-tasks are created under an already expanded parent tree. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 8942282 commit 2173da2

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

webview-ui/src/components/history/useTaskSearch.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,47 @@ export const useTaskSearch = () => {
149149
let newExpanded = { ...prevExpanded }
150150
let changed = false
151151

152-
const checkAndExpandChildrenRecursive = (currentTasks: HierarchicalHistoryItem[]) => {
153-
for (const item of currentTasks) {
154-
if (item.parent_task_id && bulkExpandedRootItems[item.parent_task_id]) {
155-
if (!newExpanded[item.id]) {
156-
newExpanded[item.id] = true
157-
changed = true
158-
}
152+
// Helper to create a flat map of all tasks for efficient lookup by ID
153+
const allTasksMap = new Map<string, HierarchicalHistoryItem>()
154+
const populateTaskMapRecursive = (currentTaskList: HierarchicalHistoryItem[]) => {
155+
for (const item of currentTaskList) {
156+
allTasksMap.set(item.id, item)
157+
if (item.children) {
158+
populateTaskMapRecursive(item.children)
159+
}
160+
}
161+
}
162+
populateTaskMapRecursive(tasks) // `tasks` is the hierarchical list from useMemo
163+
164+
const isAncestorBulkExpanded = (
165+
itemId: string | undefined,
166+
currentBulkExpandedItems: Record<string, boolean>,
167+
): boolean => {
168+
if (!itemId) return false
169+
const item = allTasksMap.get(itemId)
170+
if (!item) return false
171+
172+
if (currentBulkExpandedItems[item.id]) {
173+
return true
174+
}
175+
// Recursively check the parent
176+
return isAncestorBulkExpanded(item.parent_task_id, currentBulkExpandedItems)
177+
}
178+
179+
const applyRecursiveExpansion = (currentTaskList: HierarchicalHistoryItem[]) => {
180+
for (const item of currentTaskList) {
181+
// If the item itself or any of its ancestors are bulk-expanded, and it's not already expanded
182+
if (!newExpanded[item.id] && isAncestorBulkExpanded(item.id, bulkExpandedRootItems)) {
183+
newExpanded[item.id] = true
184+
changed = true
159185
}
160186
if (item.children && item.children.length > 0) {
161-
checkAndExpandChildrenRecursive(item.children)
187+
applyRecursiveExpansion(item.children)
162188
}
163189
}
164190
}
165191

166-
checkAndExpandChildrenRecursive(tasks) // `tasks` is the hierarchical list
192+
applyRecursiveExpansion(tasks)
167193

168194
return changed ? newExpanded : prevExpanded
169195
})

0 commit comments

Comments
 (0)