diff --git a/src/lib/site.ts b/src/lib/site.ts index d70f00f..6c223c9 100644 --- a/src/lib/site.ts +++ b/src/lib/site.ts @@ -48,6 +48,9 @@ export const siteUrl = (path: string): string => new URL(path, SITE_ORIGIN).href const PAGES: Record = { ...siteConfiguration.pages, ...fromServer.pages } +/* Every static page's path and text, for the not-found page's suggestions. */ +export const SITE_PAGES: Readonly> = PAGES + /** A static page's title and intro, from site.json's "pages". */ export function sitePage(path: string): PageText { const page = PAGES[path] diff --git a/src/lib/suggest.ts b/src/lib/suggest.ts new file mode 100644 index 0000000..d21cd2b --- /dev/null +++ b/src/lib/suggest.ts @@ -0,0 +1,70 @@ +/* ═══════════════════════════════════════════════════════════════ + SUGGEST: what a mistyped or out-of-date address was probably for + + The not-found page offers the closest pages to the address that + failed. Each candidate (a static page, or a record from + /directory) is scored by how alike its URL ID or name is to the + address's last part, and its full path to the whole address: + the Dice coefficient over letter pairs, so a typo, a missing + hyphen or an old base (/chapters/lynnwood for /groups/lynnwood) + still lands close. Scores run 0 to 1; anything under MIN_SCORE is + a guess not worth showing. + ═══════════════════════════════════════════════════════════════ */ + +export type Candidate = { href: string; label: string; detail?: string; keys: string[] } + +const MIN_SCORE = 0.4 + +/* Lowercase letters and digits only: "Lynnwood-Group" → "lynnwoodgroup". */ +const squash = (text: string) => text.toLowerCase().replace(/[^a-z0-9]/g, '') + +function pairs(text: string): Map { + const out = new Map() + for (let i = 0; i < text.length - 1; i += 1) { + const pair = text.slice(i, i + 2) + out.set(pair, (out.get(pair) ?? 0) + 1) + } + return out +} + +/* Dice coefficient of two strings' letter pairs: 1 is identical. */ +export function likeness(a: string, b: string): number { + const x = squash(a) + const y = squash(b) + if (!x || !y) return 0 + if (x === y) return 1 + if (x.length < 2 || y.length < 2) return 0 + const left = pairs(x) + const right = pairs(y) + let shared = 0 + for (const [pair, count] of left) shared += Math.min(count, right.get(pair) ?? 0) + return (2 * shared) / (x.length - 1 + (y.length - 1)) +} + +function decode(text: string): string { + try { + return decodeURIComponent(text) + } catch { + return text + } +} + +export function suggest(pathname: string, candidates: Candidate[], limit = 5): Candidate[] { + const segments = decode(pathname).split('/').filter(Boolean) + const last = segments.at(-1) ?? '' + const whole = segments.join(' ') + if (!last) return [] + + return candidates + .map((candidate) => ({ + candidate, + score: Math.max( + likeness(whole, candidate.href), + ...candidate.keys.map((key) => likeness(last, key)), + ), + })) + .filter(({ score }) => score >= MIN_SCORE) + .sort((a, b) => b.score - a.score) + .slice(0, limit) + .map(({ candidate }) => candidate) +} diff --git a/src/pages/NotFound.tsx b/src/pages/NotFound.tsx index 50e724e..70ae336 100644 --- a/src/pages/NotFound.tsx +++ b/src/pages/NotFound.tsx @@ -1,16 +1,133 @@ -import { Link } from "react-router-dom"; +/* ═══════════════════════════════════════════════════════════════ + NOT FOUND: any address no route matches + + Rather than a dead end, the closest pages to the address that + failed: the static pages (site.json's) and every public, listed + record (GET /directory), scored by suggest.ts against the + address. So /organisation offers Organization, /retreat offers + Retreats, and an old /chapters/lynnwood offers the Lynnwood group. + Below them, the main pages to start from. + + The server sends this same page with a 404 status for an unknown + address (routes/pages.js), so search engines drop it. + ═══════════════════════════════════════════════════════════════ */ + +import { ArrowRight } from '@phosphor-icons/react' +import { useMemo } from 'react' +import { Link, useLocation } from 'react-router-dom' + +import PageShell from '../components/PageShell.tsx' +import { PAGE_LINKS } from '../navConfig.ts' +import { SITE_PAGES } from '../lib/site.ts' +import { useDirectory, type Directory } from '../lib/useContent.ts' +import { blogHref, eventHref, orgHref, personHref, teamHref, awardHref } from '../lib/hrefs.ts' +import { suggest, type Candidate } from '../lib/suggest.ts' +import { ACCENTS, BRAND } from '../lib/color.ts' + +const ORG_KIND_LABEL: Record = { + national: 'Organization', + region: 'Region', + group: 'Group', + partner: 'Partner', +} + +/* Everything an address could have meant: the static pages, then + each kind of record, with the words to match it by. */ +function candidates(directory: Directory | null): Candidate[] { + const pages: Candidate[] = Object.entries(SITE_PAGES).map(([path, page]) => ({ + href: path, + label: page.title, + detail: 'Page', + keys: [path.slice(1), page.title], + })) + if (!directory) return pages + + const record = (href: string, id: string, name: string, detail: string): Candidate => ({ + href, + label: name, + detail, + keys: [id, name], + }) + + return [ + ...pages, + ...directory.events.map((e) => record(eventHref(e.id), e.id, e.name, 'Event')), + ...directory.organizations.map((o) => record(orgHref(o.id, o.kind), o.id, o.name, ORG_KIND_LABEL[o.kind] ?? 'Organization')), + ...directory.teams.map((t) => record(teamHref(t.id), t.id, t.name, `Team, ${t.org_name}`)), + ...directory.awards.map((a) => record(awardHref(a.id), a.id, a.name, 'Award')), + ...directory.people.map((p) => record(personHref(p.id), p.id, p.name, 'Person')), + ...directory.blog_posts.map((b) => record(blogHref(b.id), b.id, b.name, 'Blog post')), + ] +} export default function NotFound() { + const { pathname } = useLocation() + const { data: directory, loading } = useDirectory() + const matches = useMemo(() => suggest(pathname, candidates(directory ?? null)), [pathname, directory]) + return ( -
-

That page isn't here

-

- The link may be out of date.{" "} - - Go to the home page - - . -

-
- ); + + {loading && !directory ? ( +
+ ) : matches.length ? ( +
    + {matches.map((match) => ( +
  • + + + {match.label} + + {match.detail} · {match.href} + + +
  • + ))} +
+ ) : ( +

+ Try one of the main pages below, or the menu at the top. +

+ )} +
+ ), + }, + { + id: 'start', + title: 'Start from', + accent: ACCENTS.green, + content: ( +
+ {PAGE_LINKS.map((page) => ( + + {page.label} + + ))} +
+ ), + }, + ]} + /> + ) }