-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(meshwide): implement sync with remote node (#419)
It implements the flow to perform publish all of a node, and get the syncronization from the node you are visiting. On this way, when a user browses the map, it always have the updated version of the node It depends on libremesh/lime-packages#1108
- Loading branch information
Showing
9 changed files
with
245 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
plugins/lime-plugin-mesh-wide/src/components/FeatureDetail/UpdateNodeInfoBtn.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Trans } from "@lingui/macro"; | ||
import { useEffect, useState } from "preact/hooks"; | ||
import { useCallback } from "react"; | ||
|
||
import { Button } from "components/buttons/button"; | ||
import { RefreshIcon } from "components/icons/teenny/refresh"; | ||
import { useToast } from "components/toast/toastProvider"; | ||
|
||
import { | ||
usePublishOnRemoteNode, | ||
useSyncDataTypes, | ||
} from "plugins/lime-plugin-mesh-wide/src/meshWideQueries"; | ||
import { getFromSharedStateKeys } from "plugins/lime-plugin-mesh-wide/src/meshWideQueriesKeys"; | ||
import { | ||
DataTypes, | ||
INodeInfo, | ||
completeDataTypeKeys, | ||
} from "plugins/lime-plugin-mesh-wide/src/meshWideTypes"; | ||
|
||
import queryCache from "utils/queryCache"; | ||
|
||
const UpdateNodeInfoBtn = ({ node }: { node: INodeInfo }) => { | ||
const ip = node.ipv4; | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
const { showToast, hideToast } = useToast(); | ||
|
||
const { mutateAsync: localNodeSync } = useSyncDataTypes({ | ||
ip, | ||
}); | ||
const { mutateAsync: publishOnRemoteNode } = usePublishOnRemoteNode({ | ||
ip, | ||
}); | ||
|
||
const invalidateQueries = useCallback(() => { | ||
for (const dataType of Object.keys( | ||
completeDataTypeKeys | ||
) as DataTypes[]) { | ||
queryCache.invalidateQueries({ | ||
queryKey: | ||
getFromSharedStateKeys.getFromSharedStateAsync(dataType), | ||
}); | ||
} | ||
}, []); | ||
|
||
// useCallback to sync the node data | ||
const syncNode = useCallback(async () => { | ||
if (isLoading) return; | ||
setIsLoading(true); | ||
publishOnRemoteNode({ ip }) | ||
.catch((e) => { | ||
showToast({ | ||
text: ( | ||
<Trans> | ||
Error connecting with {node.hostname}, is node up? | ||
</Trans> | ||
), | ||
duration: 5000, | ||
}); | ||
throw e; | ||
}) | ||
.then(async () => { | ||
await localNodeSync({ ip }); | ||
await invalidateQueries(); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [ | ||
invalidateQueries, | ||
ip, | ||
isLoading, | ||
localNodeSync, | ||
node.hostname, | ||
publishOnRemoteNode, | ||
showToast, | ||
]); | ||
|
||
// Use effect to sync the node data on mount | ||
useEffect(() => { | ||
(async () => { | ||
await syncNode(); | ||
})(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [node.ipv4]); | ||
|
||
return ( | ||
<Button | ||
color={"primary"} | ||
outline={!isLoading} | ||
size={"sm"} | ||
onClick={syncNode} | ||
> | ||
<RefreshIcon /> | ||
</Button> | ||
); | ||
}; | ||
|
||
export default UpdateNodeInfoBtn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const RefreshIcon = () => { | ||
return ( | ||
<svg | ||
viewBox="0 0 15 15" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="15" | ||
height="15" | ||
> | ||
<path | ||
d="M7.5 14.5A7 7 0 013.17 2M7.5.5A7 7 0 0111.83 13m-.33-3v3.5H15M0 1.5h3.5V5" | ||
stroke="currentColor" | ||
/> | ||
</svg> | ||
); | ||
}; |