Skip to content

Commit

Permalink
removed unnecessary loading state on first page load and
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jun 4, 2024
1 parent a2786d5 commit 417c945
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 115 deletions.
7 changes: 6 additions & 1 deletion packages/state/recoil/atoms/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { atom } from 'recoil'

import { Web3AuthPrompt } from '@dao-dao/types'
import { PageHeaderProps, Web3AuthPrompt } from '@dao-dao/types'

import { localStorageEffectJSON } from '../effects'

Expand Down Expand Up @@ -57,3 +57,8 @@ export const indexerWebSocketChannelSubscriptionsAtom = atom<
key: 'indexerWebSocketChannelSubscriptions',
default: {},
})

export const pageHeaderPropsAtom = atom<PageHeaderProps>({
key: 'pageHeaderProps',
default: {},
})
12 changes: 6 additions & 6 deletions packages/state/recoil/selectors/treasury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Account,
AccountType,
AmountWithTimestamp,
ChainId,
GenericToken,
GenericTokenSource,
LoadingTokens,
Expand All @@ -18,9 +17,9 @@ import {
} from '@dao-dao/types'
import {
COMMUNITY_POOL_ADDRESS_PLACEHOLDER,
NEUTRON_GOVERNANCE_DAO,
convertMicroDenomToDenomWithDecimals,
getNativeTokenForChainId,
getSupportedChainConfig,
getTokenForChainIdAndDenom,
isConfiguredChainName,
loadableToLoadingData,
Expand Down Expand Up @@ -180,10 +179,11 @@ export const daoTvlSelector = selectorFamily<
({ get }) => {
// Native chain x/gov module.
if (isConfiguredChainName(chainId, coreAddress)) {
coreAddress =
chainId === ChainId.NeutronMainnet
? NEUTRON_GOVERNANCE_DAO
: COMMUNITY_POOL_ADDRESS_PLACEHOLDER
// If chain uses a contract-based DAO, load it instead.
const govContractAddress =
getSupportedChainConfig(chainId)?.govContractAddress

coreAddress = govContractAddress || COMMUNITY_POOL_ADDRESS_PLACEHOLDER
}

const timestamp = new Date()
Expand Down
12 changes: 0 additions & 12 deletions packages/stateful/components/AppContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@ export const AppContextProvider = ({
const [responsiveNavigationEnabled, setResponsiveNavigationEnabled] =
useState(false)

// Page header. Set state when ref is set so it re-renders immediately.
// Without this, the page header is invisible until the next render.
const [, setPageHeaderSet] = useState(false)
const pageHeaderRef = useRef<HTMLDivElement | null>(null)
const setPageHeaderRef = useCallback((ref: HTMLDivElement | null) => {
pageHeaderRef.current = ref
if (ref) {
setPageHeaderSet(true)
}
}, [])

// Unsubscribe from WebSocket channels when all subscriptions removed.
const { pusher } = useWebSocket()
Expand Down Expand Up @@ -68,10 +59,7 @@ export const AppContextProvider = ({
enabled: responsiveNavigationEnabled,
toggle: () => setResponsiveNavigationEnabled((v) => !v),
},
// Include the page header ref in the context to be accessed by the
// component portal.
pageHeaderRef,
setPageHeaderRef,
}

// Install service worker.
Expand Down
2 changes: 2 additions & 0 deletions packages/stateful/components/DappLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ButtonLink } from './ButtonLink'
import { DaoCreatedModal } from './DaoCreatedModal'
import { LinkWrapper } from './LinkWrapper'
import { DockWallet } from './NavWallet'
import { StatefulPageHeader } from './PageHeader'
import { WalletModals } from './wallet'

export const DappLayout = ({ children }: { children: ReactNode }) => {
Expand Down Expand Up @@ -146,6 +147,7 @@ export const DappLayout = ({ children }: { children: ReactNode }) => {
<StatelessDappLayout
ButtonLink={ButtonLink}
DockWallet={DockWallet}
PageHeader={StatefulPageHeader}
connect={openView}
inboxCount={
inbox.loading ||
Expand Down
23 changes: 23 additions & 0 deletions packages/stateful/components/PageHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useRecoilValue } from 'recoil'

import { pageHeaderPropsAtom } from '@dao-dao/state/recoil'
import { PageHeader } from '@dao-dao/stateless'

import { HeaderWallet } from './NavWallet'

export const StatefulPageHeader = () => {
const props = useRecoilValue(pageHeaderPropsAtom)

return (
<PageHeader
{...props}
rightNode={
<div className="flex flex-row items-center justify-end gap-3 lg:gap-4">
{props.rightNode}

<HeaderWallet />
</div>
}
/>
)
}
69 changes: 11 additions & 58 deletions packages/stateful/components/PageHeaderContent.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
import { createPortal } from 'react-dom'
import { useEffect } from 'react'
import { useSetRecoilState } from 'recoil'

import {
PageHeader,
PageLoader,
useAppContextIfAvailable,
} from '@dao-dao/stateless'
import { pageHeaderPropsAtom } from '@dao-dao/state/recoil'
import { PageHeaderProps } from '@dao-dao/types'

import { HeaderWallet } from './NavWallet'

// This is a portal that teleports content, used in pages to render content in
// the header (which is located in a separate React tree due to `DappLayout` and
// `SdaLayout` managing the layouts of the apps). This component uses a
// reference to the target div (stored in the `AppContext`) and provides a
// component that will funnel its `children` into a React portal
// (https://reactjs.org/docs/portals.html). This ref is accessible via the
// `useAppContext` hook so that descendants of the context provider (such as
// page components) can use the `PageHeaderContent` component below and specify
// what renders in the header. The API for a page is as simple as:
//
// export const Page = () => (
// <>
// <PageHeaderContent title="Page Title" />

// {/* ... Page content here ... */}
// </>
// )
//
// See https://malcolmkee.com/blog/portal-to-subtree/ for an example using
// portals to render components across subtrees with similar syntax.
//
// If not in an AppContext, this component will render a PageHeader normally
// instead of using the portal.
/**
* This is a component that sets props used by the page header.
*/
export const PageHeaderContent = (props: PageHeaderProps) => {
const appContext = useAppContextIfAvailable()

const pageHeader = (
<PageHeader
{...props}
rightNode={
<div className="flex flex-row items-center justify-end gap-3 lg:gap-4">
{props.rightNode}

<HeaderWallet />
</div>
}
/>
)
const setPageHeaderProps = useSetRecoilState(pageHeaderPropsAtom)
useEffect(() => {
setPageHeaderProps(props)
}, [setPageHeaderProps, props])

// If app context is available, but the page header ref is not, render nothing
// until the ref is available. If not in an app context, render the element
// directly. The direct render is useful when outside the AppContext, such as
// error pages in the SDA.
return appContext ? (
appContext.pageHeaderRef.current ? (
createPortal(pageHeader, appContext.pageHeaderRef.current)
) : (
<PageLoader className="absolute top-0 right-0 bottom-0 left-0 z-50 bg-background-base" />
)
) : (
pageHeader
)
return null
}
2 changes: 2 additions & 0 deletions packages/stateful/components/SdaLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useAutoRefreshData, useDaoTabs } from '../hooks'
import { daoCreatedCardPropsAtom } from '../recoil/atoms/newDao'
import { LinkWrapper } from './LinkWrapper'
import { SidebarWallet } from './NavWallet'
import { StatefulPageHeader } from './PageHeader'
import { SuspenseLoader } from './SuspenseLoader'
import { WalletModals } from './wallet'

Expand All @@ -42,6 +43,7 @@ export const SdaLayout = ({ children }: { children: ReactNode }) => {

return (
<StatelessSdaLayout
PageHeader={StatefulPageHeader}
navigationProps={{
tabs: loadingTabs.loading ? [] : loadingTabs.data,
LinkWrapper,
Expand Down
1 change: 1 addition & 0 deletions packages/stateful/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './EntityDisplay'
export * from './IconButtonLink'
export * from './LinkWrapper'
export * from './NavWallet'
export * from './PageHeader'
export * from './PageHeaderContent'
export * from './PayEntityDisplay'
export * from './PfpkNftSelectionModal'
Expand Down
10 changes: 5 additions & 5 deletions packages/stateful/recoil/selectors/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
reverseLookupPolytoneProxySelector,
} from '@dao-dao/state'
import {
ChainId,
DaoCardLazyData,
DaoInfo,
DaoPageMode,
Expand All @@ -42,7 +41,6 @@ import {
} from '@dao-dao/types'
import {
DaoVotingCw20StakedAdapterId,
NEUTRON_GOVERNANCE_DAO,
getDaoInfoForChainId,
getDaoProposalPath,
getFallbackImage,
Expand Down Expand Up @@ -75,9 +73,11 @@ export const daoCardLazyDataSelector = selectorFamily<

// Native chain x/gov module.
if (isConfiguredChainName(chainId, coreAddress)) {
// Neutron uses an actual DAO so load it instead.
if (chainId === ChainId.NeutronMainnet) {
coreAddress = NEUTRON_GOVERNANCE_DAO
// If chain uses a contract-based DAO, load it instead.
const govContractAddress =
getSupportedChainConfig(chainId)?.govContractAddress
if (govContractAddress) {
coreAddress = govContractAddress
} else {
// Get proposal count by loading one proposal and getting the total.
const { total: proposalCount } = get(
Expand Down
2 changes: 1 addition & 1 deletion packages/stateless/components/layout/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const Breadcrumbs = ({
<>
<div
className={clsx(
'header-text flex flex-row items-center gap-2 overflow-hidden text-text-secondary',
'header-text flex flex-row items-center gap-2 overflow-hidden text-text-secondary animate-fade-in',
className
)}
>
Expand Down
11 changes: 5 additions & 6 deletions packages/stateless/components/layout/DappLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ export const DefaultArgs: DappLayoutProps = {
data: 5,
},
connect: () => alert('connect'),
PageHeader: () => (
<PageHeader {...(PageHeaderStory.args as PageHeaderProps)} />
),
DockWallet,
ButtonLink,
children: (
<div className="flex h-full flex-col px-6">
<PageHeader {...(PageHeaderStory.args as PageHeaderProps)} />

<div className="flex grow items-center justify-center">
<p>App content</p>
</div>
<div className="flex grow h-full px-6 items-center justify-center">
<p>App content</p>
</div>
),
}
Expand Down
7 changes: 5 additions & 2 deletions packages/stateless/components/layout/DappLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const DappLayout = ({
navigationProps,
inboxCount,
connect,
PageHeader,
DockWallet,
ButtonLink,
children,
Expand All @@ -34,7 +35,7 @@ export const DappLayout = ({
getDaoPath,
getDaoFromPath,
} = useDaoNavHelpers()
const { responsiveNavigation, setPageHeaderRef } = useAppContext()
const { responsiveNavigation, pageHeaderRef } = useAppContext()

const scrollableContainerRef = useRef<HTMLDivElement>(null)

Expand Down Expand Up @@ -123,7 +124,9 @@ export const DappLayout = ({
: 'opacity-100'
)}
>
<div className="shrink-0 px-6" ref={setPageHeaderRef}></div>
<div className="shrink-0 px-6" ref={pageHeaderRef}>
<PageHeader />
</div>

<div
className={clsx(
Expand Down
2 changes: 1 addition & 1 deletion packages/stateless/components/layout/PageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const PageHeader = ({
{!!title && (
<p
className={clsx(
'header-text truncate text-lg leading-[5rem] sm:text-xl',
'header-text truncate text-lg leading-[5rem] sm:text-xl animate-fade-in',
titleClassName
)}
>
Expand Down
11 changes: 5 additions & 6 deletions packages/stateless/components/layout/SdaLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ export default {

export const DefaultArgs: SdaLayoutProps = {
navigationProps: SdaNavigationStory.args as SdaNavigationProps,
PageHeader: () => (
<PageHeader {...(PageHeaderStory.args as PageHeaderProps)} />
),
children: (
<div className="flex h-full flex-col px-6">
<PageHeader {...(PageHeaderStory.args as PageHeaderProps)} />

<div className="flex grow items-center justify-center">
<p>App content</p>
</div>
<div className="flex grow px-6 h-full items-center justify-center">
<p>App content</p>
</div>
),
}
Expand Down
12 changes: 9 additions & 3 deletions packages/stateless/components/layout/SdaLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import { ErrorBoundary } from '../error/ErrorBoundary'
import { useAppContext } from './AppContext'
import { SdaNavigation } from './SdaNavigation'

export const SdaLayout = ({ navigationProps, children }: SdaLayoutProps) => {
export const SdaLayout = ({
navigationProps,
PageHeader,
children,
}: SdaLayoutProps) => {
const { pathname } = useRouter()
const { responsiveNavigation, setPageHeaderRef } = useAppContext()
const { responsiveNavigation, pageHeaderRef } = useAppContext()

const scrollableContainerRef = useRef<HTMLDivElement>(null)

Expand Down Expand Up @@ -51,7 +55,9 @@ export const SdaLayout = ({ navigationProps, children }: SdaLayoutProps) => {
: 'opacity-100'
)}
>
<div className="shrink-0 px-6" ref={setPageHeaderRef}></div>
<div className="shrink-0 px-6" ref={pageHeaderRef}>
<PageHeader />
</div>

<div
className={clsx(
Expand Down
10 changes: 1 addition & 9 deletions packages/storybook/decorators/makeAppContextDecorator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DecoratorFn } from '@storybook/react'
import { useCallback, useRef, useState } from 'react'
import { useRef, useState } from 'react'

import { makeGenericContext } from '@dao-dao/stateful/command'
import { AppContext } from '@dao-dao/stateless'
Expand All @@ -15,14 +15,7 @@ export const makeAppContextDecorator: (
useState(defaultResponsiveEnabled)

// Page header.
const [, setPageHeaderSet] = useState(false)
const pageHeaderRef = useRef<HTMLDivElement | null>(null)
const setPageHeaderRef = useCallback((ref: HTMLDivElement | null) => {
if (ref) {
pageHeaderRef.current = ref
setPageHeaderSet(true)
}
}, [])

return (
<AppContext.Provider
Expand All @@ -33,7 +26,6 @@ export const makeAppContextDecorator: (
toggle: () => setResponsiveNavigationEnabled((v) => !v),
},
pageHeaderRef,
setPageHeaderRef,
rootCommandContextMaker: makeGenericContext,
setRootCommandContextMaker: () => {},
inbox: EMPTY_INBOX,
Expand Down
Loading

0 comments on commit 417c945

Please sign in to comment.