Layouts refactoring (#96)

* Refactor layouts so that we can use the hook useSidebar from within the content of the layouts
* Updated packages
This commit is contained in:
Giancarlo Buomprisco
2024-12-15 17:25:58 +08:00
committed by GitHub
parent 4efe5f3f7b
commit af6217b410
18 changed files with 403 additions and 235 deletions

View File

@@ -5,7 +5,6 @@ import {
SidebarFooter,
SidebarHeader,
SidebarNavigation,
SidebarProvider,
} from '@kit/ui/shadcn-sidebar';
import { cn } from '@kit/ui/utils';
@@ -21,46 +20,43 @@ import { HomeAccountSelector } from './home-account-selector';
interface HomeSidebarProps {
workspace: UserWorkspace;
minimized: boolean;
}
const minimized = personalAccountNavigationConfig.sidebarCollapsed;
export function HomeSidebar(props: HomeSidebarProps) {
const { workspace, user, accounts } = props.workspace;
return (
<SidebarProvider minimized={minimized}>
<Sidebar>
<SidebarHeader className={'h-16 justify-center'}>
<div className={'flex items-center justify-between space-x-2'}>
<If
condition={featuresFlagConfig.enableTeamAccounts}
fallback={
<AppLogo
className={cn({
'max-w-full': minimized,
'py-2': !minimized,
})}
/>
}
>
<HomeAccountSelector userId={user.id} accounts={accounts} />
</If>
<Sidebar>
<SidebarHeader className={'h-16 justify-center'}>
<div className={'flex items-center justify-between space-x-2'}>
<If
condition={featuresFlagConfig.enableTeamAccounts}
fallback={
<AppLogo
className={cn({
'max-w-full': props.minimized,
'py-2': !props.minimized,
})}
/>
}
>
<HomeAccountSelector userId={user.id} accounts={accounts} />
</If>
<div className={'group-data-[minimized=true]:hidden'}>
<UserNotifications userId={user.id} />
</div>
<div className={'group-data-[minimized=true]:hidden'}>
<UserNotifications userId={user.id} />
</div>
</SidebarHeader>
</div>
</SidebarHeader>
<SidebarContent>
<SidebarNavigation config={personalAccountNavigationConfig} />
</SidebarContent>
<SidebarContent>
<SidebarNavigation config={personalAccountNavigationConfig} />
</SidebarContent>
<SidebarFooter>
<ProfileAccountDropdownContainer user={user} account={workspace} />
</SidebarFooter>
</Sidebar>
</SidebarProvider>
<SidebarFooter>
<ProfileAccountDropdownContainer user={user} account={workspace} />
</SidebarFooter>
</Sidebar>
);
}

View File

@@ -3,13 +3,13 @@ import { use } from 'react';
import { cookies } from 'next/headers';
import { UserWorkspaceContextProvider } from '@kit/accounts/components';
import { If } from '@kit/ui/if';
import {
Page,
PageLayoutStyle,
PageMobileNavigation,
PageNavigation,
} from '@kit/ui/page';
import { SidebarProvider } from '@kit/ui/shadcn-sidebar';
import { AppLogo } from '~/components/app-logo';
import { personalAccountNavigationConfig } from '~/config/personal-account-navigation.config';
@@ -22,36 +22,70 @@ import { HomeSidebar } from './_components/home-sidebar';
import { loadUserWorkspace } from './_lib/server/load-user-workspace';
function UserHomeLayout({ children }: React.PropsWithChildren) {
const workspace = use(loadUserWorkspace());
const style = use(getLayoutStyle());
return (
<Page style={style}>
<PageNavigation>
<If condition={style === 'header'}>
<HomeMenuNavigation workspace={workspace} />
</If>
if (style === 'sidebar') {
return <SidebarLayout>{children}</SidebarLayout>;
}
<If condition={style === 'sidebar'}>
<HomeSidebar workspace={workspace} />
</If>
</PageNavigation>
<PageMobileNavigation className={'flex items-center justify-between'}>
<AppLogo />
<HomeMobileNavigation workspace={workspace} />
</PageMobileNavigation>
<UserWorkspaceContextProvider value={workspace}>
{children}
</UserWorkspaceContextProvider>
</Page>
);
return <HeaderLayout>{children}</HeaderLayout>;
}
export default withI18n(UserHomeLayout);
function SidebarLayout({ children }: React.PropsWithChildren) {
const workspace = use(loadUserWorkspace());
const sidebarMinimized = personalAccountNavigationConfig.sidebarCollapsed;
return (
<UserWorkspaceContextProvider value={workspace}>
<SidebarProvider minimized={sidebarMinimized}>
<Page style={'sidebar'}>
<PageNavigation>
<HomeSidebar workspace={workspace} minimized={sidebarMinimized} />
</PageNavigation>
<MobileNavigation workspace={workspace} />
{children}
</Page>
</SidebarProvider>
</UserWorkspaceContextProvider>
);
}
function HeaderLayout({ children }: React.PropsWithChildren) {
const workspace = use(loadUserWorkspace());
return (
<UserWorkspaceContextProvider value={workspace}>
<Page style={'header'}>
<PageNavigation>
<HomeMenuNavigation workspace={workspace} />
</PageNavigation>
<MobileNavigation workspace={workspace} />
{children}
</Page>
</UserWorkspaceContextProvider>
);
}
function MobileNavigation({
workspace,
}: {
workspace: Awaited<ReturnType<typeof loadUserWorkspace>>;
}) {
return (
<PageMobileNavigation className={'flex items-center justify-between'}>
<AppLogo />
<HomeMobileNavigation workspace={workspace} />
</PageMobileNavigation>
);
}
async function getLayoutStyle() {
const cookieStore = await cookies();