import React from "react"; import _ from "lodash"; import Kendra from "aws-sdk/clients/kendra"; import { AdditionalResultAttributeKeys, Relevance } from "../constants"; import { isNullOrUndefined, selectMostRecentUpdatedTimestamp } from "../utils"; import SingleLeftArrow from "./components/SingleLeftArrow"; import SingleRightArrow from "./components/SingleRightArrow"; import ResultTitle from "./components/ResultTitle"; import ResultText from "./components/ResultText"; import ResultFooter from "./components/ResultFooter"; import "../search.scss"; import ReactPlayer from 'react-player'; const KENDRA_SUGGESTED_ANSWERS = "Amazon Kendra suggested answers"; const MAX_TOP_ANSWER_LENGTH = 25; interface TopResultsProps { results: Kendra.QueryResultItemList; submitFeedback: ( relevance: Relevance, resultItem: Kendra.QueryResultItem ) => Promise; } interface TopResultsState { currentResultIndex: number; totalResults: number; } export default class TopResults extends React.Component< TopResultsProps, TopResultsState > { // All results in this component has QueryResultType === "ANSWER" state = { currentResultIndex: 0, totalResults: this.props.results.length }; private updatePreviousResultIndex = () => { if (this.state.currentResultIndex === 0) { return; } this.setState(prevState => ({ currentResultIndex: prevState.currentResultIndex - 1 })); }; private updateNextResultIndex = () => { if (this.state.currentResultIndex === this.state.totalResults - 1) { return; } this.setState(prevState => ({ currentResultIndex: prevState.currentResultIndex + 1 })); }; private getTopAnswer = (text: Kendra.TextWithHighlights) => { if (text && text.Highlights) { for (const highlight of text.Highlights) { const length = highlight.EndOffset - highlight.BeginOffset; if ( highlight && highlight.TopAnswer && length < MAX_TOP_ANSWER_LENGTH ) { return text.Text!.substring( highlight.BeginOffset, highlight.EndOffset ); } } } return null; }; private renderResults = (result: Kendra.QueryResultItem, idx: number) => { const { submitFeedback } = this.props; if (!isNullOrUndefined(result)) { let attributes = Object(); if (!isNullOrUndefined(result.DocumentAttributes)) { result.DocumentAttributes!.forEach(attribute => { attributes[attribute.Key] = attribute.Value; }); } let resultAttributes = Object(); if (!isNullOrUndefined(result.AdditionalAttributes)) { result.AdditionalAttributes!.forEach(attribute => { resultAttributes[attribute.Key] = attribute.Value; }); } const answer = resultAttributes[AdditionalResultAttributeKeys.AnswerText]; const lastUpdated = selectMostRecentUpdatedTimestamp(attributes); const topAnswer = this.getTopAnswer(answer.TextWithHighlightsValue); let documentFile = result.DocumentURI!.split('?'); let videoFile = ( documentFile[0]!.toUpperCase().endsWith("MP4") || documentFile[0]!.toUpperCase().endsWith("OGX") || documentFile[0]!.toUpperCase().endsWith("WEBM") || documentFile[0]!.toUpperCase().endsWith("OGV") ); let audioFile = ( documentFile[0]!.toUpperCase().endsWith("MP3") || documentFile[0]!.toUpperCase().endsWith("WAV") || documentFile[0]!.toUpperCase().endsWith("FLAC") || documentFile[0]!.toUpperCase().endsWith("AMR") || documentFile[0]!.toUpperCase().endsWith("3GA") || documentFile[0]!.toUpperCase().endsWith("OGA") || documentFile[0]!.toUpperCase().endsWith("OGG") || documentFile[0]!.toUpperCase().endsWith("SPX") ); return (
{!_.isEmpty(topAnswer) &&

{topAnswer}

} {audioFile && (
)} {videoFile && (
)}
); } else { return null; } }; render() { const { results } = this.props; const resultsToShow = results.map(this.renderResults); if (isNullOrUndefined(results) || results.length === 0) { return null; } return (
{KENDRA_SUGGESTED_ANSWERS}
{resultsToShow[this.state.currentResultIndex]}
{resultsToShow.map((res, idx) => (
{res}
))}
{this.state.currentResultIndex > 0 && (
)} {this.state.currentResultIndex < this.state.totalResults - 1 && (
)}
{this.state.totalResults > 1 && (
    {[...Array(this.state.totalResults)].map((x, index) => (
  • ))}
)}
); } }