128 lines
3.4 KiB
TypeScript
128 lines
3.4 KiB
TypeScript
/**
|
|
* IndexedDB storage for locale preference
|
|
*/
|
|
|
|
const DB_NAME = 'nostr_paywall_settings'
|
|
const DB_VERSION = 2 // Incremented to add locale store
|
|
const STORE_NAME = 'locale'
|
|
|
|
export type Locale = 'fr' | 'en'
|
|
|
|
class LocaleStorageService {
|
|
private db: IDBDatabase | null = null
|
|
|
|
private async initDB(): Promise<IDBDatabase> {
|
|
if (this.db) {
|
|
return this.db
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (typeof window === 'undefined' || !window.indexedDB) {
|
|
reject(new Error('IndexedDB is not available'))
|
|
return
|
|
}
|
|
|
|
const request = indexedDB.open(DB_NAME, DB_VERSION)
|
|
|
|
request.onerror = () => {
|
|
reject(new Error(`Failed to open IndexedDB: ${request.error}`))
|
|
}
|
|
|
|
request.onsuccess = () => {
|
|
this.db = request.result
|
|
resolve(this.db)
|
|
}
|
|
|
|
request.onupgradeneeded = (event) => {
|
|
const db = (event.target as IDBOpenDBRequest).result
|
|
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
db.createObjectStore(STORE_NAME, { keyPath: 'key' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Get locale from IndexedDB
|
|
*/
|
|
async getLocale(): Promise<Locale | null> {
|
|
try {
|
|
const db = await this.initDB()
|
|
const transaction = db.transaction([STORE_NAME], 'readonly')
|
|
const store = transaction.objectStore(STORE_NAME)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const request = store.get('locale')
|
|
|
|
request.onsuccess = () => {
|
|
const result = request.result as { key: string; value: Locale } | undefined
|
|
const locale = result?.value
|
|
if (locale === 'fr' || locale === 'en') {
|
|
resolve(locale)
|
|
} else {
|
|
resolve(null)
|
|
}
|
|
}
|
|
|
|
request.onerror = () => reject(request.error)
|
|
})
|
|
} catch (error) {
|
|
console.error('Error retrieving locale from IndexedDB:', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save locale to IndexedDB
|
|
*/
|
|
async saveLocale(locale: Locale): Promise<void> {
|
|
try {
|
|
const db = await this.initDB()
|
|
const transaction = db.transaction([STORE_NAME], 'readwrite')
|
|
const store = transaction.objectStore(STORE_NAME)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const request = store.put({ key: 'locale', value: locale })
|
|
|
|
request.onsuccess = () => {
|
|
resolve()
|
|
}
|
|
|
|
request.onerror = () => reject(request.error)
|
|
})
|
|
} catch (error) {
|
|
console.error('Error saving locale to IndexedDB:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Migrate locale from localStorage to IndexedDB if it exists
|
|
*/
|
|
async migrateFromLocalStorage(): Promise<void> {
|
|
if (typeof window === 'undefined') {
|
|
return
|
|
}
|
|
|
|
try {
|
|
const LOCALE_STORAGE_KEY = 'zapwall-locale'
|
|
const storedLocale = window.localStorage.getItem(LOCALE_STORAGE_KEY) as Locale | null
|
|
|
|
if (storedLocale && (storedLocale === 'fr' || storedLocale === 'en')) {
|
|
// Check if already in IndexedDB
|
|
const existingLocale = await this.getLocale()
|
|
if (!existingLocale) {
|
|
// Migrate from localStorage to IndexedDB
|
|
await this.saveLocale(storedLocale)
|
|
// Remove from localStorage after successful migration
|
|
window.localStorage.removeItem(LOCALE_STORAGE_KEY)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error migrating locale from localStorage:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
export const localeStorage = new LocaleStorageService()
|