Skip to content

Commit 09770e4

Browse files
committed
Improve team selection and initialization logic
Enhanced the handleTeamSelect function to better handle team initialization, loading, and fallback selection. Added error handling, loading state management, and more robust logic for selecting and storing the initialized team.
1 parent c1c2f7f commit 09770e4

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/frontend/src/pages/HomePage.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,54 @@ const HomePage: React.FC = () => {
132132
const handleTeamSelect = useCallback(async (team: TeamConfig | null) => {
133133
setSelectedTeam(team);
134134
if (team) {
135-
const initResponse = await TeamService.initializeTeam();
135+
136+
try {
137+
// TODO REFRACTOR THIS CODE
138+
setIsLoadingTeam(true);
139+
const initResponse = await TeamService.initializeTeam();
140+
if (initResponse.data?.status === 'Request started successfully' && initResponse.data?.team_id) {
141+
console.log('Team initialization completed:', initResponse.data?.team_id);
142+
143+
// Now fetch the actual team details using the team_id
144+
const teams = await TeamService.getUserTeams();
145+
const initializedTeam = teams.find(team => team.team_id === initResponse.data?.team_id);
146+
147+
if (initializedTeam) {
148+
setSelectedTeam(initializedTeam);
149+
TeamService.storageTeam(initializedTeam);
150+
151+
console.log('Team loaded successfully:', initializedTeam.name);
152+
console.log('Team agents:', initializedTeam.agents?.length || 0);
153+
154+
showToast(
155+
`${initializedTeam.name} team initialized successfully with ${initializedTeam.agents?.length || 0} agents`,
156+
"success"
157+
);
158+
} else {
159+
// Fallback: if we can't find the specific team, use HR team or first available
160+
console.log('Specific team not found, using default selection logic');
161+
const hrTeam = teams.find(team => team.name === "Human Resources Team");
162+
const defaultTeam = hrTeam || teams[0];
163+
164+
if (defaultTeam) {
165+
setSelectedTeam(defaultTeam);
166+
TeamService.storageTeam(defaultTeam);
167+
showToast(
168+
`${defaultTeam.name} team loaded as default`,
169+
"success"
170+
);
171+
}
172+
}
173+
174+
} else {
175+
throw new Error('Invalid response from init_team endpoint');
176+
}
177+
} catch (error) {
178+
console.error('Error setting current team:', error);
179+
} finally {
180+
setIsLoadingTeam(false);
181+
}
182+
136183

137184
showToast(
138185
`${team.name} team has been selected with ${team.agents.length} agents`,

0 commit comments

Comments
 (0)