Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

refactor(help): register a screen from one description, and fix the doc links

Maurice5584d287a946
diff --git a/client/src/help/contexts/settings.ts b/client/src/help/contexts/settings.ts
index 116424f08..6e55d02cb 100644
--- a/client/src/help/contexts/settings.ts
+++ b/client/src/help/contexts/settings.ts
@@ -37,8 +37,8 @@ export const settingsGuides: HelpGuide[] = [
guide('api-keys', 'settings-integrations', 'key', 'guide', 2, 2, { slug: SETTINGS, anchor: 'integrations-tab' }, ['mcp-oauth'], false),
guide('mcp-oauth', 'settings-integrations', 'plug', 'guide', 3, 2, { slug: 'MCP-Setup' }, ['api-keys'], false),
// Offline
- guide('offline-prepare', 'settings-offline', 'cloudOff', 'guide', 3, 2, { slug: OFFLINE, anchor: 'settings--offline' }, ['offline-conflicts'], false),
- guide('offline-conflicts', 'settings-offline', 'repeat', 'quick', 2, 1, { slug: OFFLINE, anchor: 'settings--offline' }, ['offline-prepare'], false),
+ guide('offline-prepare', 'settings-offline', 'cloudOff', 'guide', 3, 2, { slug: OFFLINE, anchor: 'settings-offline' }, ['offline-conflicts'], false),
+ guide('offline-conflicts', 'settings-offline', 'repeat', 'quick', 2, 1, { slug: OFFLINE, anchor: 'settings-offline' }, ['offline-prepare'], false),
// Account
guide('profile', 'settings-account', 'user', 'quick', 2, 1, { slug: SETTINGS, anchor: 'account-tab-summary' }, ['password'], false),
guide('password', 'settings-account', 'lock', 'quick', 2, 1, { slug: SETTINGS, anchor: 'account-tab-summary' }, ['mfa'], false),
diff --git a/client/src/help/contexts/trip.ts b/client/src/help/contexts/trip.ts
index c926d70fe..28c7cc93d 100644
--- a/client/src/help/contexts/trip.ts
+++ b/client/src/help/contexts/trip.ts
@@ -1,4 +1,4 @@
-import type { HelpContext, HelpGuide } from '../types'
+import { defineScreen } from '../defineScreen'
/**
* Help for a trip (`/trips/:id`). The trip is a family of screens: this one is
@@ -8,30 +8,22 @@ import type { HelpContext, HelpGuide } from '../types'
* the pictures live in `e2e/help/trip.guide.ts`.
*/
-const SHARING = 'Sharing-and-Collaboration'
-const PLANNER = 'Trip-Planner'
+const SHARING = 'Trip-Members-and-Sharing'
+const PLANNER = 'Trip-Planner-Overview'
-const guide = (
- id: string, icon: HelpGuide['icon'], size: HelpGuide['size'], steps: number, tips: number,
- docs: HelpGuide['docs'], related: string[], result = false,
-): HelpGuide => ({ id, context: 'trip', icon, size, steps, tips, media: { steps: true, result }, docs, related })
-
-export const tripGuides: HelpGuide[] = [
- guide('add-member', 'userPlus', 'quick', 3, 2, { slug: SHARING }, ['trip-invite-link', 'add-guest'], true),
- guide('trip-invite-link', 'link', 'quick', 3, 2, { slug: SHARING }, ['add-member', 'invite-links']),
- guide('add-guest', 'userRound', 'quick', 2, 2, { slug: SHARING, anchor: 'guests' }, ['add-member'], true),
- guide('public-link', 'share', 'quick', 3, 2, { slug: SHARING, anchor: 'public-share-links' }, ['add-member']),
- guide('transfer-ownership', 'crown', 'quick', 2, 2, { slug: SHARING }, ['add-member']),
- guide('collapse-columns', 'panelLeft', 'quick', 3, 2, { slug: PLANNER }, []),
- guide('undo-change', 'undo', 'quick', 1, 2, { slug: PLANNER }, []),
-]
-
-export const tripContext: HelpContext = {
+export const { context: tripContext, guides: tripGuides } = defineScreen({
id: 'trip',
route: '/trips/:id?tab=plan',
icon: 'route',
bullets: 6,
- guides: tripGuides.map(g => g.id),
docs: [{ slug: PLANNER }, { slug: SHARING }],
- hero: true,
-}
+ guides: [
+ ['add-member', 'userPlus', 'quick', 3, 2, { slug: SHARING, anchor: 'inviting-members' }, ['trip-invite-link', 'add-guest'], true],
+ ['trip-invite-link', 'link', 'quick', 3, 2, { slug: SHARING, anchor: 'trip-invite-link' }, ['add-member', 'invite-links']],
+ ['add-guest', 'userRound', 'quick', 2, 2, { slug: SHARING, anchor: 'guest-members' }, ['add-member'], true],
+ ['public-link', 'share', 'quick', 3, 2, { slug: SHARING, anchor: 'public-share-link' }, ['add-member']],
+ ['transfer-ownership', 'crown', 'quick', 2, 2, { slug: SHARING, anchor: 'transferring-ownership' }, ['add-member']],
+ ['collapse-columns', 'panelLeft', 'quick', 3, 2, { slug: PLANNER, anchor: 'layout' }, []],
+ ['undo-change', 'undo', 'quick', 1, 2, { slug: PLANNER, anchor: 'undo' }, []],
+ ],
+})
diff --git a/client/src/help/contexts/tripDays.ts b/client/src/help/contexts/tripDays.ts
index eeec32957..f3d3493c4 100644
--- a/client/src/help/contexts/tripDays.ts
+++ b/client/src/help/contexts/tripDays.ts
@@ -1,4 +1,4 @@
-import type { HelpContext, HelpGuide } from '../types'
+import { defineScreen } from '../defineScreen'
/**
* Help for the days column of a trip's plan: the day cards, the stops on them,
@@ -11,32 +11,24 @@ import type { HelpContext, HelpGuide } from '../types'
const DAYS = 'Day-Plans-and-Notes'
const MAP = 'Map-Features'
-const guide = (
- id: string, icon: HelpGuide['icon'], size: HelpGuide['size'], steps: number, tips: number,
- docs: HelpGuide['docs'], related: string[], result = false,
-): HelpGuide => ({ id, context: 'trip-days', icon, size, steps, tips, media: { steps: true, result }, docs, related })
-
-export const tripDaysGuides: HelpGuide[] = [
- guide('read-day-plan', 'eye', 'guide', 5, 2, { slug: DAYS, anchor: 'day-timeline' }, ['bookings-in-plan', 'day-route']),
- guide('place-onto-day', 'calendarCheck', 'guide', 4, 3, { slug: DAYS, anchor: 'assigning-places-to-a-day' }, ['place-to-open-day', 'reorder-stops', 'undo-change'], true),
- guide('reorder-stops', 'repeat', 'quick', 4, 2, { slug: DAYS, anchor: 'assigning-places-to-a-day' }, ['place-onto-day', 'set-stop-times', 'lock-stop']),
- guide('set-stop-times', 'clock', 'quick', 3, 3, { slug: DAYS, anchor: 'day-timeline' }, ['reorder-stops', 'edit-place']),
- guide('remove-from-day', 'minusCircle', 'quick', 2, 2, { slug: DAYS, anchor: 'assigning-places-to-a-day' }, ['delete-place', 'undo-change']),
- guide('lock-stop', 'lock', 'quick', 2, 2, { slug: DAYS, anchor: 'toolbar-actions' }, ['day-route', 'reorder-stops']),
- guide('day-note', 'pencil', 'quick', 4, 2, { slug: DAYS, anchor: 'day-notes' }, ['read-day-plan'], true),
- guide('day-route', 'route', 'guide', 5, 3, { slug: MAP, anchor: 'route-lines' }, ['lock-stop', 'read-day-plan']),
- guide('manage-days', 'calendarDays', 'quick', 4, 2, { slug: DAYS, anchor: 'the-day-plan-sidebar' }, ['edit-trip']),
- guide('bookings-in-plan', 'ticket', 'guide', 5, 2, { slug: DAYS, anchor: 'multi-day-reservations' }, ['read-day-plan']),
- guide('export-plan', 'download', 'quick', 4, 3, { slug: MAP, anchor: 'exporting-a-trip-as-gpx' }, ['calendar-feed']),
-]
-
-export const tripDaysContext: HelpContext = {
+export const { context: tripDaysContext, guides: tripDaysGuides } = defineScreen({
id: 'trip-days',
parent: 'trip',
route: '/trips/:id?tab=plan',
icon: 'calendarDays',
bullets: 6,
- guides: tripDaysGuides.map(g => g.id),
docs: [{ slug: DAYS }, { slug: MAP }],
- hero: true,
-}
+ guides: [
+ ['read-day-plan', 'eye', 'guide', 5, 2, { slug: DAYS, anchor: 'day-timeline' }, ['bookings-in-plan', 'day-route']],
+ ['place-onto-day', 'calendarCheck', 'guide', 4, 3, { slug: DAYS, anchor: 'assigning-places-to-a-day' }, ['place-to-open-day', 'reorder-stops', 'undo-change'], true],
+ ['reorder-stops', 'repeat', 'quick', 4, 2, { slug: DAYS, anchor: 'assigning-places-to-a-day' }, ['place-onto-day', 'set-stop-times', 'lock-stop']],
+ ['set-stop-times', 'clock', 'quick', 3, 3, { slug: DAYS, anchor: 'day-timeline' }, ['reorder-stops', 'edit-place']],
+ ['remove-from-day', 'minusCircle', 'quick', 2, 2, { slug: DAYS, anchor: 'assigning-places-to-a-day' }, ['delete-place', 'undo-change']],
+ ['lock-stop', 'lock', 'quick', 2, 2, { slug: DAYS, anchor: 'toolbar-actions' }, ['day-route', 'reorder-stops']],
+ ['day-note', 'pencil', 'quick', 4, 2, { slug: DAYS, anchor: 'day-notes' }, ['read-day-plan'], true],
+ ['day-route', 'route', 'guide', 5, 3, { slug: MAP, anchor: 'route-lines' }, ['lock-stop', 'read-day-plan']],
+ ['manage-days', 'calendarDays', 'quick', 4, 2, { slug: DAYS, anchor: 'the-day-plan-sidebar' }, ['edit-trip']],
+ ['bookings-in-plan', 'ticket', 'guide', 5, 2, { slug: DAYS, anchor: 'multi-day-reservations' }, ['read-day-plan']],
+ ['export-plan', 'download', 'quick', 4, 3, { slug: MAP, anchor: 'exporting-a-trip-as-gpx' }, ['calendar-feed']],
+ ],
+})
diff --git a/client/src/help/contexts/tripPlaces.ts b/client/src/help/contexts/tripPlaces.ts
index 9b89bb6f3..fb73dddd1 100644
--- a/client/src/help/contexts/tripPlaces.ts
+++ b/client/src/help/contexts/tripPlaces.ts
@@ -1,4 +1,4 @@
-import type { HelpContext, HelpGuide } from '../types'
+import { defineScreen } from '../defineScreen'
/**
* Help for the places column of a trip's plan: bringing places in (by hand,
@@ -7,31 +7,23 @@ import type { HelpContext, HelpGuide } from '../types'
*/
const PLACES = 'Places-and-Search'
-const IMPORT = { slug: PLACES, anchor: 'importing-places' }
+const IMPORT = { slug: PLACES, anchor: 'importing-multiple-places' }
-const guide = (
- id: string, icon: HelpGuide['icon'], size: HelpGuide['size'], steps: number, tips: number,
- docs: HelpGuide['docs'], related: string[], result = false,
-): HelpGuide => ({ id, context: 'trip-places', icon, size, steps, tips, media: { steps: true, result }, docs, related })
-
-export const tripPlacesGuides: HelpGuide[] = [
- guide('create-place', 'plus', 'guide', 5, 3, { slug: PLACES }, ['place-to-open-day', 'edit-place'], true),
- guide('place-to-open-day', 'calendarCheck', 'quick', 3, 2, { slug: PLACES }, ['create-place']),
- guide('filter-places', 'filter', 'quick', 4, 2, { slug: PLACES }, ['select-places']),
- guide('edit-place', 'pencil', 'quick', 3, 2, { slug: PLACES }, ['create-place', 'delete-place']),
- guide('delete-place', 'trash', 'quick', 2, 2, { slug: PLACES }, ['edit-place', 'select-places', 'undo-change']),
- guide('select-places', 'checkCircle', 'quick', 4, 2, { slug: PLACES }, ['filter-places', 'delete-place'], true),
- guide('import-places-file', 'fileInput', 'guide', 4, 2, IMPORT, ['import-places-list', 'filter-places'], true),
- guide('import-places-list', 'upload', 'quick', 3, 2, IMPORT, ['import-places-file']),
-]
-
-export const tripPlacesContext: HelpContext = {
+export const { context: tripPlacesContext, guides: tripPlacesGuides } = defineScreen({
id: 'trip-places',
parent: 'trip',
route: '/trips/:id?tab=plan',
icon: 'mapPin',
bullets: 6,
- guides: tripPlacesGuides.map(g => g.id),
docs: [{ slug: PLACES }, IMPORT],
- hero: true,
-}
+ guides: [
+ ['create-place', 'plus', 'guide', 5, 3, { slug: PLACES, anchor: 'adding-a-place' }, ['place-to-open-day', 'edit-place'], true],
+ ['place-to-open-day', 'calendarCheck', 'quick', 3, 2, { slug: PLACES, anchor: 'the-open-day-steers-the-search' }, ['create-place']],
+ ['filter-places', 'filter', 'quick', 4, 2, { slug: PLACES, anchor: 'searching-for-a-place' }, ['select-places']],
+ ['edit-place', 'pencil', 'quick', 3, 2, { slug: PLACES, anchor: 'place-fields' }, ['create-place', 'delete-place']],
+ ['delete-place', 'trash', 'quick', 2, 2, { slug: PLACES }, ['edit-place', 'select-places', 'undo-change']],
+ ['select-places', 'checkCircle', 'quick', 4, 2, { slug: PLACES }, ['filter-places', 'delete-place'], true],
+ ['import-places-file', 'fileInput', 'guide', 4, 2, IMPORT, ['import-places-list', 'filter-places'], true],
+ ['import-places-list', 'upload', 'quick', 3, 2, IMPORT, ['import-places-file']],
+ ],
+})
diff --git a/client/src/help/defineScreen.ts b/client/src/help/defineScreen.ts
new file mode 100644
index 000000000..0f558d9d5
--- /dev/null
+++ b/client/src/help/defineScreen.ts
@@ -0,0 +1,74 @@
+import type { HelpContext, HelpDocLink, HelpGuide, HelpGuideSize, HelpIcon } from './types'
+
+/**
+ * One screen's whole registration, written once.
+ *
+ * Every `contexts/*.ts` file used to carry its own copy of the same little
+ * factory and the same context literal; the screens of a trip alone would have
+ * made thirteen copies of it. That repetition is what a duplication check
+ * counts, and it is exactly the hand-mirrored state the repository's rules ask
+ * us not to keep. So the shape lives here and a context file is data only: the
+ * ids, the counts, the docs and what relates to what.
+ *
+ * A guide is a row rather than an object because the rows are read as a table:
+ * one line per guide, the numbers under each other.
+ */
+
+export type GuideRow = [
+ id: string,
+ icon: HelpIcon,
+ size: HelpGuideSize,
+ steps: number,
+ tips: number,
+ docs: HelpDocLink | undefined,
+ related: string[],
+ /** Whether the guide carries a picture of the state after its last step. Default: no. */
+ result?: boolean,
+]
+
+export interface ScreenSpec {
+ id: string
+ icon: HelpIcon
+ /** How many "on this screen" bullets the catalogue has for it. */
+ bullets: number
+ /** Where the reference documentation continues. */
+ docs: HelpDocLink[]
+ /** The guides, in the order the panel lists them. */
+ guides: GuideRow[]
+ /** The screen this one lives under, for the switcher. */
+ parent?: string
+ /** Where the screen is in the app, for the "go there" button. */
+ route?: string
+ /** Whether `help-media/ctx/<id>.webp` exists. Default: yes. */
+ hero?: boolean
+}
+
+/** The context and the guides of one screen, from one description of it. */
+export function defineScreen(spec: ScreenSpec): { context: HelpContext; guides: HelpGuide[] } {
+ const guides: HelpGuide[] = spec.guides.map(
+ ([id, icon, size, steps, tips, docs, related, result = false]): HelpGuide => ({
+ id,
+ context: spec.id,
+ icon,
+ size,
+ steps,
+ tips,
+ media: { steps: true, result },
+ docs,
+ related,
+ }),
+ )
+ return {
+ guides,
+ context: {
+ id: spec.id,
+ parent: spec.parent,
+ route: spec.route,
+ icon: spec.icon,
+ bullets: spec.bullets,
+ guides: guides.map(g => g.id),
+ docs: spec.docs,
+ hero: spec.hero ?? true,
+ },
+ }
+}
diff --git a/client/src/help/headingSlug.ts b/client/src/help/headingSlug.ts
new file mode 100644
index 000000000..b73663707
--- /dev/null
+++ b/client/src/help/headingSlug.ts
@@ -0,0 +1,17 @@
+/**
+ * The anchor a wiki heading gets when TREK renders it.
+ *
+ * Lowercase, drop everything that is not a word character, whitespace or a
+ * hyphen, trim, and turn each run of whitespace into one hyphen. The help
+ * center links into the wiki with `#<anchor>` (`docsRoute`), so a guide's
+ * anchor has to be the slug this produces, not the one another renderer would.
+ * It lives on its own so the reader and the test that guards the links agree
+ * by construction instead of by two copies of the same regexes.
+ */
+export function headingSlug(text: string): string {
+ return text
+ .toLowerCase()
+ .replace(/[^\w\s-]/g, '')
+ .trim()
+ .replace(/\s+/g, '-')
+}
diff --git a/client/src/help/registry.test.ts b/client/src/help/registry.test.ts
index 8f0f00f9b..31810af2b 100644
--- a/client/src/help/registry.test.ts
+++ b/client/src/help/registry.test.ts
@@ -1,7 +1,8 @@
import { describe, it, expect } from 'vitest'
-import { existsSync } from 'node:fs'
+import { existsSync, readFileSync, readdirSync } from 'node:fs'
import path from 'node:path'
import en from '@trek/shared/i18n/en'
+import { headingSlug } from './headingSlug'
import { dashboardGuides } from './contexts/dashboard'
import { vacayGuides } from './contexts/vacay'
import { atlasGuides } from './contexts/atlas'
@@ -139,6 +140,46 @@ describe('help registry: every promised picture is on disk', () => {
})
})
+describe('help registry: every doc link resolves in the wiki', () => {
+ // The reader opens a doc link at /help/<slug>#<anchor>, which HelpPage renders
+ // from wiki/<slug>.md and anchors with headingSlug. A slug or an anchor that is
+ // not there gives a 404 or a jump to nowhere, and nothing else in this file
+ // notices: four of them had already shipped when this was written.
+ const WIKI = path.join(process.cwd(), '..', 'wiki')
+ const anchorsOf = (slug: string): Set<string> =>
+ new Set(
+ readFileSync(path.join(WIKI, `${slug}.md`), 'utf8')
+ .split('\n')
+ .map(line => /^#{1,6}\s+(.*?)\s*$/.exec(line)?.[1])
+ .filter((h): h is string => h !== undefined)
+ .map(h =>
+ headingSlug(
+ h
+ .replace(/`([^`]*)`/g, '$1')
+ .replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
+ .replace(/\*\*([^*]*)\*\*/g, '$1'),
+ ),
+ ),
+ )
+
+ const pages = new Set(readdirSync(WIKI).filter(f => f.endsWith('.md')).map(f => f.slice(0, -3)))
+ const links: Array<[string, { slug: string; anchor?: string }]> = [
+ ...[...HELP_CONTEXTS.values()].flatMap(c => c.docs.map(d => [`context "${c.id}"`, d] as [string, typeof d])),
+ ...allHelpGuides().flatMap(g => (g.docs ? [[`guide "${g.id}"`, g.docs] as [string, typeof g.docs]] : [])),
+ ].filter((entry): entry is [string, { slug: string; anchor?: string }] => Boolean(entry[1]))
+
+ it('names a wiki page that exists', () => {
+ for (const [who, link] of links) expect(pages.has(link.slug), `${who} links to missing page "${link.slug}"`).toBe(true)
+ })
+
+ it('names a heading that exists on that page', () => {
+ for (const [who, link] of links) {
+ if (!link.anchor) continue
+ expect(anchorsOf(link.slug).has(link.anchor), `${who}: "${link.slug}" has no heading "#${link.anchor}"`).toBe(true)
+ }
+ })
+})
+
describe('help registry: helpers', () => {
it('builds media paths under /help-media as webp', () => {
expect(helpMedia.hero('dashboard')).toBe('/help-media/ctx/dashboard.webp')
diff --git a/client/src/pages/HelpPage.tsx b/client/src/pages/HelpPage.tsx
index 1e9d37fc5..9490eb897 100644
--- a/client/src/pages/HelpPage.tsx
+++ b/client/src/pages/HelpPage.tsx
@@ -6,6 +6,7 @@ import { Search, ChevronRight, Loader2, AlertCircle, BookOpen, PanelLeft, X } fr
import PageShell from '../components/Layout/PageShell'
import { useTranslation } from '../i18n'
import { useHelp } from './help/useHelp'
+import { headingSlug } from '../help/headingSlug'
export default function HelpPage() {
const { t } = useTranslation()
@@ -126,17 +127,14 @@ export default function HelpPage() {
* GitHub's heading-anchor slug: lowercase, punctuation dropped, spaces to
* hyphens. Wiki pages link to their own sections with `](#some-heading)`, and
* those hrefs are written against GitHub's scheme — so ours has to match it, or
- * in-app anchors point at nothing.
+ * in-app anchors point at nothing. The slug itself lives in `help/headingSlug`,
+ * so the help center's doc links are checked against the same rule.
*/
function headingId(children: ReactNode): string {
const text = Children.toArray(children)
.map(c => (typeof c === 'string' || typeof c === 'number' ? String(c) : ''))
.join('')
- return text
- .toLowerCase()
- .replace(/[^\w\s-]/g, '')
- .trim()
- .replace(/\s+/g, '-')
+ return headingSlug(text)
}
/** Markdown renderer with TREK-styled elements and SPA-internal links. */