From 8d8af0be22273f896a4756739332b7b7f99ac7c5 Mon Sep 17 00:00:00 2001 From: Francisco Tobar Date: Wed, 27 Nov 2024 10:29:13 -0600 Subject: [PATCH] refactor: pr comments --- .../components/BuilderAllocationHeader.tsx | 9 +++------ .../builder/BuilderRewardsSettingsForm.tsx | 2 +- .../builder/context/BuilderSettingsContext.tsx | 11 +++++++---- .../settings/builder/hooks/index.ts | 1 - .../builder/hooks/useGetIsBuilderOperational.ts | 17 ----------------- .../shared/components/Table/TableCells.tsx | 12 ++++-------- .../utils/getMostAdvancedProposal.ts | 4 ++-- src/app/collective-rewards/utils/index.ts | 1 + .../collective-rewards/utils/isBuilderActive.ts | 7 ------- .../utils/isBuilderOperational.ts | 5 +++++ 10 files changed, 23 insertions(+), 46 deletions(-) delete mode 100644 src/app/collective-rewards/settings/builder/hooks/useGetIsBuilderOperational.ts delete mode 100644 src/app/collective-rewards/utils/isBuilderActive.ts create mode 100644 src/app/collective-rewards/utils/isBuilderOperational.ts diff --git a/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx b/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx index 94b51eba..7c6f0693 100644 --- a/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx +++ b/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx @@ -3,15 +3,12 @@ import { Badge } from '@/components/Badge' import { Jdenticon } from '@/components/Header/Jdenticon' import { Paragraph, Typography } from '@/components/Typography' import { FC } from 'react' -import { Builder, BuilderProposal, BuilderStateFlags } from '../../types' +import { Builder, BuilderProposal, BuilderStateFlags } from '@/app/collective-rewards/types' +import { isBuilderOperational } from '@/app/collective-rewards/utils' export type BuilderAllocationHeaderProps = Pick & Pick -const isBuilderActive = ({ communityApproved, kycApproved, paused }: BuilderStateFlags) => { - return communityApproved && kycApproved && !paused -} - export const BuilderAllocationHeader: FC = ({ address, builderName, @@ -34,7 +31,7 @@ export const BuilderAllocationHeader: FC = ({ {state.paused && state.communityApproved && ( )} - {isBuilderActive(state) && Joined {date}} + {isBuilderOperational(state) && Joined {date}} ) diff --git a/src/app/collective-rewards/settings/builder/BuilderRewardsSettingsForm.tsx b/src/app/collective-rewards/settings/builder/BuilderRewardsSettingsForm.tsx index 5a9298e2..d13d4e4a 100644 --- a/src/app/collective-rewards/settings/builder/BuilderRewardsSettingsForm.tsx +++ b/src/app/collective-rewards/settings/builder/BuilderRewardsSettingsForm.tsx @@ -40,7 +40,7 @@ const formSchema = z.object({ export const BuilderRewardsSettingsForm: FC = () => { const router = useRouter() const [backButtonName, setBackButtonName] = useState('Cancel') - let { + const { current: { refetch, isLoading: isCurrentRewardsLoading }, update: { isSuccess, setNewReward, isPending }, isBuilderOperational, diff --git a/src/app/collective-rewards/settings/builder/context/BuilderSettingsContext.tsx b/src/app/collective-rewards/settings/builder/context/BuilderSettingsContext.tsx index 8db27951..b41def17 100644 --- a/src/app/collective-rewards/settings/builder/context/BuilderSettingsContext.tsx +++ b/src/app/collective-rewards/settings/builder/context/BuilderSettingsContext.tsx @@ -8,13 +8,14 @@ import { import { createContext, FC, ReactNode, useContext } from 'react' import { Address } from 'viem' import { useAccount } from 'wagmi' -import { useGetIsBuilderOperational } from '@/app/collective-rewards/settings' +import { useBuilderContext } from '@/app/collective-rewards/user' +import { isBuilderOperational } from '@/app/collective-rewards/utils' type BackerRewardsPercentageContext = { update: SetBackerRewardsForBuilder current: BackerRewardResponse rewardPercentageToApply: RewardPercentageToApply - isBuilderOperational?: boolean + isBuilderOperational: boolean } const BuilderSettingsContext = createContext( @@ -24,17 +25,19 @@ const BuilderSettingsContext = createContext( export const useBuilderSettingsContext = () => useContext(BuilderSettingsContext) export const BuilderSettingsProvider: FC<{ children: ReactNode }> = ({ children }) => { + const { getBuilderByAddress } = useBuilderContext() const { address } = useAccount() const current = useGetBackerRewardsForBuilder(address as Address) const update = useSetBackerRewardsForBuilder() const rewardPercentageToApply = useGetRewardPercentageToApply(address as Address) - const { data: isBuilderOperational } = useGetIsBuilderOperational(address as Address) + const builder = getBuilderByAddress(address as Address) + const isOperational = builder ? isBuilderOperational(builder.stateFlags) : false const contextValue: BackerRewardsPercentageContext = { update, current, rewardPercentageToApply, - isBuilderOperational, + isBuilderOperational: isOperational, } return {children} diff --git a/src/app/collective-rewards/settings/builder/hooks/index.ts b/src/app/collective-rewards/settings/builder/hooks/index.ts index 5f40d8f4..880f1170 100644 --- a/src/app/collective-rewards/settings/builder/hooks/index.ts +++ b/src/app/collective-rewards/settings/builder/hooks/index.ts @@ -1,2 +1 @@ export * from './useBuilderConfig' -export * from './useGetIsBuilderOperational' diff --git a/src/app/collective-rewards/settings/builder/hooks/useGetIsBuilderOperational.ts b/src/app/collective-rewards/settings/builder/hooks/useGetIsBuilderOperational.ts deleted file mode 100644 index 54b69a31..00000000 --- a/src/app/collective-rewards/settings/builder/hooks/useGetIsBuilderOperational.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { AVERAGE_BLOCKTIME } from '@/lib/constants' -import { BackersManagerAddress } from '@/lib/contracts' -import { useReadContract } from 'wagmi' -import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi' -import { Address } from 'viem' - -export const useGetIsBuilderOperational = (builder: Address) => { - return useReadContract({ - address: BackersManagerAddress, - abi: BuilderRegistryAbi, - functionName: 'isBuilderOperational', - args: [builder], - query: { - refetchInterval: AVERAGE_BLOCKTIME, - }, - }) -} diff --git a/src/app/collective-rewards/shared/components/Table/TableCells.tsx b/src/app/collective-rewards/shared/components/Table/TableCells.tsx index a42430a8..7d080eb0 100644 --- a/src/app/collective-rewards/shared/components/Table/TableCells.tsx +++ b/src/app/collective-rewards/shared/components/Table/TableCells.tsx @@ -13,6 +13,7 @@ import { ProgressBar } from '@/components/ProgressBar' import { Button } from '@/components/Button' import { BuilderStateFlags } from '@/app/collective-rewards/types' import { AllocationsContext } from '@/app/collective-rewards/allocations/context' +import { isBuilderOperational } from '@/app/collective-rewards/utils' export function getFormattedCurrency(value: number, symbol: string) { const formattedCurrency = formatCurrency(value, symbol) @@ -202,17 +203,12 @@ export const ActionCell: FC = ({ tableHeader: { className }, bu [selections, builderAddress, isPreallocated], ) - const isBuilderOperational = useMemo(() => { + const isOperational = useMemo(() => { if (!builder) { console.log('Builder not found in selection') // TODO: handle this case better return } - return ( - builder.stateFlags && - builder.stateFlags.kycApproved && - builder.stateFlags.communityApproved && - !builder.stateFlags.paused - ) + return isBuilderOperational(builder.stateFlags) }, [builder]) const selectBuilder = () => { @@ -223,7 +219,7 @@ export const ActionCell: FC = ({ tableHeader: { className }, bu