aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/frontend/src/main/components/search/searchResultRow.tsx
blob: 83dd2bcb3700f71ae08530e47def5ef06a19c40b (plain)
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
import React from "react";
import {OptionWithSearchResult, SearchProps, SearchRank} from "./types";
import _ from "lodash";

type HighlighterProps = {
    label: string
}

const Highlighter: React.FC<HighlighterProps> = ({label}: HighlighterProps) => {
    return <strong>{label}</strong>
}

export const SearchResultRow: React.FC<SearchProps> = ({searchResult}: SearchProps) => {
    const signatureFromSearchResult = (searchResult: OptionWithSearchResult): string => {
        if(searchResult.rank == SearchRank.SearchKeyMatch){
            return searchResult.name.replace(searchResult.searchKey, searchResult.highlight)
        }
        return searchResult.highlight
    }

    /*
    This is a work-around for an issue: https://youtrack.jetbrains.com/issue/RG-2108
    */
    const out = _.chunk(signatureFromSearchResult(searchResult).split('**'), 2).flatMap(([txt, label]) => [
        txt,
        label ? <Highlighter label={label}></Highlighter> : null,
      ]);

    return (
        <div className="template-wrapper">
            <span>{out}</span>
            <span className="template-description">{searchResult.description}</span>
        </div>
    )
}