返回 CodeWhale
links.ts
根目录 / web / lib / i18n / links.ts
1 /**
2 * Locale-aware chrome link sets (#4934).
3 *
4 * Nav and footer used to carry hardcoded `/en/...` and `/zh/...` arrays plus
5 * a thin dictionary branch for everything else, so foreign locales silently
6 * lost the Start and FAQ links. There is now one generator per link set:
7 * labels come from `ChromeDict`, hrefs come from the locale, and every
8 * routed locale gets the identical route shape. Locale-swap parity is a
9 * unit test (`docs-ia.test.ts`) instead of a string scrape over the TSX.
10 */
11 import type { ChromeDict } from "./dictionaries/types";
12
13 export const REPO_URL = "https://github.com/codewhale-hq/CodeWhale";
14 export const REPO_ISSUES_URL = `${REPO_URL}/issues`;
15 export const REPO_RELEASES_URL = `${REPO_URL}/releases`;
16 export const REPO_LICENSE_URL = `${REPO_URL}/blob/main/LICENSE`;
17 /** Community chat. A brand name, so it is not a dictionary string. */
18 export const DISCORD_URL = "https://discord.gg/37gfS3ksug";
19
20 /** The hosted Codewhale app (account sign-in / sign-up). */
21 export const APP_URL = "https://app.codewhale.net";
22 export const APP_LOGIN_URL = `${APP_URL}/login`;
23 export const APP_SIGNUP_URL = `${APP_URL}/signup`;
24
25 /** A chrome link: one label per destination. */
26 export interface ChromeLink {
27 href: string;
28 label: string;
29 }
30
31 export const SECURITY_POLICY_URL = `${REPO_URL}/blob/main/.github/SECURITY.md`;
32
33 export function navLinks(locale: string, chrome: ChromeDict): ChromeLink[] {
34 return [
35 { href: `/${locale}/docs`, label: chrome.navDocs },
36 { href: `/${locale}/models`, label: chrome.navModels },
37 { href: `/${locale}/plugins`, label: chrome.navPlugins },
38 { href: `/${locale}/app`, label: chrome.navDesktop },
39 { href: `/${locale}/changelog`, label: chrome.footerChangelog },
40 { href: `/${locale}/contribute`, label: chrome.navContribute },
41 ];
42 }
43
44 export function signInLink(locale: string, chrome: ChromeDict): ChromeLink {
45 return { href: `/${locale}/signin`, label: chrome.navSignIn };
46 }
47
48 /** Footer "Product" column: what it is and where to get it. */
49 export function footerProductLinks(locale: string, chrome: ChromeDict): ChromeLink[] {
50 return [
51 { href: `/${locale}/install`, label: chrome.footerInstall },
52 { href: `/${locale}/models`, label: chrome.footerModels },
53 { href: `/${locale}/plugins`, label: chrome.navPlugins },
54 { href: `/${locale}/computer-use`, label: "Computer Use" },
55 ];
56 }
57
58 /** Footer "Docs" column: reading material for people building on Codewhale. */
59 export function footerDocsLinks(locale: string, chrome: ChromeDict): ChromeLink[] {
60 return [
61 { href: `/${locale}/docs/guide`, label: chrome.footerGuide },
62 { href: `/${locale}/docs`, label: chrome.footerDocs },
63 { href: `/${locale}/changelog`, label: chrome.footerChangelog },
64 { href: `/${locale}/faq`, label: chrome.footerFaq },
65 { href: `/${locale}/ratatui`, label: "Ratatui" },
66 ];
67 }
68
69 export function footerProjectLinks(locale: string, chrome: ChromeDict): ChromeLink[] {
70 return [
71 { href: REPO_URL, label: "GitHub" },
72 { href: `/${locale}/contribute`, label: chrome.footerContribute },
73 { href: DISCORD_URL, label: "Discord" },
74 { href: `/${locale}/merch`, label: chrome.footerMerch },
75 { href: REPO_LICENSE_URL, label: chrome.footerLicense },
76 ];
77 }
78
79 /** Footer legal destinations — reachable, never a 404. */
80 export function footerLegalLinks(locale: string, chrome: ChromeDict, usageData: string): ChromeLink[] {
81 return [
82 { href: `/${locale}/legal/terms`, label: chrome.footerTerms },
83 { href: `/${locale}/legal/privacy`, label: chrome.footerPrivacy },
84 { href: `/${locale}/legal/privacy#usage-counting`, label: usageData },
85 { href: SECURITY_POLICY_URL, label: chrome.footerSecurity },
86 ];
87 }
88
89 /**
90 * The one link in a chrome set that names the page currently being viewed.
91 *
92 * Nav and the compact sheet used to decide this per link with a plain
93 * prefix test (`pathname === href || pathname.startsWith(href + "/")`).
94 * Two nav links are ancestor and descendant of each other — `/de/docs` and
95 * `/de/docs/guide` — so on the guide route that test was true for both:
96 * the nav underline landed under Docs and Start at once, and two links
97 * carried `aria-current="page"`, telling assistive technology the reader is
98 * on two pages simultaneously.
99 *
100 * The longest matching href is the page; anything shorter is an ancestor
101 * section, not a location. `/de/docs/configuration` is not a nav link, so
102 * Docs still wins there — only the collision resolves.
103 */
104 export function currentNavHref(
105 links: readonly ChromeLink[],
106 pathname: string,
107 ): string | null {
108 let current: string | null = null;
109 for (const link of links) {
110 const matches = pathname === link.href || pathname.startsWith(`${link.href}/`);
111 if (!matches) continue;
112 if (current === null || link.href.length > current.length) current = link.href;
113 }
114 return current;
115 }
116
116 lines TYPESCRIPT