From 5303bb895de8b40ec267a60e121f18bf7c470369 Mon Sep 17 00:00:00 2001 From: SD Date: Sat, 22 Jun 2024 20:03:56 +0400 Subject: [PATCH] fix: fix ssr window --- .../account/(account)/messages/page.tsx | 11 ++-------- src/app/[locale]/account/layout.tsx | 22 ++++++++----------- src/app/layout.tsx | 6 +++++ src/components/Account/AccountWrapper.tsx | 20 +++++++++++++++++ .../Page/Header/HeaderAuthLinks.tsx | 8 ++++--- src/components/Page/Header/HeaderMenu.tsx | 6 +++-- .../Page/Header/HeaderMobileMenu.tsx | 6 +++-- 7 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 src/components/Account/AccountWrapper.tsx diff --git a/src/app/[locale]/account/(account)/messages/page.tsx b/src/app/[locale]/account/(account)/messages/page.tsx index 56b40d6..746df4e 100644 --- a/src/app/[locale]/account/(account)/messages/page.tsx +++ b/src/app/[locale]/account/(account)/messages/page.tsx @@ -1,18 +1,11 @@ +'use client' + import React, { Suspense } from 'react'; -import type { Metadata } from 'next'; -import { useTranslations } from 'next-intl'; import { Link } from '../../../../../navigation'; import { CustomInput } from '../../../../../components/view/CustomInput'; import { i18nText } from '../../../../../i18nKeys'; -export const metadata: Metadata = { - title: 'Bbuddy - Account - Messages', - description: 'Bbuddy desc messages' -}; - export default function Messages({ params: { locale } }: { params: { locale: string } }) { - const t = useTranslations('Account.Messages'); - return ( <>
    diff --git a/src/app/[locale]/account/layout.tsx b/src/app/[locale]/account/layout.tsx index f9f0410..bb642a6 100644 --- a/src/app/[locale]/account/layout.tsx +++ b/src/app/[locale]/account/layout.tsx @@ -1,18 +1,14 @@ 'use client'; -import { ReactNode, useEffect } from 'react'; -import { redirect, notFound } from 'next/navigation'; -import { useLocalStorage } from '../../../hooks/useLocalStorage'; -import { AUTH_TOKEN_KEY } from '../../../constants/common'; +import { ReactNode } from 'react'; +import dynamic from 'next/dynamic'; -export default function AccountLayout({ children }: { children: ReactNode }) { - const [token] = useLocalStorage(AUTH_TOKEN_KEY, ''); +const Account = dynamic(() => import('../../../components/Account/AccountWrapper'), { ssr: false }); - useEffect(() => { - if(!token){ - notFound(); - } - }, []); - - return children; +export default function AccountBaseLayout({ children }: { children: ReactNode }) { + return ( + + {children} + + ); }; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 06f7fdd..742fee4 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,4 +1,5 @@ import React, { ReactNode } from 'react'; +import type { Metadata } from 'next'; import { comfortaa, inter } from './fonts'; import '../styles/style.scss'; @@ -7,6 +8,11 @@ type RootLayoutProps = { params: { locale: string }; }; +export const metadata: Metadata = { + title: 'Bbuddy', + description: 'Bbuddy' +}; + export default function RootLayout({ children, params: { locale } }: RootLayoutProps) { return ( diff --git a/src/components/Account/AccountWrapper.tsx b/src/components/Account/AccountWrapper.tsx new file mode 100644 index 0000000..57270cd --- /dev/null +++ b/src/components/Account/AccountWrapper.tsx @@ -0,0 +1,20 @@ +'use client' + +import { ReactNode, useEffect } from 'react'; +import { notFound } from 'next/navigation'; +import { AUTH_TOKEN_KEY } from '../../constants/common'; +import { useLocalStorage } from '../../hooks/useLocalStorage'; + +function AccountWrapper ({ children }: { children: ReactNode }) { + const [token] = useLocalStorage(AUTH_TOKEN_KEY, ''); + + useEffect(() => { + if(!token){ + return notFound(); + } + }, []); + + return children; +} + +export default AccountWrapper; diff --git a/src/components/Page/Header/HeaderAuthLinks.tsx b/src/components/Page/Header/HeaderAuthLinks.tsx index e891919..362dfbe 100644 --- a/src/components/Page/Header/HeaderAuthLinks.tsx +++ b/src/components/Page/Header/HeaderAuthLinks.tsx @@ -14,10 +14,10 @@ type HeaderAuthLinksProps = { separatorClass?: string; }; -export const HeaderAuthLinks: FC = ({ +function HeaderAuthLinks ({ locale, separatorClass = 'b-header__nav__list__line' -}) => { +}: HeaderAuthLinksProps) { const [isOpenModal, setIsOpenModal] = useState(false); const [mode, setMode] = useState<'enter' | 'register' | 'reset' | 'finish'>('enter'); const selectedLayoutSegment = useSelectedLayoutSegment(); @@ -75,4 +75,6 @@ export const HeaderAuthLinks: FC = ({ /> ); -}; +} + +export default HeaderAuthLinks; diff --git a/src/components/Page/Header/HeaderMenu.tsx b/src/components/Page/Header/HeaderMenu.tsx index ced0dd4..62eae81 100644 --- a/src/components/Page/Header/HeaderMenu.tsx +++ b/src/components/Page/Header/HeaderMenu.tsx @@ -2,14 +2,16 @@ import React from 'react'; import { useSelectedLayoutSegment } from 'next/navigation'; +import dynamic from 'next/dynamic'; import { Link } from '../../../navigation'; -import { HeaderAuthLinks } from './HeaderAuthLinks'; type HeaderMenuProps = { locale: string; linkConfig: { path: string, title: string }[]; }; +const AuthLinks = dynamic(() => import('./HeaderAuthLinks'), { ssr: false }); + export const HeaderMenu = ({ locale, linkConfig @@ -26,7 +28,7 @@ export const HeaderMenu = ({ {title} ))} - + diff --git a/src/components/Page/Header/HeaderMobileMenu.tsx b/src/components/Page/Header/HeaderMobileMenu.tsx index cd888f9..461ea62 100644 --- a/src/components/Page/Header/HeaderMobileMenu.tsx +++ b/src/components/Page/Header/HeaderMobileMenu.tsx @@ -1,8 +1,8 @@ 'use client' import React, { FC, useState } from 'react'; +import dynamic from 'next/dynamic'; import { useSelectedLayoutSegment } from 'next/navigation'; -import { HeaderAuthLinks } from './HeaderAuthLinks'; import { Link } from '../../../navigation'; type HeaderMenuMobileProps = { @@ -10,6 +10,8 @@ type HeaderMenuMobileProps = { linkConfig: { path: string, title: string }[]; }; +const AuthLinks = dynamic(() => import('./HeaderAuthLinks'), { ssr: false }); + export const HeaderMobileMenu: FC = ({ locale, linkConfig @@ -35,7 +37,7 @@ export const HeaderMobileMenu: FC = ({
      -