import React, { useMemo } from 'react'; import { QueryResultItem } from '@aws-sdk/client-kendra'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faArrowUpRightFromSquare } from '@fortawesome/free-solid-svg-icons'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'; import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers'; const IDENTITY_POOL_ID = process.env.REACT_APP_IDENTITY_POOL_ID!; const REGION = process.env.REACT_APP_REGION!; interface DocumentsProps { items: QueryResultItem[]; } interface DocumentProps { document: QueryResultItem; } function Document(props: DocumentProps) { const { docTitle, docText, hasDocumentURI, hasS3DocumentURI, downloadFile } = useMemo(() => { const docTitle = props.document.DocumentTitle?.Text || ''; const docText = props.document.DocumentExcerpt?.Text || ''; const hasDocumentURI = !!props.document.DocumentURI; const hasS3DocumentURI = props.document.DocumentURI?.startsWith('https://s3.'); const downloadFile = async (): Promise => { const bucket_keys = new URL(props.document.DocumentURI!).pathname.split( '/' ); const bucket = bucket_keys[1]; const key = bucket_keys.slice(2, bucket_keys.length).join('/'); const s3 = new S3Client({ region: REGION, credentials: fromCognitoIdentityPool({ identityPoolId: IDENTITY_POOL_ID, clientConfig: { region: REGION }, }), }); const contentType = key.endsWith('.txt') ? 'text/plain; charset=utf-8' : undefined; const getObject = new GetObjectCommand({ Bucket: bucket, Key: key, ResponseContentType: contentType, }); const objectUrl = await getSignedUrl(s3, getObject, { expiresIn: 3600, }); window.open(objectUrl, '_blank', 'noopener,noreferrer'); }; return { docTitle, docText, hasDocumentURI, hasS3DocumentURI, downloadFile, }; }, [props]); return (
  • {hasDocumentURI && hasS3DocumentURI && (
    downloadFile()} >
    {docTitle}
    )} {hasDocumentURI && !hasS3DocumentURI && (
    {docTitle}
    )} {!hasDocumentURI && (
    docTitle
    )}
    {docText}
  • ); } function Documents(props: DocumentsProps) { return (
    関連するドキュメントはこちらです
    ); } export default Documents;