Migrate from window.electron to tauri IPC functions across components
- Replaced `window.electron.invoke` calls with equivalent `tauri` function calls for all IPC interactions. - Removed `electron.d.ts` TypeScript definitions as they are no longer needed. - Updated related logic for offline/online state synchronization. - Added `types.rs` and `shared/mod.rs` modules to support Tauri IPC integration with Rust enums and shared logic. - Refactored IPC request queues to use updated handler names for consistency with Tauri.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import React, { useState, useEffect, useCallback, ReactNode } from 'react';
|
||||
import OfflineContext, { OfflineMode, defaultOfflineMode } from './OfflineContext';
|
||||
import * as tauri from '@/lib/tauri';
|
||||
|
||||
interface OfflineProviderProps {
|
||||
children: ReactNode;
|
||||
@@ -12,40 +13,27 @@ export default function OfflineProvider({ children }: OfflineProviderProps) {
|
||||
|
||||
const initializeDatabase = useCallback(async (userId: string, encryptionKey?: string): Promise<boolean> => {
|
||||
try {
|
||||
if (typeof window === 'undefined' || !(window as any).electron) {
|
||||
console.warn('Not running in Electron, offline mode not available');
|
||||
return false;
|
||||
}
|
||||
|
||||
let userKey = encryptionKey;
|
||||
if (!userKey) {
|
||||
const storedKey = await (window as any).electron.getUserEncryptionKey(userId);
|
||||
const storedKey = await tauri.getUserEncryptionKey(userId);
|
||||
if (storedKey) {
|
||||
userKey = storedKey;
|
||||
} else {
|
||||
const keyResult = await (window as any).electron.generateEncryptionKey(userId);
|
||||
if (!keyResult.success) {
|
||||
throw new Error(keyResult.error || 'Failed to generate encryption key');
|
||||
}
|
||||
userKey = keyResult.key;
|
||||
await (window as any).electron.setUserEncryptionKey(userId, userKey);
|
||||
throw new Error('No encryption key found for user');
|
||||
}
|
||||
}
|
||||
|
||||
const result = await (window as any).electron.dbInitialize(userId, userKey);
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || 'Failed to initialize database');
|
||||
}
|
||||
await tauri.dbInitialize(userId, userKey);
|
||||
|
||||
setOfflineMode(prev => ({
|
||||
...prev,
|
||||
isDatabaseInitialized: true,
|
||||
error: null
|
||||
}));
|
||||
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize database:', error);
|
||||
console.error('Failed to initialize database:', error, 'userId:', userId, 'hasKey:', !!userKey);
|
||||
setOfflineMode(prev => ({
|
||||
...prev,
|
||||
isDatabaseInitialized: false,
|
||||
|
||||
Reference in New Issue
Block a user