Skip to content

Commit

Permalink
fix: fallback to off-chain config for tiers
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Dec 16, 2022
1 parent d851b77 commit 2c35e90
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
46 changes: 26 additions & 20 deletions packages/react/src/core/diamonds/providers/DiamondProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import {
Environment,
TransactionData,
TransactionListener,
} from '@flair-sdk/common';
} from "@flair-sdk/common";
import {
ContractCall,
EIP165InterfaceID,
FacetManifest,
} from '@flair-sdk/registry';
import * as React from 'react';
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import { useAccount } from 'wagmi';
} from "@flair-sdk/registry";
import * as React from "react";
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
import { useAccount } from "wagmi";

import { SmartContract, useChainId, useSmartContract } from '../../../common';
import { useWalletContext } from '../../wallet';
import { useDiamond } from '../hooks/useDiamond';
import { Diamond } from '../types';
import { SmartContract, useChainId, useSmartContract } from "../../../common";
import { useWalletContext } from "../../wallet";
import { useDiamond } from "../hooks/useDiamond";
import { Diamond } from "../types";

type DiamondContextValue = {
data: {
Expand Down Expand Up @@ -62,11 +62,11 @@ type DiamondContextValue = {
};

export const DiamondContext = React.createContext<DiamondContextValue | null>(
null,
null
);

type FunctionalChildren = (
contextValue: DiamondContextValue,
contextValue: DiamondContextValue
) => ReactNode | ReactNode[];

type Props = {
Expand Down Expand Up @@ -114,7 +114,7 @@ export const DiamondProvider = ({
? diamond?.chainId
: chainId_
? Number(chainId_)
: undefined,
: undefined
);

const {
Expand Down Expand Up @@ -189,11 +189,11 @@ export const DiamondProvider = ({
}
setProposedCalls((calls) =>
[...calls.filter((existing) => existing.id !== call.id), call].filter(
(c) => c.contract && c.function && c.args !== undefined,
),
(c) => c.contract && c.function && c.args !== undefined
)
);
},
[proposedCallsLoading],
[proposedCallsLoading]
);

const refresh = useCallback(() => {
Expand All @@ -204,7 +204,7 @@ export const DiamondProvider = ({
(id: string, listener: TransactionListener) => {
setListeners((listeners) => ({ ...listeners, [id]: listener }));
},
[],
[]
);

const invokeListeners = useCallback(
Expand All @@ -213,7 +213,7 @@ export const DiamondProvider = ({
await listener(data);
}
},
[listeners],
[listeners]
);

const value = {
Expand Down Expand Up @@ -251,12 +251,18 @@ export const DiamondProvider = ({
return React.createElement(
DiamondContext.Provider,
{ value },
typeof children === 'function' ? children(value) : children,
typeof children === "function" ? children(value) : children
);
};

export const useDiamondContext = () => {
const context = React.useContext(DiamondContext);
if (!context) throw Error('Must be used within <DiamondProvider>');
return context;
return (
context ||
({
data: {},
isLoading: false,
error: `Must be used within <DiamondProvider>`,
} as unknown as DiamondContextValue)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,42 @@ export const TieredSalesSelector = ({
hideSoldOutTiers = false,
}: TieredSalesSelectorProps) => {
const {
data: { chainId, autoDetectedTierId, currentTierId, tiers },
data: { chainId, autoDetectedTierId, currentTierId, tiers: contractTiers },
isLoading: { tiersLoading, isAutoDetectingTier },
setCurrentTierId,
} = useTieredSalesContext();

const chainInfo = useChainInfo(chainId);

const {
data: { configValues },
data: { diamond, configValues },
} = useDiamondContext();

const visibleTiers = Object.entries(tiers || {}).filter(([tierId, tier]) => {
if (hideNotEligibleTiers && !tier?.isEligible) {
return false;
}
if (hideNotActiveTiers && !tier?.isActive) {
return false;
}
if (hideSoldOutTiers && tier?.remainingSupply !== undefined) {
return BigNumber.from(tier?.remainingSupply).gt(0);
}
return true;
});
const diamondConfigTiers = diamond?.config?.['admin:tiered-sales']
?.tiers as Record<string, Tier>;
const configValuesTiers = configValues?.['admin:tiered-sales']
?.tiers as Record<string, Tier>;
const finalTiers =
contractTiers && Object.values(contractTiers).length
? contractTiers
: configValuesTiers && Object.values(configValuesTiers).length
? configValuesTiers
: diamondConfigTiers;

const visibleTiers = Object.entries(finalTiers || {}).filter(
([tierId, tier]) => {
if (hideNotEligibleTiers && !tier?.isEligible) {
return false;
}
if (hideNotActiveTiers && !tier?.isActive) {
return false;
}
if (hideSoldOutTiers && tier?.remainingSupply !== undefined) {
return BigNumber.from(tier?.remainingSupply).gt(0);
}
return true;
},
);

useEffect(() => {
if (
Expand All @@ -118,15 +131,15 @@ export const TieredSalesSelector = ({
if (visibleTiers.length > 0) {
setCurrentTierId(autoDetectedTierId || visibleTiers[0][0]);
} else {
setCurrentTierId(autoDetectedTierId || Object.keys(tiers || {})[0]);
setCurrentTierId(autoDetectedTierId || Object.keys(finalTiers || {})[0]);
}
}, [
visibleTiers,
autoDetectedTierId,
currentTierId,
isAutoDetectingTier,
setCurrentTierId,
tiers,
finalTiers,
]);

const renderLabel = labelElement
Expand Down

0 comments on commit 2c35e90

Please sign in to comment.