Skip to content

Commit

Permalink
⚡️ perf: refactor the responsive layout to improve performance (lobeh…
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Mar 12, 2024
1 parent a4023b7 commit 9f1635e
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 63 deletions.
12 changes: 8 additions & 4 deletions src/app/chat/(desktop)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import dynamic from 'next/dynamic';
import { FC, memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import ResponsiveIndex from '@/components/ResponsiveIndex';
import ResponsiveContainer from '@/components/ResponsiveContainer';
import MobileSwitchLoading from '@/features/MobileSwitchLoading';

import PageTitle from '../features/PageTitle';
import ChatHeader from './features/ChatHeader';
import Conversation from './features/Conversation';
import SideBar from './features/SideBar';
import Layout from './layout.desktop';

const Mobile: FC = dynamic(() => import('../(mobile)'), { ssr: false }) as FC;
const Mobile: FC = dynamic(() => import('../(mobile)'), {
loading: MobileSwitchLoading,
ssr: false,
}) as FC;

const DesktopPage = memo(() => (
<ResponsiveIndex Mobile={Mobile}>
<ResponsiveContainer Mobile={Mobile}>
<Layout>
<PageTitle />
<ChatHeader />
Expand All @@ -24,6 +28,6 @@ const DesktopPage = memo(() => (
<SideBar />
</Flexbox>
</Layout>
</ResponsiveIndex>
</ResponsiveContainer>
));
export default DesktopPage;
12 changes: 8 additions & 4 deletions src/app/chat/settings/(desktop)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import dynamic from 'next/dynamic';
import { FC, memo } from 'react';

import SessionHydration from '@/app/chat/components/SessionHydration';
import ResponsiveIndex from '@/components/ResponsiveIndex';
import ResponsiveContainer from '@/components/ResponsiveContainer';
import MobileSwitchLoading from '@/features/MobileSwitchLoading';

import EditPage from '../features/EditPage';
import Layout from './layout.desktop';

const Mobile: FC = dynamic(() => import('../(mobile)'), { ssr: false }) as FC;
const Mobile: FC = dynamic(() => import('../(mobile)'), {
loading: MobileSwitchLoading,
ssr: false,
}) as FC;

const ChatSettings = memo(() => (
<>
<ResponsiveIndex Mobile={Mobile}>
<ResponsiveContainer Mobile={Mobile}>
<Layout>
<EditPage />
</Layout>
</ResponsiveIndex>
</ResponsiveContainer>
<SessionHydration />
</>
));
Expand Down
14 changes: 9 additions & 5 deletions src/app/market/(desktop)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import { SpotlightCard, SpotlightCardProps } from '@lobehub/ui';
import dynamic from 'next/dynamic';
import { FC, memo } from 'react';

import AgentCard from '@/app/market/features/AgentCard';
import ResponsiveIndex from '@/components/ResponsiveIndex';
import ResponsiveContainer from '@/components/ResponsiveContainer';
import MobileSwitchLoading from '@/features/MobileSwitchLoading';

import AgentCard from '../features/AgentCard';
import Index from '../index';
import Layout from './layout.desktop';

const Mobile: FC = dynamic(() => import('../(mobile)'), { ssr: false }) as FC;
const Mobile: FC = dynamic(() => import('../(mobile)'), {
loading: MobileSwitchLoading,
ssr: false,
}) as FC;

export default memo(() => (
<ResponsiveIndex Mobile={Mobile}>
<ResponsiveContainer Mobile={Mobile}>
<Layout>
<Index />
<AgentCard CardRender={SpotlightCard as FC<SpotlightCardProps>} />
</Layout>
</ResponsiveIndex>
</ResponsiveContainer>
));
12 changes: 8 additions & 4 deletions src/app/settings/(desktop)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import dynamic from 'next/dynamic';
import { FC, memo } from 'react';

import ResponsiveIndex from '@/components/ResponsiveIndex';
import ResponsiveContainer from '@/components/ResponsiveContainer';
import MobileSwitchLoading from '@/features/MobileSwitchLoading';
import { SettingsTabs } from '@/store/global/initialState';

import Common from '../common';
import { SettingsCommonProps } from '../common/Common';
import DesktopLayout from './layout.desktop';

const Mobile: FC = dynamic(() => import('../(mobile)'), { ssr: false }) as FC;
const Mobile: FC = dynamic(() => import('../(mobile)'), {
loading: MobileSwitchLoading,
ssr: false,
}) as FC;

export default memo<SettingsCommonProps>((props) => (
<ResponsiveIndex Mobile={Mobile}>
<ResponsiveContainer Mobile={Mobile}>
<DesktopLayout activeTab={SettingsTabs.Common}>
<Common {...props} />
</DesktopLayout>
</ResponsiveIndex>
</ResponsiveContainer>
));
9 changes: 7 additions & 2 deletions src/app/settings/(desktop)/layout.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import dynamic from 'next/dynamic';
import { FC, memo } from 'react';

import ResponsiveLayout from '@/layout/ResponsiveLayout.client';
import MobileSwitchLoading from '@/features/MobileSwitchLoading';
// TODO: need to be removed
import ResponsiveLayout from '@/layout/ServerResponsiveLayout/Client';

import DesktopLayout, { DesktopLayoutProps } from './layout.desktop';

const MobileLayout = dynamic(() => import('../(mobile)/layout.mobile'), { ssr: false }) as FC;
const MobileLayout = dynamic(() => import('../(mobile)/layout.mobile'), {
loading: MobileSwitchLoading,
ssr: false,
}) as FC;

export default memo<DesktopLayoutProps>(({ children, activeTab }) => (
<ResponsiveLayout Desktop={DesktopLayout} Mobile={MobileLayout} activeTab={activeTab}>
Expand Down
2 changes: 1 addition & 1 deletion src/app/settings/layout.server.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PropsWithChildren, ReactNode, memo } from 'react';

import ResponsiveLayout from '@/layout/ResponsiveLayout.server';
import ResponsiveLayout from '@/layout/ServerResponsiveLayout';
import { SettingsTabs } from '@/store/global/initialState';

import DesktopLayout from './(desktop)/layout.responsive';
Expand Down
12 changes: 8 additions & 4 deletions src/app/welcome/(desktop)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import dynamic from 'next/dynamic';
import { FC, memo } from 'react';

import ResponsiveIndex from '@/components/ResponsiveIndex';
import ResponsiveContainer from '@/components/ResponsiveContainer';
import MobileSwitchLoading from '@/features/MobileSwitchLoading';

import Footer from './features/Footer';
import Showcase from './features/Showcase';
import Layout from './layout.desktop';

const Mobile: FC = dynamic(() => import('../(mobile)'), { ssr: false }) as FC;
const Mobile: FC = dynamic(() => import('../(mobile)'), {
loading: MobileSwitchLoading,
ssr: false,
}) as FC;

export default memo(() => (
<ResponsiveIndex Mobile={Mobile}>
<ResponsiveContainer Mobile={Mobile}>
<Layout>
<Showcase />
<Footer />
</Layout>
</ResponsiveIndex>
</ResponsiveContainer>
));
20 changes: 20 additions & 0 deletions src/components/ResponsiveContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client';

import { FC, ReactNode, memo } from 'react';

import { useIsMobile } from '@/hooks/useIsMobile';

interface ResponsiveContainerProps {
Mobile: FC;
children: ReactNode;
}

const ResponsiveContainer = memo(({ children, Mobile }: ResponsiveContainerProps) => {
const mobile = useIsMobile();

return mobile ? <Mobile /> : children;
});

ResponsiveContainer.displayName = 'ResponsiveContainer';

export default ResponsiveContainer;
26 changes: 0 additions & 26 deletions src/components/ResponsiveIndex/index.tsx

This file was deleted.

11 changes: 11 additions & 0 deletions src/features/MobileSwitchLoading/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useTranslation } from 'react-i18next';

import FullscreenLoading from '@/components/FullscreenLoading';

const MobileSwitchLoading = () => {
const { t } = useTranslation('common');

return <FullscreenLoading title={t('layoutInitializing')} />;
};

export default MobileSwitchLoading;
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use client';

import { ReactNode, Suspense, memo } from 'react';
import { useTranslation } from 'react-i18next';
import { ReactNode, memo } from 'react';

import FullscreenLoading from '@/components/FullscreenLoading';
import { useIsMobile } from '@/hooks/useIsMobile';

interface ServerResponsiveLayoutProps extends Record<string, any> {
Expand All @@ -14,16 +12,9 @@ interface ServerResponsiveLayoutProps extends Record<string, any> {

const ResponsiveLayout = memo(
({ children, Desktop, Mobile, ...res }: ServerResponsiveLayoutProps) => {
const { t } = useTranslation();
const mobile = useIsMobile();

return mobile ? (
<Suspense fallback={<FullscreenLoading title={t('layoutInitializing', { ns: 'common' })} />}>
<Mobile {...res}>{children}</Mobile>
</Suspense>
) : (
<Desktop {...res}>{children}</Desktop>
);
return mobile ? <Mobile {...res}>{children}</Mobile> : <Desktop {...res}>{children}</Desktop>;
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ interface ServerResponsiveLayoutProps extends Record<string, any> {
Mobile: (props: any) => ReactNode;
children?: ReactNode;
}
const ResponsiveLayout = ({ children, Desktop, Mobile, ...res }: ServerResponsiveLayoutProps) => {
const ServerResponsiveLayout = ({
children,
Desktop,
Mobile,
...res
}: ServerResponsiveLayoutProps) => {
const mobile = isMobileDevice();

return mobile ? <Mobile {...res}>{children}</Mobile> : <Desktop {...res}>{children}</Desktop>;
};

export default ResponsiveLayout;
export default ServerResponsiveLayout;

0 comments on commit 9f1635e

Please sign in to comment.