Clean up and fixes
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
* App context hook for accessing controllers and app-level functions.
|
||||
* App context hook for accessing AppService and app-level functions.
|
||||
*/
|
||||
|
||||
import React, { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
|
||||
import type { WalletController } from '../../controllers/wallet-controller.js';
|
||||
import type { InvitationController } from '../../controllers/invitation-controller.js';
|
||||
import { AppService } from '../../services/app.js';
|
||||
import type { AppConfig } from '../../app.js';
|
||||
import type { AppContextType, DialogState } from '../types.js';
|
||||
|
||||
/**
|
||||
@@ -37,27 +37,50 @@ const StatusContext = createContext<StatusContextType | null>(null);
|
||||
*/
|
||||
interface AppProviderProps {
|
||||
children: ReactNode;
|
||||
walletController: WalletController;
|
||||
invitationController: InvitationController;
|
||||
config: AppConfig;
|
||||
onExit: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* App provider component.
|
||||
* Provides controllers, dialog management, and app-level functions to children.
|
||||
* Provides AppService, dialog management, and app-level functions to children.
|
||||
*/
|
||||
export function AppProvider({
|
||||
children,
|
||||
walletController,
|
||||
invitationController,
|
||||
config,
|
||||
onExit,
|
||||
}: AppProviderProps): React.ReactElement {
|
||||
const [appService, setAppService] = useState<AppService | null>(null);
|
||||
const [dialog, setDialog] = useState<DialogState | null>(null);
|
||||
const [status, setStatusState] = useState<string>('Ready');
|
||||
const [isWalletInitialized, setWalletInitialized] = useState(false);
|
||||
|
||||
// Promise resolver for confirm dialogs
|
||||
const [confirmResolver, setConfirmResolver] = useState<((value: boolean) => void) | null>(null);
|
||||
/**
|
||||
* Initialize wallet with seed phrase and create AppService.
|
||||
*/
|
||||
const initializeWallet = useCallback(async (seed: string) => {
|
||||
try {
|
||||
// Create the AppService with the seed
|
||||
const service = await AppService.create(seed, {
|
||||
syncServerUrl: config.syncServerUrl,
|
||||
engineConfig: {
|
||||
databasePath: config.databasePath,
|
||||
databaseFilename: config.databaseFilename,
|
||||
},
|
||||
invitationStoragePath: config.invitationStoragePath,
|
||||
});
|
||||
|
||||
// Start the AppService (loads existing invitations)
|
||||
await service.start();
|
||||
|
||||
// Set the service and mark as initialized
|
||||
setAppService(service);
|
||||
setWalletInitialized(true);
|
||||
} catch (error) {
|
||||
// Re-throw the error so the caller can handle it
|
||||
throw error;
|
||||
}
|
||||
}, [config]);
|
||||
|
||||
/**
|
||||
* Show an error dialog.
|
||||
@@ -88,7 +111,6 @@ export function AppProvider({
|
||||
*/
|
||||
const confirm = useCallback((message: string): Promise<boolean> => {
|
||||
return new Promise((resolve) => {
|
||||
setConfirmResolver(() => resolve);
|
||||
setDialog({
|
||||
visible: true,
|
||||
type: 'confirm',
|
||||
@@ -113,15 +135,15 @@ export function AppProvider({
|
||||
}, []);
|
||||
|
||||
const appValue: AppContextType = {
|
||||
walletController,
|
||||
invitationController,
|
||||
appService,
|
||||
initializeWallet,
|
||||
isWalletInitialized,
|
||||
config,
|
||||
showError,
|
||||
showInfo,
|
||||
confirm,
|
||||
exit: onExit,
|
||||
setStatus,
|
||||
isWalletInitialized,
|
||||
setWalletInitialized,
|
||||
};
|
||||
|
||||
const dialogValue: DialogContextType = {
|
||||
|
||||
Reference in New Issue
Block a user