Skip to content

Commit dc149f6

Browse files
committed
Refactor task creation and editing focus flow
- when typing a task, it should not be saved to tasks.json until we hit enter/escape. make sure there are no race conditions - in case of duplicated workspaces: when creating task in one, the input field also appears in the other. should not. ├── apps │ └── editor │ └── src │ ├── commands │ │ ├── duplicate-workspace-command.ts │ │ └── set-ranges-command.ts │ ├── constants │ │ └── state-keys.ts │ ├── context │ │ ├── context-initialization.ts │ │ └── providers │ │ └── workspace │ │ └── workspace-provider.ts │ ├── extension.ts │ ├── migrations │ │ └── migrate-ranges-to-global-storage.ts │ ├── utils │ │ └── global-workspace-storage.ts │ └── views │ └── prompt │ ├── backend │ │ ├── message-handlers │ │ │ ├── handle-clear-completed-tasks.ts │ │ │ ├── handle-delete-task.ts │ │ │ ├── handle-get-completed-tasks.ts │ │ │ ├── handle-get-tasks.ts │ │ │ ├── handle-save-completed-tasks.ts │ │ │ ├── handle-save-tasks.ts │ │ │ └── index.ts │ │ └── prompt-view-provider.ts │ ├── frontend │ │ └── Home │ │ ├── Home.tsx │ │ └── hooks │ │ └── use-tasks.ts │ └── types │ └── messages.ts └── packages ├── shared │ └── src │ └── types │ └── task.ts └── ui └── src └── components └── editor └── prompt-view └── Tasks ├── Tasks.tsx ├── components │ └── TaskGroup.tsx └── hooks └── use-auto-focus-new-task.ts
1 parent 9c36b8c commit dc149f6

4 files changed

Lines changed: 58 additions & 75 deletions

File tree

