shell: the panel, createPanel(site) (moved from wayBetter for X)
packages/shell/src/panel (waybetter/shell/panel): an app's own tab. An app describes itself (PanelSite: brand and the home page's words, its own sections and their order, its theme store, catalog and the preview component a theme document is drawn in, Make's examples and docs, the install controller, its languages, what its native side serves) and createPanel returns the component for ShellSite.panel. It is in the shell package because it is drawn in the frame and shares its stores, native bridge and language. - Panel.tsx (header, settings search over every settings section, navigation, toasts; Accounts and Updates), nav.ts (which built-in sections an app gets: each shows only when the app serves what it needs; the app's sections in its order), Home.tsx (hero, highlights, About from the app's data), controls.tsx + panel.css. - Appearance.tsx, ThemeBuilder.tsx + builderModel.ts + builder.css, fonts/ (FontBrowser, FontRoles, catalog, the offline Google Fonts list; scripts/google-fonts-catalog.mjs regenerates it): the theme documents are the runtime's, the store, catalog, accents, draft palette, file suffix and preview are the app's (PanelSite.themes). - Plugins.tsx (the plugins list and custom CSS over the host's plugins_*/custom_css_*), StoreSection.tsx, MakePanel.tsx + makeModel.ts + MarkdownDoc.tsx + markdown.ts + make.css (the examples, the theme file and the docs are the app's), Settings.tsx (links and session over settings_get/set with `siteLinks`, keyword alerts, the media panel), FeatureToggles.tsx (useFeatures + FeatureGroups over features_get/set: the app names its flags and groups), PowerSettings.tsx (deck defaults from site.deck: its flags and presets; DeckPreset gets `name` and `hint`), BackupPanel.tsx + backup.css, DeveloperPanel.tsx, LanguageSettings.tsx, compare.ts. - frame: language.ts (createLanguage: follow the site / the system / one language, i18n_set; moved from the app), appI18n.ts (createAppI18n: the app's typed i18n instance, useT, dates and numbers; the app passes React's useSyncExternalStore), formatDate in i18n.ts (ShellI18n.date). - tests/panel.test.ts: every panel key has English and is used, sections per app, the builder model, the font catalog, plugin header rules, the UI kit demos, Markdown, compare. - npm package: exports ./shell/panel, ./shell/panel/*, ./shell/panel/*.css; the build is scripts/build.mjs (the shell is compiled with the runtime sources it imports under one root and its imports are pointed at dist/runtime; stylesheets and the font list are copied). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7a3f23aa82
commit
ad2a3ad375
44 changed files with 4864 additions and 2 deletions
101
packages/shell/src/frame/appI18n.ts
Normal file
101
packages/shell/src/frame/appI18n.ts
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// An app's i18n instance for its shell webview (the shell frame, the panel, its own windows):
|
||||
// English (the app's catalog over the framework's) is always loaded, other catalogs load on
|
||||
// demand through the loader the app registers (its bundler's lazy imports), and components call
|
||||
// `useT()` so a language switch re-renders them in place. The frame is bound to the instance.
|
||||
//
|
||||
// export const { i18n, t, tx, useT, setCatalogLoader } = createAppI18n<MessageKey>({
|
||||
// source: { ...frameworkEn.messages, ...appEn.messages }, languages, useSyncExternalStore, dev: import.meta.env.DEV,
|
||||
// });
|
||||
//
|
||||
// Keys are typed by the app (`MessageKey`: its English catalog's keys and the framework's). The
|
||||
// app passes React's `useSyncExternalStore`, so this module has no dependency of its own and the
|
||||
// app's pure modules that use `t` stay testable under node.
|
||||
|
||||
import { createI18n, type I18n, type Messages, type Params } from '../../../runtime/src/i18n.ts';
|
||||
import { setShellI18n } from './i18n.ts';
|
||||
|
||||
/** One language the app has a catalog for (a row of the app's generated `languages.json`). */
|
||||
export type AppLanguage = {
|
||||
code: string;
|
||||
name: string;
|
||||
englishName: string;
|
||||
dir: 'ltr' | 'rtl';
|
||||
status: 'source' | 'machine' | 'reviewed';
|
||||
reviewers: string[];
|
||||
fallback?: string;
|
||||
/** Share of English messages this catalog translates (its fallback's included), 0..1. */
|
||||
coverage: number;
|
||||
};
|
||||
|
||||
export type AppI18nOptions = {
|
||||
/** English: the framework's messages with the app's over them. */
|
||||
source: Messages;
|
||||
/** Every language with a catalog, English first. */
|
||||
languages: readonly AppLanguage[];
|
||||
/** React's hook (`import { useSyncExternalStore } from 'react'`), for `useT` and `useLocale`. */
|
||||
useSyncExternalStore<Snapshot>(subscribe: (listener: () => void) => () => void, getSnapshot: () => Snapshot, getServerSnapshot?: () => Snapshot): Snapshot;
|
||||
/** A development build: untranslated and malformed messages are logged. */
|
||||
dev?: boolean;
|
||||
};
|
||||
|
||||
export type AppI18n<Key extends string> = {
|
||||
i18n: I18n<Key>;
|
||||
/** Registers how catalogs load (the app's lazy imports; tests may pass a map). */
|
||||
setCatalogLoader(load: (locale: string) => Promise<Messages | null>): void;
|
||||
/** The message for `key` in the active language. */
|
||||
t(key: Key, params?: Params): string;
|
||||
/** For keys assembled at runtime (feature ids, section names): `fallback` when the key is unknown. */
|
||||
tx(key: string, params?: Params, fallback?: string): string;
|
||||
/** Re-renders the calling component when the language changes; returns `t`. */
|
||||
useT(): (key: Key, params?: Params) => string;
|
||||
/** The active locale (re-renders on change). */
|
||||
useLocale(): string;
|
||||
/** "3 minutes ago" style text for a timestamp in the active language. */
|
||||
relativeTime(then: number, now?: number): string;
|
||||
/** A date (and time) in the active language: `formatDate(iso, { dateStyle: 'medium' })`. */
|
||||
formatDate(value: string | number | Date, options?: Intl.DateTimeFormatOptions): string;
|
||||
formatNumber(value: number, options?: Intl.NumberFormatOptions): string;
|
||||
};
|
||||
|
||||
export function createAppI18n<Key extends string>(options: AppI18nOptions): AppI18n<Key> {
|
||||
let loader: ((locale: string) => Promise<Messages | null>) | null = null;
|
||||
const i18n = createI18n<Key>({
|
||||
source: options.source,
|
||||
available: options.languages.map(language => language.code),
|
||||
fallbacks: Object.fromEntries(options.languages.filter(language => language.fallback).map(language => [language.code, language.fallback!])),
|
||||
load: locale => loader?.(locale) ?? null,
|
||||
onMissing: options.dev
|
||||
? (key, locale, reason) => { if (reason !== 'key' || locale === 'en') console.warn(`[i18n] ${reason} ${locale}: ${key}`); else console.debug(`[i18n] untranslated ${locale}: ${key}`); }
|
||||
: undefined,
|
||||
});
|
||||
// The wayBetter shell frame and panel speak through this instance.
|
||||
setShellI18n(i18n);
|
||||
|
||||
const { useSyncExternalStore } = options;
|
||||
const t = (key: Key, params?: Params) => i18n.t(key, params);
|
||||
const subscribe = (listener: () => void) => i18n.onChange(listener);
|
||||
const snapshot = () => i18n.locale;
|
||||
return {
|
||||
i18n,
|
||||
setCatalogLoader(load) { loader = load; },
|
||||
t,
|
||||
tx: (key, params, fallback) => i18n.tx(key, params, fallback),
|
||||
useT() {
|
||||
useSyncExternalStore(subscribe, snapshot, snapshot);
|
||||
return t;
|
||||
},
|
||||
useLocale: () => useSyncExternalStore(subscribe, snapshot, snapshot),
|
||||
relativeTime(then, now = Date.now()) {
|
||||
const seconds = Math.round((then - now) / 1000);
|
||||
const abs = Math.abs(seconds);
|
||||
if (abs < 45) return i18n.relative(0, 'second');
|
||||
if (abs < 3600) return i18n.relative(Math.round(seconds / 60), 'minute');
|
||||
if (abs < 86400) return i18n.relative(Math.round(seconds / 3600), 'hour');
|
||||
if (abs < 86400 * 30) return i18n.relative(Math.round(seconds / 86400), 'day');
|
||||
if (abs < 86400 * 365) return i18n.relative(Math.round(seconds / (86400 * 30)), 'month');
|
||||
return i18n.relative(Math.round(seconds / (86400 * 365)), 'year');
|
||||
},
|
||||
formatDate: (value, formatOptions = { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }) => i18n.date(value, formatOptions),
|
||||
formatNumber: (value, formatOptions) => i18n.number(value, formatOptions),
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue