Skip to content

Commit af2ceec

Browse files
committed
Refactor task utility functions to use consistent object parameter patterns
1 parent 4f27e9f commit af2ceec

4 files changed

Lines changed: 90 additions & 70 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/preview-handler.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export const preview_handler = async (params: {
110110
}
111111

112112
if (params.raw_instructions) {
113-
const updated_tasks = mark_task_as_completed_if_matches_prompt(
114-
params.context,
115-
params.raw_instructions
116-
)
113+
const updated_tasks = mark_task_as_completed_if_matches_prompt({
114+
context: params.context,
115+
prompt_text: params.raw_instructions
116+
})
117117
if (updated_tasks) {
118118
params.panel_provider.send_message({
119119
command: 'TASKS',

‎apps/editor/src/utils/tasks-utils.ts‎

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import { Task } from '@shared/types/task'
55

66
const GLOBAL_TASKS_FILENAME = 'tasks.json'
77

8-
export const get_tasks_file_path = (context: vscode.ExtensionContext) => {
9-
return path.join(context.globalStorageUri.fsPath, GLOBAL_TASKS_FILENAME)
8+
export const get_tasks_file_path = (params: {
9+
context: vscode.ExtensionContext
10+
}) => {
11+
return path.join(
12+
params.context.globalStorageUri.fsPath,
13+
GLOBAL_TASKS_FILENAME
14+
)
1015
}
1116

1217
export const load_all_tasks = (
1318
context: vscode.ExtensionContext
1419
): Record<string, Task[]> => {
15-
const file_path = get_tasks_file_path(context)
20+
const file_path = get_tasks_file_path({ context })
1621
try {
1722
if (fs.existsSync(file_path)) {
1823
const content = fs.readFileSync(file_path, 'utf8')
@@ -24,19 +29,19 @@ export const load_all_tasks = (
2429
return {}
2530
}
2631

27-
export const save_all_tasks = (
28-
context: vscode.ExtensionContext,
32+
export const save_all_tasks = (params: {
33+
context: vscode.ExtensionContext
2934
tasks: Record<string, Task[]>
30-
) => {
35+
}) => {
3136
const filtered_tasks: Record<string, Task[]> = {}
3237

33-
for (const root in tasks) {
38+
for (const root in params.tasks) {
3439
if (fs.existsSync(root)) {
35-
filtered_tasks[root] = tasks[root]
40+
filtered_tasks[root] = params.tasks[root]
3641
}
3742
}
3843

39-
const file_path = get_tasks_file_path(context)
44+
const file_path = get_tasks_file_path({ context: params.context })
4045
const dir_path = path.dirname(file_path)
4146

4247
if (!fs.existsSync(dir_path)) {
@@ -50,69 +55,81 @@ export const save_all_tasks = (
5055
}
5156
}
5257

53-
export const delete_task_from_tree = (
54-
tasks: Task[],
58+
export const delete_task_from_tree = (params: {
59+
tasks: Task[]
5560
timestamp: number
56-
): Task[] => {
57-
return tasks
58-
.filter((t) => t.created_at !== timestamp)
61+
}): Task[] => {
62+
return params.tasks
63+
.filter((t) => t.created_at !== params.timestamp)
5964
.map((t) => ({
6065
...t,
61-
children: t.children ? delete_task_from_tree(t.children, timestamp) : []
66+
children: t.children
67+
? delete_task_from_tree({
68+
tasks: t.children,
69+
timestamp: params.timestamp
70+
})
71+
: []
6272
}))
6373
}
6474

65-
export const find_task_in_tree_with_location = (
66-
tasks: Task[],
67-
id: number,
68-
parent_id: number | null = null
69-
): { task: Task; parent_id: number | null; index: number } | null => {
70-
for (let i = 0; i < tasks.length; i++) {
71-
if (tasks[i].created_at === id) {
72-
return { task: tasks[i], parent_id, index: i }
75+
export const find_task_in_tree_with_location = (params: {
76+
tasks: Task[]
77+
id: number
78+
parent_id?: number | null
79+
}): { task: Task; parent_id: number | null; index: number } | null => {
80+
const effective_parent_id =
81+
params.parent_id !== undefined ? params.parent_id : null
82+
for (let i = 0; i < params.tasks.length; i++) {
83+
if (params.tasks[i].created_at === params.id) {
84+
return { task: params.tasks[i], parent_id: effective_parent_id, index: i }
7385
}
74-
if (tasks[i].children) {
75-
const result = find_task_in_tree_with_location(
76-
tasks[i].children!,
77-
id,
78-
tasks[i].created_at
79-
)
86+
if (params.tasks[i].children) {
87+
const result = find_task_in_tree_with_location({
88+
tasks: params.tasks[i].children!,
89+
id: params.id,
90+
parent_id: params.tasks[i].created_at
91+
})
8092
if (result) return result
8193
}
8294
}
8395
return null
8496
}
8597

86-
export const insert_task_in_tree = (
87-
tasks: Task[],
88-
task: Task,
89-
parent_id: number | null,
98+
export const insert_task_in_tree = (params: {
99+
tasks: Task[]
100+
task: Task
101+
parent_id: number | null
90102
index: number
91-
): { tasks: Task[]; success: boolean } => {
92-
if (parent_id === null) {
93-
const new_tasks = [...tasks]
94-
if (index >= 0 && index <= new_tasks.length) {
95-
new_tasks.splice(index, 0, task)
103+
}): { tasks: Task[]; success: boolean } => {
104+
if (params.parent_id === null) {
105+
const new_tasks = [...params.tasks]
106+
if (params.index >= 0 && params.index <= new_tasks.length) {
107+
new_tasks.splice(params.index, 0, params.task)
96108
} else {
97-
new_tasks.push(task)
109+
new_tasks.push(params.task)
98110
}
99111
return { tasks: new_tasks, success: true }
100112
}
101113

102114
let success = false
103-
const new_tasks = tasks.map((t) => {
104-
if (t.created_at === parent_id) {
115+
const new_tasks = params.tasks.map((t) => {
116+
if (t.created_at === params.parent_id) {
105117
success = true
106118
const new_children = t.children ? [...t.children] : []
107-
if (index >= 0 && index <= new_children.length) {
108-
new_children.splice(index, 0, task)
119+
if (params.index >= 0 && params.index <= new_children.length) {
120+
new_children.splice(params.index, 0, params.task)
109121
} else {
110-
new_children.push(task)
122+
new_children.push(params.task)
111123
}
112124
return { ...t, children: new_children }
113125
}
114126
if (t.children) {
115-
const result = insert_task_in_tree(t.children, task, parent_id, index)
127+
const result = insert_task_in_tree({
128+
tasks: t.children,
129+
task: params.task,
130+
parent_id: params.parent_id,
131+
index: params.index
132+
})
116133
if (result.success) {
117134
success = true
118135
return { ...t, children: result.tasks }
@@ -123,21 +140,21 @@ export const insert_task_in_tree = (
123140
return { tasks: new_tasks, success }
124141
}
125142

126-
export const mark_task_as_completed_if_matches_prompt = (
127-
context: vscode.ExtensionContext,
143+
export const mark_task_as_completed_if_matches_prompt = (params: {
144+
context: vscode.ExtensionContext
128145
prompt_text: string
129-
): Record<string, Task[]> | null => {
130-
if (!prompt_text.trim()) return null
146+
}): Record<string, Task[]> | null => {
147+
if (!params.prompt_text.trim()) return null
131148

132-
const all_tasks = load_all_tasks(context)
149+
const all_tasks = load_all_tasks(params.context)
133150
if (Object.keys(all_tasks).length == 0) return null
134151

135152
let file_changed = false
136153
const relevant_tasks: Record<string, Task[]> = {}
137154

138155
const process_tasks = (tasks: Task[]) => {
139156
for (const task of tasks) {
140-
if (!task.is_checked && task.text.trim() == prompt_text.trim()) {
157+
if (!task.is_checked && task.text.trim() == params.prompt_text.trim()) {
141158
task.is_checked = true
142159
file_changed = true
143160
}
@@ -158,7 +175,7 @@ export const mark_task_as_completed_if_matches_prompt = (
158175
}
159176

160177
if (file_changed) {
161-
save_all_tasks(context, all_tasks)
178+
save_all_tasks({ context: params.context, tasks: all_tasks })
162179
return relevant_tasks
163180
}
164181

‎apps/editor/src/views/panel/backend/message-handlers/handle-delete-task.ts‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ export const handle_delete_task = async (
3131
// 1. Load and Find
3232
let all_data = load_all_tasks(panel_provider.context)
3333
const root_tasks = all_data[message.root] || []
34-
const task_info = find_task_in_tree_with_location(
35-
root_tasks,
36-
message.timestamp
37-
)
34+
const task_info = find_task_in_tree_with_location({
35+
tasks: root_tasks,
36+
id: message.timestamp
37+
})
3838

3939
if (!task_info) {
4040
return
4141
}
4242

4343
// 2. Delete
44-
const new_root_tasks = delete_task_from_tree(root_tasks, message.timestamp)
44+
const new_root_tasks = delete_task_from_tree({
45+
tasks: root_tasks,
46+
timestamp: message.timestamp
47+
})
4548
all_data[message.root] = new_root_tasks
46-
save_all_tasks(panel_provider.context, all_data)
49+
save_all_tasks({ context: panel_provider.context, tasks: all_data })
4750
broadcast_tasks(all_data)
4851

4952
const is_empty = (task: Task): boolean => {
@@ -66,12 +69,12 @@ export const handle_delete_task = async (
6669
all_data = load_all_tasks(panel_provider.context) // Reload to get latest state
6770
const current_root_tasks = all_data[message.root] || []
6871

69-
const result = insert_task_in_tree(
70-
current_root_tasks,
71-
task_info.task,
72-
task_info.parent_id,
73-
task_info.index
74-
)
72+
const result = insert_task_in_tree({
73+
tasks: current_root_tasks,
74+
task: task_info.task,
75+
parent_id: task_info.parent_id,
76+
index: task_info.index
77+
})
7578

7679
if (result.success) {
7780
all_data[message.root] = result.tasks
@@ -80,7 +83,7 @@ export const handle_delete_task = async (
8083
all_data[message.root] = [...current_root_tasks, task_info.task]
8184
}
8285

83-
save_all_tasks(panel_provider.context, all_data)
86+
save_all_tasks({ context: panel_provider.context, tasks: all_data })
8487
broadcast_tasks(all_data)
8588
}
8689
}

‎apps/editor/src/views/panel/backend/message-handlers/handle-save-tasks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export const handle_save_tasks = async (
1313
...message.tasks
1414
}
1515

16-
save_all_tasks(panel_provider.context, all_data)
16+
save_all_tasks({ context: panel_provider.context, tasks: all_data })
1717
}

0 commit comments

Comments
 (0)