File size: 1,402 Bytes
377fc96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// The twelve sections, read from the shelf table the build writes from packs/sections.tsv.
// No screen names a section in code; every list, title, and icon comes from here.

import type { Database } from './db';
import { rows } from './db';

export interface SectionRow {
  id: string;
  order: number;
  title: string;
  scope: string;
  icon: string;
  gloss: string;
  accent: string | null;
}

const cache = new WeakMap<Database, SectionRow[]>();

/** Every section in order, memoised per database instance. */
export function sections(db: Database): SectionRow[] {
  let list = cache.get(db);
  if (!list) {
    const cols = rows<{ name: string }>(db, 'PRAGMA table_info(shelf)').map((c) => c.name);
    const accent = cols.includes('accent') ? 'accent' : 'NULL AS accent';
    list = rows<{ id: string; order: number; title: string; scope_line: string | null; icon: string | null; gloss: string | null; accent: string | null }>(
      db,
      `SELECT id, "order" AS "order", title, scope_line, icon, gloss, ${accent} FROM shelf ORDER BY "order"`,
    ).map((r) => ({ id: r.id, order: r.order, title: r.title, scope: r.scope_line ?? '', icon: r.icon ?? 'book-open', gloss: r.gloss ?? '', accent: r.accent ?? null }));
    cache.set(db, list);
  }
  return list;
}

export function sectionTitle(db: Database, id: string): string {
  return sections(db).find((s) => s.id === id)?.title ?? id;
}