import React, { useMemo } from 'react'; import { TextWithHighlights } from '@aws-sdk/client-kendra'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCircleExclamation, faFile } from '@fortawesome/free-solid-svg-icons'; import { QueryResultItem } from '@aws-sdk/client-kendra'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'; import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers'; import HighlightText from './HighlightText'; import useLoginUser from '../lib/useLoginUser'; const IDENTITY_POOL_ID = process.env.REACT_APP_IDENTITY_POOL_ID!; const USER_POOL_ID = process.env.REACT_APP_USER_POOL_ID!; const REGION = process.env.REACT_APP_REGION!; const COGNITO_ID = `cognito-idp.${REGION}.amazonaws.com/${USER_POOL_ID}`; interface TypeDocumentProps { item: QueryResultItem; // Featured Results の場合は、Trueを設定 // https://docs.aws.amazon.com/ja_jp/kendra/latest/dg/featured-results.html isFeatured?: boolean; } function TypeDocument(props: TypeDocumentProps) { const { token } = useLoginUser(); const { title, body, hasDocumentURI, hasS3DocumentURI, downloadFile } = useMemo(() => { const title: TextWithHighlights = props.item.DocumentTitle || { Text: '', Highlights: [], }; const body: TextWithHighlights = props.item.DocumentExcerpt || { Text: '', Highlights: [], }; const hasDocumentURI = !!props.item.DocumentURI; const hasS3DocumentURI = props.item.DocumentURI?.startsWith('https://s3.'); const downloadFile = async (event: any): Promise => { // [Auth 拡張実装] JWT トークンが取得できていない場合は処理しない if (!token) { // デモのため、エラー処理は Alert を表示するだけの簡易的な実装 alert( '認証トークンが取得できませんでした。サインアウトしてから再度試してみてください。' ); return; } const bucket_keys = new URL(props.item.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 }, // [Auth 拡張実装] ログイン情報を付与する logins: { [COGNITO_ID]: token, }, }), }); 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 { title, body, hasDocumentURI, hasS3DocumentURI, downloadFile }; }, [props, token]); return (
{hasDocumentURI && hasS3DocumentURI && (
)} {hasDocumentURI && !hasS3DocumentURI && (
)} {!hasDocumentURI && (
ドキュメントのソースが見つかりませんでした
)}
{props.isFeatured && (
注目
)}
); } export default TypeDocument;