shell: the shell frame, createShell(site) (moved from wayBetter for X)
packages/shell/src/frame (waybetter/shell/frame): the whole shell of an app. An app describes its site (ShellSite: names and pages, account rules, search, address chips and completions, its panel tab, the new-tab page, palette items, deck column kinds and flags and an optional compose dock, the media panel's composer rule, a theme store, overlays, the native features it serves) and passes its Tauri API and i18n instance; createShell returns the root component. - Shell.tsx: dockview host (tabs in the titlebar row, splits, drag-link drops, shortcuts, context menu, deck back-and-forth, session restore, workspace on start, overlay raise); state.ts (pane placeholders, tabs_sync one-in-flight via syncGate, shell_raise layer, profiles, layout). - WebPanel (address bar), NewTabPage, CommandPalette + paletteModel (frame groups; site items), DeckView + AddColumnDialog + deckStore (site column kinds), PaneTheme + themeStore (createThemeStore over the app's ThemeAdapter) + chromeTheme (ChromeTheme input) + themeScopes, Titlebar + TitlebarExtras + icons + brand (wB mark data URI), AccountSwitcher + accountModel, workspaces + workspaceModel, backupModel, syncHost, devHost + devModel, pluginCommandStore, MediaPanel + mediaModel, searchInput, hooks, bridge (installPreviewMocks), i18n, boot.ts (bootScript: the first-frame boot.js), CSS themed with --wb-* tokens. - paneModel: paneVisibility, raiseSupported, cssRgb; deckModel: isSourcePage. - Includes the live-QA titlebar fix (dock ResizeObserver + rAF titlebar marks, dock sized by its insets). - tests/frame.test.ts: every frame key has English and is used, palette order with a site, address fallback, layout gate, boot script, chrome variables, switcher rules, workspaces, deck settings with site flags. - npm package 0.4.0: exports ./shell/frame, ./shell/frame/*, ./shell/frame/*.css (copied by the build); dockview and acorn optional peers; root devDependencies dockview, acorn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
37e3d8e5fc
commit
60faed66be
49 changed files with 6442 additions and 6 deletions
72
packages/shell/src/frame/i18n.ts
Normal file
72
packages/shell/src/frame/i18n.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// The shell frame's words. They live in the framework catalogs (`waybetter/locales/<bcp47>.json`,
|
||||
// `wb.*`); the app hands the frame its i18n instance (`setShellI18n`, a `createI18n` from
|
||||
// waybetter/i18n that has the framework catalogs merged in), so the frame speaks the app's language
|
||||
// and re-renders when it changes (`useT`, hooks.ts). Without one (a unit test), a key reads as itself.
|
||||
// Pure (no React, no Tauri), so the models that use it are unit-tested under node.
|
||||
//
|
||||
// Placeholders: `{site}` is the site's name ("X").
|
||||
|
||||
export type Params = Record<string, string | number | boolean | Date | null | undefined>;
|
||||
|
||||
/** The part of a waybetter/i18n instance the frame uses. */
|
||||
export type ShellI18n = {
|
||||
readonly locale: string;
|
||||
tx(key: string, params?: Params, fallback?: string): string;
|
||||
onChange(listener: (locale: string) => void): () => void;
|
||||
number(value: number, options?: Intl.NumberFormatOptions): string;
|
||||
relative(value: number, unit: Intl.RelativeTimeFormatUnit): string;
|
||||
};
|
||||
|
||||
let current: ShellI18n | null = null;
|
||||
/** Parameters every frame message gets (`{site}`: the site's name, set by createShell). */
|
||||
let defaults: Params = {};
|
||||
const listeners = new Set<() => void>();
|
||||
let unbind: (() => void) | null = null;
|
||||
|
||||
/** Binds the frame to the app's i18n instance (once, before the shell renders). */
|
||||
export function setShellI18n(i18n: ShellI18n) {
|
||||
unbind?.();
|
||||
current = i18n;
|
||||
unbind = i18n.onChange(() => { for (const listener of [...listeners]) listener(); });
|
||||
for (const listener of [...listeners]) listener();
|
||||
}
|
||||
|
||||
/** Sets the parameters every frame message gets (createShell: `{ site }`). */
|
||||
export function setTextDefaults(values: Params) {
|
||||
defaults = { ...values };
|
||||
}
|
||||
|
||||
/** The message for a framework key in the active language (the key itself when unbound or missing). */
|
||||
export function t(key: string, params?: Params): string {
|
||||
return current ? current.tx(key, { ...defaults, ...params }) : key;
|
||||
}
|
||||
|
||||
/** Like `t` for keys assembled at runtime: `fallback` when the key is unknown. */
|
||||
export function tx(key: string, params?: Params, fallback?: string): string {
|
||||
return current ? current.tx(key, { ...defaults, ...params }, fallback) : fallback ?? key;
|
||||
}
|
||||
|
||||
/** Language changes (for `useT`). */
|
||||
export function subscribeText(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => { listeners.delete(listener); };
|
||||
}
|
||||
export const textLocale = () => current?.locale ?? '';
|
||||
|
||||
/** A number in the active language ("5 min", "5 dk" with a unit style). */
|
||||
export function formatNumber(value: number, options?: Intl.NumberFormatOptions): string {
|
||||
return current ? current.number(value, options) : String(value);
|
||||
}
|
||||
|
||||
/** "3 minutes ago" style text for a timestamp in the active language. */
|
||||
export function relativeTime(then: number, now = Date.now()): string {
|
||||
if (!current) return new Date(then).toISOString();
|
||||
const seconds = Math.round((then - now) / 1000);
|
||||
const abs = Math.abs(seconds);
|
||||
if (abs < 45) return current.relative(0, 'second');
|
||||
if (abs < 3600) return current.relative(Math.round(seconds / 60), 'minute');
|
||||
if (abs < 86400) return current.relative(Math.round(seconds / 3600), 'hour');
|
||||
if (abs < 86400 * 30) return current.relative(Math.round(seconds / 86400), 'day');
|
||||
if (abs < 86400 * 365) return current.relative(Math.round(seconds / (86400 * 30)), 'month');
|
||||
return current.relative(Math.round(seconds / (86400 * 365)), 'year');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue