Configuration Management & Storage
Relevant source files
The following files were used as context for generating this wiki page:The configuration management system in Read Frog is responsible for persisting user settings, managing the application's state, and synchronizing configurations across different contexts and devices. It uses browser.storage.local for persistence, Jotai for reactive state management, and offers both manual and automated (Google Drive) synchronization mechanisms. The system is designed to be robust, handling schema migrations and synchronization conflicts gracefully.
Sources: CLAUDE.md, src/utils/google-drive/sync.ts
Core Configuration Structure
The application's settings are encapsulated within a central Config object. This object is defined with a Zod schema to ensure type safety and validation. A default configuration is provided for new installations or resets.
Sources: src/utils/constants/config.ts:16-92
Default Configuration
The DEFAULT_CONFIG object provides the initial state for all user-configurable settings.
// src/utils/constants/config.ts:16-92
export const DEFAULT_CONFIG: Config = {
language: {
detectedCode: 'eng',
sourceCode: 'auto',
targetCode: 'cmn',
level: 'intermediate',
},
providersConfig: DEFAULT_PROVIDER_CONFIG_LIST,
read: {
providerId: 'openai-default',
},
translate: {
providerId: 'microsoft-default',
mode: 'bilingual',
// ... other translation settings
},
tts: DEFAULT_TTS_CONFIG,
floatingButton: {
enabled: true,
position: DEFAULT_FLOATING_BUTTON_POSITION,
disabledFloatingButtonPatterns: [],
},
selectionToolbar: {
enabled: true,
disabledSelectionToolbarPatterns: [],
},
sideContent: {
width: DEFAULT_SIDE_CONTENT_WIDTH,
},
betaExperience: {
enabled: false,
},
contextMenu: {
enabled: true,
},
}
Sources: src/utils/constants/config.ts:16-92
Configuration Schema Diagram
The following diagram illustrates the high-level structure of the Config object.
This diagram shows the main components of the configuration object. Note that this is a simplified representation.
Sources: src/utils/constants/config.ts:16-92
State Management with Jotai
The application uses Jotai for state management, with a central configAtom serving as the single source of truth for all extension settings. This atom is synchronized with browser.storage.local via a custom storageAdapter, ensuring that state is both reactive across UI components and persistent.
Changes to the configuration from any part of the extension (popup, options page, content scripts) update the atom, which in turn triggers the storage adapter to save the new state. The adapter also watches for storage changes, allowing different extension contexts to stay in sync.
Sources: CLAUDE.md
State Update Flow
The diagram below shows how a configuration change flows from the UI to the browser storage.
Local Storage Persistence
The extension uses browser.storage.local to persist configuration data. All configuration-related data is prefixed with local:.
Sources: src/utils/google-drive/sync.ts:32, src/entrypoints/host.content/index.tsx:100
Storage Keys
The following keys are used to store configuration and related metadata in browser.storage.
| Key | Description | Source File |
|---|---|---|
config | The main configuration object for the extension. | src/utils/constants/config.ts:11 |
__configSchemaVersion | The schema version of the stored configuration, used for migrations. | src/utils/constants/config.ts:12 |
__lastGoogleDriveSyncTime | Timestamp of the last successful sync with Google Drive. | src/utils/constants/config.ts:13 |
__lastSyncedConfig | A copy of the configuration object at the time of the last successful sync, used as a base for conflict resolution. | src/utils/constants/config.ts:14 |
__googleDriveToken | Stores the OAuth token for Google Drive API access. | src/utils/constants/config.ts:15 |
Configuration Syncing
The extension provides two methods for syncing configuration: manual import/export and automated cloud sync via Google Drive.
Manual Sync (Import/Export)
Users can manually import and export their configuration from the options page. This is useful for backups or transferring settings between browsers.
- Export: The current configuration is serialized into a JSON file. Users can choose whether to include sensitive API keys in the export.
- Import: A user-selected JSON file is read, parsed, and validated. If the imported configuration has an older schema version, it is migrated to the current version before being applied. The existing configuration is backed up before the new one is applied.
Sources: src/entrypoints/options/pages/config/manual-config-sync.tsx:43-61, 83-91
Import Process Sequence Diagram
This diagram shows the sequence of events when a user imports a configuration file.
Google Drive Cloud Sync
The extension can automatically sync settings to a user's Google Drive, using the sandboxed "App Data" folder. This allows for seamless configuration synchronization across different devices. The necessary permissions (storage, identity, https://www.googleapis.com/*) and OAuth2 client ID are declared in wxt.config.ts.
Sources: wxt.config.ts:19-28, [src/utils/google-drive/sync.ts:10]
The core logic is handled by the syncConfig function, which orchestrates the synchronization process based on timestamps and change detection.
Sources: src/utils/google-drive/sync.ts:200-297
Sync Logic Flow
The syncConfig function follows a clear decision tree to handle different synchronization scenarios.
This flow ensures that data is synchronized correctly, with a dedicated path for handling conflicts that require user intervention.
Sources: src/utils/google-drive/sync.ts:200-297, src/utils/google-drive/tests/sync.test.ts:153-277
A ConfigConflictError is thrown when both the local and remote configurations have been modified since the last sync, and the changes overlap. This error contains the base, local, and remote versions of the config, allowing the UI to present a merge resolution interface to the user.
Sources: src/utils/google-drive/sync.ts:25-29, 243-251
Configuration Migration
To support evolving application features, the configuration schema is versioned using the CONFIG_SCHEMA_VERSION constant. When the application detects a configuration with an older schema version (e.g., during import or cloud sync), it triggers a migration process.
Migration scripts are located in src/utils/config/migration-scripts and are responsible for transforming a configuration object from one version to the next. The testing system for these migrations is designed to automatically discover and run tests for each version, supporting both simple and complex multi-scenario test cases.
Sources: src/utils/constants/config.ts:16, src/utils/google-drive/sync.ts:141-148, src/utils/config/tests/migration-scripts/README.md
Conclusion
Read Frog's configuration management system is a comprehensive solution for handling user settings. It effectively combines reactive state management with persistent storage and provides flexible synchronization options. The inclusion of a versioned schema and a migration path ensures that the application can evolve without breaking user configurations, while the conflict resolution mechanism for cloud sync provides a robust user experience.