Add a QR code page at /qr for any page on the site
Pick "Main navigation" or a kind of record (events, organizations, teams, awards, people), then the page or record, and for a main page optionally one of its #sections. The code, a copy-link button, and PNG (1024px) and SVG downloads follow. An "NGU dove in the centre" option puts the dove on a white badge over the code and raises error correction to H so it still scans. Codes always point at SITE_ORIGIN (https://nextgenerationofunity.org, in src/lib/site.ts), so one made on localhost works on a poster. Main pages come from navConfig.ts and record URLs from hrefs.ts, the same sources the site links with. GET /api/directory lists every published record with a page, as id, name and (for organizations) kind, each filtered by the same rule as that record's own page: a team also needs its organization published. /api/people only answers for ids it's given, so there was no public list to read. The `qrcode` package does the encoding; QrCode.tsx draws the modules as one SVG path, so the preview and both downloads are one picture. The page is lazy-loaded so the encoder isn't in the main bundle. The page is unlisted: not in navConfig, so not in the header or footer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8c26ec4906
commit
97c1827ca9
11 changed files with 892 additions and 0 deletions
48
src/lib/qr.ts
Normal file
48
src/lib/qr.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
QR CODES
|
||||
|
||||
The encoding comes from the `qrcode` package: which modules are
|
||||
dark, at which error-correction level. Drawing is QrCode.tsx's
|
||||
job, so the on-screen code, the SVG download and the PNG download
|
||||
are one picture rather than three renderers that could disagree.
|
||||
|
||||
Error correction is the share of the code that can be damaged and
|
||||
still scan. A logo over the middle is damage, so a code carrying
|
||||
the dove uses 'H' (about 30% recoverable); a plain one uses 'M',
|
||||
which keeps it less dense and easier to scan from a distance.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { create } from 'qrcode'
|
||||
|
||||
export type QrLevel = 'L' | 'M' | 'Q' | 'H'
|
||||
|
||||
export type QrMatrix = {
|
||||
/** Modules per side, not counting the quiet zone. */
|
||||
size: number
|
||||
dark: (row: number, col: number) => boolean
|
||||
}
|
||||
|
||||
export function qrMatrix(text: string, level: QrLevel): QrMatrix {
|
||||
const { modules } = create(text, { errorCorrectionLevel: level })
|
||||
return { size: modules.size, dark: (row, col) => modules.get(row, col) === 1 }
|
||||
}
|
||||
|
||||
/* The dark modules as one SVG path, a run of horizontal neighbours
|
||||
per subpath, offset by the quiet zone. One element instead of a
|
||||
rect per module keeps the markup (and the SVG download) small. */
|
||||
export function qrPath(matrix: QrMatrix, margin: number): string {
|
||||
const parts: string[] = []
|
||||
for (let row = 0; row < matrix.size; row++) {
|
||||
let col = 0
|
||||
while (col < matrix.size) {
|
||||
if (!matrix.dark(row, col)) {
|
||||
col++
|
||||
continue
|
||||
}
|
||||
const start = col
|
||||
while (col < matrix.size && matrix.dark(row, col)) col++
|
||||
parts.push(`M${start + margin} ${row + margin}h${col - start}v1h-${col - start}z`)
|
||||
}
|
||||
}
|
||||
return parts.join('')
|
||||
}
|
||||
13
src/lib/site.ts
Normal file
13
src/lib/site.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
SITE
|
||||
|
||||
The public address of the live site. Used where a URL has to
|
||||
leave the browser and still work — a printed QR code made on
|
||||
localhost or a preview has to point here, not at wherever it was
|
||||
generated. Everything inside the site links by path, not this.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
export const SITE_ORIGIN = 'https://nextgenerationofunity.org'
|
||||
|
||||
/** A site path as a full URL on the live site. */
|
||||
export const siteUrl = (path: string): string => new URL(path, SITE_ORIGIN).href
|
||||
|
|
@ -336,3 +336,20 @@ export type PersonRecord = {
|
|||
|
||||
export const usePerson = (id?: string): Resource<PersonRecord> =>
|
||||
useRecord<PersonRecord>(detailPath('/people', id), 'person')
|
||||
|
||||
/* ── Directory ─────────────────────────────────────────────────
|
||||
GET /directory: every public record that has a page, one list
|
||||
per kind, each row just enough to build its URL with hrefs.ts. */
|
||||
|
||||
export type DirectoryEntry = { id: string; name: string }
|
||||
|
||||
export type Directory = {
|
||||
events: Array<DirectoryEntry & { starts_on?: string | null }>
|
||||
organizations: Array<DirectoryEntry & { kind: OrganizationRecord['kind'] }>
|
||||
teams: Array<DirectoryEntry & { org_name: string }>
|
||||
awards: DirectoryEntry[]
|
||||
people: DirectoryEntry[]
|
||||
}
|
||||
|
||||
export const useDirectory = (): Resource<Directory> =>
|
||||
useRecord<Directory>('/directory', 'directory')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue