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

Add keep agent running alert #129

Merged
merged 8 commits into from
May 27, 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
8 changes: 8 additions & 0 deletions electron/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const schema = {
type: 'boolean',
default: false,
},
firstStakingRewardAchieved: {
type: 'boolean',
default: false,
},
firstRewardNotificationShown: {
type: 'boolean',
default: false,
},
};

const setupStoreIpc = async (ipcChannel, mainWindow) => {
Expand Down
34 changes: 34 additions & 0 deletions frontend/components/Main/KeepAgentRunning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Flex, Typography } from 'antd';

import { DeploymentStatus } from '@/client';
import { useServices } from '@/hooks';
import { useStore } from '@/hooks/useStore';

import { Alert } from '../common/Alert';
import { CardSection } from '../styled/CardSection';

const { Text } = Typography;

export const KeepAgentRunning = () => {
const { storeState } = useStore();
const { serviceStatus } = useServices();

if (storeState?.firstStakingRewardAchieved) return null;
if (serviceStatus !== DeploymentStatus.DEPLOYED) return null;

return (
<CardSection>
<Alert
type="info"
fullWidth
showIcon
message={
<Flex vertical>
<Text>Your agent has not hit its target yet.</Text>
<Text>Keep the agent running to earn today’s rewards.</Text>
</Flex>
}
/>
</CardSection>
);
};
2 changes: 2 additions & 0 deletions frontend/components/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useEffect } from 'react';
import { PageState } from '@/enums';
import { useBalance, usePageState, useServices } from '@/hooks';

import { KeepAgentRunning } from './KeepAgentRunning';
import { MainAddFunds } from './MainAddFunds';
import { MainGasBalance } from './MainGasBalance';
import { MainHeader } from './MainHeader';
Expand Down Expand Up @@ -41,6 +42,7 @@ export const Main = () => {
<Flex vertical>
<MainOlasBalance />
<MainRewards />
<KeepAgentRunning />
<MainGasBalance />
<MainNeedsFunds />
<MainAddFunds />
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/Main/MainNeedsFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const useNeedsFunds = () => {
const { storeState } = useStore();
const { safeBalance } = useBalance();

const isInitialFunded = storeState?.isInitialFunded as boolean | undefined;
const isInitialFunded = storeState?.isInitialFunded;

const serviceFundRequirements = useMemo(() => {
const monthlyGasEstimate = Number(
Expand Down
30 changes: 19 additions & 11 deletions frontend/components/Main/MainRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { COLOR } from '@/constants';
import { useBalance } from '@/hooks';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useReward } from '@/hooks/useReward';
import { useStore } from '@/hooks/useStore';

import { ConfettiAnimation } from '../common/ConfettiAnimation';

Expand Down Expand Up @@ -81,22 +82,27 @@ const DisplayRewards = () => {
const NotifyRewards = () => {
const { isEligibleForRewards, availableRewardsForEpochEth } = useReward();
const { totalOlasBalance } = useBalance();
const { showNotification } = useElectronApi();
const { showNotification, store } = useElectronApi();
const { storeState } = useStore();

const [canShowNotification, setCanShowNotification] = useState(false);

// hook to set the flag to show the notification
useEffect(() => {
// TODO: Implement this once state persistence is available
const hasAlreadyNotified = true;

if (!isEligibleForRewards) return;
if (hasAlreadyNotified) return;
if (!storeState) return;
if (storeState?.firstRewardNotificationShown) return;
if (!availableRewardsForEpochEth) return;

setCanShowNotification(true);
}, [isEligibleForRewards, availableRewardsForEpochEth, showNotification]);

// hook to show app notification
}, [
isEligibleForRewards,
availableRewardsForEpochEth,
showNotification,
storeState,
]);

// hook to show desktop app notification
useEffect(() => {
if (!canShowNotification) return;

Expand All @@ -108,8 +114,10 @@ const NotifyRewards = () => {

const closeNotificationModal = useCallback(() => {
setCanShowNotification(false);
// TODO: add setter for hasAlreadyNotified
}, []);

// once the notification is closed, set the flag to true
store?.set?.('firstRewardNotificationShown', true);
}, [store]);

if (!canShowNotification) return null;

Expand All @@ -126,7 +134,7 @@ const NotifyRewards = () => {
size="large"
className="mt-8"
disabled
// TODO: add twitter share functionality
style={{ display: 'none' }} // TODO: add twitter share functionality
>
<Flex align="center" justify="center" gap={2}>
Share on
Expand Down
15 changes: 15 additions & 0 deletions frontend/context/RewardProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import {
PropsWithChildren,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import { useInterval } from 'usehooks-ts';

import { useElectronApi } from '@/hooks/useElectronApi';
import { useStore } from '@/hooks/useStore';
import { AutonolasService } from '@/service/Autonolas';

import { ServicesContext } from './ServicesProvider';
Expand All @@ -32,6 +35,8 @@ export const RewardContext = createContext<{
export const RewardProvider = ({ children }: PropsWithChildren) => {
const { services } = useContext(ServicesContext);
const service = useMemo(() => services?.[0], [services]);
const { storeState } = useStore();
const electronApi = useElectronApi();

const [accruedServiceStakingRewards, setAccruedServiceStakingRewards] =
useState<number>();
Expand Down Expand Up @@ -78,6 +83,16 @@ export const RewardProvider = ({ children }: PropsWithChildren) => {
setAvailableRewardsForEpoch(rewards);
}, [service]);

useEffect(() => {
if (isEligibleForRewards && !storeState?.firstStakingRewardAchieved) {
electronApi.store?.set?.('firstStakingRewardAchieved', true);
}
}, [
electronApi.store,
isEligibleForRewards,
storeState?.firstStakingRewardAchieved,
]);

useInterval(async () => updateRewards(), 5000);

return (
Expand Down
7 changes: 5 additions & 2 deletions frontend/types/ElectronApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export type ElectronStore = Record<string, unknown>;
export type ElectronStore = {
isInitialFunded?: boolean;
firstStakingRewardAchieved?: boolean;
firstRewardNotificationShown?: boolean;
};

export type ElectronTrayIconStatus = 'low-gas' | 'running' | 'paused';
Loading