// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 import { DocumentMetadata, FormMetadata } from '@aws/api-typescript'; import { Button, Column, ColumnLayout, Container, Grid, Inline, Link, LoadingIndicator, Table } from 'aws-northstar'; import { Column as TableColumn } from 'aws-northstar/components/Table/types'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useParams } from 'react-router-dom'; import { API } from '../../api/client'; import { listAllPages } from '../../api/utils'; import { updateStatus } from '../../utils/status-update-helper'; import { PdfViewer } from '../pdf/pdf-viewer'; import { ExtractionExecutionStatusIndicator } from '../status/extractionExecutionStatusIndicator'; export interface DocumentProps {} const formColumnDefinitions: TableColumn[] = [ { id: 'formId', width: 220, Header: 'Form', accessor: 'formId', Cell: ({ value, row }) => { const link = `/view/${row.original.documentId}/${value}`; return {row.original.documentName} {value}; }, }, { id: 'status', width: 200, Header: 'Status', // backlog/in review/reviewed accessor: 'extractionExecution.status', Cell: ({ value, row }) => ( updateStatus(documentId, formId, 'REVIEWING')} statusReason={row.original.extractionExecution.statusReason} /> ), }, { id: 'startPageIndex', width: 100, Header: 'Start Page', accessor: 'startPageIndex', // Page index starts from 0, and page number starts from 1 Cell: ({ value }) => value + 1, }, { id: 'endPageIndex', width: 100, Header: 'End Page', accessor: 'endPageIndex', // Page index starts from 0, and page number starts from 1 Cell: ({ value }) => value + 1, }, ]; /** * Detail page for a document */ export const Document: React.FC = () => { const [isLoading, setIsLoading] = useState(true); const [isLoadingForms, setIsLoadingForms] = useState(true); const [document, setDocument] = useState(); const [forms, setForms] = useState([]); const selectedForm = useRef(); const [pageNumber, setPageNumber] = useState(1); const { documentId } = useParams<{ documentId: string}>(); const fetchForms = useCallback(async () => { setIsLoadingForms(true); setForms(await listAllPages(API.listDocumentForms.bind(API), 'forms', { documentId })); setIsLoadingForms(false); }, [documentId]); const fetchDocumentAndForms = useCallback(async () => { setIsLoading(true); const [doc] = await Promise.all([ API.getDocument({ documentId }), fetchForms(), ]); setDocument(doc); setIsLoading(false); }, []); useEffect(() => { void (async () => { await fetchDocumentAndForms(); })(); }, []); const onSelectForm = useCallback((form: FormMetadata) => { selectedForm.current = form; // Page index is 0-based setPageNumber(form.startPageIndex + 1); }, [selectedForm, setPageNumber]); return isLoading || !document ? ( ) : ( <>