fix: update syncState and how status updates work and their types

This commit is contained in:
Derrick Hammer 2023-10-09 02:35:36 -04:00
parent 349fa936d5
commit 0551582887
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 19 additions and 11 deletions

View File

@ -10,11 +10,13 @@ import type { ReactNode } from "react";
import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client";
import { createNetworkClient } from "@lumeweb/libkernel/module";
type SyncState = "done" | "syncing" | "error";
export interface Network extends NetworkStatus {
name: string;
id: string;
type: string;
syncState: "done" | "syncing" | "error";
syncState: SyncState;
}
interface NetworkStatus {
@ -42,14 +44,17 @@ const LumeProvider = ({ children }: { children: ReactNode }) => {
// Map to store unsubscribe functions for client.status subscriptions
const statusUnsubs = useRef(new Map());
const handleStatusUpdate = useCallback((id: string, newStatus: Network) => {
const handleStatusUpdate = useCallback(
(id: string, newNetwork: NetworkStatus & { syncState: SyncState }) => {
setLume((prevLume) => {
const updatedNetworks = prevLume.networks.map((network) =>
network.id === id ? { ...network, ...newStatus } : network
network.id === id ? { ...network, ...newNetwork } : network
);
return { ...prevLume, networks: updatedNetworks };
});
}, []);
},
[]
);
const update = async () => {
const types = await networkRegistry.getTypes();
@ -75,7 +80,7 @@ const LumeProvider = ({ children }: { children: ReactNode }) => {
// Subscribe to status updates
const statusUnsub = client.status((newStatus: NetworkStatus) => {
let syncState = "syncing";
let syncState: SyncState = "syncing";
if (newStatus.ready) {
syncState = "done";
@ -83,7 +88,10 @@ const LumeProvider = ({ children }: { children: ReactNode }) => {
syncState = "error";
}
handleStatusUpdate(module, { ...newStatus, syncState });
handleStatusUpdate(module, {
...newStatus,
syncState,
});
});
newStatusUnsubs.set(module, statusUnsub);