Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

810 B
Raw Blame
interface ISearchAVMatchItem {
    matched?: boolean;
    children?: ISearchAVMatchItem[];
}

interface ISearchAVFocus {
    resultIndex: number;
    viewIndex?: number;
}

export const getSearchAVFocus = (results: ISearchAVMatchItem[], keyword: string): ISearchAVFocus | undefined => {
    if (results.length === 0) {
        return;
    }
    if (keyword.trim() === "") {
        return {resultIndex: 0};
    }
    for (let resultIndex = 0; resultIndex < results.length; resultIndex++) {
        const item = results[resultIndex];
        if (item.matched) {
            return {resultIndex};
        }
        const viewIndex = item.children?.findIndex((view) => view.matched) ?? -1;
        if (viewIndex > -1) {
            return {resultIndex, viewIndex};
        }
    }
    return {resultIndex: 0};
};