Skip to content

Commit

Permalink
♻️ refactor: refactor the sidebar to fix first render state (lobehub#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Feb 14, 2024
1 parent 9593382 commit c477491
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 102 deletions.
4 changes: 1 addition & 3 deletions src/app/chat/(desktop)/layout.desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import { PropsWithChildren, memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import AppLayoutDesktop from '@/layout/AppLayout.desktop';
import { useSwitchSideBarOnInit } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';

import ResponsiveSessionList from './features/SessionList';

export default memo(({ children }: PropsWithChildren) => {
useSwitchSideBarOnInit(SidebarTabKey.Chat);
return (
<AppLayoutDesktop>
<AppLayoutDesktop sidebarKey={SidebarTabKey.Chat}>
<ResponsiveSessionList />
<Flexbox
flex={1}
Expand Down
7 changes: 2 additions & 5 deletions src/app/chat/(mobile)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import { useRouter } from 'next/navigation';
import { memo, useEffect } from 'react';

import AppLayoutMobile from '@/layout/AppLayout.mobile';
import { useSwitchSideBarOnInit } from '@/store/global';
import { SidebarTabKey } from '@/store/global/slices/common/initialState';
import { SidebarTabKey } from '@/store/global/initialState';

import PageTitle from '../features/PageTitle';
import SessionHeader from './features/SessionHeader';
import SessionList from './features/SessionList';

const ChatMobilePage = memo(() => {
useSwitchSideBarOnInit(SidebarTabKey.Chat);

const router = useRouter();
useEffect(() => {
router.prefetch('/chat/mobile');
router.prefetch('/settings');
}, []);

return (
<AppLayoutMobile navBar={<SessionHeader />} showTabBar>
<AppLayoutMobile navBar={<SessionHeader />} showTabBar tabBarKey={SidebarTabKey.Chat}>
<PageTitle />
<SessionList />
</AppLayoutMobile>
Expand Down
2 changes: 1 addition & 1 deletion src/app/chat/features/ChatHeader/SettingButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next';

import { DESKTOP_HEADER_ICON_SIZE, MOBILE_HEADER_ICON_SIZE } from '@/const/layoutTokens';
import { useGlobalStore } from '@/store/global';
import { SidebarTabKey } from '@/store/global/slices/common/initialState';
import { SidebarTabKey } from '@/store/global/initialState';
import { useSessionStore } from '@/store/session';
import { sessionSelectors } from '@/store/session/selectors';
import { pathString } from '@/utils/url';
Expand Down
4 changes: 0 additions & 4 deletions src/app/home/Redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { memo, useEffect } from 'react';

import { messageService } from '@/services/message';
import { sessionService } from '@/services/session';
import { useGlobalStore } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';
import { useSessionStore } from '@/store/session';

const checkHasConversation = async () => {
Expand All @@ -21,8 +19,6 @@ const Redirect = memo(() => {

useEffect(() => {
checkHasConversation().then((hasData) => {
useGlobalStore.getState().switchSideBar(SidebarTabKey.Chat);

if (hasData) {
router.replace('/chat');

Expand Down
5 changes: 1 addition & 4 deletions src/app/market/(desktop)/layout.desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Center, Flexbox } from 'react-layout-kit';
import SafeSpacing from '@/components/SafeSpacing';
import { MAX_WIDTH } from '@/const/layoutTokens';
import AppLayoutDesktop from '@/layout/AppLayout.desktop';
import { useSwitchSideBarOnInit } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';

import Header from './features/Header';
Expand All @@ -30,10 +29,8 @@ const useStyles = createStyles(({ css }) => ({
const MarketLayout = memo<PropsWithChildren>(({ children }) => {
const { theme, styles } = useStyles();

useSwitchSideBarOnInit(SidebarTabKey.Market);

return (
<AppLayoutDesktop>
<AppLayoutDesktop sidebarKey={SidebarTabKey.Market}>
<Flexbox
flex={1}
height={'100%'}
Expand Down
5 changes: 1 addition & 4 deletions src/app/market/(mobile)/layout.mobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import { PropsWithChildren } from 'react';
import { Flexbox } from 'react-layout-kit';

import AppLayoutMobile from '@/layout/AppLayout.mobile';
import { useSwitchSideBarOnInit } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';

import Header from './features/Header';

const DetailModal = dynamic(() => import('./features/AgentDetail'));

const MarketLayout = ({ children }: PropsWithChildren) => {
useSwitchSideBarOnInit(SidebarTabKey.Market);

return (
<AppLayoutMobile navBar={<Header />} showTabBar>
<AppLayoutMobile navBar={<Header />} showTabBar tabBarKey={SidebarTabKey.Market}>
<Flexbox flex={1} gap={16} style={{ padding: 16 }}>
{children}
</Flexbox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { Center } from 'react-layout-kit';

import { useGlobalStore } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';
import { agentMarketSelectors, useMarketStore } from '@/store/market';
import { useSessionStore } from '@/store/session';

Expand All @@ -18,7 +16,6 @@ const Header = memo(() => {
const { t } = useTranslation('market');
const { styles, theme } = useStyles();
const createSession = useSessionStore((s) => s.createSession);
const switchSideBar = useGlobalStore((s) => s.switchSideBar);
const agentItem = useMarketStore(agentMarketSelectors.currentAgentItem);
const { message } = App.useApp();

Expand Down Expand Up @@ -56,7 +53,6 @@ const Header = memo(() => {
if (!agentItem) return;

createSession({ config, meta });
switchSideBar(SidebarTabKey.Chat);
}}
type={'primary'}
>
Expand Down
5 changes: 1 addition & 4 deletions src/app/settings/(desktop)/layout.desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Center, Flexbox } from 'react-layout-kit';

import SafeSpacing from '@/components/SafeSpacing';
import AppLayoutDesktop from '@/layout/AppLayout.desktop';
import { useSwitchSideBarOnInit } from '@/store/global';
import { SettingsTabs, SidebarTabKey } from '@/store/global/initialState';

import Header from './features/Header';
Expand All @@ -17,10 +16,8 @@ export interface DesktopLayoutProps {
}

const DesktopLayout = memo<DesktopLayoutProps>(({ children, activeTab }) => {
useSwitchSideBarOnInit(SidebarTabKey.Setting);

return (
<AppLayoutDesktop>
<AppLayoutDesktop sidebarKey={SidebarTabKey.Setting}>
<SideBar activeTab={activeTab} />
<Flexbox flex={1} height={'100%'} style={{ position: 'relative' }}>
<Header activeTab={activeTab} />
Expand Down
4 changes: 1 addition & 3 deletions src/app/settings/(mobile)/layout.mobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ReactNode, memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import AppLayoutMobile from '@/layout/AppLayout.mobile';
import { useSwitchSideBarOnInit } from '@/store/global';
import { SettingsTabs, SidebarTabKey } from '@/store/global/initialState';

import Header from './features/Header';
Expand All @@ -14,9 +13,8 @@ export interface SettingLayoutProps {
children: ReactNode;
}
export default memo(({ children, activeTab }: SettingLayoutProps) => {
useSwitchSideBarOnInit(SidebarTabKey.Setting);
return (
<AppLayoutMobile navBar={<Header activeTab={activeTab} />}>
<AppLayoutMobile navBar={<Header activeTab={activeTab} />} tabBarKey={SidebarTabKey.Setting}>
<Flexbox style={{ overflow: 'auto' }}>{children}</Flexbox>
</AppLayoutMobile>
);
Expand Down
21 changes: 7 additions & 14 deletions src/features/MobileTabBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { rgba } from 'polished';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';

const useStyles = createStyles(({ css, token }) => ({
Expand All @@ -17,8 +16,12 @@ const useStyles = createStyles(({ css, token }) => ({
`,
}));

export default memo<{ className?: string }>(({ className }) => {
const [tab, setTab] = useGlobalStore((s) => [s.sidebarKey, s.switchSideBar]);
interface Props {
className?: string;
tabBarKey?: SidebarTabKey;
}

export default memo<Props>(({ className, tabBarKey }) => {
const { t } = useTranslation('common');
const { styles } = useStyles();
const router = useRouter();
Expand All @@ -30,7 +33,6 @@ export default memo<{ className?: string }>(({ className }) => {
),
key: SidebarTabKey.Chat,
onClick: () => {
setTab(SidebarTabKey.Chat);
router.push('/chat');
},
title: t('tab.chat'),
Expand All @@ -39,7 +41,6 @@ export default memo<{ className?: string }>(({ className }) => {
icon: (active) => <Icon className={active ? styles.active : undefined} icon={Bot} />,
key: SidebarTabKey.Market,
onClick: () => {
setTab(SidebarTabKey.Market);
router.push('/market');
},
title: t('tab.market'),
Expand All @@ -48,20 +49,12 @@ export default memo<{ className?: string }>(({ className }) => {
icon: (active) => <Icon className={active ? styles.active : undefined} icon={User} />,
key: SidebarTabKey.Setting,
onClick: () => {
setTab(SidebarTabKey.Setting);
router.push('/settings');
},
title: t('tab.setting'),
},
],
[t],
);
return (
<MobileTabBar
activeKey={tab}
className={className}
items={items}
onChange={(key) => setTab(key as any)}
/>
);
return <MobileTabBar activeKey={tabBarKey} className={className} items={items} />;
});
9 changes: 2 additions & 7 deletions src/features/SideBar/BottomActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import { GlobalStore, useGlobalStore } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';

export interface BottomActionProps {
setTab: GlobalStore['switchSideBar'];
tab: GlobalStore['sidebarKey'];
tab?: GlobalStore['sidebarKey'];
}

const BottomActions = memo<BottomActionProps>(({ tab, setTab }) => {
const BottomActions = memo<BottomActionProps>(({ tab }) => {
const router = useRouter();
const { t } = useTranslation('common');

Expand Down Expand Up @@ -113,10 +112,6 @@ const BottomActions = memo<BottomActionProps>(({ tab, setTab }) => {
</Flexbox>
),
onClick: () => {
setTab(SidebarTabKey.Setting);
useGlobalStore.setState({
sidebarKey: SidebarTabKey.Setting,
});
router.push('/settings/common');
},
},
Expand Down
9 changes: 2 additions & 7 deletions src/features/SideBar/TopActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import { SidebarTabKey } from '@/store/global/initialState';
import { useSessionStore } from '@/store/session';

export interface TopActionProps {
setTab: GlobalStore['switchSideBar'];
tab: GlobalStore['sidebarKey'];
tab?: GlobalStore['sidebarKey'];
}

const TopActions = memo<TopActionProps>(({ tab, setTab }) => {
const TopActions = memo<TopActionProps>(({ tab }) => {
const { t } = useTranslation('common');
const switchBackToChat = useGlobalStore((s) => s.switchBackToChat);

Expand All @@ -24,7 +23,6 @@ const TopActions = memo<TopActionProps>(({ tab, setTab }) => {
onClick={(e) => {
e.preventDefault();
switchBackToChat(useSessionStore.getState().activeId);
setTab(SidebarTabKey.Chat);
}}
>
<ActionIcon
Expand All @@ -39,9 +37,6 @@ const TopActions = memo<TopActionProps>(({ tab, setTab }) => {
<ActionIcon
active={tab === SidebarTabKey.Market}
icon={Compass}
onClick={() => {
setTab(SidebarTabKey.Market);
}}
placement={'right'}
size="large"
title={t('tab.market')}
Expand Down
12 changes: 7 additions & 5 deletions src/features/SideBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import { SideNav } from '@lobehub/ui';
import { memo } from 'react';

import AvatarWithUpload from '@/features/AvatarWithUpload';
import { useGlobalStore } from '@/store/global';
import { SidebarTabKey } from '@/store/global/initialState';

import BottomActions from './BottomActions';
import TopActions from './TopActions';

export default memo(() => {
const [tab, setTab] = useGlobalStore((s) => [s.sidebarKey, s.switchSideBar]);
interface Props {
sidebarKey?: SidebarTabKey;
}

export default memo<Props>(({ sidebarKey }) => {
return (
<SideNav
avatar={<AvatarWithUpload id={'avatar'} />}
bottomActions={<BottomActions setTab={setTab} tab={tab} />}
bottomActions={<BottomActions tab={sidebarKey} />}
style={{ height: '100%' }}
topActions={<TopActions setTab={setTab} tab={tab} />}
topActions={<TopActions tab={sidebarKey} />}
/>
);
});
11 changes: 8 additions & 3 deletions src/layout/AppLayout.desktop.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useTheme } from 'antd-style';
import { PropsWithChildren, memo } from 'react';
import { ReactNode, memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import SideBar from '@/features/SideBar';
import { useIsPWA } from '@/hooks/useIsPWA';
import { SidebarTabKey } from '@/store/global/initialState';

const AppLayoutDesktop = memo<PropsWithChildren>(({ children }) => {
interface AppLayoutDesktopProps {
children: ReactNode;
sidebarKey?: SidebarTabKey;
}
const AppLayoutDesktop = memo<AppLayoutDesktopProps>(({ children, sidebarKey }) => {
const isPWA = useIsPWA();
const theme = useTheme();

Expand All @@ -16,7 +21,7 @@ const AppLayoutDesktop = memo<PropsWithChildren>(({ children }) => {
style={isPWA ? { borderTop: `1px solid ${theme.colorBorder}` } : {}}
width={'100%'}
>
<SideBar />
<SideBar sidebarKey={sidebarKey} />
{children}
</Flexbox>
);
Expand Down
6 changes: 4 additions & 2 deletions src/layout/AppLayout.mobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CSSProperties, PropsWithChildren, ReactNode, memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import SafeSpacing from '@/components/SafeSpacing';
import { SidebarTabKey } from '@/store/global/slices/common/initialState';

const MobileTabBar = dynamic(() => import('@/features/MobileTabBar'));

Expand Down Expand Up @@ -39,11 +40,12 @@ interface AppMobileLayoutProps extends PropsWithChildren {
navBar?: ReactNode;
showTabBar?: boolean;
style?: CSSProperties;
tabBarKey?: SidebarTabKey;
title?: MobileNavBarTitleProps;
}

const AppLayoutMobile = memo<AppMobileLayoutProps>(
({ children, showTabBar, navBar, style, className }) => {
({ children, showTabBar, tabBarKey, navBar, style, className }) => {
const { styles, cx } = useStyles();

return (
Expand All @@ -58,7 +60,7 @@ const AppLayoutMobile = memo<AppMobileLayoutProps>(
{showTabBar && (
<>
<SafeSpacing mobile position={'bottom'} />
<MobileTabBar className={styles.mobileTabBar} />
<MobileTabBar className={styles.mobileTabBar} tabBarKey={tabBarKey} />
</>
)}
</Flexbox>
Expand Down
2 changes: 1 addition & 1 deletion src/store/global/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './useEffectAfterHydrated';
export * from './useOnFinishHydrationGlobal';
export * from './useSwitchSideBarOnInit';

13 changes: 0 additions & 13 deletions src/store/global/hooks/useSwitchSideBarOnInit.ts

This file was deleted.

Loading

0 comments on commit c477491

Please sign in to comment.