import React from "react"; import Kendra from "aws-sdk/clients/kendra"; import { AdditionalResultAttributeKeys, FAQExpandedMapType, FAQ_MATCHES, Relevance } from "../constants"; import { isNullOrUndefined, isNullOrEmpty, selectMostRecentUpdatedTimestamp } from "./../utils"; import ResultText from "./components/ResultText"; import ResultFooter from "./components/ResultFooter"; import TextHighlights from "./components/TextWithHighlight"; import expand from "../images/solid-right.svg"; import collapse from "../images/solid-down.svg"; import "../search.scss"; interface FAQResultsProps { results: Kendra.QueryResultItemList; submitFeedback: ( relevance: Relevance, resultItem: Kendra.QueryResultItem ) => Promise; } interface FAQResultsState { faqExpandedMap: FAQExpandedMapType[]; } export default class FAQResults extends React.Component< FAQResultsProps, FAQResultsState > { constructor(props: FAQResultsProps) { super(props); this.state = { faqExpandedMap: [] }; } componentDidMount() { if (!isNullOrEmpty(this.props.results)) { let expandedMap = []; for (var i = 0; i < 10; i++) { expandedMap.push({ expanded: false }); } this.setState({ faqExpandedMap: expandedMap }); } } handleExpansionClick = ( event: React.MouseEvent, index: number ) => { const previousValue = this.state.faqExpandedMap[index].expanded; let temp = this.state.faqExpandedMap; temp[index] = { expanded: !previousValue }; this.setState({ faqExpandedMap: temp }); }; renderResults = (result: Kendra.QueryResultItem, index: number) => { const { submitFeedback } = this.props; const { faqExpandedMap } = this.state; let resultAttributes = Object(); if (!isNullOrUndefined(result.AdditionalAttributes)) { result.AdditionalAttributes!.forEach(attribute => { resultAttributes[attribute.Key] = attribute.Value; }); } const question = resultAttributes[AdditionalResultAttributeKeys.QuestionText]; const answer = resultAttributes[AdditionalResultAttributeKeys.AnswerText]; let title; if (!isNullOrUndefined(question)) { title = (
); } let content: Kendra.TextWithHighlights; if (!isNullOrUndefined(answer)) { content = answer.TextWithHighlightsValue; } let attributes = Object(); if (!isNullOrUndefined(result.DocumentAttributes)) { result.DocumentAttributes!.forEach(attribute => { attributes[attribute.Key] = attribute.Value; }); } const lastUpdated = selectMostRecentUpdatedTimestamp(attributes); return (
) => this.handleExpansionClick(event, index) } > {!isNullOrEmpty(faqExpandedMap) && !faqExpandedMap[index].expanded && ( expand )} {!isNullOrEmpty(faqExpandedMap) && faqExpandedMap[index].expanded && ( collapse )}
{title}
{!isNullOrEmpty(faqExpandedMap) && faqExpandedMap[index].expanded && (
)}
); }; render() { const { results } = this.props; if (isNullOrUndefined(results) || results.length === 0) { return null; } return (
{FAQ_MATCHES}
{results.map(this.renderResults)}
); } }