All files / core/blong-browser/src/components/Text Text.tsx

85.71% Statements 6/7
61.53% Branches 8/13
100% Functions 1/1
100% Lines 6/6

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                                                    1430x 1430x 1430x       1430x   1430x 1430x                  
/**
 * Text — i18n text component.
 *
 * Translates a string by looking it up in the app-store translation dictionary.
 * Two usage patterns:
 *   <Text id="key" />             — explicit key
 *   <Text>{someStringVar}</Text>  — raw text used as both key and fallback
 *
 * Non-string children are rendered as-is without translation.
 */
import React from 'react';
import {useFeedbackTarget} from '../../feedback/useFeedbackTarget.js';
import {useText} from '../../hooks/useText.js';
 
interface ITextProps {
    /** Explicit translation key. */
    id?: string;
    /** Raw text used as both the translation key and the English fallback. */
    children?: React.ReactNode;
    params?: Record<string, string | number | undefined>;
    prefix?: string;
    as?: keyof React.JSX.IntrinsicElements;
    className?: string;
}
 
export function Text({id, children, params, prefix, as: Tag = 'span', className}: ITextProps) {
    const raw = typeof children === 'string' ? children : (id ?? '');
    const key = prefix ? `${prefix}.${raw}` : raw;
    const text = useText(key, params);
    // A label is an unnamed step in the path: the widget above it carries the name,
    // and the text itself travels as the target's label (`@@T-487`, `@@T-491`). The
    // badge then reads `clubShortName.label`, which is what the reader needs.
    const target = useFeedbackTarget('label', undefined, text);
    // Pass through non-string children unchanged (already JSX)
    Iif (typeof children !== 'string' && id == null) return <>{children ?? null}</>;
    return (
        <Tag
            className={className}
            {...target}
        >
            {text}
        </Tag>
    );
}