mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
feat(help): in-app help center with per-screen guides and pictures
A help icon on every screen opens a panel with the screen's overview, its sub-screens, task guides with a framed picture per step, search across guides and the wiki, and a switch to any other screen's help. Guides exist for the dashboard, Vacay, Atlas, Collections, Journey (list, journal, studio), Settings (all tabs), Admin (all tabs) and the trip frame; the trip's own screens are in progress. Pictures are produced by Playwright (client/e2e/help), texts in all 23 languages.
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { http, HttpResponse, delay } from 'msw'
|
||||
import { server } from '../../tests/helpers/msw/server'
|
||||
import { useHelpStore, docTitle } from './helpStore'
|
||||
|
||||
const initial = useHelpStore.getState()
|
||||
|
||||
const HIT = { slug: 'Creating-a-Trip', title: 'Creating a Trip', section: 'Planning', anchor: 'fields', heading: 'Fields', snippet: 'Give the trip a name…', score: 12 }
|
||||
|
||||
beforeEach(() => {
|
||||
useHelpStore.setState(initial, true)
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
/** Let the debounce elapse and the request settle. */
|
||||
async function flush(ms = 300): Promise<void> {
|
||||
await vi.advanceTimersByTimeAsync(ms)
|
||||
}
|
||||
|
||||
describe('helpStore: dialog and navigation', () => {
|
||||
it('opens, closes and toggles', () => {
|
||||
const s = useHelpStore.getState()
|
||||
expect(s.open).toBe(false)
|
||||
s.openHelp()
|
||||
expect(useHelpStore.getState().open).toBe(true)
|
||||
s.toggleHelp()
|
||||
expect(useHelpStore.getState().open).toBe(false)
|
||||
s.toggleHelp()
|
||||
expect(useHelpStore.getState().open).toBe(true)
|
||||
s.closeHelp()
|
||||
expect(useHelpStore.getState().open).toBe(false)
|
||||
})
|
||||
|
||||
it('closing clears the search and the lightbox but keeps the guide for next time', () => {
|
||||
const s = useHelpStore.getState()
|
||||
s.openGuide('create-trip')
|
||||
s.openLightbox({ src: '/x.webp', alt: 'x' })
|
||||
useHelpStore.setState({ query: 'trip', docHits: [HIT], searchStatus: 'done' })
|
||||
s.closeHelp()
|
||||
const after = useHelpStore.getState()
|
||||
expect(after.open).toBe(false)
|
||||
expect(after.lightbox).toBeNull()
|
||||
expect(after.query).toBe('')
|
||||
expect(after.docHits).toEqual([])
|
||||
expect(after.searchStatus).toBe('idle')
|
||||
expect(after.guideId).toBe('create-trip')
|
||||
})
|
||||
|
||||
it('records the screen and keeps an open guide across a screen change while the dialog is open', () => {
|
||||
const s = useHelpStore.getState()
|
||||
s.setContext('dashboard')
|
||||
s.openGuide('widgets')
|
||||
s.setContext('settings')
|
||||
const now = useHelpStore.getState()
|
||||
expect(now.contextId).toBe('settings')
|
||||
expect(now.guideId).toBe('widgets')
|
||||
s.setContext(null)
|
||||
expect(useHelpStore.getState().contextId).toBeNull()
|
||||
})
|
||||
|
||||
it('drops a leftover guide when another screen is reached with the dialog closed', () => {
|
||||
const s = useHelpStore.getState()
|
||||
s.setContext('dashboard')
|
||||
s.openGuide('create-trip')
|
||||
s.closeHelp()
|
||||
s.setContext('vacay')
|
||||
const now = useHelpStore.getState()
|
||||
expect(now.contextId).toBe('vacay')
|
||||
expect(now.guideId).toBeNull()
|
||||
})
|
||||
|
||||
it('opening a guide opens the dialog and drops a pending search', async () => {
|
||||
const s = useHelpStore.getState()
|
||||
s.setQuery('cover')
|
||||
expect(useHelpStore.getState().searchStatus).toBe('loading')
|
||||
s.openGuide('cover-image')
|
||||
const now = useHelpStore.getState()
|
||||
expect(now.open).toBe(true)
|
||||
expect(now.guideId).toBe('cover-image')
|
||||
expect(now.query).toBe('')
|
||||
expect(now.searchStatus).toBe('idle')
|
||||
await flush()
|
||||
expect(useHelpStore.getState().docHits).toEqual([])
|
||||
})
|
||||
|
||||
it('closeGuide returns to the overview and closes the lightbox', () => {
|
||||
const s = useHelpStore.getState()
|
||||
s.openGuide('create-trip')
|
||||
s.openLightbox({ src: '/x.webp', alt: 'x' })
|
||||
s.closeGuide()
|
||||
expect(useHelpStore.getState().guideId).toBeNull()
|
||||
expect(useHelpStore.getState().lightbox).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('helpStore: search', () => {
|
||||
it('debounces, then asks the server and stores the hits', async () => {
|
||||
const seen: string[] = []
|
||||
server.use(
|
||||
http.get('/api/help/search', ({ request }) => {
|
||||
seen.push(new URL(request.url).searchParams.get('q') ?? '')
|
||||
return HttpResponse.json({ hits: [HIT] })
|
||||
}),
|
||||
)
|
||||
const s = useHelpStore.getState()
|
||||
s.setQuery('c')
|
||||
s.setQuery('co')
|
||||
s.setQuery('cover')
|
||||
expect(useHelpStore.getState().searchStatus).toBe('loading')
|
||||
await flush()
|
||||
expect(seen).toEqual(['cover'])
|
||||
expect(useHelpStore.getState().docHits).toEqual([HIT])
|
||||
expect(useHelpStore.getState().searchStatus).toBe('done')
|
||||
})
|
||||
|
||||
it('a blank query clears the results without a request', async () => {
|
||||
let calls = 0
|
||||
server.use(http.get('/api/help/search', () => { calls++; return HttpResponse.json({ hits: [HIT] }) }))
|
||||
const s = useHelpStore.getState()
|
||||
s.setQuery('trip')
|
||||
await flush()
|
||||
expect(useHelpStore.getState().docHits).toHaveLength(1)
|
||||
s.setQuery(' ')
|
||||
await flush()
|
||||
expect(useHelpStore.getState().docHits).toEqual([])
|
||||
expect(useHelpStore.getState().searchStatus).toBe('idle')
|
||||
expect(calls).toBe(1)
|
||||
})
|
||||
|
||||
it('a slow older answer never overwrites a newer one', async () => {
|
||||
server.use(
|
||||
http.get('/api/help/search', async ({ request }) => {
|
||||
const q = new URL(request.url).searchParams.get('q')
|
||||
if (q === 'old') {
|
||||
await delay(500)
|
||||
return HttpResponse.json({ hits: [{ ...HIT, title: 'OLD' }] })
|
||||
}
|
||||
return HttpResponse.json({ hits: [{ ...HIT, title: 'NEW' }] })
|
||||
}),
|
||||
)
|
||||
const s = useHelpStore.getState()
|
||||
s.setQuery('old')
|
||||
await flush(250)
|
||||
s.setQuery('new')
|
||||
await flush(1000)
|
||||
expect(useHelpStore.getState().docHits.map(h => h.title)).toEqual(['NEW'])
|
||||
})
|
||||
|
||||
it('reports a failed search instead of pretending there are no docs', async () => {
|
||||
server.use(http.get('/api/help/search', () => HttpResponse.json({ error: 'boom' }, { status: 502 })))
|
||||
useHelpStore.getState().setQuery('trip')
|
||||
await flush()
|
||||
expect(useHelpStore.getState().searchStatus).toBe('error')
|
||||
expect(useHelpStore.getState().docHits).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('helpStore: doc index', () => {
|
||||
it('loads the table of contents once and resolves titles from it', async () => {
|
||||
let calls = 0
|
||||
server.use(
|
||||
http.get('/api/help/index', () => {
|
||||
calls++
|
||||
return HttpResponse.json({ sections: [{ title: 'Planning', pages: [{ slug: 'Creating-a-Trip', title: 'Creating a Trip' }] }] })
|
||||
}),
|
||||
)
|
||||
await useHelpStore.getState().loadDocIndex()
|
||||
await useHelpStore.getState().loadDocIndex()
|
||||
expect(calls).toBe(1)
|
||||
const index = useHelpStore.getState().docIndex
|
||||
expect(docTitle(index, 'Creating-a-Trip')).toBe('Creating a Trip')
|
||||
expect(docTitle(index, 'Dashboard-Widgets')).toBe('Dashboard Widgets')
|
||||
expect(docTitle(null, 'My-Trips-Dashboard')).toBe('My Trips Dashboard')
|
||||
})
|
||||
|
||||
it('falls back to slugs when the index cannot be loaded', async () => {
|
||||
server.use(http.get('/api/help/index', () => HttpResponse.json({}, { status: 500 })))
|
||||
await useHelpStore.getState().loadDocIndex()
|
||||
expect(useHelpStore.getState().docIndex).toEqual([])
|
||||
expect(docTitle(useHelpStore.getState().docIndex, 'Calendar-Feeds')).toBe('Calendar Feeds')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,147 @@
|
||||
import { create } from 'zustand'
|
||||
import axios from 'axios'
|
||||
import { searchHelp, type HelpSearchHit } from '../api/help'
|
||||
import { helpApi, type HelpNavSection } from '../api/client'
|
||||
|
||||
/**
|
||||
* State of the contextual help panel: whether it is open, which screen it is
|
||||
* for, which guide is being read, and the search that runs across guides and
|
||||
* docs. Screens set the context on mount (`useHelpContext`); the panel reads it.
|
||||
*/
|
||||
|
||||
export type HelpSearchStatus = 'idle' | 'loading' | 'done' | 'error'
|
||||
|
||||
/** One picture enlarged over the dialog. */
|
||||
export interface HelpLightbox {
|
||||
src: string
|
||||
alt: string
|
||||
}
|
||||
|
||||
interface HelpState {
|
||||
open: boolean
|
||||
/** The screen the reader is on, as registered by that screen. */
|
||||
contextId: string | null
|
||||
/**
|
||||
* A screen the reader chose to browse in the dialog, other than the one they
|
||||
* are on. Null means "follow the current screen"; opening the dialog resets it.
|
||||
*/
|
||||
browseId: string | null
|
||||
/** The guide currently open inside the panel, if any. */
|
||||
guideId: string | null
|
||||
lightbox: HelpLightbox | null
|
||||
query: string
|
||||
searchStatus: HelpSearchStatus
|
||||
docHits: HelpSearchHit[]
|
||||
/** Wiki table of contents, loaded once so doc links can show real titles. */
|
||||
docIndex: HelpNavSection[] | null
|
||||
|
||||
openHelp: () => void
|
||||
closeHelp: () => void
|
||||
toggleHelp: () => void
|
||||
setContext: (id: string | null) => void
|
||||
/** Browse another screen's help from the switcher; null returns to the current screen. */
|
||||
browse: (id: string | null) => void
|
||||
openGuide: (id: string) => void
|
||||
closeGuide: () => void
|
||||
openLightbox: (content: HelpLightbox | null) => void
|
||||
setQuery: (query: string) => void
|
||||
loadDocIndex: () => Promise<void>
|
||||
}
|
||||
|
||||
/** Wait this long after the last keystroke before asking the server. */
|
||||
const SEARCH_DEBOUNCE_MS = 220
|
||||
|
||||
let searchTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let searchAbort: AbortController | null = null
|
||||
|
||||
function cancelSearch(): void {
|
||||
if (searchTimer) clearTimeout(searchTimer)
|
||||
searchTimer = null
|
||||
searchAbort?.abort()
|
||||
searchAbort = null
|
||||
}
|
||||
|
||||
export const useHelpStore = create<HelpState>((set, get) => ({
|
||||
open: false,
|
||||
contextId: null,
|
||||
browseId: null,
|
||||
guideId: null,
|
||||
lightbox: null,
|
||||
query: '',
|
||||
searchStatus: 'idle',
|
||||
docHits: [],
|
||||
docIndex: null,
|
||||
|
||||
openHelp: () => set({ open: true, browseId: null }),
|
||||
closeHelp: () => {
|
||||
cancelSearch()
|
||||
set({ open: false, lightbox: null, query: '', searchStatus: 'idle', docHits: [] })
|
||||
},
|
||||
toggleHelp: () => (get().open ? get().closeHelp() : get().openHelp()),
|
||||
|
||||
setContext: (id) => {
|
||||
const { contextId, open } = get()
|
||||
if (contextId === id) return
|
||||
// While the dialog is open a guide survives a screen change on purpose:
|
||||
// the "widgets" guide walks the reader from the dashboard into Settings,
|
||||
// and closing it the moment they arrive would defeat the point. With the
|
||||
// dialog closed, arriving on another screen means the next open should
|
||||
// show that screen, not a guide left over from the last one.
|
||||
set(open ? { contextId: id } : { contextId: id, guideId: null })
|
||||
},
|
||||
|
||||
browse: (id) => {
|
||||
cancelSearch()
|
||||
set({ browseId: id, guideId: null, lightbox: null, query: '', searchStatus: 'idle', docHits: [] })
|
||||
},
|
||||
|
||||
openGuide: (id) => {
|
||||
cancelSearch()
|
||||
set({ guideId: id, open: true, query: '', searchStatus: 'idle', docHits: [] })
|
||||
},
|
||||
closeGuide: () => set({ guideId: null, lightbox: null }),
|
||||
openLightbox: (content) => set({ lightbox: content }),
|
||||
|
||||
setQuery: (query) => {
|
||||
cancelSearch()
|
||||
const q = query.trim()
|
||||
if (!q) {
|
||||
set({ query, searchStatus: 'idle', docHits: [] })
|
||||
return
|
||||
}
|
||||
set({ query, searchStatus: 'loading' })
|
||||
searchTimer = setTimeout(() => {
|
||||
const controller = new AbortController()
|
||||
searchAbort = controller
|
||||
searchHelp(q, controller.signal)
|
||||
.then((hits) => {
|
||||
if (controller.signal.aborted) return
|
||||
set({ docHits: hits, searchStatus: 'done' })
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (controller.signal.aborted || axios.isCancel(err)) return
|
||||
set({ docHits: [], searchStatus: 'error' })
|
||||
})
|
||||
}, SEARCH_DEBOUNCE_MS)
|
||||
},
|
||||
|
||||
loadDocIndex: async () => {
|
||||
if (get().docIndex) return
|
||||
try {
|
||||
const { sections } = await helpApi.index()
|
||||
set({ docIndex: sections })
|
||||
} catch {
|
||||
// Titles fall back to the slug; the links still work.
|
||||
set({ docIndex: [] })
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
||||
/** Title of a wiki page from the loaded index, or a readable form of its slug. */
|
||||
export function docTitle(index: HelpNavSection[] | null, slug: string): string {
|
||||
for (const section of index ?? []) {
|
||||
const page = section.pages.find(p => p.slug === slug)
|
||||
if (page) return page.title
|
||||
}
|
||||
return slug.replace(/-/g, ' ')
|
||||
}
|
||||
Reference in New Issue
Block a user