Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHARD-1108: Add notification for unstaking becoming available #70

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion components/molecules/NodeStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ export const getDurationBreakdownString = (duration: number) => {
};

export const NodeStatus = ({ isWalletConnected, address }: NodeStatusProps) => {
const { nodeStatus, isLoading, startNode, stopNode } = useNodeStatus();
const { nodeStatus, isLoading, startNode, stopNode, notifyUnstake } =
useNodeStatus();
// const { nodeStatusHistory } = useNodeStatusHistory(address || "");

const state: NodeState = getNodeState(nodeStatus);
Expand Down Expand Up @@ -402,6 +403,21 @@ export const NodeStatus = ({ isWalletConnected, address }: NodeStatusProps) => {
}
}, [nodeStatus?.state, currentStatus]);

useEffect(() => {
if (notifyUnstake) {
setCurrentToast({
severity: ToastSeverity.SUCCESS,
title: "Node can be unstaked",
description: "Your node can now be unstaked.",
followupNotification: {
type: NotificationType.UNSTAKE_STATUS,
severity: NotificationSeverity.SUCCESS,
title: "Your node can now be unstaked.",
},
});
}
}, [notifyUnstake, setCurrentToast]);

const toggleShowMoreInfo = () => {
setShowMoreInfo((prevState: boolean) => !prevState);
};
Expand Down
26 changes: 19 additions & 7 deletions hooks/useNodeStatus.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import useSWR from 'swr'
import { fetcher } from './fetcher'
import { NodeStatus } from '../model/node-status'
import { useContext, useState } from 'react'
import { useGlobals } from '../utils/globals'
import { FetcherContext } from '../components/FetcherContextProvider';
import { showErrorMessage } from './useToastStore'
import { useContext, useEffect, useState } from "react";
import { useGlobals } from "../utils/globals";
import { FetcherContext } from "../components/FetcherContextProvider";
import { showErrorMessage } from "./useToastStore";

type NodeStatusResponse = {
nodeStatus: NodeStatus | undefined;
startNode: Function;
stopNode: Function;
isLoading: boolean;
}
notifyUnstake: boolean;
};

export const useNodeStatus = (): NodeStatusResponse => {
const { apiBase } = useGlobals()
const nodeStatusApi = `${apiBase}/api/node/status`
const fetcherWithContext = useContext(FetcherContext);
const { data, mutate } = useSWR<NodeStatus>(nodeStatusApi, fetcherWithContext, { refreshInterval: 1000 })
const [isLoading, setIsLoading] = useState<boolean>(false)
const [hasShownUnstakeNotification, setHasShownUnstakeNotification] =
useState<boolean>(false);

useEffect(() => {
if (data?.stakeState?.unlocked && !hasShownUnstakeNotification) {
setHasShownUnstakeNotification(true);
} else if (!data?.stakeState?.unlocked && hasShownUnstakeNotification) {
setHasShownUnstakeNotification(false);
}
}, [data]);

const startNode = async (): Promise<void> => {
setIsLoading(true)
Expand Down Expand Up @@ -46,8 +57,9 @@ export const useNodeStatus = (): NodeStatusResponse => {
nodeStatus: data,
startNode,
stopNode,
isLoading
}
isLoading,
notifyUnstake: hasShownUnstakeNotification,
};
}


Expand Down
3 changes: 2 additions & 1 deletion hooks/useNotificationsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export enum NotificationType {
NODE_STATUS = "NODE_STATUS",
ERROR = "ERROR",
REWARD = "REWARD",
VERSION_UPDATE = "VERSION_UPDATE"
VERSION_UPDATE = "VERSION_UPDATE",
UNSTAKE_STATUS = "UNSTAKE_STATUS",
}

export enum NotificationSeverity {
Expand Down
Loading