Skip to content

Commit

Permalink
refactor: simplify service loading state management in AgentButton an…
Browse files Browse the repository at this point in the history
…d StakingContractDetailsProvider
  • Loading branch information
mohandast52 committed Dec 6, 2024
1 parent 6ea914d commit 235e8bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
11 changes: 4 additions & 7 deletions frontend/components/MainPage/header/AgentButton/AgentButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ import { AgentStartingButton } from './AgentStartingButton';
import { AgentStoppingButton } from './AgentStoppingButton';

export const AgentButton = () => {
const {
selectedService,
isFetched: isServicesLoaded,
selectedServiceStatusOverride,
} = useServices();
const { selectedService, isFetched, selectedServiceStatusOverride } =
useServices();

const {
isEligibleForStaking,
Expand All @@ -30,7 +27,7 @@ export const AgentButton = () => {
} = useActiveStakingContractInfo();

const button = useMemo(() => {
if (!isServicesLoaded || !isSelectedStakingContractDetailsLoaded) {
if (!isFetched || !isSelectedStakingContractDetailsLoaded) {
return (
<Button type="primary" size="large" disabled loading>
Loading...
Expand Down Expand Up @@ -68,7 +65,7 @@ export const AgentButton = () => {

return <CannotStartAgentDueToUnexpectedError />;
}, [
isServicesLoaded,
isFetched,
isSelectedStakingContractDetailsLoaded,
selectedServiceStatusOverride,
selectedService,
Expand Down
15 changes: 5 additions & 10 deletions frontend/context/StakingContractDetailsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
import { FIVE_SECONDS_INTERVAL } from '@/constants/intervals';
import { REACT_QUERY_KEYS } from '@/constants/react-query-keys';
import { StakingProgramId } from '@/enums/StakingProgram';
import { useService } from '@/hooks/useService';
import { useServices } from '@/hooks/useServices';
import { useStakingProgram } from '@/hooks/useStakingProgram';
import {
ServiceStakingDetails,
StakingContractDetails,
} from '@/types/Autonolas';
import { asMiddlewareChain } from '@/utils/middlewareHelpers';

import { StakingProgramContext } from './StakingProgramProvider';

Expand Down Expand Up @@ -171,18 +171,13 @@ export const StakingContractDetailsProvider = ({
children,
}: PropsWithChildren) => {
const [isPaused, setIsPaused] = useState(false);
const { selectedService, selectedAgentConfig } = useServices();
const { selectedService } = useServices();
const { serviceNftTokenId } = useService(selectedService?.service_config_id);
const { selectedStakingProgramId } = useContext(StakingProgramContext);

const serviceNftTokenId = !isNil(selectedService?.service_config_id)
? selectedService?.chain_configs?.[
asMiddlewareChain(selectedAgentConfig.evmHomeChainId)
]?.chain_data?.token
: null;

const {
data: selectedStakingContractDetails,
isFetched,
isLoading,
refetch,
} = useStakingContractDetailsByStakingProgram({
serviceNftTokenId,
Expand All @@ -200,7 +195,7 @@ export const StakingContractDetailsProvider = ({
<StakingContractDetailsContext.Provider
value={{
selectedStakingContractDetails,
isSelectedStakingContractDetailsLoaded: isFetched,
isSelectedStakingContractDetailsLoaded: !isLoading,
isAllStakingContractDetailsRecordLoaded:
isAllStakingContractDetailsLoaded,
allStakingContractDetailsRecord,
Expand Down

0 comments on commit 235e8bf

Please sign in to comment.