Skip to content

Commit 450d73e

Browse files
committed
Replace separate preset, group, and separator creation with a unified quick-pick dialog for item type selection.
1 parent d1ab374 commit 450d73e

10 files changed

Lines changed: 190 additions & 462 deletions

File tree

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

Lines changed: 16 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ export const chatbot_to_icon: Record<keyof typeof CHATBOTS, Icon.Variant> = {
3232
'Z.AI': 'Z_AI'
3333
}
3434

35-
const TRAILING_GROUP_ID = '__trailing_group_drag_handle__'
36-
const TRAILING_SEPARATOR_ID = '__trailing_separator_drag_handle__'
37-
3835
export namespace Presets {
3936
export type Preset = {
4037
name?: string
@@ -56,14 +53,9 @@ export namespace Presets {
5653
presets: Preset[]
5754
on_preset_click: (preset_name: string, without_submission?: boolean) => void
5855
on_group_click: (group_name: string, without_submission?: boolean) => void
59-
on_create_preset: (options?: { add_on_top?: boolean }) => void
60-
on_create_group: (options?: {
61-
add_on_top?: boolean
62-
instant?: boolean
63-
create_on_index?: number
64-
move_preset_with_name_after?: string
56+
on_create_preset_group_or_separator: (options?: {
57+
placement?: 'top' | 'bottom'
6558
}) => void
66-
on_create_separator: (options?: { create_on_index?: number }) => void
6759
on_preset_copy: (name: string) => void
6860
on_presets_reorder: (reordered_presets: Preset[]) => void
6961
on_preset_edit: (name: string) => void
@@ -133,27 +125,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
133125
return false
134126
}
135127

136-
const sortable_list = (() => {
137-
let list: (Presets.Preset & { id: string; original_index?: number })[] =
138-
with_ids(get_visible_presets(props.presets))
139-
const last_preset = props.presets[props.presets.length - 1]
140-
// A group is a preset without a `chatbot` property
141-
const is_last_item_a_group =
142-
last_preset && last_preset.name && !last_preset.chatbot
143-
if (!is_last_item_a_group) {
144-
const trailing_group_item: Presets.Preset & { id: string } = {
145-
name: 'Group',
146-
id: TRAILING_GROUP_ID
147-
}
148-
list = [...list, trailing_group_item]
149-
}
150-
const trailing_separator_item: Presets.Preset & { id: string } = {
151-
id: TRAILING_SEPARATOR_ID
152-
}
153-
list = [...list, trailing_separator_item]
154-
155-
return list
156-
})()
128+
const sortable_list = with_ids(get_visible_presets(props.presets))
157129

158130
return (
159131
<div className={styles.container}>
@@ -281,9 +253,9 @@ export const Presets: React.FC<Presets.Props> = (props) => {
281253
codicon_icon="add"
282254
on_click={(e) => {
283255
e.stopPropagation()
284-
props.on_create_preset({ add_on_top: true })
256+
props.on_create_preset_group_or_separator({ placement: 'top' })
285257
}}
286-
title="Add New Preset"
258+
title="Create New..."
287259
/>
288260
}
289261
/>
@@ -293,144 +265,10 @@ export const Presets: React.FC<Presets.Props> = (props) => {
293265
<ReactSortable
294266
list={sortable_list}
295267
setList={(new_state) => {
296-
// Handle trailing separator item drag
297-
const trailing_separator_item = new_state.find(
298-
(item) => item.id == TRAILING_SEPARATOR_ID
299-
)
300-
const trailing_separator_was_moved =
301-
trailing_separator_item &&
302-
sortable_list.findIndex(
303-
(item) => item.id == TRAILING_SEPARATOR_ID
304-
) !=
305-
new_state.findIndex(
306-
(item) => item.id == TRAILING_SEPARATOR_ID
307-
)
308-
309-
if (trailing_separator_item && trailing_separator_was_moved) {
310-
const trailing_separator_item_index = new_state.findIndex(
311-
(item) => item.id == TRAILING_SEPARATOR_ID
312-
)
313-
const new_state_without_trailing_separator = new_state.filter(
314-
(item) => item.id != TRAILING_SEPARATOR_ID
315-
)
316-
317-
// Determine where to insert the new separator
318-
let target_full_index: number
319-
320-
if (
321-
trailing_separator_item_index <
322-
new_state_without_trailing_separator.length
323-
) {
324-
// Dropped between items - find the preset at or after this position
325-
const preset_at_drop_location =
326-
new_state_without_trailing_separator[
327-
trailing_separator_item_index
328-
]
329-
330-
if (preset_at_drop_location) {
331-
target_full_index = props.presets.findIndex(
332-
(p) => p.name == preset_at_drop_location.name
333-
)
334-
335-
// Fallback if preset not found
336-
if (target_full_index == -1) {
337-
target_full_index = props.presets.length
338-
}
339-
} else {
340-
target_full_index = props.presets.length
341-
}
342-
} else {
343-
// Dropped at the end
344-
target_full_index = props.presets.length
345-
}
346-
347-
props.on_create_separator({
348-
create_on_index: target_full_index
349-
})
350-
return
351-
}
352-
353-
// Handle trailing group item drag
354-
const trailing_group_item = new_state.find(
355-
(item) => item.id == TRAILING_GROUP_ID
356-
)
357-
const trailing_group_was_moved =
358-
trailing_group_item &&
359-
sortable_list.findIndex(
360-
(item) => item.id == TRAILING_GROUP_ID
361-
) !==
362-
new_state.findIndex((item) => item.id == TRAILING_GROUP_ID)
363-
364-
if (trailing_group_item && trailing_group_was_moved) {
365-
const trailing_group_item_index = new_state.findIndex(
366-
(item) => item.id == TRAILING_GROUP_ID
367-
)
368-
const item_after_tg = new_state[trailing_group_item_index + 1]
369-
const item_after_that =
370-
new_state[trailing_group_item_index + 2]
371-
372-
// A preset was dragged to the end to create a new group
373-
if (
374-
item_after_tg?.chatbot &&
375-
item_after_that?.id == TRAILING_SEPARATOR_ID
376-
) {
377-
props.on_create_group({
378-
instant: true,
379-
move_preset_with_name_after: item_after_tg.name
380-
})
381-
return
382-
}
383-
384-
const new_state_without_trailing_group = new_state.filter(
385-
(item) => item.id !== TRAILING_GROUP_ID
386-
)
387-
388-
// Determine where to insert the new group
389-
let target_full_index: number
390-
391-
if (
392-
trailing_group_item_index <
393-
new_state_without_trailing_group.length
394-
) {
395-
// Dropped between items - find the preset at or after this position
396-
const preset_at_drop_location =
397-
new_state_without_trailing_group[
398-
trailing_group_item_index
399-
]
400-
401-
if (preset_at_drop_location) {
402-
target_full_index = props.presets.findIndex(
403-
(p) => p.name == preset_at_drop_location.name
404-
)
405-
406-
// Fallback if preset not found
407-
if (target_full_index == -1) {
408-
target_full_index = props.presets.length
409-
}
410-
} else {
411-
target_full_index = props.presets.length
412-
}
413-
} else {
414-
// Dropped at the end
415-
target_full_index = props.presets.length
416-
}
417-
418-
// Create group at calculated position
419-
props.on_create_group({
420-
create_on_index: target_full_index,
421-
instant: true
422-
})
423-
return
424-
}
425-
426268
// Handle normal preset/group reordering
427-
const new_visible_presets = new_state
428-
.filter(
429-
(item) =>
430-
item.id !== TRAILING_GROUP_ID &&
431-
item.id !== TRAILING_SEPARATOR_ID
432-
)
433-
.map(({ id, ...preset }) => preset) as Presets.Preset[]
269+
const new_visible_presets = new_state.map(
270+
({ id, ...preset }) => preset
271+
) as Presets.Preset[]
434272

435273
const reordered_presets: Presets.Preset[] = []
436274

@@ -468,26 +306,6 @@ export const Presets: React.FC<Presets.Props> = (props) => {
468306
animation={150}
469307
>
470308
{sortable_list.map((preset) => {
471-
if (preset.id == TRAILING_SEPARATOR_ID) {
472-
return (
473-
<div
474-
key={TRAILING_SEPARATOR_ID}
475-
className={cn(
476-
styles.presets__item,
477-
styles['presets__item--separator']
478-
)}
479-
>
480-
<div className={styles.presets__item__left}>
481-
<div
482-
className={styles.presets__item__left__drag_handle}
483-
>
484-
<span className="codicon codicon-gripper" />
485-
</div>
486-
</div>
487-
</div>
488-
)
489-
}
490-
491309
if (!preset.chatbot && !preset.name) {
492310
return (
493311
<div
@@ -529,40 +347,6 @@ export const Presets: React.FC<Presets.Props> = (props) => {
529347
)
530348
}
531349

532-
if (preset.id == TRAILING_GROUP_ID) {
533-
return (
534-
<div
535-
key={TRAILING_GROUP_ID}
536-
className={cn(
537-
styles.presets__item,
538-
styles['presets__item--ungrouped']
539-
)}
540-
>
541-
<div className={styles.presets__item__left}>
542-
<div
543-
className={styles.presets__item__left__drag_handle}
544-
>
545-
<span className="codicon codicon-gripper" />
546-
</div>
547-
<div
548-
className={
549-
styles['presets__item__left__collapse-icon']
550-
}
551-
>
552-
<span
553-
className={cn('codicon', {
554-
'codicon-dash': true
555-
})}
556-
/>
557-
</div>
558-
<div className={styles.presets__item__left__text}>
559-
Group
560-
</div>
561-
</div>
562-
</div>
563-
)
564-
}
565-
566350
const is_unnamed =
567351
!preset.name || /^\(\d+\)$/.test(preset.name.trim())
568352
let display_name: string
@@ -782,8 +566,14 @@ export const Presets: React.FC<Presets.Props> = (props) => {
782566
</ReactSortable>
783567
</div>
784568
<div className={styles.footer}>
785-
<Button on_click={() => props.on_create_preset()}>
786-
Add New Preset
569+
<Button
570+
on_click={() =>
571+
props.on_create_preset_group_or_separator({
572+
placement: 'bottom'
573+
})
574+
}
575+
>
576+
Create New...
787577
</Button>
788578
</div>
789579
</>

‎packages/vscode/src/views/panel/backend/message-handlers/handle-create-group.ts‎

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

0 commit comments

Comments
 (0)