@@ -5,16 +5,20 @@ import React, {
55 useMemo ,
66 useState ,
77 useEffect ,
8+ useRef ,
89} from 'react' ; // Import useMemo
910import useSWR from 'swr' ;
1011
1112import * as chatAPI from './transformerlab-api-sdk.ts' ; // Adjust the import path as necessary
1213import { fetcher } from './transformerlab-api-sdk.ts' ;
14+ import { useAuth } from './authContext' ;
1315
1416const ExperimentInfoContext = createContext ( undefined ) ;
1517
1618export function ExperimentInfoProvider ( { connection, children } ) {
1719 const [ experimentId , setExperimentId ] = useState ( null ) ;
20+ const authContext = useAuth ( ) ;
21+ const lastAutoSelectedTeamId = useRef ( null ) ;
1822
1923 // Load experimentId from storage or default
2024 useEffect ( ( ) => {
@@ -56,6 +60,72 @@ export function ExperimentInfoProvider({ connection, children }) {
5660 experimentId ? chatAPI . Endpoints . Experiment . Get ( experimentId ) : null ,
5761 fetcher ,
5862 ) ;
63+
64+ // Fetch all experiments to auto-select first one when team changes
65+ const { data : allExperiments , mutate : mutateAllExperiments } = useSWR (
66+ chatAPI . API_URL ( ) === null ? null : chatAPI . Endpoints . Experiment . GetAll ( ) ,
67+ fetcher ,
68+ ) ;
69+
70+ // Auto-select first experiment when team changes and no experiment is selected
71+ // or if current experiment doesn't exist in the new team's experiments
72+ useEffect ( ( ) => {
73+ const currentTeamId = authContext ?. team ?. id ;
74+
75+ // Only run if we have a team, connection, and experiments are loaded
76+ if (
77+ ! currentTeamId ||
78+ ! connection ||
79+ connection === '' ||
80+ ! allExperiments
81+ ) {
82+ return ;
83+ }
84+
85+ // Skip if we've already auto-selected for this team
86+ if ( lastAutoSelectedTeamId . current === currentTeamId ) {
87+ return ;
88+ }
89+
90+ // If no experiments available, clear selection
91+ if ( allExperiments . length === 0 ) {
92+ if ( experimentId ) {
93+ setExperimentId ( null ) ;
94+ }
95+ lastAutoSelectedTeamId . current = currentTeamId ;
96+ return ;
97+ }
98+
99+ // Check if current experiment exists in the new team's experiments
100+ const currentExperimentExists = experimentId
101+ ? allExperiments . some (
102+ ( exp ) =>
103+ exp . id === experimentId ||
104+ exp . name === experimentId ||
105+ exp . id === experimentInfo ?. id ||
106+ exp . name === experimentInfo ?. name ,
107+ )
108+ : false ;
109+
110+ // If no experiment is selected, or current experiment doesn't exist in new team,
111+ // auto-select the first one
112+ if ( ( ! experimentId || ! currentExperimentExists ) && allExperiments . length > 0 ) {
113+ const firstExperiment = allExperiments [ 0 ] ;
114+ setExperimentId ( firstExperiment . name || firstExperiment . id ) ;
115+ lastAutoSelectedTeamId . current = currentTeamId ;
116+ } else if ( currentExperimentExists ) {
117+ // Current experiment exists, mark this team as handled
118+ lastAutoSelectedTeamId . current = currentTeamId ;
119+ }
120+ } , [
121+ authContext ?. team ?. id ,
122+ allExperiments ,
123+ connection ,
124+ experimentId ,
125+ experimentInfo ?. id ,
126+ experimentInfo ?. name ,
127+ ] ) ;
128+
59129 // Use useMemo to memoize the contextValue object
60130 const contextValue = useMemo ( ( ) => {
61131 return {
0 commit comments