File size: 2,045 Bytes
1187856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { getDb } from "@/lib/db";
import { all } from "@/lib/db/helpers";
import { contentValue, hasLocalizedText, localized, type LocalizedText } from "@/lib/i18n";

export interface TriWorldNavItem {
  id: number;
  itemKey: string;
  labelText: LocalizedText;
  href: string;
  openInNewTab: boolean;
}

interface NavItemRow {
  id: number;
  item_key: string;
  label_en: string;
  label_zh: string;
  href: string;
  open_in_new_tab: number;
  is_visible: number;
}

const FALLBACK_NAV_ITEMS: TriWorldNavItem[] = ([
  ["abstract", "Abstract", "摘要", "#abstract"],
  ["overview", "Overview", "概览", "#overview"],
  ["advantages", "Advantages", "优势", "#advantages"],
  ["cases", "Cases", "案例", "#cases"],
  ["metrics", "Metrics", "指标", "#metrics"],
  ["leaderboard", "Leaderboard", "榜单", "#leaderboard"],
  ["visualization", "Visualization", "可视化", "#visualization"],
  ["datasets", "Datasets", "数据集", "#datasets"],
  ["policies", "Policy&Rules", "政策与规则", "#policies"],
  ["submission", "Submission", "提交", "#submission"],
  ["contact", "Contact Us", "联系我们", "#contact"],
] as const).map(([itemKey, labelEn, labelZh, href], index) => ({
  id: index + 1,
  itemKey,
  labelText: localized(labelEn, labelZh),
  href,
  openInNewTab: false,
}));

export function getTriWorldNavItems(): TriWorldNavItem[] {
  try {
    const rows = all<NavItemRow>(
      getDb().prepare("SELECT * FROM triworld_nav_items ORDER BY sort_order, id")
    );
    if (!rows.length) return FALLBACK_NAV_ITEMS;
    return rows
      .map((row) => ({
        id: row.id,
        itemKey: row.item_key,
        labelText: localized(row.label_en, row.label_zh),
        href: contentValue(row.href),
        openInNewTab: Number(row.open_in_new_tab) > 0,
        isVisible: Number(row.is_visible) > 0,
      }))
      .filter((item) => item.isVisible && hasLocalizedText(item.labelText) && Boolean(item.href))
      .map(({ isVisible: _isVisible, ...item }) => item);
  } catch {
    return FALLBACK_NAV_ITEMS;
  }
}