mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
- Move stores/utils/types to their relative directories (i.e chat stores in chat directory) - Move utility files to shared/utils - Move component files to shared/components - Move type definitions to shared/types - Move stores to shared/stores - Update import paths across the project
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
|
|
/**
|
|
* Hook to initialize and provide access to the IndexedDB database
|
|
*/
|
|
export function useIndexedDB() {
|
|
const [db, setDb] = useState<IDBDatabase | null>(null);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [error, setError] = useState<Error | null>(null);
|
|
|
|
useEffect(() => {
|
|
const initDB = async () => {
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
const request = indexedDB.open('boltDB', 1);
|
|
|
|
request.onupgradeneeded = (event) => {
|
|
const db = (event.target as IDBOpenDBRequest).result;
|
|
|
|
// Create object stores if they don't exist
|
|
if (!db.objectStoreNames.contains('chats')) {
|
|
const chatStore = db.createObjectStore('chats', { keyPath: 'id' });
|
|
chatStore.createIndex('updatedAt', 'updatedAt', { unique: false });
|
|
}
|
|
|
|
if (!db.objectStoreNames.contains('settings')) {
|
|
db.createObjectStore('settings', { keyPath: 'key' });
|
|
}
|
|
};
|
|
|
|
request.onsuccess = (event) => {
|
|
const database = (event.target as IDBOpenDBRequest).result;
|
|
setDb(database);
|
|
setIsLoading(false);
|
|
};
|
|
|
|
request.onerror = (event) => {
|
|
setError(new Error(`Database error: ${(event.target as IDBOpenDBRequest).error?.message}`));
|
|
setIsLoading(false);
|
|
};
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err : new Error('Unknown error initializing database'));
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
initDB();
|
|
|
|
return () => {
|
|
if (db) {
|
|
db.close();
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
return { db, isLoading, error };
|
|
}
|