// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 import { DocumentMetadata, FormSchema } from '@aws/api-typescript'; import { Button, Inline, Link, Modal, Select } from 'aws-northstar'; import FileUpload from 'aws-northstar/components/FileUpload'; import { SelectOption } from 'aws-northstar/components/Select/types'; import Table from 'aws-northstar/components/Table'; import { Column as TableColumn } from 'aws-northstar/components/Table/types'; import Grid from 'aws-northstar/layouts/Grid'; import React, { useCallback, useState } from 'react'; const documentListColumns: TableColumn[] = [ { id: 'name', width: 200, Header: 'Name', accessor: 'name', Cell: ({ value, row }) => {value}, }, { id: 'createdBy', width: 150, Header: 'Uploaded By', accessor: 'createdBy', }, { id: 'createdTimestamp', width: 150, Header: 'Uploaded At', accessor: 'createdTimestamp', Cell: ({ value }) => <>{new Date(value).toLocaleString()}, }, { id: 'numberOfPages', width: 100, Header: 'Pages', accessor: 'numberOfPages', }, ]; export interface DocumentsTableProps { readonly dataLoaded?: boolean; readonly documents: DocumentMetadata[]; readonly upload: (file: File, schemaId: string) => Promise; readonly schemas: FormSchema[]; readonly reloadAction?: any; } export const DocumentsTable: React.FC = ({ dataLoaded, documents, upload, schemas, reloadAction }) => { const [isUploadModalVisible, setIsUploadModalVisible] = useState(false); const [isUploading, setIsUploading] = useState(false); const [file, setFile] = useState(); const [options, setOptions] = React.useState([]); const [selectedOption, setSelectedOption] = React.useState(); const schemaOptions = schemas.map((schema) => { return { label: schema.schemaId, value: schema.schemaId, }; }); const onFocus = () => { setTimeout(() => { setOptions(schemaOptions); }, 1000); }; const doUpload = useCallback(async () => { if (selectedOption) { setIsUploading(true); await upload(file!, selectedOption.value!); setIsUploading(false); setIsUploadModalVisible(false); } }, [file, selectedOption]); const actionGroup = ( {reloadAction ?