Skip to content

Commit af0cd5b

Browse files
committed
Don't change the actual mode when you view different mode configurations on the prompts tab
1 parent 5596614 commit af0cd5b

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

webview-ui/src/components/prompts/PromptsView.tsx

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
6565
const [isToolsEditMode, setIsToolsEditMode] = useState(false)
6666
const [isCreateModeDialogOpen, setIsCreateModeDialogOpen] = useState(false)
6767
const [activeSupportTab, setActiveSupportTab] = useState<SupportPromptType>("ENHANCE")
68+
const [selectedModeTab, setSelectedModeTab] = useState<string>(mode)
6869

6970
// Direct update functions
7071
const updateAgentPrompt = useCallback(
@@ -110,26 +111,23 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
110111
text: slug,
111112
})
112113
}, [])
113-
114-
// Handle mode switching with explicit state initialization
114+
// Handle mode tab selection without actually switching modes
115115
const handleModeSwitch = useCallback(
116116
(modeConfig: ModeConfig) => {
117-
if (modeConfig.slug === mode) return // Prevent unnecessary updates
118-
119-
// First switch the mode
120-
switchMode(modeConfig.slug)
117+
if (modeConfig.slug === selectedModeTab) return // Prevent unnecessary updates
121118

122-
// Exit tools edit mode when switching modes
119+
// Update selected tab and reset tools edit mode
120+
setSelectedModeTab(modeConfig.slug)
123121
setIsToolsEditMode(false)
124122
},
125-
[mode, switchMode, setIsToolsEditMode],
123+
[selectedModeTab, setIsToolsEditMode],
126124
)
127125

128126
// Helper function to get current mode's config
129127
const getCurrentMode = useCallback((): ModeConfig | undefined => {
130-
const findMode = (m: ModeConfig): boolean => m.slug === mode
128+
const findMode = (m: ModeConfig): boolean => m.slug === selectedModeTab
131129
return customModes?.find(findMode) || modes.find(findMode)
132-
}, [mode, customModes, modes])
130+
}, [selectedModeTab, customModes, modes])
133131

134132
// Helper function to safely access mode properties
135133
const getModeProperty = <T extends keyof ModeConfig>(
@@ -155,6 +153,11 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
155153
}
156154
}, [isCreateModeDialogOpen])
157155

