diff --git a/pages/_app.tsx b/pages/_app.tsx index 17e6d933..cc58e5f5 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,7 +1,7 @@ import { ApolloProvider } from '@apollo/client' import Bugsnag from '@bugsnag/js' import BugsnagPluginReact from '@bugsnag/plugin-react' -import { Box, ChakraProvider } from '@chakra-ui/react' +import { Box, ChakraProvider, useConst } from '@chakra-ui/react' import { LiteflowProvider } from '@liteflow/react' import { RainbowKitProvider, lightTheme } from '@rainbow-me/rainbowkit' import '@rainbow-me/rainbowkit/styles.css' @@ -170,12 +170,12 @@ function AccountProvider({ export type MyAppProps = { jwt: string | null - now: Date environment: Environment } function MyApp({ Component, pageProps }: AppProps): JSX.Element { const { environment } = pageProps + const mountTime = useConst(() => new Date()) const [errorCode, setErrorCode] = useState() const router = useRouter() dayjs.locale(router.locale) @@ -249,7 +249,7 @@ function MyApp({ Component, pageProps }: AppProps): JSX.Element { {errorCode ? ( ) : ( - + )} @@ -274,15 +274,11 @@ MyApp.getInitialProps = async ( appContext, )) as AppInitialProps<{}> // force type of props to empty object instead of any so TS will properly require MyAppProps to be returned by this function const jwt = appContext.ctx.req?.cookies?.[COOKIE_JWT_TOKEN] || null - // Generate the now time, rounded to the second to avoid re-rendering on the client - // TOFIX: find a better way to share the time between the app and document - const now = new Date(Math.floor(Date.now() / 1000) * 1000) return { ...initialProps, pageProps: { ...initialProps.pageProps, jwt, - now, environment: await getEnvironment(), }, } diff --git a/pages/_document.tsx b/pages/_document.tsx index 63c0096f..210ccd4f 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -64,14 +64,11 @@ class MyDocument extends Document { true, console.error, ) - // Generate the now time, rounded to the second to avoid re-rendering on the client - // TOFIX: find a better way to share the time between the app and document - const now = new Date(Math.floor(Date.now() / 1000) * 1000) // properly type the AppTree with the props from MyApp const AppTree = context.AppTree as typeof context.AppTree & ComponentType> // This renders the page and wait for all requests to be resolved - await getDataFromTree() // This `defaultGetInitialProps` should be as late as possible and after the data are resolved by `getDataFromTree` + await getDataFromTree() // This `defaultGetInitialProps` should be as late as possible and after the data are resolved by `getDataFromTree` const initialProps = await context.defaultGetInitialProps(context) return { ...initialProps, diff --git a/pages/checkout/[id].tsx b/pages/checkout/[id].tsx index c075a745..e87afd16 100644 --- a/pages/checkout/[id].tsx +++ b/pages/checkout/[id].tsx @@ -37,7 +37,7 @@ import useSigner from '../../hooks/useSigner' import SmallLayout from '../../layouts/small' type Props = { - now: string + now: Date } const CheckoutPage: NextPage = ({ now }) => { @@ -49,13 +49,12 @@ const CheckoutPage: NextPage = ({ now }) => { const { address } = useAccount() - const date = useMemo(() => new Date(now), [now]) const { data: offerData } = useCheckoutQuery({ variables: { id: offerId } }) const offer = offerData?.offer const { data: assetData } = useFetchAssetForCheckoutQuery({ variables: { - now: date, + now, address: address || '', chainId: offer?.asset.chainId || 0, collectionAddress: offer?.asset.collectionAddress || '', diff --git a/pages/collection/[chainId]/[id].tsx b/pages/collection/[chainId]/[id].tsx index 47575ca3..311565e1 100644 --- a/pages/collection/[chainId]/[id].tsx +++ b/pages/collection/[chainId]/[id].tsx @@ -58,7 +58,7 @@ import LargeLayout from '../../../layouts/large' import { removeEmptyFromObject } from '../../../utils' type Props = { - now: string + now: Date } const CollectionPage: FC = ({ now }) => { @@ -73,7 +73,6 @@ const CollectionPage: FC = ({ now }) => { { fallback: 'md' }, ) const { t } = useTranslation('templates') - const date = useMemo(() => new Date(now), [now]) const { address } = useAccount() const { data: collectionData } = useFetchCollectionDetailsQuery({ variables: { @@ -98,13 +97,13 @@ const CollectionPage: FC = ({ now }) => { const { data: assetData } = useFetchCollectionAssetsQuery({ variables: { collectionAddress, - now: date, + now, currentAccount: address || '', limit, offset, orderBy, chainId: chainId, - filter: convertFilterToAssetFilter(filter, date), + filter: convertFilterToAssetFilter(filter, now), }, }) diff --git a/pages/explore/explore.tsx b/pages/explore/explore.tsx index ef43f3b9..b66baeef 100644 --- a/pages/explore/explore.tsx +++ b/pages/explore/explore.tsx @@ -15,7 +15,7 @@ import { import { NextPage } from 'next' import useTranslation from 'next-translate/useTranslation' import { useRouter } from 'next/router' -import { useCallback, useMemo } from 'react' +import { useCallback } from 'react' import Empty from '../../components/Empty/Empty' import ExploreTemplate from '../../components/Explore' import FilterAsset, { NoFilter } from '../../components/Filter/FilterAsset' @@ -46,7 +46,7 @@ import usePaginateQuery from '../../hooks/usePaginateQuery' import { removeEmptyFromObject } from '../../utils' type Props = { - now: string + now: Date } const ExplorePage: NextPage = ({ now }) => { @@ -57,19 +57,18 @@ const ExplorePage: NextPage = ({ now }) => { { fallback: 'md' }, ) const { t } = useTranslation('templates') - const date = useMemo(() => new Date(now), [now]) const { address } = useAccount() const filter = useAssetFilterFromQuery() const orderBy = useOrderByQuery('CREATED_AT_DESC') const { page, limit, offset } = usePaginateQuery() const { data: assetsData } = useFetchAllErc721And1155Query({ variables: { - now: date, + now, address: address || '', limit, offset, orderBy, - filter: convertFilterToAssetFilter(filter, date), + filter: convertFilterToAssetFilter(filter, now), }, }) diff --git a/pages/index.tsx b/pages/index.tsx index 2c73074b..47d9f233 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -6,24 +6,23 @@ import FeaturedHomeSection from 'components/HomeSection/Featured' import ResourcesHomeSection from 'components/HomeSection/Resources' import UsersHomeSection from 'components/HomeSection/Users' import { NextPage } from 'next' -import { useMemo } from 'react' import Head from '../components/Head' import LargeLayout from '../layouts/large' type Props = { - now: number + now: Date } + const HomePage: NextPage = ({ now }) => { - const date = useMemo(() => new Date(now), [now]) return ( - + - - + + diff --git a/pages/tokens/[id]/bid.tsx b/pages/tokens/[id]/bid.tsx index 7f665468..01f06da5 100644 --- a/pages/tokens/[id]/bid.tsx +++ b/pages/tokens/[id]/bid.tsx @@ -42,7 +42,7 @@ import useSigner from '../../../hooks/useSigner' import SmallLayout from '../../../layouts/small' type Props = { - now: string + now: Date } const BidPage: NextPage = ({ now }) => { @@ -60,13 +60,12 @@ const BidPage: NextPage = ({ now }) => { ) invariant(chainId && collectionAddress && tokenId, 'Invalid asset id') - const date = useMemo(() => new Date(now), [now]) const { data } = useBidOnAssetQuery({ variables: { chainId: parseInt(chainId, 10), collectionAddress: collectionAddress, tokenId: tokenId, - now: date, + now, address: address || '', }, }) diff --git a/pages/tokens/[id]/index.tsx b/pages/tokens/[id]/index.tsx index 052a0cdc..f05fc045 100644 --- a/pages/tokens/[id]/index.tsx +++ b/pages/tokens/[id]/index.tsx @@ -62,7 +62,7 @@ import LargeLayout from '../../../layouts/large' import { formatError } from '../../../utils' type Props = { - now: string + now: Date } enum AssetTabs { @@ -72,7 +72,7 @@ enum AssetTabs { const tabs = [AssetTabs.bids, AssetTabs.history] -const DetailPage: NextPage = ({ now: nowProp }) => { +const DetailPage: NextPage = ({ now }) => { const { CHAINS, REPORT_EMAIL } = useEnvironment() const signer = useSigner() const { t } = useTranslation('templates') @@ -87,13 +87,12 @@ const DetailPage: NextPage = ({ now: nowProp }) => { invariant(tokenId, 'tokenId is required') const chainId = parseInt(_chainId, 10) - const date = useMemo(() => new Date(nowProp), [nowProp]) const { data, refetch } = useFetchAssetQuery({ variables: { chainId, collectionAddress, tokenId, - now: date, + now, address: address || '', }, }) diff --git a/pages/tokens/[id]/offer.tsx b/pages/tokens/[id]/offer.tsx index 7aa7e667..7763d83a 100644 --- a/pages/tokens/[id]/offer.tsx +++ b/pages/tokens/[id]/offer.tsx @@ -46,14 +46,7 @@ import SmallLayout from '../../../layouts/small' import { isSameAddress } from '../../../utils' type Props = { - assetId: string - now: string - currentAccount: string | null - meta: { - title: string - description: string - image: string - } + now: Date } enum SaleType { @@ -83,13 +76,12 @@ const OfferPage: NextPage = ({ now }) => { ) invariant(chainId && collectionAddress && tokenId, 'Invalid asset id') - const date = useMemo(() => new Date(now), [now]) const { data } = useOfferForAssetQuery({ variables: { chainId: parseInt(chainId, 10), collectionAddress: collectionAddress, tokenId: tokenId, - now: date, + now, address: address || '', }, }) diff --git a/pages/users/[id]/bids/received.tsx b/pages/users/[id]/bids/received.tsx index 4bae9f6a..4a1ee504 100644 --- a/pages/users/[id]/bids/received.tsx +++ b/pages/users/[id]/bids/received.tsx @@ -47,7 +47,7 @@ import LargeLayout from '../../../../layouts/large' import { dateFromNow, formatError } from '../../../../utils' type Props = { - now: string + now: Date } const BidReceivedPage: NextPage = ({ now }) => { @@ -63,14 +63,13 @@ const BidReceivedPage: NextPage = ({ now }) => { const userAddress = useRequiredQueryParamSingle('id') const ownerLoggedIn = useIsLoggedIn(userAddress) - const date = useMemo(() => new Date(now), [now]) const { data, refetch } = useFetchUserBidsReceivedQuery({ variables: { address: userAddress, limit, offset, orderBy, - now: date, + now, }, }) diff --git a/pages/users/[id]/created.tsx b/pages/users/[id]/created.tsx index 9b2aded5..f6350001 100644 --- a/pages/users/[id]/created.tsx +++ b/pages/users/[id]/created.tsx @@ -25,7 +25,7 @@ import useSigner from '../../../hooks/useSigner' import LargeLayout from '../../../layouts/large' type Props = { - now: string + now: Date } const CreatedPage: NextPage = ({ now }) => { @@ -39,7 +39,6 @@ const CreatedPage: NextPage = ({ now }) => { const { address } = useAccount() const userAddress = useRequiredQueryParamSingle('id') - const date = useMemo(() => new Date(now), [now]) const { data } = useFetchCreatedAssetsQuery({ variables: { address: userAddress, @@ -47,7 +46,7 @@ const CreatedPage: NextPage = ({ now }) => { limit, offset, orderBy, - now: date, + now, }, }) diff --git a/pages/users/[id]/on-sale.tsx b/pages/users/[id]/on-sale.tsx index cc36f8ee..7e3e4039 100644 --- a/pages/users/[id]/on-sale.tsx +++ b/pages/users/[id]/on-sale.tsx @@ -25,7 +25,7 @@ import useSigner from '../../../hooks/useSigner' import LargeLayout from '../../../layouts/large' type Props = { - now: string + now: Date } const OnSalePage: NextPage = ({ now }) => { @@ -39,7 +39,6 @@ const OnSalePage: NextPage = ({ now }) => { const { address } = useAccount() const userAddress = useRequiredQueryParamSingle('id') - const date = useMemo(() => new Date(now), [now]) const { data } = useFetchOnSaleAssetsQuery({ variables: { address: userAddress, @@ -47,7 +46,7 @@ const OnSalePage: NextPage = ({ now }) => { limit, offset, orderBy, - now: date, + now, }, }) diff --git a/pages/users/[id]/owned.tsx b/pages/users/[id]/owned.tsx index 9c2af8d7..b22891d9 100644 --- a/pages/users/[id]/owned.tsx +++ b/pages/users/[id]/owned.tsx @@ -25,7 +25,7 @@ import useSigner from '../../../hooks/useSigner' import LargeLayout from '../../../layouts/large' type Props = { - now: string + now: Date } const OwnedPage: NextPage = ({ now }) => { @@ -39,7 +39,6 @@ const OwnedPage: NextPage = ({ now }) => { const { address } = useAccount() const userAddress = useRequiredQueryParamSingle('id') - const date = useMemo(() => new Date(now), [now]) const { data } = useFetchOwnedAssetsQuery({ variables: { address: userAddress, @@ -47,7 +46,7 @@ const OwnedPage: NextPage = ({ now }) => { limit, offset, orderBy, - now: date, + now, }, })