<commit_before>import * as b from "bobril";

export function Anchor({ children, name, params }: { children: b.IBobrilChildren; name?: string; params?: b.Params }): b.IBobrilNode {
    return b.anchor(children, name, params);
}<commit_msg>Fix anchors<commit_after>import * as b from "bobril";

export function Anchor({ children, name, params }: { children: b.IBobrilChildren; name?: string; params?: b.Params }): b.IBobrilNode {
    const me = b.useRef(null);
    const lastTransitionRunCount = b.useState<number | undefined>(undefined);

    b.useEffect(() => {
        const current = me.current;
        current && handleAnchorRoute(current, lastTransitionRunCount, name, params);
    });

    return <span ref={me}>{children}</span>;
}

function handleAnchorRoute(
    me: b.IBobrilCacheNode,
    lastTransitionRunCount: b.IProp<number | undefined>,
    name?: string,
    params?: b.Params
): void {
    let routeName: string | undefined;
    if (name) {
        routeName = name;
    } else {
        const firstChild = (me.children && me.children[0]) as b.IBobrilCacheNode;
        routeName = firstChild.attrs && firstChild.attrs.id;
    }
    if (!b.isActive(routeName, params)) {
        lastTransitionRunCount(0);
        return;
    }
    if (lastTransitionRunCount() === b.transitionRunCount) {
        return;
    }
    scrollIntoView(b.getDomNode(me) as HTMLElement);
    lastTransitionRunCount(b.transitionRunCount);
}

function scrollIntoView(el: HTMLElement, offset = 0): void {
    window.scroll(0, el.offsetTop - offset);
}
{'fraction_non_alphanumeric': 0.13424124513618677, 'fraction_numerical': 0.0025940337224383916, 'mean_word_length': 3.5922619047619047, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3362899', 'n_tokens_mistral': 480, 'n_tokens_neox': 474, 'n_words': 112}