From 31c91abf2ae10e06662225ee5c896c1c84f54c17 Mon Sep 17 00:00:00 2001 From: Bohdan Shcherbyna Date: Mon, 4 Nov 2024 13:13:41 +0200 Subject: [PATCH] refactor: 626 - remove unnecessary use client directives --- src/core/base-layout/base-layout.tsx | 12 +--- .../base-layout/components/header/header.tsx | 2 + src/core/base-layout/components/index.ts | 1 - .../components/scroll-to-hash/index.ts | 1 - .../scroll-to-hash/scroll-to-hash.test.tsx | 59 ------------------- .../scroll-to-hash/scroll-to-hash.tsx | 31 ---------- src/shared/ui/logo/logo.tsx | 1 - 7 files changed, 4 insertions(+), 103 deletions(-) delete mode 100644 src/core/base-layout/components/scroll-to-hash/index.ts delete mode 100644 src/core/base-layout/components/scroll-to-hash/scroll-to-hash.test.tsx delete mode 100644 src/core/base-layout/components/scroll-to-hash/scroll-to-hash.tsx diff --git a/src/core/base-layout/base-layout.tsx b/src/core/base-layout/base-layout.tsx index 642bc6172..0898778f0 100644 --- a/src/core/base-layout/base-layout.tsx +++ b/src/core/base-layout/base-layout.tsx @@ -1,17 +1,9 @@ -'use client'; - -import { PropsWithChildren, useEffect } from 'react'; -import { Footer, Header, Partnered, ScrollToHashElement } from './components'; +import { PropsWithChildren } from 'react'; +import { Footer, Header, Partnered } from './components'; export const BaseLayout = ({ children }: PropsWithChildren) => { - useEffect(() => { - if (typeof window !== 'undefined' && window.location.hostname.includes('rollingscopes.com')) { - window.location.href = 'https://rs.school'; - } - }, []); return ( <> -
{children} diff --git a/src/core/base-layout/components/header/header.tsx b/src/core/base-layout/components/header/header.tsx index b29002fd3..fc1824eb9 100644 --- a/src/core/base-layout/components/header/header.tsx +++ b/src/core/base-layout/components/header/header.tsx @@ -1,3 +1,5 @@ +'use client'; + import { useEffect, useState } from 'react'; import classNames from 'classnames/bind'; import { usePathname } from 'next/navigation'; diff --git a/src/core/base-layout/components/index.ts b/src/core/base-layout/components/index.ts index 97b26938c..b90566029 100644 --- a/src/core/base-layout/components/index.ts +++ b/src/core/base-layout/components/index.ts @@ -1,4 +1,3 @@ export { Footer } from './footer'; export { Header } from './header'; export { Partnered } from './partnered'; -export { ScrollToHashElement } from './scroll-to-hash'; diff --git a/src/core/base-layout/components/scroll-to-hash/index.ts b/src/core/base-layout/components/scroll-to-hash/index.ts deleted file mode 100644 index 19e34a1fe..000000000 --- a/src/core/base-layout/components/scroll-to-hash/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { ScrollToHashElement } from './scroll-to-hash'; diff --git a/src/core/base-layout/components/scroll-to-hash/scroll-to-hash.test.tsx b/src/core/base-layout/components/scroll-to-hash/scroll-to-hash.test.tsx deleted file mode 100644 index 29a33ba28..000000000 --- a/src/core/base-layout/components/scroll-to-hash/scroll-to-hash.test.tsx +++ /dev/null @@ -1,59 +0,0 @@ -import { waitFor } from '@testing-library/react'; -import { beforeEach, describe, expect, it, vi } from 'vitest'; -import { ScrollToHashElement } from './scroll-to-hash'; -import { ROUTES } from '@/core/const'; -import { renderWithRouter } from '@/shared/__tests__/utils'; - -const mockUsePathname = vi.fn(); - -vi.mock('next/navigation', () => ({ - usePathname() { - return mockUsePathname(); - }, -})); - -describe('ScrollToHashElement', () => { - const scrollIntoViewMock = vi.fn(); - const getElementByIdMock = vi.fn(); - - beforeEach(() => { - scrollIntoViewMock.mockClear(); - getElementByIdMock.mockClear(); - - global.document.getElementById = getElementByIdMock.mockReturnValue({ scrollIntoView: scrollIntoViewMock }); - }); - - const setup = (entry: string = ROUTES.HOME) => { - mockUsePathname.mockImplementation(() => entry); - renderWithRouter(, { route: entry }); - }; - - it('scrolls to an element matching the hash', async () => { - setup('/#value'); - await waitFor(() => { - expect(getElementByIdMock).toHaveBeenCalled(); - expect(scrollIntoViewMock).toHaveBeenCalledWith({ - behavior: 'smooth', - block: 'start', - }); - }); - }); - - it("doesn't scroll when there is no target element", async () => { - getElementByIdMock.mockReturnValueOnce(null); - - setup('/#missing'); - await waitFor(() => { - expect(scrollIntoViewMock).not.toHaveBeenCalled(); - }); - }); - - it("doesn't scroll when the URL doesn't contain a hash", async () => { - getElementByIdMock.mockReturnValueOnce(null); - - setup(); - await waitFor(() => { - expect(scrollIntoViewMock).not.toHaveBeenCalled(); - }); - }); -}); diff --git a/src/core/base-layout/components/scroll-to-hash/scroll-to-hash.tsx b/src/core/base-layout/components/scroll-to-hash/scroll-to-hash.tsx deleted file mode 100644 index 4770dd580..000000000 --- a/src/core/base-layout/components/scroll-to-hash/scroll-to-hash.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import { useEffect } from 'react'; -import { usePathname } from 'next/navigation'; - -export const ScrollToHashElement = () => { - const pathname = usePathname(); - - useEffect(() => { - const scrollToElement = (hash: string) => { - const element = document.getElementById(hash.replace('#', '')); - - if (element) { - element.scrollIntoView({ - behavior: 'smooth', - block: 'start', - }); - } - }; - - const hash = pathname.split('#')[1]; - - if (hash) { - const id = setTimeout(() => scrollToElement(`#${hash}`), 0); - - return () => { - clearTimeout(id); - }; - } - }, [pathname]); - - return null; -}; diff --git a/src/shared/ui/logo/logo.tsx b/src/shared/ui/logo/logo.tsx index 194c17f23..a68624cf0 100644 --- a/src/shared/ui/logo/logo.tsx +++ b/src/shared/ui/logo/logo.tsx @@ -23,7 +23,6 @@ export const Logo = ({ type, className }: LogoProps) => { className, })} data-testid="logo" - onClick={() => window.scrollTo({ top: 0 })} > RSS-logo