import * as React from 'react';
import { clipboard, search, document, filesystem } from '@oliveai/ldk';
import * as Renderer from '@oliveai/ldk/dist/whisper/react/renderer';
import { Row, Worksheet } from '@oliveai/ldk/dist/document/types';
// Put the file wherever you'd like on your filesystem
// Just make sure to update your path permissions accordingly
const ERROR_CODES_FILEPATH = './error_codes.xlsx';
const INDEX_NAME = 'ERROR_CODES';
const WHISPER_LABEL = 'Error Code';
// Setting up the whisper component
const ErrorDisplay: React.FunctionComponent<ErrorProps> = (props) => {
const messageHeader = `${props.errorCode}`;
const messageBody = `${props.description}`;
<oh-whisper label={WHISPER_LABEL} onClose={() => {}}>
<oh-message header={messageHeader} body={messageBody} />
// We'll iterate over each worksheet in the spreadsheet and group
// all the rows per worksheet into a single document
function workSheet2Document(worksheet: Worksheet) {
const { name, rows } = worksheet;
const data = JSON.stringify(
rows.map((row: Row) => ({
errorCode: row.cells[0].value,
name: row.cells[1].value,
description: row.cells[2].value,
// Reading in the file and converting it is simple, even more so with async/await
const fileRaw = await filesystem.readFile(ERROR_CODES_FILEPATH);
const workbook = await document.xlsxDecode(fileRaw);
const documents = workbook.worksheets.map(workSheet2Document);
const index = await search.createIndex(INDEX_NAME, documents, {});
clipboard.listen(false, async (text: string) => {
const trimmedText = text.trim();
// We're going to just be searching the error code, so if its multiple words
// assume they copied something else for now
if (trimmedText.split(/\s+/).length != 1) {
const results = await index.search(trimmedText);
if (results.total === 0) {
const firstResult = results.data[0];
Renderer.renderNewWhisper(
<ErrorDisplay errorCode={firstResult.errorCode} description={firstResult.description} />,