Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add IndexedDB Provider Context #663

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thecodacus
Copy link
Collaborator

Add IndexedDB Provider Context

Overview

This PR introduces a dedicated provider context for IndexedDB operations, replacing the previous global database instance approach. This architectural change improves reliability, provides better state management, and enhances the application's ability to handle database lifecycle events.

Key Changes

1. Provider Architecture

  • Added new IndexedDBProvider component for centralized database management
  • Implemented proper database lifecycle handling (initialization, error states, loading states)
  • Added TypeScript types for provider context
  • Introduced useIndexedDB hook for consuming components

2. Database Management

  • Moved database initialization logic into provider
  • Added proper database cleanup on unmount
  • Implemented loading state tracking
  • Added error handling and state management
  • Made database name and version configurable via props

3. Migration from Global Instance

  • Removed global database instance
  • Updated all consumers to use provider context
  • Modified chat history components to handle loading states
  • Updated database operations to work with provider context

4. Components Updated

  • Updated Menu component to use provider
  • Modified Messages component to use provider
  • Updated ChatHistoryTab to use provider
  • Modified useEditChatDescription hook to use provider

Technical Details

IndexedDBProvider

The provider manages:

  • Database connection lifecycle
  • Loading states during initialization
  • Error handling and propagation
  • Database cleanup on unmount
  • Type-safe context access

useIndexedDB Hook

Provides:

  • Type-safe access to database instance
  • Loading state information
  • Error state information
  • Proper context validation

Testing

  • Verified database initialization
  • Tested error handling scenarios
  • Verified cleanup on component unmount
  • Tested loading states
  • Verified context access in components

Migration Impact

  • No breaking changes for existing functionality
  • Database operations now handled through provider
  • Better error handling and loading states
  • Improved database lifecycle management

Future Improvements

  • Add support for multiple database instances (super important for some use cases related to git)
  • Add database migration support
  • Add database version upgrade handling

Usage Example

// Root component
<IndexedDbProvider databaseName="boltHistory" version={1}>
  <App />
</IndexedDbProvider>

// Consumer components
const { db, isLoading, error } = useIndexedDB();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant