Skip to content

Commit ebaff21

Browse files
committed
Refactor task rendering to use object parameters and extract task utilities into a shared module
1 parent df692d7 commit ebaff21

7 files changed

Lines changed: 232 additions & 196 deletions

File tree

‎packages/ui/src/components/editor/panel/Tasks/Tasks.tsx‎

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,27 @@ export const Tasks: React.FC<Props> = (props) => {
6262
props.on_change({ ...rest, is_checked: checked })
6363
}
6464

65-
const render_item = (
66-
task: Task,
67-
is_visually_checked: boolean,
65+
const render_item = (params: {
66+
task: Task
67+
is_visually_checked: boolean
6868
depth: number
69-
) => {
70-
const is_editing = editing_timestamp == task.created_at
71-
const is_copied = last_copied_timestamp == task.created_at
72-
const has_children = task.children && task.children.length > 0
69+
}) => {
70+
const is_editing = editing_timestamp == params.task.created_at
71+
const is_copied = last_copied_timestamp == params.task.created_at
72+
const has_children = params.task.children && params.task.children.length > 0
7373

7474
const checked_children_count = has_children
75-
? task.children!.filter((child) => child.is_checked).length
75+
? params.task.children!.filter((child) => child.is_checked).length
7676
: 0
7777

7878
return (
7979
<div
8080
className={cn(styles.item, {
8181
[styles['item--copied']]: is_copied
8282
})}
83-
style={{ paddingLeft: `${10 + depth * 20}px` }}
83+
style={{ paddingLeft: `${10 + params.depth * 20}px` }}
8484
>
85-
{[...Array(depth)].map((_, i) => (
85+
{[...Array(params.depth)].map((_, i) => (
8686
<div
8787
key={i}
8888
className={styles['indent-guide']}
@@ -91,34 +91,34 @@ export const Tasks: React.FC<Props> = (props) => {
9191
))}
9292
<div className={styles.item__left}>
9393
<SimpleCheckbox
94-
checked={task.is_checked}
95-
on_change={(checked) => handle_check_change(task, checked)}
94+
checked={params.task.is_checked}
95+
on_change={(checked) => handle_check_change(params.task, checked)}
9696
/>
9797

9898
<button
9999
className={cn(
100100
styles['add-button'],
101101
has_children
102-
? task.is_collapsed
102+
? params.task.is_collapsed
103103
? styles['add-button--expand']
104104
: styles['add-button--collapse']
105105
: styles['add-button--indent']
106106
)}
107107
onClick={(e) => {
108108
e.stopPropagation()
109109
if (has_children) {
110-
const { id, ...rest } = task as SortableTask
110+
const { id, ...rest } = params.task as SortableTask
111111
props.on_change({
112112
...rest,
113-
is_collapsed: !task.is_collapsed
113+
is_collapsed: !params.task.is_collapsed
114114
})
115115
} else {
116-
props.on_add_subtask?.(task)
116+
props.on_add_subtask?.(params.task)
117117
}
118118
}}
119119
title={
120120
has_children
121-
? task.is_collapsed
121+
? params.task.is_collapsed
122122
? 'Expand'
123123
: 'Collapse'
124124
: 'Add subtask'
@@ -131,26 +131,26 @@ export const Tasks: React.FC<Props> = (props) => {
131131
<div className={styles['item__header-left']}>
132132
<span
133133
className={cn(styles.item__time, {
134-
[styles['item__time--checked']]: is_visually_checked
134+
[styles['item__time--checked']]: params.is_visually_checked
135135
})}
136136
>
137-
{dayjs(task.created_at).fromNow()}
137+
{dayjs(params.task.created_at).fromNow()}
138138
{has_children &&
139-
` · ${checked_children_count}/${task.children!.length}`}
139+
` · ${checked_children_count}/${params.task.children!.length}`}
140140
</span>
141141
</div>
142142
<div
143143
className={cn(styles.item__actions, {
144144
[styles['item__actions--visible']]: is_editing
145145
})}
146146
>
147-
{task.text && (
147+
{params.task.text && (
148148
<IconButton
149149
codicon_icon="copy"
150150
on_click={(e) => {
151151
e.stopPropagation()
152-
props.on_copy(task.text)
153-
set_last_copied_timestamp(task.created_at)
152+
props.on_copy(params.task.text)
153+
set_last_copied_timestamp(params.task.created_at)
154154
}}
155155
title="Copy Task"
156156
/>
@@ -161,15 +161,17 @@ export const Tasks: React.FC<Props> = (props) => {
161161
is_editing ? (e) => e.preventDefault() : undefined
162162
}
163163
on_click={() =>
164-
set_editing_timestamp(is_editing ? null : task.created_at)
164+
set_editing_timestamp(
165+
is_editing ? null : params.task.created_at
166+
)
165167
}
166168
title={is_editing ? 'Save task' : 'Edit task'}
167169
/>
168170
<IconButton
169171
codicon_icon="trash"
170172
on_click={(e) => {
171173
e.stopPropagation()
172-
props.on_delete(task.created_at)
174+
props.on_delete(params.task.created_at)
173175
}}
174176
title="Delete task"
175177
/>
@@ -178,9 +180,9 @@ export const Tasks: React.FC<Props> = (props) => {
178180

179181
{is_editing ? (
180182
<Textarea
181-
value={task.text}
183+
value={params.task.text}
182184
on_change={(value) => {
183-
const { id, ...rest } = task as any
185+
const { id, ...rest } = params.task as any
184186
props.on_change({ ...rest, text: value })
185187
}}
186188
autofocus
@@ -195,13 +197,13 @@ export const Tasks: React.FC<Props> = (props) => {
195197
) : (
196198
<div
197199
className={cn(styles.item__text, {
198-
[styles['item__text--checked']]: is_visually_checked,
199-
[styles['item__text--placeholder']]: !task.text
200+
[styles['item__text--checked']]: params.is_visually_checked,
201+
[styles['item__text--placeholder']]: !params.task.text
200202
})}
201203
onMouseDown={() => {
202204
if (
203205
editing_timestamp !== null &&
204-
editing_timestamp !== task.created_at
206+
editing_timestamp !== params.task.created_at
205207
) {
206208
prevent_edit_ref.current = true
207209
setTimeout(() => {
@@ -211,10 +213,10 @@ export const Tasks: React.FC<Props> = (props) => {
211213
}}
212214
onClick={() => {
213215
if (prevent_edit_ref.current) return
214-
set_editing_timestamp(task.created_at)
216+
set_editing_timestamp(params.task.created_at)
215217
}}
216218
>
217-
{task.text || 'Click to add text...'}
219+
{params.task.text || 'Click to add text...'}
218220
</div>
219221
)}
220222
</div>

‎packages/ui/src/components/editor/panel/Tasks/components/TaskGroup.tsx‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export type SortableTask = Task & {
1212
type TaskGroupProps = {
1313
items: SortableTask[]
1414
on_reorder: (items: SortableTask[]) => void
15-
render_item: (
16-
task: Task,
17-
is_visually_checked: boolean,
15+
render_item: (params: {
16+
task: Task
17+
is_visually_checked: boolean
1818
depth: number
19-
) => React.ReactNode
19+
}) => React.ReactNode
2020
className?: string
2121
on_add?: () => void
2222
get_on_add_subtask?: (parent: Task) => () => void
@@ -41,7 +41,7 @@ export const TaskGroup: React.FC<TaskGroupProps> = (props) => {
4141
const is_visually_checked = parent_checked || item.is_checked
4242
return (
4343
<div key={item.id}>
44-
{props.render_item(item, is_visually_checked, depth)}
44+
{props.render_item({ task: item, is_visually_checked, depth })}
4545
{item.children &&
4646
item.children.length > 0 &&
4747
!item.is_collapsed && (
@@ -56,7 +56,9 @@ export const TaskGroup: React.FC<TaskGroupProps> = (props) => {
5656
render_item={props.render_item}
5757
className={props.className}
5858
on_add={
59-
props.get_on_add_subtask ? props.get_on_add_subtask(item) : undefined
59+
props.get_on_add_subtask
60+
? props.get_on_add_subtask(item)
61+
: undefined
6062
}
6163
get_on_add_subtask={props.get_on_add_subtask}
6264
parent_checked={is_visually_checked}

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview-handler.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { undo_files } from './file-operations'
55
import { preview } from './preview'
66
import { PanelProvider } from '@/views/panel/backend/panel-provider'
77
import { update_undo_button_state } from './state-manager'
8+
import { mark_task_as_completed_if_matches_prompt } from '@/utils/tasks-utils'
89

910
export let ongoing_preview_cleanup_promise: Promise<void> | null = null
1011

@@ -105,6 +106,20 @@ export const preview_handler = async (params: {
105106
history: []
106107
})
107108
}
109+
110+
if (params.raw_instructions) {
111+
const updated_tasks = mark_task_as_completed_if_matches_prompt(
112+
params.context,
113+
params.raw_instructions
114+
)
115+
if (updated_tasks) {
116+
params.panel_provider.send_message({
117+
command: 'TASKS',
118+
tasks: updated_tasks
119+
})
120+
}
121+
}
122+
108123
update_undo_button_state({
109124
context: params.context,
110125
panel_provider: params.panel_provider,
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import * as vscode from 'vscode'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
4+
import { Task } from '@shared/types/task'
5+
6+
const GLOBAL_TASKS_FILENAME = 'tasks.json'
7+
8+
export const get_tasks_file_path = (context: vscode.ExtensionContext) => {
9+
return path.join(context.globalStorageUri.fsPath, GLOBAL_TASKS_FILENAME)
10+
}
11+
12+
export const load_all_tasks = (
13+
context: vscode.ExtensionContext
14+
): Record<string, Task[]> => {
15+
const file_path = get_tasks_file_path(context)
16+
try {
17+
if (fs.existsSync(file_path)) {
18+
const content = fs.readFileSync(file_path, 'utf8')
19+
return JSON.parse(content)
20+
}
21+
} catch (error) {
22+
console.error('Error loading global tasks:', error)
23+
}
24+
return {}
25+
}
26+
27+
export const save_all_tasks = (
28+
context: vscode.ExtensionContext,
29+
tasks: Record<string, Task[]>
30+
) => {
31+
const file_path = get_tasks_file_path(context)
32+
const dir_path = path.dirname(file_path)
33+
34+
if (!fs.existsSync(dir_path)) {
35+
fs.mkdirSync(dir_path, { recursive: true })
36+
}
37+
38+
try {
39+
fs.writeFileSync(file_path, JSON.stringify(tasks, null, 2), 'utf8')
40+
} catch (error) {
41+
console.error('Failed to save tasks', error)
42+
}
43+
}
44+
45+
export const delete_task_from_tree = (
46+
tasks: Task[],
47+
timestamp: number
48+
): Task[] => {
49+
return tasks
50+
.filter((t) => t.created_at !== timestamp)
51+
.map((t) => ({
52+
...t,
53+
children: t.children ? delete_task_from_tree(t.children, timestamp) : []
54+
}))
55+
}
56+
57+
export const find_task_in_tree_with_location = (
58+
tasks: Task[],
59+
id: number,
60+
parent_id: number | null = null
61+
): { task: Task; parent_id: number | null; index: number } | null => {
62+
for (let i = 0; i < tasks.length; i++) {
63+
if (tasks[i].created_at === id) {
64+
return { task: tasks[i], parent_id, index: i }
65+
}
66+
if (tasks[i].children) {
67+
const result = find_task_in_tree_with_location(
68+
tasks[i].children!,
69+
id,
70+
tasks[i].created_at
71+
)
72+
if (result) return result
73+
}
74+
}
75+
return null
76+
}
77+
78+
export const insert_task_in_tree = (
79+
tasks: Task[],
80+
task: Task,
81+
parent_id: number | null,
82+
index: number
83+
): { tasks: Task[]; success: boolean } => {
84+
if (parent_id === null) {
85+
const new_tasks = [...tasks]
86+
if (index >= 0 && index <= new_tasks.length) {
87+
new_tasks.splice(index, 0, task)
88+
} else {
89+
new_tasks.push(task)
90+
}
91+
return { tasks: new_tasks, success: true }
92+
}
93+
94+
let success = false
95+
const new_tasks = tasks.map((t) => {
96+
if (t.created_at === parent_id) {
97+
success = true
98+
const new_children = t.children ? [...t.children] : []
99+
if (index >= 0 && index <= new_children.length) {
100+
new_children.splice(index, 0, task)
101+
} else {
102+
new_children.push(task)
103+
}
104+
return { ...t, children: new_children }
105+
}
106+
if (t.children) {
107+
const result = insert_task_in_tree(t.children, task, parent_id, index)
108+
if (result.success) {
109+
success = true
110+
return { ...t, children: result.tasks }
111+
}
112+
}
113+
return t
114+
})
115+
return { tasks: new_tasks, success }
116+
}
117+
118+
export const mark_task_as_completed_if_matches_prompt = (
119+
context: vscode.ExtensionContext,
120+
prompt_text: string
121+
): Record<string, Task[]> | null => {
122+
if (!prompt_text.trim()) return null
123+
124+
const all_tasks = load_all_tasks(context)
125+
if (Object.keys(all_tasks).length === 0) return null
126+
127+
let file_changed = false
128+
129+
const process_tasks = (tasks: Task[]) => {
130+
for (const task of tasks) {
131+
if (!task.is_checked && task.text.trim() == prompt_text.trim()) {
132+
task.is_checked = true
133+
file_changed = true
134+
}
135+
if (task.children) {
136+
process_tasks(task.children)
137+
}
138+
}
139+
}
140+
141+
if (vscode.workspace.workspaceFolders) {
142+
vscode.workspace.workspaceFolders.forEach((folder) => {
143+
const tasks = all_tasks[folder.uri.fsPath]
144+
if (tasks) {
145+
process_tasks(tasks)
146+
}
147+
})
148+
}
149+
150+
if (file_changed) {
151+
save_all_tasks(context, all_tasks)
152+
return all_tasks
153+
}
154+
155+
return null
156+
}

0 commit comments

Comments
 (0)