Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | 49x 49x 49x 49x 49x 4x 4x 4x 4x 49x 49x 49x 49x 30x 30x 4x 30x 12x 12x 49x 4x 5x 6x 23x 34x 34x 21x 34x 19x 15x 8x 10x 8x 8x 2x 2x 2x 10x 19x 19x 7x 19x 19x 19x 1x 41x 24x 24x 24x 3x 13x 4x 4x 4x 10x 9x 9x 9x 9x 9x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 3x 9x 8x 8x 21x 3x 3x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 6x 6x | /**
* Global application Zustand store.
* Manages auth state, portal tabs, toasts, and loader.
*/
import type {ReactNode} from 'react';
import {create} from 'zustand';
import type {IFeedbackItem} from '@feasibleone/blong';
import {composeTranslations} from '../i18n/bundled.js';
import {setTranslationProvider} from '../i18n/collect.js';
import type {IFormatConfig} from '../lib/format.js';
import type {ActionRegistry, IBlongError} from '../types/action.js';
import type {IAuthState, IUserProfile, PermissionMap} from '../types/permission.js';
import type {IPortalConfig, IPortalState, ITab} from '../types/portal.js';
/** Toast notification */
export interface IToast {
id: string;
severity: 'success' | 'info' | 'warn' | 'error';
summary?: string;
detail?: ReactNode;
life?: number;
}
/** Translation dictionary */
export type TranslationDict = Record<string, string>;
/** Inline hint shown near a button (success or error feedback) */
export interface IHint {
target: HTMLElement | null;
message: string;
error: boolean;
}
/**
* The user's explicit theme choice (set by the theme switcher). Undefined
* fields mean "not chosen" — the Theme component then derives them from the
* `IThemeConfig` prop. Persisted to `localStorage` so the choice survives a
* reload.
*/
export interface IThemeSelection {
/** Selected theme-option id (e.g. `lara-blue`, `glass`). */
themeId?: string;
/** Selected palette for themes that offer both light and dark variants. */
palette?: 'light' | 'dark';
/**
* Text direction. Set by the Storybook toolbar (the portal itself offers no
* direction control), so it lives beside the user's theme choice and shares
* its persistence — `Theme` reads it in preference to its `direction` prop,
* which is how a toolbar toggle reaches a story whose container owns `Theme`.
*/
direction?: 'ltr' | 'rtl';
}
const THEME_STORAGE_KEY = 'blong.theme';
function readThemeSelection(): IThemeSelection {
Iif (typeof localStorage === 'undefined') return {};
try {
const raw = localStorage.getItem(THEME_STORAGE_KEY);
Eif (!raw) return {};
const parsed = JSON.parse(raw) as {
themeId?: unknown;
palette?: unknown;
direction?: unknown;
};
const selection: IThemeSelection = {};
if (typeof parsed.themeId === 'string') selection.themeId = parsed.themeId;
if (parsed.palette === 'light' || parsed.palette === 'dark')
selection.palette = parsed.palette;
if (parsed.direction === 'ltr' || parsed.direction === 'rtl') {
selection.direction = parsed.direction;
}
return selection;
} catch {
return {};
}
}
function writeThemeSelection(selection: IThemeSelection): void {
Iif (typeof localStorage === 'undefined') return;
try {
Iif (!selection.themeId && !selection.palette && !selection.direction) {
localStorage.removeItem(THEME_STORAGE_KEY);
} else {
localStorage.setItem(THEME_STORAGE_KEY, JSON.stringify(selection));
}
} catch {
// Ignore storage failures (private mode, quota, …).
}
}
/** One language's database half: the label dictionary. */
export interface ITranslationBundle {
label?: TranslationDict;
}
/**
* Feedback mode's draft (`@@T-461`).
*
* `active` is the only member the deep components subscribe to — a card, a field
* row or a `Text` asks "am I addressable?" and nothing else. Hover geometry and
* callout placement live in the overlay's own state, so moving the pointer over a
* page does not re-render the page.
*/
export interface IFeedbackDraft {
active: boolean;
/** Draft comments by target id; one box per target, so a second click edits the first. */
items: Record<string, IFeedbackItem>;
}
/** Complete app state */
export interface IAppState {
auth: IAuthState;
portal: IPortalState;
toasts: IToast[];
feedback: IFeedbackDraft;
loader: {active: boolean; message?: string; count: number};
translations: TranslationDict;
/**
* Per-language dictionaries registered by the app via
* `setTranslationsByLanguage`. When non-empty, `setLanguage` swaps the
* active `translations` table to the matching language's dictionary
* (English falls back to an empty dict = English strings).
*/
translationsByLanguage: Record<string, TranslationDict>;
/**
* The database half, per language: the label dictionary. Loaded after login
* and on a language switch, so a corrected translation needs no release. A
* record's own name is not here on purpose — it is translated by the query
* that reads it (`D-635`).
*/
translationBundle: Record<string, ITranslationBundle>;
/**
* Reads one language's bundle from the server. `App` registers it from the
* merged portal configuration, so a suite without `blong-core` simply has
* none and the bundled dictionary remains the whole answer.
*/
translationLoader?: (language: string) => Promise<ITranslationBundle>;
/**
* A dictionary set directly through `setTranslations`.
*
* It is the last layer and survives a language switch, which is the contract
* tests and Storybook rely on: set the dictionary, then set the language.
* `setTranslationsByLanguage` clears it, because that call declares the
* configuration layer instead.
*/
translationOverride?: TranslationDict;
language: string;
/**
* Formatting configuration, resolved user → system → the language default by
* `useFormatValue` (see `lib/format.ts`). The user layer is the person's own
* preference and arrives with the profile; the system layer is the app's.
*/
format: {user?: IFormatConfig; system?: IFormatConfig};
actions: ActionRegistry;
error: IBlongError | null;
hint: IHint | null;
/** Explicit theme choice made via the theme switcher (see IThemeSelection). */
theme: IThemeSelection;
/**
* When true, the login popup is shown — set when an operation hits an
* expired/invalid session (401) and the client-side token renewal failed.
* The user logs in via the popup and re-invokes the operation.
*/
loginPrompt: boolean;
}
/** App store actions */
export interface IAppActions {
// Auth
setToken: (token: string | null) => void;
setProfile: (profile: IUserProfile | null) => void;
setPermissions: (permissions: PermissionMap) => void;
logout: () => void;
// Portal
openTab: (tab: ITab) => void;
closeTab: (id: string) => void;
setActiveTab: (id: string | null) => void;
setTabDirty: (id: string, dirty: boolean) => void;
setTabTitle: (id: string, title: string) => void;
updateTabComponent: (
id: string,
component: React.ComponentType<Record<string, unknown>>,
) => void;
setPortalConfig: (config: IPortalConfig | null) => void;
// Toasts
showToast: (toast: Omit<IToast, 'id'>) => void;
clearToast: (id: string) => void;
clearAllToasts: () => void;
// Loader
setLoading: (active: boolean, message?: string) => void;
// Translations
setTranslations: (dict: TranslationDict) => void;
setTranslationsByLanguage: (dicts: Record<string, TranslationDict>) => void;
setLanguage: (language: string) => void;
setTranslationBundle: (language: string, bundle: ITranslationBundle) => void;
setTranslationLoader: (loader?: (language: string) => Promise<ITranslationBundle>) => void;
/**
* Switch language and load its database bundle when it is not loaded yet.
* The synchronous `setLanguage` is the one every caller keeps using; this is
* the awaitable form a test or the login handler wants.
*/
languageActivate: (language: string) => Promise<void>;
// Formatting
/** Set one layer of the formatting configuration; `undefined` clears it. */
setFormat: (scope: 'user' | 'system', config: IFormatConfig | undefined) => void;
// Actions registry
registerActions: (actions: ActionRegistry) => void;
// Error
showError: (error: IBlongError) => void;
clearError: () => void;
// Theme
/** Replace the explicit theme selection (persisted to localStorage). */
setTheme: (selection: IThemeSelection) => void;
/**
* Merge a text direction into the theme selection. Separate from `setTheme`
* because that one replaces: a caller that only knows the direction (the
* Storybook toolbar) must not discard the theme and palette beside it.
*/
setDirection: (direction: 'ltr' | 'rtl') => void;
// Login prompt
setLoginPrompt: (visible: boolean) => void;
// Hint
showHint: (target: HTMLElement | null, message: string, error: boolean) => void;
clearHint: () => void;
// Feedback mode
/** Enter or leave the mode. Leaving keeps the draft until `feedbackReset`. */
feedbackActivate: (active: boolean) => void;
/** Add or replace one draft item, keyed by its logical target id. */
feedbackAdd: (item: IFeedbackItem) => void;
/** Patch one draft item (its comment as the person types). */
feedbackUpdate: (targetId: string, patch: Partial<IFeedbackItem>) => void;
feedbackRemove: (targetId: string) => void;
/** Drop the whole draft and clear the target rings. */
feedbackReset: () => void;
}
const initialAuth: IAuthState = {
token: null,
profile: null,
permissions: {},
isAuthenticated: false,
};
const initialPortal: IPortalState = {
tabs: [],
activeTabId: null,
portalConfig: null,
};
let toastIdCounter = 0;
/** The bundle reads in flight, keyed by language, so a switch loads once. */
const pendingBundles = new Map<string, Promise<ITranslationBundle>>();
/**
* The active dictionary for a language, from every layer.
*
* Composed on every write rather than on every render: `useText` then stays one
* object lookup, and the collector reports exactly the dictionary the screen used.
*/
function appliedTranslations(
state: Pick<IAppState, 'translationsByLanguage' | 'translationBundle' | 'translationOverride'>,
language = (state as {language?: string}).language ?? 'en',
): Pick<IAppState, 'translations'> {
const database: Record<string, TranslationDict> = {};
for (const [code, bundle] of Object.entries(state.translationBundle)) {
Eif (bundle.label) database[code] = bundle.label;
}
return {
translations: {
...composeTranslations(language, state.translationsByLanguage, database),
...(state.translationOverride ?? {}),
},
};
}
/**
* Mark the active language on the document.
*
* A Playwright test waits on `html[data-language]` instead of guessing how long a
* dictionary load takes, the same trick the portal uses for its composed config.
*/
function markLanguage(language: string): void {
Eif (typeof document !== 'undefined') {
document.documentElement.dataset.language = language;
}
}
export const useAppStore = create<IAppState & IAppActions>((set, get) => ({
auth: initialAuth,
portal: initialPortal,
toasts: [],
feedback: {active: false, items: {}},
loader: {active: false, count: 0},
translations: {},
translationsByLanguage: {},
translationBundle: {},
language: 'en',
format: {},
actions: {},
error: null,
hint: null,
theme: readThemeSelection(),
loginPrompt: false,
// Auth actions
setToken: token =>
set(state => ({
auth: {...state.auth, token, isAuthenticated: token != null},
})),
setProfile: profile =>
set(state => ({
auth: {...state.auth, profile},
})),
setPermissions: permissions =>
set(state => ({
auth: {...state.auth, permissions},
})),
logout: () =>
set({
auth: initialAuth,
portal: initialPortal,
toasts: [],
}),
// Portal actions
openTab: tab =>
set(state => {
// Navigate to existing tab with same action+params instead of duplicating
const existing = state.portal.tabs.find(
t =>
t.actionName === tab.actionName &&
JSON.stringify(t.params) === JSON.stringify(tab.params),
);
if (existing) {
return {portal: {...state.portal, activeTabId: existing.id}};
}
return {
portal: {
...state.portal,
tabs: [...state.portal.tabs, tab],
activeTabId: tab.id,
},
};
}),
closeTab: id =>
set(state => {
const tabs = state.portal.tabs.filter(t => t.id !== id);
const activeTabId =
state.portal.activeTabId === id
? (tabs[tabs.length - 1]?.id ?? null)
: state.portal.activeTabId;
return {portal: {...state.portal, tabs, activeTabId}};
}),
setActiveTab: id => set(state => ({portal: {...state.portal, activeTabId: id}})),
setTabDirty: (id, dirty) =>
set(state => ({
portal: {
...state.portal,
tabs: state.portal.tabs.map(t => (t.id === id ? {...t, dirty} : t)),
},
})),
setTabTitle: (id, title) =>
set(state => ({
portal: {
...state.portal,
tabs: state.portal.tabs.map(t => (t.id === id ? {...t, title} : t)),
},
})),
updateTabComponent: (id, component) =>
set(state => ({
portal: {
...state.portal,
tabs: state.portal.tabs.map(t => (t.id === id ? {...t, component} : t)),
},
})),
setPortalConfig: config => set(state => ({portal: {...state.portal, portalConfig: config}})),
// Toast actions
showToast: toast => {
const id = String(++toastIdCounter);
// Mirror error toasts to the browser console so agents and Playwright
// tests can observe them via page.on('console') even when a transient
// toast is missed by screenshot assertions.
if (toast.severity === 'error') {
console.error('[blong] error toast', toast.summary ?? '', toast.detail ?? '');
}
set(state => ({toasts: [...state.toasts, {...toast, id}]}));
// Auto-dismiss
const life = toast.life ?? (toast.severity === 'error' ? 8000 : 4000);
setTimeout(() => get().clearToast(id), life);
},
clearToast: id => set(state => ({toasts: state.toasts.filter(t => t.id !== id)})),
clearAllToasts: () => set({toasts: []}),
// Loader actions
setLoading: (active, message) =>
set(state => {
const count = state.loader.count + (active ? 1 : -1);
return {loader: {active: count > 0, message, count: Math.max(0, count)}};
}),
// Translations
//
// The active dictionary is a layered merge — bundled, then the suite's portal
// configuration, then the database — so a corrected translation needs no
// release and the pre-session screens still translate (D-627). `setTranslations`
// therefore writes the configuration layer for the active language rather than
// replacing the table outright.
setTranslations: dict =>
set(state => ({
translationOverride: dict,
...appliedTranslations({...state, translationOverride: dict}),
})),
setTranslationsByLanguage: dicts =>
set(state => ({
translationsByLanguage: dicts,
translationOverride: undefined,
// Re-apply the current language's dictionary immediately so the UI
// reflects it even if the language was set before the dicts loaded.
...appliedTranslations({
...state,
translationsByLanguage: dicts,
translationOverride: undefined,
}),
})),
setTranslationBundle: (language, bundle) =>
set(state => {
const translationBundle = {...state.translationBundle, [language]: bundle};
return state.language === language
? {translationBundle, ...appliedTranslations({...state, translationBundle})}
: {translationBundle};
}),
setTranslationLoader: loader => set({translationLoader: loader}),
setLanguage: language => {
set(state => {
markLanguage(language);
return {language, ...appliedTranslations(state, language)};
});
// A language whose database half has not been read yet is loaded now;
// without a loader the bundled and configured layers are the answer.
const {translationLoader, translationBundle, languageActivate} = get();
Iif (translationLoader && !translationBundle[language]) void languageActivate(language);
},
languageActivate: async language => {
set(state => {
markLanguage(language);
return {language, ...appliedTranslations(state, language)};
});
const {translationLoader, translationBundle, setTranslationBundle} = get();
Iif (!translationLoader || translationBundle[language]) return;
let pending = pendingBundles.get(language);
if (!pending) {
pending = translationLoader(language).finally(() => pendingBundles.delete(language));
pendingBundles.set(language, pending);
}
try {
setTranslationBundle(language, await pending);
} catch {
// A database without the tables, or a session without permission to
// read them, must not break login: the bundled and configured layers
// stay in place and the UI keeps its English fallback.
}
},
// Formatting — one layer at a time; the hook resolves the chain.
setFormat: (scope, config) =>
set(state => ({
format: {...state.format, [scope]: config},
})),
// Actions registry
registerActions: actions => set(state => ({actions: {...state.actions, ...actions}})),
// Error
showError: error => {
// Mirror the error popup (ErrorDialog) to the browser console so agents
// and Playwright tests can observe it via page.on('console').
console.error('[blong] error dialog', error.type, error.print ?? error.message, error);
set({error});
},
clearError: () => set({error: null}),
// Theme
setTheme: selection => {
writeThemeSelection(selection);
set({theme: selection});
},
setDirection: direction =>
set(state => {
Iif (state.theme.direction === direction) return {};
const theme = {...state.theme, direction};
writeThemeSelection(theme);
return {theme};
}),
// Login prompt
setLoginPrompt: visible => set({loginPrompt: visible}),
// Hint
showHint: (target, message, error) => set({hint: {target, message, error}}),
clearHint: () => set({hint: null}),
// Feedback mode
feedbackActivate: active =>
set(state => ({feedback: {...state.feedback, active}})),
feedbackAdd: item =>
set(state => ({
feedback: {
...state.feedback,
items: {...state.feedback.items, [item.targetId]: item},
},
})),
feedbackUpdate: (targetId, patch) =>
set(state => {
const existing = state.feedback.items[targetId];
Iif (!existing) return {};
return {
feedback: {
...state.feedback,
items: {...state.feedback.items, [targetId]: {...existing, ...patch}},
},
};
}),
feedbackRemove: targetId =>
set(state => {
Iif (!(targetId in state.feedback.items)) return {};
const items = {...state.feedback.items};
delete items[targetId];
return {feedback: {...state.feedback, items}};
}),
feedbackReset: () => set(state => ({feedback: {...state.feedback, items: {}}})),
}));
// The collector's report reads the live store, so it answers with the language and
// dictionary the screen actually used, without importing the store itself (`D-625`).
setTranslationProvider(() => {
const state = useAppStore.getState();
return {language: state.language, dictionary: state.translations};
});
|