Skip to content

Commit 38c8a69

Browse files
committed
Add checkpoint description editing functionality with autofocus support in Textarea component
1 parent fdd63e9 commit 38c8a69

10 files changed

Lines changed: 154 additions & 4 deletions

File tree

‎packages/ui/src/components/editor/common/Textarea/Textarea.tsx‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Props = {
66
id?: string
77
value?: string
88
placeholder?: string
9+
autofocus?: boolean
910
min_rows?: number
1011
max_rows?: number
1112
on_change: (value: string) => void
@@ -16,6 +17,30 @@ type Props = {
1617

1718
export const Textarea = React.forwardRef<HTMLTextAreaElement, Props>(
1819
(props, ref) => {
20+
const internal_ref = React.useRef<HTMLTextAreaElement | null>(null)
21+
22+
const set_ref = React.useCallback(
23+
(node: HTMLTextAreaElement | null) => {
24+
internal_ref.current = node
25+
if (typeof ref === 'function') {
26+
ref(node)
27+
} else if (ref) {
28+
ref.current = node
29+
}
30+
},
31+
[ref]
32+
)
33+
34+
React.useEffect(() => {
35+
if (props.autofocus && internal_ref.current) {
36+
internal_ref.current.focus()
37+
internal_ref.current.setSelectionRange(
38+
internal_ref.current.value.length,
39+
internal_ref.current.value.length
40+
)
41+
}
42+
}, [])
43+
1944
const handle_key_down = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
2045
if (e.key == 'Enter' && !e.shiftKey) {
2146
e.preventDefault()
@@ -36,7 +61,7 @@ export const Textarea = React.forwardRef<HTMLTextAreaElement, Props>(
3661
minRows={props.min_rows}
3762
maxRows={props.max_rows}
3863
onFocus={props.onFocus}
39-
ref={ref}
64+
ref={set_ref}
4065
/>
4166
)
4267
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type TimelineItemProps = {
1515
label: string
1616
description?: string
1717
is_starred?: boolean
18+
can_edit?: boolean
1819
}
1920

2021
type Props = {
@@ -78,7 +79,7 @@ export const Timeline: React.FC<Props> = ({
7879
on_toggle_starred?.(item.id)
7980
}}
8081
/>
81-
{on_edit && (
82+
{on_edit && item.can_edit && (
8283
<IconButton
8384
codicon_icon="edit"
8485
title="Edit description"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useState } from 'react'
2+
import { Button } from '../../../common/Button'
3+
import { Modal } from '../Modal'
4+
import { Textarea } from '../../../common/Textarea'
5+
6+
type Props = {
7+
description: string
8+
on_save: (description: string) => void
9+
on_cancel: () => void
10+
}
11+
12+
export const EditCheckpointDescriptionModal: React.FC<Props> = (props) => {
13+
const [description, set_description] = useState(props.description)
14+
15+
return (
16+
<Modal
17+
title="Edit Description"
18+
content_slot={
19+
<Textarea
20+
value={description}
21+
on_change={set_description}
22+
min_rows={2}
23+
autofocus
24+
on_key_down={(e) => {
25+
if (e.key == 'Enter' && (e.metaKey || e.ctrlKey)) {
26+
e.preventDefault()
27+
props.on_save(description)
28+
}
29+
}}
30+
/>
31+
}
32+
footer_slot={
33+
<>
34+
<Button on_click={props.on_cancel} is_secondary={true}>
35+
Cancel
36+
</Button>
37+
<Button on_click={() => props.on_save(description)}>Save</Button>
38+
</>
39+
}
40+
/>
41+
)
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './EditCheckpointDescriptionModal'

‎packages/vscode/src/commands/checkpoints-command/actions/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './get-checkpoints'
66
export * from './restore-checkpoint'
77
export * from './sync-workspace-from-dir'
88
export * from './toggle-checkpoint-star'
9+
export * from './update-checkpoint-description'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as vscode from 'vscode'
2+
import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
3+
import type { Checkpoint } from '../types'
4+
import { PanelProvider } from '@/views/panel/backend/panel-provider'
5+
6+
export const update_checkpoint_description = async (params: {
7+
context: vscode.ExtensionContext
8+
timestamp: number
9+
description: string
10+
panel_provider: PanelProvider
11+
}) => {
12+
const checkpoints =
13+
params.context.workspaceState.get<Checkpoint[]>(
14+
CHECKPOINTS_STATE_KEY,
15+
[]
16+
) ?? []
17+
const checkpoint_to_update = checkpoints.find(
18+
(c) => c.timestamp == params.timestamp
19+
)
20+
if (checkpoint_to_update) {
21+
checkpoint_to_update.description = params.description
22+
await params.context.workspaceState.update(
23+
CHECKPOINTS_STATE_KEY,
24+
checkpoints
25+
)
26+
await params.panel_provider.send_checkpoints()
27+
}
28+
}

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { EditFormat } from '@shared/types/edit-format'
1111
import {
1212
get_checkpoints,
1313
toggle_checkpoint_star,
14-
restore_checkpoint
14+
restore_checkpoint,
15+
update_checkpoint_description
1516
} from '@/commands/checkpoints-command/actions'
1617
import {
1718
handle_copy_prompt,
@@ -449,6 +450,13 @@ export class PanelProvider implements vscode.WebviewViewProvider {
449450
}
450451
})
451452
}
453+
} else if (message.command == 'UPDATE_CHECKPOINT_DESCRIPTION') {
454+
await update_checkpoint_description({
455+
context: this.context,
456+
timestamp: message.timestamp,
457+
description: message.description,
458+
panel_provider: this
459+
})
452460
} else if (message.command == 'GET_HISTORY') {
453461
handle_get_history(this)
454462
} else if (message.command == 'GET_RESPONSE_HISTORY') {

‎packages/vscode/src/views/panel/frontend/Home/Home.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Props = {
2727
selected_history_item_created_at?: number
2828
on_selected_history_item_change: (created_at: number) => void
2929
on_response_history_item_remove: (created_at: number) => void
30+
on_edit_checkpoint_description: (timestamp: number) => void
3031
}
3132

3233
export const Home: React.FC<Props> = (props) => {
@@ -127,12 +128,14 @@ export const Home: React.FC<Props> = (props) => {
127128
label: c.title,
128129
timestamp: c.timestamp,
129130
description: c.description,
130-
is_starred: c.is_starred
131+
is_starred: c.is_starred,
132+
can_edit: c.title == 'Created by user'
131133
}))}
132134
on_toggle_starred={(id) =>
133135
props.on_toggle_checkpoint_starred(id)
134136
}
135137
on_item_click={(id) => props.on_restore_checkpoint(id)}
138+
on_edit={props.on_edit_checkpoint_description}
136139
/>
137140
)}
138141
</div>

‎packages/vscode/src/views/panel/frontend/Panel.tsx‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useState } from 'react'
12
import { Main } from './Main'
23
import { Page as UiPage } from '@ui/components/editor/panel/Page'
34
import { EditPresetForm } from '@/views/panel/frontend/components/edit-preset-form/EditPresetForm'
@@ -12,6 +13,7 @@ import { ProgressModal as UiProgressModal } from '@ui/components/editor/panel/mo
1213
import { AutoClosingModal as UiAutoClosingModal } from '@ui/components/editor/panel/modals/AutoClosingModal'
1314
import { CommitMessageModal as UiCommitMessageModal } from '@ui/components/editor/panel/modals/CommitMessageModal'
1415
import { StageFilesModal as UiStageFilesModal } from '@ui/components/editor/panel/modals/StageFilesModal'
16+
import { EditCheckpointDescriptionModal as UiEditCheckpointDescriptionModal } from '@ui/components/editor/panel/modals/EditCheckpointDescriptionModal'
1517
import { use_panel } from './hooks/use-panel'
1618
import { FileInPreview } from '@shared/types/file-in-preview'
1719
import { LayoutContext } from './contexts/LayoutContext'
@@ -88,6 +90,10 @@ export const Panel = () => {
8890
handle_remove_response_history_item
8991
} = use_panel(vscode)
9092

93+
const [checkpoint_to_edit, set_checkpoint_to_edit] = useState<
94+
{ timestamp: number; description: string } | undefined
95+
>(undefined)
96+
9197
const { viewing_donations, set_viewing_donations, ...donations_state } =
9298
use_latest_donations()
9399

@@ -300,6 +306,17 @@ export const Panel = () => {
300306
timestamp
301307
})
302308
}}
309+
on_edit_checkpoint_description={(timestamp) => {
310+
const checkpoint = checkpoints.find(
311+
(c) => c.timestamp == timestamp
312+
)
313+
if (checkpoint) {
314+
set_checkpoint_to_edit({
315+
timestamp: checkpoint.timestamp,
316+
description: checkpoint.description || ''
317+
})
318+
}
319+
}}
303320
/>
304321
</div>
305322
</Layout>
@@ -501,6 +518,23 @@ export const Panel = () => {
501518
</div>
502519
)}
503520

521+
{checkpoint_to_edit && (
522+
<div className={styles.slot}>
523+
<UiEditCheckpointDescriptionModal
524+
description={checkpoint_to_edit.description}
525+
on_save={(description) => {
526+
post_message(vscode, {
527+
command: 'UPDATE_CHECKPOINT_DESCRIPTION',
528+
timestamp: checkpoint_to_edit.timestamp,
529+
description
530+
})
531+
set_checkpoint_to_edit(undefined)
532+
}}
533+
on_cancel={() => set_checkpoint_to_edit(undefined)}
534+
/>
535+
</div>
536+
)}
537+
504538
{files_to_stage && (
505539
<div className={styles.slot}>
506540
<UiStageFilesModal

‎packages/vscode/src/views/panel/types/messages.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ export interface RemoveResponseHistoryItemMessage extends BaseMessage {
384384
created_at: number
385385
}
386386

387+
export interface UpdateCheckpointDescriptionMessage extends BaseMessage {
388+
command: 'UPDATE_CHECKPOINT_DESCRIPTION'
389+
timestamp: number
390+
description: string
391+
}
392+
387393
export type FrontendMessage =
388394
| GetInstructionsMessage
389395
| SaveInstructionsMessage
@@ -454,6 +460,7 @@ export type FrontendMessage =
454460
| ToggleCheckpointStarMessage
455461
| RestoreCheckpointMessage
456462
| RemoveResponseHistoryItemMessage
463+
| UpdateCheckpointDescriptionMessage
457464

458465
// === FROM BACKEND TO FRONTEND ===
459466
export interface InstructionsMessage extends BaseMessage {

0 commit comments

Comments
 (0)