Skip to content

Commit

Permalink
Replace discover's usage of lodash.template with static react rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Apr 22, 2021
1 parent 49a1848 commit 7e328bc
Showing 1 changed file with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
* Side Public License, v 1.
*/

import { template } from 'lodash';
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom/server';
import { MAX_DOC_FIELDS_DISPLAYED } from '../../../../common';
import { getServices, IndexPattern } from '../../../kibana_services';

function noWhiteSpace(html: string) {
const TAGS_WITH_WS = />\s+</g;
return html.replace(TAGS_WITH_WS, '><');
interface Props {
defPairs: Array<[string, unknown]>;
}

const templateHtml = `
<dl class="source truncate-by-height">
<% defPairs.forEach(function (def) { %>
<dt><%- def[0] %>:</dt>
<dd><%= def[1] %></dd>
<%= ' ' %>
<% }); %>
</dl>`;
export const doTemplate = template(noWhiteSpace(templateHtml));
const TemplateComponent = ({ defPairs }: Props) => {
return (
<dl className={'source truncate-by-height'}>
{defPairs.map((pair, idx) => (
<Fragment key={idx}>
<dt>{pair[0]}:</dt>
<dd
dangerouslySetInnerHTML={{ __html: `${pair[1]}` }} // eslint-disable-line react/no-danger
/>{' '}
</Fragment>
))}
</dl>
);
};

export const formatRow = (hit: Record<string, any>, indexPattern: IndexPattern) => {
const highlights = hit?.highlight ?? {};
Expand All @@ -38,7 +42,9 @@ export const formatRow = (hit: Record<string, any>, indexPattern: IndexPattern)
pairs.push([displayKey ? displayKey : key, val]);
});
const maxEntries = getServices().uiSettings.get(MAX_DOC_FIELDS_DISPLAYED);
return doTemplate({ defPairs: [...highlightPairs, ...sourcePairs].slice(0, maxEntries) });
return ReactDOM.renderToStaticMarkup(
<TemplateComponent defPairs={[...highlightPairs, ...sourcePairs].slice(0, maxEntries)} />
);
};

export const formatTopLevelObject = (
Expand Down Expand Up @@ -70,5 +76,7 @@ export const formatTopLevelObject = (
pairs.push([displayKey ? displayKey : key, formatted]);
});
const maxEntries = getServices().uiSettings.get(MAX_DOC_FIELDS_DISPLAYED);
return doTemplate({ defPairs: [...highlightPairs, ...sourcePairs].slice(0, maxEntries) });
return ReactDOM.renderToStaticMarkup(
<TemplateComponent defPairs={[...highlightPairs, ...sourcePairs].slice(0, maxEntries)} />
);
};

0 comments on commit 7e328bc

Please sign in to comment.