diff --git a/react-app/index.html b/react-app/index.html index 37f11202a..121a8539f 100644 --- a/react-app/index.html +++ b/react-app/index.html @@ -13,5 +13,12 @@
+ diff --git a/react-app/src/App.tsx b/react-app/src/App.tsx index 9e7e24ab4..58a821073 100644 --- a/react-app/src/App.tsx +++ b/react-app/src/App.tsx @@ -1,18 +1,24 @@ import { Suspense } from 'react'; import { Outlet } from 'react-router-dom'; +import { usePageViews } from './analytics/usePageViews'; +import { Footer } from './components/Footer/Footer'; +import { Header } from './components/Header/Header'; import { Loader } from './components/Loader/Loader'; import { useSettings } from './settings'; import './styles/app.scss'; export default function App() { const { settings } = useSettings(); + usePageViews(); return (
+
}> +
); diff --git a/react-app/src/analytics/usePageViews.ts b/react-app/src/analytics/usePageViews.ts new file mode 100644 index 000000000..365013e69 --- /dev/null +++ b/react-app/src/analytics/usePageViews.ts @@ -0,0 +1,17 @@ +import { useEffect } from 'react'; +import { useLocation } from 'react-router-dom'; + +declare global { + interface Window { + ga?: (...args: unknown[]) => void; + } +} + +// Mirrors AppComponent's NavigationEnd hook: one GA pageview per route change. +export function usePageViews() { + const { pathname, search } = useLocation(); + useEffect(() => { + window.ga?.('set', 'page', pathname + search); + window.ga?.('send', 'pageview'); + }, [pathname, search]); +} diff --git a/react-app/src/components/Footer/Footer.scss b/react-app/src/components/Footer/Footer.scss new file mode 100644 index 000000000..1749e3d63 --- /dev/null +++ b/react-app/src/components/Footer/Footer.scss @@ -0,0 +1,22 @@ +@use "../../styles/media" as *; + +#footer { + position: relative; + padding: 10px; + height: 60px; + letter-spacing: 0.7px; + text-align: center; + + a { + font-weight: bold; + text-decoration: none; + + &:hover { + text-decoration: underline; + } + } + + @media #{$mobile-only} { + display: none; + } +} diff --git a/react-app/src/components/Footer/Footer.tsx b/react-app/src/components/Footer/Footer.tsx new file mode 100644 index 000000000..07a86f8e0 --- /dev/null +++ b/react-app/src/components/Footer/Footer.tsx @@ -0,0 +1,14 @@ +import './Footer.scss'; + +export function Footer() { + return ( + + ); +} diff --git a/react-app/src/components/Header/Header.scss b/react-app/src/components/Header/Header.scss new file mode 100644 index 000000000..2dd50da00 --- /dev/null +++ b/react-app/src/components/Header/Header.scss @@ -0,0 +1,158 @@ +@use "../../styles/media" as *; + +#header { + color: #fff; + padding: 6px 0; + line-height: 18px; + vertical-align: middle; + position: relative; + z-index: 1; + width: 100%; + + @media #{$mobile-only} { + height: 50px; + position: fixed; + top: 0; + } + + a { + display: inline; + } + + // Scoped: React has no Angular view encapsulation, and feed rows also use .name/.left etc. + + .home-link { + width: 50px; + height: 66px; + } + + .logo-inner { + width: 32px; + position: absolute; + left: 17px; + top: 18px; + z-index: -1; + height: 32px; + border-radius: 50%; + + @media #{$mobile-only} { + left: 16px; + top: 12px; + } + } + + .logo { + width: 50px; + padding: 3px 8px 0; + + @media #{$mobile-only} { + width: 45px; + padding: 0 0 0 10px; + } + } + + h1 { + font-weight: normal; + display: inline-block; + vertical-align:middle; + margin: 0; + font-size: 16px; + + a { + color: #fff; + text-decoration: none; + } + } + + .name { + margin-right: 30px; + margin-bottom: 2px; + + @media #{$mobile-only} { + display: none; + } + } + + .header-text { + position: absolute; + width: inherit; + height: 20px; + left: 10px; + top: 27px; + z-index: -1; + + @media #{$mobile-only} { + top: 22px; + } + } + + .left { + position: absolute; + left: 60px; + font-size: 16px; + + @media #{$mobile-only} { + width: 100%; + left: 0; + } + } + + .header-nav { + display: inline-block; + margin-left: 20px; + + @media #{$mobile-only} { + margin-left: 60px; + } + + a { + color: hsla(0,0%,100%,.9); + text-decoration: none; + margin: 0 5px; + letter-spacing: 1.8px; + + &:hover { + color: #fff; + } + } + + .active { + color: #fff; + } + } + + .info { + position: absolute; + top: 0; + right: 20px; + height: 100%; + + @media #{$mobile-only} { + right: 10px; + } + + img { + opacity: 0.8; + width: 25px; + margin-top: 21.5px; + display: block; + + &:hover { + opacity: 1; + cursor: pointer; + } + + @media #{$mobile-only} { + margin-top: 15px; + } + } + } + + .settings-button { + padding: 0; + border: 0; + background: none; + cursor: pointer; + font: inherit; + } +} diff --git a/react-app/src/components/Header/Header.tsx b/react-app/src/components/Header/Header.tsx new file mode 100644 index 000000000..f941e03f0 --- /dev/null +++ b/react-app/src/components/Header/Header.tsx @@ -0,0 +1,49 @@ +import { NavLink } from 'react-router-dom'; +import { useSettings } from '../../settings'; +import { SettingsPanel } from '../Settings/SettingsPanel'; +import './Header.scss'; + +const NAV_LINKS = [ + { to: '/newest/1', label: 'new' }, + { to: '/show/1', label: 'show' }, + { to: '/ask/1', label: 'ask' }, + { to: '/jobs/1', label: 'jobs' }, +]; + +function scrollTop() { + window.scrollTo(0, 0); +} + +export function Header() { + const { settings, toggleSettings } = useSettings(); + return ( +
+ + {settings.showSettings && } +
+ ); +} diff --git a/react-app/src/components/Settings/Settings.scss b/react-app/src/components/Settings/Settings.scss new file mode 100644 index 000000000..1cc49ed05 --- /dev/null +++ b/react-app/src/components/Settings/Settings.scss @@ -0,0 +1,78 @@ +@use "../../styles/media" as *; + +.overlay { + position: fixed; + top: 0; + bottom: 0; + left: 0; + right: 0; + background: rgba(0, 0, 0, 0.7); + opacity: 1; + z-index: 1; +} + +.popup { + margin: 70px auto; + padding: 30px; + border-radius: 5px; + width: 30%; + position: relative; + h1 { + margin-top: 0; + margin-bottom: 0px; + color: #fff; + text-align: center; + letter-spacing: 1px; + } + h2 { + padding-top: 10px; + } + hr { + width: 40%; + margin-bottom: 20px; + } + .close { + padding: 0; + border: 0; + background: none; + cursor: pointer; + line-height: 1; + position: absolute; + top: 12px; + right: 20px; + font-size: 30px; + font-weight: bold; + text-decoration: none; + color: rgba(255,255,255,0.8); + &:hover { + color: #fff; + cursor: pointer; + } + } + .content { + max-height: 30%; + color: #fff; + letter-spacing: 1px; + overflow: auto; + } + input[type=number] { + display: block; + width: 80%; + height: 20px; + margin-bottom: 15px; + border-radius: 5px; + padding: 2px; + } +} + +.control-section { + margin-bottom: 15px; + padding-bottom: 15px; + border-bottom: 1px solid white; +} + +@media screen and (max-width: 700px) { + .box, .popup { + width: 70%; + } +} diff --git a/react-app/src/components/Settings/SettingsPanel.tsx b/react-app/src/components/Settings/SettingsPanel.tsx new file mode 100644 index 000000000..5f7802f60 --- /dev/null +++ b/react-app/src/components/Settings/SettingsPanel.tsx @@ -0,0 +1,81 @@ +import type { Theme } from '../../models'; +import { useSettings } from '../../settings'; +import './Settings.scss'; + +const THEME_OPTIONS: { value: Theme; label: string }[] = [ + { value: 'default', label: 'Default' }, + { value: 'night', label: 'Night' }, + { value: 'amoledblack', label: 'Black (AMOLED)' }, +]; + +export function SettingsPanel() { + const { settings, toggleSettings, toggleOpenLinksInNewTab, setTheme, setFont, setSpacing } = useSettings(); + return ( +
+
+

Settings

+
+ +
+
+

Links

+ +
+
+
+

Select a theme

+ {THEME_OPTIONS.map(({ value, label }) => ( +
+ +
+ ))} +
+
+

Change Font

+
+ +
+
+ +
+
+
+
+
+
+ ); +} diff --git a/react-app/src/components/shell.test.tsx b/react-app/src/components/shell.test.tsx new file mode 100644 index 000000000..001760d0a --- /dev/null +++ b/react-app/src/components/shell.test.tsx @@ -0,0 +1,83 @@ +import { render, screen, within } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { createMemoryRouter, RouterProvider } from 'react-router-dom'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { routes } from '../routes'; +import { SettingsProvider } from '../settings'; + +function renderAt(path: string) { + const router = createMemoryRouter(routes, { initialEntries: [path] }); + const view = render( + + + + ); + return { router, ...view }; +} + +afterEach(() => { + delete window.ga; +}); + +describe('app shell', () => { + it('renders header nav links and footer', async () => { + renderAt('/news/1'); + await screen.findByTestId('feed-page'); + for (const label of ['new', 'show', 'ask', 'jobs']) { + expect(screen.getByRole('link', { name: label })).toBeInTheDocument(); + } + expect(screen.getByRole('link', { name: 'GitHub' })).toHaveAttribute( + 'href', + 'https://github.com/hdjirdeh/angular2-hn' + ); + }); + + it('navigates via the header and marks the active link', async () => { + const { router } = renderAt('/news/1'); + await screen.findByTestId('feed-page'); + await userEvent.click(screen.getByRole('link', { name: 'show' })); + expect(router.state.location.pathname).toBe('/show/1'); + await screen.findByTestId('feed-page'); + expect(screen.getByRole('link', { name: 'show' })).toHaveClass('active'); + expect(screen.getByRole('link', { name: 'new' })).not.toHaveClass('active'); + }); + + it('opens settings, changes theme and preferences, and closes', async () => { + const { container } = renderAt('/news/1'); + await screen.findByTestId('feed-page'); + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); + + await userEvent.click(screen.getByRole('button', { name: 'Settings' })); + const dialog = screen.getByRole('dialog', { name: 'Settings' }); + + await userEvent.click(within(dialog).getByLabelText('Night')); + expect(container.firstChild).toHaveClass('night'); + expect(localStorage.getItem('theme')).toBe('night'); + + await userEvent.click(within(dialog).getByLabelText('Open links in a new tab')); + expect(localStorage.getItem('openLinkInNewTab')).toBe('true'); + + const font = within(dialog).getByLabelText('Font size:'); + await userEvent.clear(font); + await userEvent.type(font, '20'); + expect(localStorage.getItem('titleFontSize')).toBe('20'); + + await userEvent.click(within(dialog).getByRole('button', { name: 'Close settings' })); + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); + }); + + it('sends a GA pageview on each route change', async () => { + const ga = vi.fn(); + window.ga = ga; + const { router } = renderAt('/'); + await screen.findByTestId('feed-page'); + await router.navigate('/item/1'); + await screen.findByTestId('item-page'); + expect(ga).toHaveBeenCalledWith('set', 'page', '/news/1'); + expect(ga).toHaveBeenCalledWith('set', 'page', '/item/1'); + expect(ga.mock.calls.filter(([cmd]) => cmd === 'send')).toEqual([ + ['send', 'pageview'], + ['send', 'pageview'], + ]); + }); +}); diff --git a/react-app/src/routes.tsx b/react-app/src/routes.tsx index f3a591242..a2ced3822 100644 --- a/react-app/src/routes.tsx +++ b/react-app/src/routes.tsx @@ -1,5 +1,5 @@ import { lazy } from 'react'; -import { Navigate, type RouteObject } from 'react-router-dom'; +import { redirect, type RouteObject } from 'react-router-dom'; import App from './App'; import { FEED_TYPES } from './models'; import FeedPage from './pages/FeedPage'; @@ -13,7 +13,8 @@ export const routes: RouteObject[] = [ path: '/', element: , children: [ - { index: true, element: }, + // A loader redirect resolves before render, so `/` never produces its own pageview. + { index: true, loader: () => redirect('/news/1') }, ...FEED_TYPES.map((feedType) => ({ path: `${feedType}/:page`, element: ,