Skip to content

Commit 5219490

Browse files
fix: Enhance TeamSelector and HomePage with team upload persistence
2 parents 1ef598e + fc938cd commit 5219490

3 files changed

Lines changed: 64 additions & 14 deletions

File tree

src/App/src/components/common/TeamSelector.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import styles from '../../styles/TeamSelector.module.css';
3939

4040
interface TeamSelectorProps {
4141
onTeamSelect?: (team: TeamConfig | null) => void;
42-
onTeamUpload?: () => Promise<void>;
42+
onTeamUpload?: (team?: TeamConfig) => Promise<void>;
4343
selectedTeam?: TeamConfig | null;
4444
isHomePage: boolean;
4545
}
@@ -280,7 +280,7 @@ const TeamSelector: React.FC<TeamSelectorProps> = ({
280280
}
281281

282282
if (onTeamUpload) {
283-
await onTeamUpload();
283+
await onTeamUpload(result.team);
284284
}
285285
} else if (result.raiError) {
286286
setError('❌ Content Safety Check Failed\n\nYour team configuration contains content that doesn\'t meet our safety guidelines.');
@@ -382,7 +382,7 @@ const TeamSelector: React.FC<TeamSelectorProps> = ({
382382
}
383383

384384
if (onTeamUpload) {
385-
await onTeamUpload();
385+
await onTeamUpload(result.team);
386386
}
387387
} else if (result.raiError) {
388388
setError(' Content Safety Check Failed\n\nYour team configuration contains content that doesn\'t meet our safety guidelines.');

src/App/src/pages/HomePage.tsx

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,30 @@ const HomePage: React.FC = () => {
3535
const initTeam = async () => {
3636
dispatch(setIsLoadingTeam(true));
3737
try {
38+
// Get available teams first
39+
const teams = await TeamService.getUserTeams();
40+
41+
// Check if we have a stored team and if it still exists in the backend
42+
const storedTeam = TeamService.getStoredTeam();
43+
if (storedTeam) {
44+
const backendTeam = teams.find(t => t.team_id === storedTeam.team_id);
45+
if (backendTeam) {
46+
// Stored team still exists, use backend-fresh object
47+
dispatch(setSelectedTeam(backendTeam));
48+
showToast(`${backendTeam.name} team restored from storage`, 'success');
49+
dispatch(setIsLoadingTeam(false));
50+
return;
51+
} else {
52+
// Stored team was deleted, clear localStorage
53+
console.warn(`Stored team ${storedTeam.team_id} no longer exists, clearing storage`);
54+
TeamService.clearStoredTeam();
55+
}
56+
}
57+
58+
// Now initialize team from backend
3859
const initResponse = await TeamService.initializeTeam();
3960

4061
if (initResponse.data?.status === 'Request started successfully' && initResponse.data?.team_id) {
41-
const teams = await TeamService.getUserTeams();
4262
const initializedTeam = teams.find(team => team.team_id === initResponse.data?.team_id);
4363

4464
if (initializedTeam) {
@@ -58,7 +78,6 @@ const HomePage: React.FC = () => {
5878
dispatch(setSelectedTeam(null));
5979
showToast('Welcome! Please upload a team configuration file to get started.', 'info');
6080
} else if (!initResponse.success) {
61-
// API call failed — surface the error
6281
console.error('Team init failed:', initResponse.error);
6382
showToast(initResponse.error || 'Team initialization failed. Please try again.', 'warning');
6483
}
@@ -82,6 +101,12 @@ const HomePage: React.FC = () => {
82101
async (team: TeamConfig | null) => {
83102
dispatch(setSelectedTeam(team));
84103
dispatch(setReloadLeftList(true));
104+
105+
// Immediately save selected team to localStorage so it persists on reload
106+
if (team) {
107+
TeamService.storageTeam(team);
108+
}
109+
85110
if (team) {
86111
try {
87112
dispatch(setIsLoadingTeam(true));
@@ -118,17 +143,31 @@ const HomePage: React.FC = () => {
118143
[dispatch, showToast],
119144
);
120145

121-
const handleTeamUpload = useCallback(async () => {
146+
const handleTeamUpload = useCallback(async (uploadedTeam?: TeamConfig) => {
122147
try {
123-
const teams = await TeamService.getUserTeams();
124-
if (teams.length > 0) {
125-
const hrTeam = teams.find(team => team.name === 'Human Resources Team');
126-
const defaultTeam = hrTeam || teams[0];
127-
dispatch(setSelectedTeam(defaultTeam));
128-
showToast(`Team uploaded successfully! ${defaultTeam.name} remains your default team.`, 'success');
148+
console.log('handleTeamUpload called with:', uploadedTeam);
149+
if (uploadedTeam) {
150+
const teamName = uploadedTeam.name || 'Uploaded Team';
151+
dispatch(setSelectedTeam(uploadedTeam));
152+
TeamService.storageTeam(uploadedTeam);
153+
showToast(`Default team set to ${teamName}`, 'success');
154+
155+
// Also inform backend to use this team for the session
156+
if (uploadedTeam.team_id) {
157+
try {
158+
await TeamService.selectTeam(uploadedTeam.team_id);
159+
console.log('Team selected in backend:', uploadedTeam.team_id);
160+
} catch (selectError) {
161+
console.warn('Failed to select team in backend:', selectError);
162+
// Don't fail the upload if backend selection fails
163+
}
164+
}
165+
} else {
166+
console.warn('No uploaded team provided to handleTeamUpload');
129167
}
130-
} catch {
131-
console.error('Team upload failed');
168+
} catch (error) {
169+
console.error('Team upload failed:', error);
170+
showToast('Team upload failed. Please try again.', 'warning');
132171
}
133172
}, [dispatch, showToast]);
134173

src/App/src/store/TeamService.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ export class TeamService {
7373
}
7474
}
7575

76+
static clearStoredTeam(): boolean {
77+
// Remove persisted team from localStorage
78+
if (typeof window === 'undefined' || !window.localStorage) return false;
79+
try {
80+
window.localStorage.removeItem(TeamService.STORAGE_KEY);
81+
return true;
82+
} catch {
83+
return false;
84+
}
85+
}
86+
7687
static async uploadCustomTeam(teamFile: File): Promise<{
7788
modelError?: any; success: boolean; team?: TeamConfig; error?: string; raiError?: any; searchError?: any
7889
}> {

0 commit comments

Comments
 (0)