(apiKeyData)
| 87 | } |
| 88 | |
| 89 | async setApiKey(apiKeyData) { |
| 90 | try { |
| 91 | this.state = { |
| 92 | apiKey: apiKeyData.key, |
| 93 | apiKeyInfo: apiKeyData, |
| 94 | expiresAt: apiKeyData.expiresAt || apiKeyData.expires_at, |
| 95 | ticketUsed: apiKeyData.ticketUsed || apiKeyData.ticket_used |
| 96 | }; |
| 97 | |
| 98 | let persisted = false; |
| 99 | if (typeof chatDB !== 'undefined') { |
| 100 | if (!chatDB.db && typeof chatDB.init === 'function') { |
| 101 | await chatDB.init(); |
| 102 | } |
| 103 | if (chatDB.db) { |
| 104 | await chatDB.saveSetting(ACCESS_DB_KEY, apiKeyData); |
| 105 | persisted = true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (persisted) { |
| 110 | localStorage.removeItem(ACCESS_STORAGE_KEY); |
| 111 | console.log('💾 Saved API key to IndexedDB'); |
| 112 | } else { |
| 113 | localStorage.setItem(ACCESS_STORAGE_KEY, JSON.stringify(apiKeyData)); |
| 114 | console.warn('💾 Saved API key to localStorage (IndexedDB unavailable)'); |
| 115 | } |
| 116 | |
| 117 | window.dispatchEvent(new CustomEvent('apikey-changed')); |
| 118 | this.notify(); |
| 119 | } catch (error) { |
| 120 | console.error('❌ Error saving API key:', error); |
| 121 | throw error; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | clearApiKey() { |
| 126 | this.state = { |
nothing calls this directly
no test coverage detected