Skip to content

Commit c754173

Browse files
committed
sync changes for persona selector
1 parent c57a1c8 commit c754173

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

ai/ai-react-app/src/components/Layout/LeftSidebar.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ const LeftSidebar: React.FC<LeftSidebarProps> = ({
3131
const [selectedPersonaId, setSelectedPersonaId] = useState<string>("default");
3232
const [customPersona, setCustomPersona] = useState<string>("");
3333

34+
// Memoize the calculation of the new instruction text based on UI state.
3435
const newInstructionText = React.useMemo(() => {
3536
const selected = PREDEFINED_PERSONAS.find(
3637
(p) => p.id === selectedPersonaId,
3738
);
38-
// Should not happen with the dropdown, but good to have a fallback.
39-
if (!selected) return "";
39+
if (!selected) return ""; // Should not happen, but a safe fallback.
4040

4141
return selected.id === "custom"
4242
? customPersona
4343
: selected.systemInstruction;
4444
}, [selectedPersonaId, customPersona]);
4545

46-
// Effect to update systemInstruction when persona changes
46+
// Effect to sync UI changes (from dropdown or textarea) UP to the parent state.
4747
useEffect(() => {
4848
setGenerativeParams((prevParams) => {
4949
const currentInstructionText =
@@ -65,6 +65,34 @@ const LeftSidebar: React.FC<LeftSidebarProps> = ({
6565
});
6666
}, [newInstructionText, setGenerativeParams]);
6767

68+
// Effect to sync parent state changes DOWN to the local UI state.
69+
// This ensures the UI reflects the state if it's changed elsewhere.
70+
useEffect(() => {
71+
const instructionText =
72+
generativeParams.systemInstruction?.parts?.[0]?.text ?? "";
73+
74+
const matchingPersona = PREDEFINED_PERSONAS.find(
75+
(p) => p.id !== "custom" && p.systemInstruction === instructionText,
76+
);
77+
78+
if (matchingPersona) {
79+
// A predefined persona matches the current instruction.
80+
setSelectedPersonaId(matchingPersona.id);
81+
setCustomPersona("");
82+
} else {
83+
// No predefined persona matches. It's either custom or the default empty state.
84+
if (instructionText) {
85+
// It's a custom persona.
86+
setSelectedPersonaId("custom");
87+
setCustomPersona(instructionText);
88+
} else {
89+
// It's the default empty state.
90+
setSelectedPersonaId("default");
91+
setCustomPersona("");
92+
}
93+
}
94+
}, [generativeParams.systemInstruction]);
95+
6896
// Define the available modes and their display names
6997
const modes: { id: AppMode; label: string }[] = [
7098
{ id: "chat", label: "Chat" },
@@ -104,7 +132,7 @@ const LeftSidebar: React.FC<LeftSidebarProps> = ({
104132

105133
{/* Backend Selection */}
106134
<div className={styles.backendSelector}>
107-
<h6 className={styles.selectorTitle}>Backend API</h6>
135+
<h5 className={styles.selectorTitle}>Backend API</h5>
108136
<div className={styles.radioGroup}>
109137
<label>
110138
<input
@@ -134,7 +162,7 @@ const LeftSidebar: React.FC<LeftSidebarProps> = ({
134162
{/* Persona Selector */}
135163
{activeMode === "chat" && (
136164
<div className={styles.personaSelector}>
137-
<h6 className={styles.selectorTitle}>Persona</h6>
165+
<h5 className={styles.selectorTitle}>Persona</h5>
138166
<select
139167
value={selectedPersonaId}
140168
onChange={handlePersonaChange}

0 commit comments

Comments
 (0)