fix loading issue
Browse files97 unmodified lines98991001011021031041051061077 unmodified lines1151161171181191201213 unmodified lines1251261271281291301311328 unmodified lines14114214314414514614797 unmodified linesreturn this.token;}async getModels(): Promise<Model[]> {try {const response = await this.client.get<Model[]>('/api/models');return response.data;} catch (error: any) {// Handle connection errors gracefullyconst isConnectionError =7 unmodified lineserror.response?.status === 502;if (isConnectionError) {// Backend is not available - return empty array instead of throwingconsole.warn('Backend not available, cannot load models');return [];}3 unmodified lines}async getLanguages(): Promise<{ languages: Language[] }> {try {const response = await this.client.get<{ languages: Language[] }>('/api/languages');return response.data;} catch (error: any) {// Handle connection errors gracefully8 unmodified lineserror.response?.status === 502;if (isConnectionError) {// Backend is not available - return default languages instead of throwingconsole.warn('Backend not available, using default languages');return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] };}97 unmodified lines98991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581597 unmodified lines1671681691701711721731741751761771781793 unmodified lines1831841851861871881891901911921931941951961971981992002012022032042052068 unmodified lines21521621721821922022122222322422522622722897 unmodified linesreturn this.token;}// Cache helpersprivate getCachedData<T>(key: string, maxAgeMs: number): T | null {if (typeof window === 'undefined') return null;try {const cached = localStorage.getItem(key);if (!cached) return null;const { data, timestamp } = JSON.parse(cached);const age = Date.now() - timestamp;if (age > maxAgeMs) {localStorage.removeItem(key);return null;}return data;} catch (error) {console.error(`Failed to get cached data for ${key}:`, error);return null;}}private setCachedData<T>(key: string, data: T): void {if (typeof window === 'undefined') return;try {localStorage.setItem(key, JSON.stringify({data,timestamp: Date.now()}));} catch (error) {console.error(`Failed to cache data for ${key}:`, error);}}async getModels(): Promise<Model[]> {// Check cache first (24 hour TTL)const cached = this.getCachedData<Model[]>('anycoder_models', 24 * 60 * 60 * 1000);if (cached) {console.log('Using cached models:', cached.length, 'models');return cached;}try {console.log('Fetching models from API...');const response = await this.client.get<Model[]>('/api/models');const models = response.data;// Cache the successful responseif (models && models.length > 0) {this.setCachedData('anycoder_models', models);console.log('Cached', models.length, 'models');}return models;} catch (error: any) {// Handle connection errors gracefullyconst isConnectionError =7 unmodified lineserror.response?.status === 502;if (isConnectionError) {// Try to return stale cache if availableconst staleCache = this.getCachedData<Model[]>('anycoder_models', Infinity);if (staleCache && staleCache.length > 0) {console.warn('Backend not available, using stale cached models');return staleCache;}console.warn('Backend not available, cannot load models');return [];}3 unmodified lines}async getLanguages(): Promise<{ languages: Language[] }> {// Check cache first (24 hour TTL)const cached = this.getCachedData<Language[]>('anycoder_languages', 24 * 60 * 60 * 1000);if (cached) {console.log('Using cached languages:', cached.length, 'languages');return { languages: cached };}try {console.log('Fetching languages from API...');const response = await this.client.get<{ languages: Language[] }>('/api/languages');const languages = response.data.languages;// Cache the successful responseif (languages && languages.length > 0) {this.setCachedData('anycoder_languages', languages);console.log('Cached', languages.length, 'languages');}return response.data;} catch (error: any) {// Handle connection errors gracefully8 unmodified lineserror.response?.status === 502;if (isConnectionError) {// Try to return stale cache if availableconst staleCache = this.getCachedData<Language[]>('anycoder_languages', Infinity);if (staleCache && staleCache.length > 0) {console.warn('Backend not available, using stale cached languages');return { languages: staleCache };}// Fall back to default languagesconsole.warn('Backend not available, using default languages');return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] };}
frontend/src/lib/api.ts+84-3