Branchwell

Inkspring / website / src / App.tsx

1import React, { useEffect, useState } from 'react';
2import Homepage, { Footer, Header } from './Homepage';
3import Download from './Download';
4import Help from './Help';
5import Compare from './Compare.tsx';
6import { compare } from './compare.js';
7import Legal from './Legal';
8import Research from './Research';
9import NotFound from './NotFound';
10
11const routes = {
12 '/': 'Inkspring · Craft and schedule social posts on your Mac',
13 '/help': 'Help · Inkspring',
14 '/compare': compare.title,
15 '/download': 'Download and install Inkspring for Mac',
16 '/privacy': 'Privacy · Inkspring',
17 '/app-notices': 'App notices · Inkspring',
18 '/research': 'Research · Inkspring',
19};
20
21function routePath(pathname: string) {
22 return pathname.replace(/\/$/, '') || '/';
23}
24
25export function App({ pathname = '/' }: { pathname?: string }) {
26 const [path, setPath] = useState(() => routePath(pathname));
27
28 useEffect(() => {
29 const restore = () => setPath(routePath(location.pathname));
30 const navigate = (event: MouseEvent) => {
31 if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey ||
32 event.shiftKey || event.altKey || !(event.target instanceof Element)) return;
33
34 const link = event.target.closest<HTMLAnchorElement>('a[href]');
35 if (!link || link.hasAttribute('download') || (link.target && link.target !== '_self')) return;
36
37 const url = new URL(link.href);
38 const destination = routePath(url.pathname);
39 if (url.origin !== location.origin || url.search || !Object.hasOwn(routes, destination) ||
40 destination === routePath(location.pathname)) return;
41
42 event.preventDefault();
43 // Let the current page's click handlers finish before changing the URL.
44 queueMicrotask(() => {
45 history.pushState(null, '', url);
46 setPath(destination);
47 window.scrollTo({ top: 0, behavior: 'instant' });
48 });
49 };
50
51 window.addEventListener('popstate', restore);
52 window.addEventListener('click', navigate);
53 return () => {
54 window.removeEventListener('popstate', restore);
55 window.removeEventListener('click', navigate);
56 };
57 }, []);
58
59 useEffect(() => {
60 document.title = routes[path as keyof typeof routes] ?? 'Page not found · Inkspring';
61 const fragment = location.hash.slice(1);
62 if (fragment) {
63 try { document.getElementById(decodeURIComponent(fragment))?.scrollIntoView({ behavior: 'instant' }); }
64 catch { /* Ignore an invalid encoded fragment. */ }
65 }
66 }, [path]);
67
68 return <div className={path === '/' ? 'page' : path === '/download' ? 'download-page' : path === '/help' ? 'help-page' : path === '/compare' ? 'compare-page' : undefined} id={path === '/' ? 'top' : undefined}>
69 <Header home={path === '/'} />
70 {path === '/' ? <Homepage />
71 : path === '/help' ? <Help />
72 : path === '/compare' ? <Compare />
73 : path === '/download' ? <Download />
74 : path === '/research' ? <Research />
75 : path === '/privacy' || path === '/app-notices' ? <Legal kind={path === '/privacy' ? 'privacy' : 'notices'} key={path} />
76 : <NotFound />}
77 <Footer />
78 </div>;
79}