All files / core/blong-browser/src/hooks useFormat.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 7/7

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                            376x 376x 376x 124x 70x     124x   3x        
/**
 * `useFormatValue` — the formatter bound to the resolved configuration.
 *
 * The specification and the layers live in `lib/format.ts`; this hook is the one
 * place that reads them: the user's preference, then the system-wide configuration,
 * then the per-language default, with the UI language as the locale when nobody chose
 * one. It subscribes to all three, so a language switch or a settings change re-renders
 * the text instead of leaving yesterday's month names on screen. `@@D-616`
 */
import {useCallback, useMemo} from 'react';
import {type FormatFn, formatValue, resolveFormatConfig} from '../lib/format.js';
import {useAppStore} from '../state/appStore.js';
 
export function useFormatValue(): FormatFn {
    const language = useAppStore(state => state.language);
    const user = useAppStore(state => state.format.user);
    const system = useAppStore(state => state.format.system);
    const config = useMemo(
        () => resolveFormatConfig({user, system, language}),
        [user, system, language],
    );
    return useCallback(
        (value, type, options, useGrouping) =>
            formatValue(value, type, options, useGrouping, config),
        [config],
    );
}