‎apps/editor/src/views/prompt/frontend/Home/Home.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,15 @@ export const Home: React.FC<Props> = (props) => {
287287
on_change={(updated_task) => {
288288
handle_change(active_root, tasks[active_root] || [], updated_task)
289289
}}
290-
on_add={() => {
291-
handle_add(active_root, tasks[active_root] || [])
290+
on_add={(timestamp) => {
291+
handle_add(active_root, tasks[active_root] || [], timestamp)
292292
}}
293-
on_add_subtask={(parent_task) => {
293+
on_add_subtask={(parent_task, timestamp) => {
294294
handle_add_subtask(
295295
active_root,
296296
tasks[active_root] || [],
297-
parent_task
297+
parent_task,
298+
timestamp
298299
)
299300
}}
300301
on_delete={(timestamp) => {

‎apps/editor/src/views/prompt/frontend/Home/hooks/use-tasks.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ export const use_tasks = (vscode: any) => {
100100
)
101101
}
102102

103-
const handle_add = (root: string, root_tasks: Task[]) => {
103+
const handle_add = (root: string, root_tasks: Task[], timestamp: number) => {
104104
const new_task: Task = {
105105
text: '',
106106
is_checked: false,
107-
created_at: Date.now(),
107+
created_at: timestamp,
108108
children: []
109109
}
110110
const new_list = [...root_tasks, new_task]
@@ -114,12 +114,13 @@ export const use_tasks = (vscode: any) => {
114114
const handle_add_subtask = (
115115
root: string,
116116
root_tasks: Task[],
117-
parent_task: Task
117+
parent_task: Task,
118+
timestamp: number
118119
) => {
119120
const new_task: Task = {
120121
text: '',
121122
is_checked: false,
122-
created_at: Date.now(),
123+
created_at: timestamp,
123124
children: []
124125
}
125126
handle_tasks_change(

‎packages/ui/src/components/editor/prompt-view/Tasks/Tasks.tsx‎

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import cn from 'classnames'
1010
import { Task } from '@shared/types/task'
1111
import { IconButton } from '../../common/IconButton'
1212
import { TaskGroup, SortableTask } from './components/TaskGroup'
13-
import { use_auto_focus_new_task } from './hooks/use-auto-focus-new-task'
1413

1514
dayjs.extend(relativeTime)
1615

@@ -19,8 +18,8 @@ export namespace Tasks {
1918
tasks: Task[]
2019
on_reorder: (tasks: Task[]) => void
2120
on_change: (task: Task) => void
22-
on_add: () => void
23-
on_add_subtask?: (parent_task: Task) => void
21+
on_add: (timestamp: number) => void
22+
on_add_subtask?: (parent_task: Task, timestamp: number) => void
2423
on_delete: (created_at: number) => void
2524
translations: {
2625
placeholder: string
@@ -51,6 +50,7 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
5150
null
5251
)
5352
const [editing_initial_text, set_editing_initial_text] = useState<string>('')
53+
const [editing_current_text, set_editing_current_text] = useState<string>('')
5454
const [copied_timestamp, set_copied_timestamp] = useState<number | null>(null)
5555
const prevent_edit_ref = useRef(false)
5656
const is_canceling_ref = useRef(false)
@@ -59,10 +59,24 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
5959
set_editing_timestamp(timestamp)
6060
if (text !== undefined) {
6161
set_editing_initial_text(text)
62+
set_editing_current_text(text)
6263
}
6364
}
6465

65-
use_auto_focus_new_task(props.tasks, handle_set_editing)
66+
const commit_edit = (task: Task) => {
67+
const text = editing_current_text.trim()
68+
const has_children = task.children && task.children.length > 0
69+
70+
if (!text) {
71+
if (!editing_initial_text.trim() && !has_children) {
72+
props.on_delete(task.created_at)
73+
}
74+
} else {
75+
if (editing_current_text !== task.text) {
76+
props.on_change({ ...task, text: editing_current_text })
77+
}
78+
}
79+
}
6680

6781
const handle_cancel_edit = (task: Task) => {
6882
is_canceling_ref.current = true
@@ -71,9 +85,6 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
7185
const has_children = task.children && task.children.length > 0
7286
if (!editing_initial_text.trim() && !has_children) {
7387
props.on_delete(task.created_at)
74-
} else {
75-
const { ...rest } = task as any
76-
props.on_change({ ...rest, text: editing_initial_text })
7788
}
7889

7990
setTimeout(() => {
@@ -85,15 +96,16 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
8596
if (is_canceling_ref.current) return
8697
if (editing_timestamp !== task.created_at) return
8798
set_editing_timestamp(null)
88-
if (!task.text.trim()) {
89-
const has_children = task.children && task.children.length > 0
90-
if (!editing_initial_text.trim() && !has_children) {
91-
props.on_delete(task.created_at)
92-
} else if (editing_initial_text.trim()) {
93-
const { ...rest } = task as any
94-
props.on_change({ ...rest, text: editing_initial_text })
95-
}
96-
}
99+
commit_edit(task)
100+
}
101+
102+
const handle_save_edit = (task: Task) => {
103+
is_canceling_ref.current = true
104+
set_editing_timestamp(null)
105+
commit_edit(task)
106+
setTimeout(() => {
107+
is_canceling_ref.current = false
108+
}, 100)
97109
}
98110

99111
const tree = useMemo(() => add_ids(props.tasks), [props.tasks])
@@ -163,7 +175,9 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
163175
is_collapsed: !params.task.is_collapsed
164176
})
165177
} else {
166-
props.on_add_subtask?.(params.task)
178+
const timestamp = Date.now()
179+
props.on_add_subtask?.(params.task, timestamp)
180+
handle_set_editing(timestamp, '')
167181
}
168182
}}
169183
title={
@@ -194,7 +208,7 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
194208
[styles['item__actions--visible']]: is_editing
195209
})}
196210
>
197-
{params.task.text && (
211+
{(is_editing ? editing_current_text : params.task.text) && (
198212
<IconButton
199213
codicon_icon={
200214
copied_timestamp === params.task.created_at
@@ -207,7 +221,9 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
207221
}
208222
on_click={(e) => {
209223
e.stopPropagation()
210-
navigator.clipboard.writeText(params.task.text)
224+
navigator.clipboard.writeText(
225+
is_editing ? editing_current_text : params.task.text
226+
)
211227
set_copied_timestamp(params.task.created_at)
212228
setTimeout(() => {
213229
set_copied_timestamp((current) =>
@@ -234,10 +250,9 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
234250

235251
{is_editing ? (
236252
<Textarea
237-
value={params.task.text}
253+
value={editing_current_text}
238254
on_change={(value) => {
239-
const { ...rest } = params.task as any
240-
props.on_change({ ...rest, text: value })
255+
set_editing_current_text(value)
241256
}}
242257
autofocus
243258
on_blur={() => {
@@ -249,6 +264,7 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
249264
handle_cancel_edit(params.task)
250265
} else if (e.key === 'Enter' && !e.shiftKey) {
251266
e.preventDefault()
267+
handle_save_edit(params.task)
252268
}
253269
}}
254270
/>
@@ -289,8 +305,16 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
289305
on_reorder={handle_tree_reorder}
290306
render_item={render_item}
291307
className={styles.tasks}
292-
on_add={props.on_add}
293-
get_on_add_subtask={(parent) => () => props.on_add_subtask?.(parent)}
308+
on_add={() => {
309+
const timestamp = Date.now()
310+
props.on_add(timestamp)
311+
handle_set_editing(timestamp, '')
312+
}}
313+
get_on_add_subtask={(parent) => () => {
314+
const timestamp = Date.now()
315+
props.on_add_subtask?.(parent, timestamp)
316+
handle_set_editing(timestamp, '')
317+
}}
294318
add_text={props.translations.add_new}
295319
/>
296320
</div>

‎packages/ui/src/components/editor/prompt-view/Tasks/hooks/use-auto-focus-new-task.ts‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)