Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions apps/3000-home/components/SharedNav.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import React from 'react';
import { Menu, Layout } from 'antd';
import { useRouter } from 'next/router';
import Router from 'next/router';
import './menu';

const SharedNav = () => {
const { asPath, push } = useRouter();
const router = (
Router as typeof Router & {
router?: {
asPath?: string;
push: (url: string) => Promise<boolean>;
};
}
).router;
const asPath =
typeof window !== 'undefined'
? window.location.pathname || router?.asPath || '/'
: router?.asPath || '/';
let activeMenu;

if (asPath === '/' || asPath.startsWith('/home')) {
Expand Down Expand Up @@ -53,7 +64,13 @@ const SharedNav = () => {
mode="horizontal"
selectedKeys={activeMenu ? [activeMenu] : undefined}
onClick={({ key }) => {
push(key);
if (router) {
void router.push(String(key));
return;
}
if (typeof window !== 'undefined') {
window.location.assign(String(key));
}
}}
items={menuItems}
/>
Expand Down
28 changes: 24 additions & 4 deletions apps/3000-home/components/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ItemType } from 'antd/es/menu/interface';

import { useRouter } from 'next/router';
import Router from 'next/router';
import { Menu } from 'antd';

const menuItems: ItemType[] = [
Expand All @@ -16,7 +16,18 @@ const menuItems: ItemType[] = [
];

export default function AppMenu() {
const router = useRouter();
const router = (
Router as typeof Router & {
router?: {
asPath?: string;
push: (url: string) => Promise<boolean>;
};
}
).router;
const selectedKey =
typeof window !== 'undefined'
? window.location.pathname || router?.asPath || '/'
: router?.asPath || '/';

return (
<>
Expand All @@ -27,9 +38,18 @@ export default function AppMenu() {
</div>
<Menu
mode="inline"
selectedKeys={[router.asPath]}
selectedKeys={[selectedKey]}
style={{ height: '100%' }}
onClick={({ key }) => router.push(key)}
onClick={({ key }) => {
if (typeof window === 'undefined') {
return;
}
if (router) {
void router.push(String(key));
return;
}
window.location.assign(String(key));
}}
items={menuItems}
/>
</>
Expand Down
15 changes: 11 additions & 4 deletions apps/3000-home/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import App from 'next/app';
import { Layout, version, ConfigProvider } from 'antd';
import { StyleProvider } from '@ant-design/cssinjs';

import Router, { useRouter } from 'next/router';
import Router from 'next/router';
const SharedNav = React.lazy(() => import('../components/SharedNav'));
import HostAppMenu from '../components/menu';
function MyApp(props) {
const { Component, pageProps } = props;
const { asPath } = useRouter();
const [MenuComponent, setMenuComponent] = useState(() => HostAppMenu);
const handleRouteChange = async (url) => {
if (url.startsWith('/shop')) {
Expand All @@ -28,8 +27,16 @@ function MyApp(props) {
};
// handle first route hit.
React.useEffect(() => {
handleRouteChange(asPath);
}, [asPath]);
const initialPath =
(
Router as typeof Router & {
router?: {
asPath?: string;
};
}
).router?.asPath || '/';
handleRouteChange(initialPath);
}, []);

//handle route change
React.useEffect(() => {
Expand Down
28 changes: 24 additions & 4 deletions apps/3001-shop/components/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ItemType } from 'antd/es/menu/interface';

import { useRouter } from 'next/router';
import Router from 'next/router';
import { Menu } from 'antd';

const menuItems: ItemType[] = [
Expand All @@ -19,7 +19,18 @@ const menuItems: ItemType[] = [
];

export default function AppMenu() {
const router = useRouter();
const router = (
Router as typeof Router & {
router?: {
asPath?: string;
push: (url: string) => Promise<boolean>;
};
}
).router;
const selectedKey =
typeof window !== 'undefined'
? window.location.pathname || router?.asPath || '/shop'
: router?.asPath || '/shop';

return (
<>
Expand All @@ -30,9 +41,18 @@ export default function AppMenu() {
</div>
<Menu
mode="inline"
selectedKeys={[router.asPath]}
selectedKeys={[selectedKey]}
style={{ height: '100%' }}
onClick={({ key }) => router.push(key)}
onClick={({ key }) => {
if (typeof window === 'undefined') {
return;
}
if (router) {
void router.push(String(key));
return;
}
window.location.assign(String(key));
}}
items={menuItems}
/>
</>
Expand Down
15 changes: 11 additions & 4 deletions apps/3001-shop/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React, { Suspense, useState, lazy } from 'react';
import App from 'next/app';
import { Layout, version, ConfigProvider } from 'antd';
import Router, { useRouter } from 'next/router';
import Router from 'next/router';
import { StyleProvider } from '@ant-design/cssinjs';
import HostAppMenu from '../components/menu';

const SharedNav = lazy(() => import('home/SharedNav'));

function MyApp({ Component, pageProps }) {
const { asPath } = useRouter();
const [MenuComponent, setMenuComponent] = useState(() => HostAppMenu);
const handleRouteChange = async (url) => {
if (url.startsWith('/home') || url === '/') {
Expand All @@ -25,8 +24,16 @@ function MyApp({ Component, pageProps }) {
};
// handle first route hit.
React.useEffect(() => {
handleRouteChange(asPath);
}, [asPath]);
const initialPath =
(
Router as typeof Router & {
router?: {
asPath?: string;
};
}
).router?.asPath || '/shop';
handleRouteChange(initialPath);
}, []);

//handle route change
React.useEffect(() => {
Expand Down
28 changes: 24 additions & 4 deletions apps/3002-checkout/components/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ItemType } from 'antd/es/menu/interface';

import { useRouter } from 'next/router';
import Router from 'next/router';
import { Menu } from 'antd';

const menuItems: ItemType[] = [
Expand All @@ -17,7 +17,18 @@ const menuItems: ItemType[] = [
];

export default function AppMenu() {
const router = useRouter();
const router = (
Router as typeof Router & {
router?: {
asPath?: string;
push: (url: string) => Promise<boolean>;
};
}
).router;
const selectedKey =
typeof window !== 'undefined'
? window.location.pathname || router?.asPath || '/checkout'
: router?.asPath || '/checkout';

return (
<>
Expand All @@ -28,9 +39,18 @@ export default function AppMenu() {
</div>
<Menu
mode="inline"
selectedKeys={[router.asPath]}
selectedKeys={[selectedKey]}
style={{ height: '100%' }}
onClick={({ key }) => router.push(key)}
onClick={({ key }) => {
if (typeof window === 'undefined') {
return;
}
if (router) {
void router.push(String(key));
return;
}
window.location.assign(String(key));
}}
items={menuItems}
/>
</>
Expand Down
15 changes: 11 additions & 4 deletions apps/3002-checkout/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { Suspense, lazy, useState } from 'react';
import App from 'next/app';
import { Layout, version, ConfigProvider } from 'antd';
import Router, { useRouter } from 'next/router';
import Router from 'next/router';
import { StyleProvider } from '@ant-design/cssinjs';

import HostAppMenu from '../components/menu';

const SharedNav = lazy(() => import('home/SharedNav'));

function MyApp({ Component, pageProps }) {
const { asPath } = useRouter();
const [MenuComponent, setMenuComponent] = useState(() => HostAppMenu);
const handleRouteChange = async (url) => {
if (url.startsWith('/home') || url === '/') {
Expand All @@ -26,8 +25,16 @@ function MyApp({ Component, pageProps }) {
};
// handle first route hit.
React.useEffect(() => {
handleRouteChange(asPath);
}, [asPath]);
const initialPath =
(
Router as typeof Router & {
router?: {
asPath?: string;
};
}
).router?.asPath || '/checkout';
handleRouteChange(initialPath);
}, []);

//handle route change
React.useEffect(() => {
Expand Down
Loading
Loading