g1t/apps/web/app/routes/settings/layout.tsx

37 lines1,378 bytesCodeBlame
1import { useEffect } from "react";
2import { Outlet, useLocation, useNavigate } from "react-router";
3
4import { ACCOUNT_SETTINGS, accountSettingsPage, settingsPathForHash } from "../../lib/account-settings";
5
6/**
7 * Your settings: each page under `/settings/<page>`, listed in the sidebar.
8 * The heading is named from the address.
9 *
10 * The settings used to be one page with a section per `#anchor`. A server
11 * never sees the anchor, so an old link (`/settings#emails`) arrives at the
12 * first page with the anchor still on it, and is sent on to its own page
13 * from here.
14 */
15export default function SettingsLayout() {
16 const { pathname, hash } = useLocation();
17 const navigate = useNavigate();
18 useEffect(() => {
19 const to = settingsPathForHash(hash);
20 if (to && to !== pathname) navigate(to, { replace: true });
21 }, [hash, pathname, navigate]);
22 const page = accountSettingsPage(pathname);
23 const about = page ? ACCOUNT_SETTINGS[page] : null;
24 return (
25 <main className="mx-auto max-w-5xl px-4 py-10 sm:px-8">
26 {about && (
27 <header className="mb-8 border-b border-line pb-5">
28 <h1 className="text-lg font-semibold tracking-tight">{about.title}</h1>
29 <p className="mt-1 max-w-2xl text-sm text-muted">{about.about}</p>
30 </header>
31 )}
32 <div className="max-w-2xl">
33 <Outlet />
34 </div>
35 </main>
36 );
37}