156+
// Keep selected tab in sync with actual mode
157+
useEffect(() => {
158+
setSelectedModeTab(mode)
159+
}, [mode])
160+
158161
// Helper function to generate a unique slug from a name
159162
const generateSlug = useCallback((name: string, attempt = 0): string => {
160163
const baseSlug = name
@@ -184,7 +187,6 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
184187
groups: newModeGroups,
185188
}
186189
updateCustomMode(newModeSlug, newMode)
187-
switchMode(newModeSlug)
188190
setIsCreateModeDialogOpen(false)
189191
setNewModeName("")
190192
setNewModeSlug("")
@@ -479,7 +481,7 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
479481
padding: "4px 0",
480482
}}>
481483
{modes.map((modeConfig) => {
482-
const isActive = mode === modeConfig.slug
484+
const isActive = selectedModeTab === modeConfig.slug
483485
return (
484486
<button
485487
key={modeConfig.slug}
@@ -507,20 +509,22 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
507509

508510
<div style={{ marginBottom: "20px" }}>
509511
{/* Only show name and delete for custom modes */}
510-
{mode && findModeBySlug(mode, customModes) && (
512+
{selectedModeTab && findModeBySlug(selectedModeTab, customModes) && (
511513
<div style={{ display: "flex", gap: "12px", marginBottom: "16px" }}>
512514
<div style={{ flex: 1 }}>
513515
<div style={{ fontWeight: "bold", marginBottom: "4px" }}>Name</div>
514516
<div style={{ display: "flex", gap: "8px" }}>
515517
<VSCodeTextField
516-
value={getModeProperty(findModeBySlug(mode, customModes), "name") ?? ""}
518+
value={
519+
getModeProperty(findModeBySlug(selectedModeTab, customModes), "name") ?? ""
520+
}
517521
onChange={(e: Event | React.FormEvent<HTMLElement>) => {
518522
const target =
519523
(e as CustomEvent)?.detail?.target ||
520524
((e as any).target as HTMLInputElement)
521-
const customMode = findModeBySlug(mode, customModes)
525+
const customMode = findModeBySlug(selectedModeTab, customModes)
522526
if (customMode) {
523-
updateCustomMode(mode, {
527+
updateCustomMode(selectedModeTab, {
524528
...customMode,
525529
name: target.value,
526530
})
@@ -534,7 +538,7 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
534538
onClick={() => {
535539
vscode.postMessage({
536540
type: "deleteCustomMode",
537-
slug: mode,
541+
slug: selectedModeTab,
538542
})
539543
}}>
540544
<span className="codicon codicon-trash"></span>
@@ -552,7 +556,7 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
552556
marginBottom: "4px",
553557
}}>
554558
<div style={{ fontWeight: "bold" }}>Role Definition</div>
555-
{!findModeBySlug(mode, customModes) && (
559+
{!findModeBySlug(selectedModeTab, customModes) && (
556560
<VSCodeButton
557561
appearance="icon"
558562
onClick={() => {
@@ -578,24 +582,28 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
578582
</div>
579583
<VSCodeTextArea
580584
value={(() => {
581-
const customMode = findModeBySlug(mode, customModes)
582-
const prompt = customModePrompts?.[mode] as PromptComponent
583-
return customMode?.roleDefinition ?? prompt?.roleDefinition ?? getRoleDefinition(mode)
585+
const customMode = findModeBySlug(selectedModeTab, customModes)
586+
const prompt = customModePrompts?.[selectedModeTab] as PromptComponent
587+
return (
588+
customMode?.roleDefinition ??
589+
prompt?.roleDefinition ??
590+
getRoleDefinition(selectedModeTab)
591+
)
584592
})()}
585593
onChange={(e) => {
586594
const value =
587595
(e as CustomEvent)?.detail?.target?.value ||
588596
((e as any).target as HTMLTextAreaElement).value
589-
const customMode = findModeBySlug(mode, customModes)
597+
const customMode = findModeBySlug(selectedModeTab, customModes)
590598
if (customMode) {
591599
// For custom modes, update the JSON file
592-
updateCustomMode(mode, {
600+
updateCustomMode(selectedModeTab, {
593601
...customMode,
594602
roleDefinition: value.trim() || "",
595603
})
596604
} else {
597605
// For built-in modes, update the prompts
598-
updateAgentPrompt(mode, {
606+
updateAgentPrompt(selectedModeTab, {
599607
roleDefinition: value.trim() || undefined,
600608
})
601609
}
@@ -760,25 +768,25 @@ const PromptsView = ({ onDone }: PromptsViewProps) => {
760768
</div>
761769
<VSCodeTextArea
762770
value={(() => {
763-
const customMode = findModeBySlug(mode, customModes)
764-
const prompt = customModePrompts?.[mode] as PromptComponent
771+
const customMode = findModeBySlug(selectedModeTab, customModes)
772+
const prompt = customModePrompts?.[selectedModeTab] as PromptComponent
765773
return customMode?.customInstructions ?? prompt?.customInstructions ?? ""
766774
})()}
767775
onChange={(e) => {
768776
const value =
769777
(e as CustomEvent)?.detail?.target?.value ||
770778
((e as any).target as HTMLTextAreaElement).value
771-
const customMode = findModeBySlug(mode, customModes)
779+
const customMode = findModeBySlug(selectedModeTab, customModes)
772780
if (customMode) {
773781
// For custom modes, update the JSON file
774-
updateCustomMode(mode, {
782+
updateCustomMode(selectedModeTab, {
775783
...customMode,
776784
customInstructions: value.trim() || undefined,
777785
})
778786
} else {
779787
// For built-in modes, update the prompts
780-
const existingPrompt = customModePrompts?.[mode] as PromptComponent
781-
updateAgentPrompt(mode, {
788+
const existingPrompt = customModePrompts?.[selectedModeTab] as PromptComponent
789+
updateAgentPrompt(selectedModeTab, {
782790
...existingPrompt,
783791
customInstructions: value.trim() || undefined,
784792
})

0 commit comments

Comments
 (0)